1 2 3 x = torch.randn(3 ,4 , requires_grad=True ) x
tensor([[ 2.2854, 0.9512, -2.4467, -0.7123],
[-0.7575, 1.1755, 0.4198, -0.2310],
[-0.9698, 1.2129, 1.3573, -0.6167]], requires_grad=True)
1 2 3 4 x = torch.randn(3 ,4 ) x.requires_grad = True x
tensor([[-0.5929, 0.5802, -0.1118, -2.5752],
[-0.1841, -0.5498, -0.0166, -1.0571],
[-0.3336, 0.5076, -0.1564, -0.3230]], requires_grad=True)
1 b = torch.randn(3 ,4 , requires_grad=True )
tensor(-0.6402, grad_fn=<SumBackward0>)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
True
1 2 3 4 5 6 7 8 x = torch.rand(1 ) b = torch.rand(1 , requires_grad = True ) w = torch.rand(1 , requires_grad = True ) y = w*b z = y+b
1 2 3 z.backward(retain_graph=True ) w.grad
tensor([0.4825])
tensor([1.1871])
1 2 3 4 5 6 import numpy as npx_values = [i for i in range (11 )] x_train = np.array(x_values, dtype=np.float32) x_train = x_train.reshape(-1 , 1 ) x_train.shape
(11, 1)
1 2 3 4 y_values = [2 *i + 1 for i in x_values] y_train = np.array(y_values, dtype=np.float32) y_train = y_train.reshape(-1 , 1 ) y_train.shape
(11, 1)
1 2 import torchimport torch.nn as nn
1 2 3 4 5 6 7 8 class LinearRegressionMode (nn.Module ): def __init__ (self, input_dim, output_dim ): super (LinearRegressionMode, self).__init__() self.linear = nn.Linear(input_dim, output_dim) def forward (self, x ): out = self.linear(x) return out
1 2 3 4 5 6 7 8 9 input_dim = 1 output_dim = 1 model = LinearRegressionMode(input_dim, output_dim) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' ) model.to(device) model
LinearRegressionMode(
(linear): Linear(in_features=1, out_features=1, bias=True)
)
1 2 3 4 5 epochs = 1000 learning_rate = 0.01 optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) criterion = nn.MSELoss()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 for epoch in range (epochs): epoch += 1 inputs = torch.from_numpy(x_train) labels = torch.from_numpy(y_train) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() if epoch%50 == 0 : print ('epoch {}, loss {}' .format (epoch, loss.item()))
epoch 50, loss 0.061197102069854736
epoch 100, loss 0.03490455821156502
epoch 150, loss 0.019908230751752853
epoch 200, loss 0.011354891583323479
epoch 250, loss 0.0064764260314404964
epoch 300, loss 0.0036938709672540426
epoch 350, loss 0.0021068572532385588
epoch 400, loss 0.00120164779946208
epoch 450, loss 0.0006853902596049011
epoch 500, loss 0.0003909121442120522
epoch 550, loss 0.00022296742827165872
epoch 600, loss 0.0001271772343898192
epoch 650, loss 7.253594958456233e-05
epoch 700, loss 4.137093128520064e-05
epoch 750, loss 2.359419704589527e-05
epoch 800, loss 1.3457529348670505e-05
epoch 850, loss 7.676342647755519e-06
epoch 900, loss 4.378400262794457e-06
epoch 950, loss 2.49663571594283e-06
epoch 1000, loss 1.424497213520226e-06
1 2 3 predicted = model(torch.from_numpy(x_train).requires_grad_()).data.numpy() predicted
array([[ 0.99778 ],
[ 2.9980998],
[ 4.9984193],
[ 6.9987392],
[ 8.999059 ],
[10.999378 ],
[12.999699 ],
[15.000018 ],
[17.000338 ],
[19.000658 ],
[21.000977 ]], dtype=float32)
1 2 torch.save(model.state_dict(),'model.pkl' )
1 model.load_state_dict(torch.load('model.pkl' ))
<All keys matched successfully>