Creating tensor: tns = torch.tensor(2500, dtype = torch.int) ## torch.float is another dtype BaseClass for all neural networks : nn.Modulenn.Linear and nn.Sequential are objects/derived class of nn.Module class/Parent Class.
nn.Linear(2,3) // Initializes with random paramters# Building sequential networkmodel = nn.Sequential( nn.Linear(2,3), nn.ReLU(), nn.Linear(3,1))model(input) //FeedForward# buildinv custom networkclass NN_Regression(nn.Module):super(NN_regression, self).__init():#initialize componentsself.layer1 = nn.Linear(3,6)self.layer2 = nn.Linear(4,1)self.relu = nn.ReLU()def forward(self, x): x =self.layer1(x) x =self.relu(x) x =self.layer2(x)return xoptimizer = optim.Adam(model.parameters(), lr=0.01)loss = nn.MSELoss()MSE = loss(model(input), y)MSE.backward() #Backward propogation or gradient calculationoptimizer.step() #Steppingoptimizer.zero_grad() #reset the gradients# Model Evaluationmodel.eval()with torch.no_grad(): test_MSE = loss(model(X_test), y_test)torch.save(model, 'model.pth')torch.load('model.pth')
To split dataset into validation and testing; use sklearn.model_selection.train_test_split