Skip to content

Commit

Permalink
model change
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaanukstar123 committed Nov 21, 2022
1 parent e2be226 commit 7a8bc21
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions part2_house_value_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ def __init__(self, x, nb_epoch=1000, learningRate=0.01, neuronArchitecture=[9,9,
X, _ = self._preprocessor(x, training = True)
self.input_size = X.shape[1]
self.output_size = 1
self.layer_list = self.get_layer_list(neuronArchitecture)
self.model = torch.nn.Sequential(*self.layer_list)#nn.Linear(dim_in, dim_h),nn.ReLU(),nn.Linear(dim_h, dim_out))
self.input_layer = nn.Linear(neuronArchitecture[0],neuronArchitecture[1])
self.output_layer = nn.Linear(neuronArchitecture[-2],neuronArchitecture[-1])
self.layers = nn.ModuleList()
self.layers.append(self.input_layer)
self.activation = nn.ReLU()
for i in range(1,len(neuronArchitecture)-1): #adds hidden layers to model
self.layers.append(nn.Linear(neuronArchitecture[i],neuronArchitecture[i+1]))

self.layers.append(self.output_layer)

if paramDict:
self.nb_epoch = paramDict["nb_epoch"]
Expand Down Expand Up @@ -129,17 +136,18 @@ def fit(self, x, y, xValidation=None, yValidation=None, minImprovement=0.01):
batch_list = torch.randperm(len(X)) # generates random indices

for i in range(0,len(X),self.batchSize):
optimiser = torch.optim.Adam(self.model.parameters(), lr=0.0001)
optimiser.zero_grad()

optimiser = torch.optim.Adam(nn.model.parameters(), lr=0.0001)

index = batch_list[i:i+ self.batchSize]
batch_x = X[index]
batch_y = Y[index]
prediction = self.predict(batch_x)
batch_loss = nn.MSELoss(prediction,batch_y)
batch_loss.backward()

optimiser.step()
optimiser.zero_grad()



#######################################################################
# ** END OF YOUR CODE **
Expand All @@ -158,7 +166,10 @@ def predict(self, x):
{np.ndarray} -- Predicted value for the given input (batch_size, 1).
"""
return self.model(x)
for layer in self.layers:
prediction = layer(x)
prediction = nn.functional.relu(prediction)
return prediction

#######################################################################
# ** START OF YOUR CODE **
Expand Down Expand Up @@ -196,21 +207,6 @@ def score(self, x, y):
# ** END OF YOUR CODE **
#######################################################################

@staticmethod
def get_layer_list(dims):
layers = [nn.Linear(9,dims[0])]
for i in range(1,len(dims)):
layers.append(nn.ReLU())
print(i,len(dims)-1)
if i == len(dims)-1:

layers.append(nn.Linear(dims[i-1],1))
else:
layers.append(nn.Linear(dims[i-1],dims[i]))

print(layers)
return layers

def save_regressor(trained_model):
"""
Utility function to save the trained regressor model in part2_model.pickle.
Expand Down Expand Up @@ -319,4 +315,5 @@ def example_main():
## https://pandas.pydata.org/
## https://pytorch.org/docs/
## https://www.projectpro.io/recipes/optimize-function-adam-pytorch
## https://stackoverflow.com/questions/32896651/pass-multiple-arguments-in-form-of-tuple
## https://stackoverflow.com/questions/32896651/pass-multiple-arguments-in-form-of-tuple
## https://rubikscode.net/2021/08/02/pytorch-for-beginners-building-neural-networks/

0 comments on commit 7a8bc21

Please sign in to comment.