Neural network regression of car-prices
Neural network regression of car-prices
We assume that the car prices (y) can be predicted as ?=?(?) where ?=[?0..?9..] are the car features and ? is a three layer fully connected neural network. We first create the matrix X_train, whose columns are the normalized training features.
Create a three layer neural network with hidden layers of h1 and h2 neurons respectively. Use RELU activations at the hidden layers and no activation on the output layer.
class Net(nn.Module):
def __init__(self,Nfeatures,Noutput,Nh1=10,Nh2=10):
super(Net, self).__init__()
# YOUR CODE HERE
def forward(self, x):
# YOUR CODE HERE
featureScale = np.max(np.abs(X_train),axis=0,keepdims=True)
X_train_T = X_train/featureScale
X_train_T = torch.tensor(X_train_T.astype(np.float32))
ymax = np.max(y_train,axis=0,keepdims=True)
y_train_T = torch.tensor(y_train.astype(np.float32)/ymax).unsqueeze(1)
Training
YOUR CODE BELOW
- Define a network with 20 features/neurons in hidden layer 1 and 25 in layer 2
- Define optimizer to be SGD
- Define the loss as the norm of the error
- Train the network for 10,000 epochs with a learning rate of 1e-3
net = Net(16,1,50,50)
out = net(X_train_T)
# YOUR CODE HERE
for epoch in range(10000):
#YOUR CODE HERE
if(np.mod(epoch,1000)==0):
print("Error =",error.detach().cpu().item())
fig,ax = plt.subplots(1,1,figsize=(12,4))
ax.plot(y_train_T.abs().detach().cpu(),label='Actual Price')
ax.plot(out.abs().detach().cpu(),label='Prediction')
ax.legend()
plt.show()
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
I am getting an error as below:
RuntimeError: Found dtype Double but expected Float