Jovian
⭐️
Sign In
In [1]:
import numpy as np
import torch
In [8]:
#Input (temp, rainfall, humidity)
inputs = np.array([
    [73, 67, 43],
    [91, 88, 64],
    [87, 134, 58],
    [102, 43, 37],
    [69, 96, 70]
], dtype='float32')
In [9]:
inputs
Out[9]:
array([[ 73.,  67.,  43.],
       [ 91.,  88.,  64.],
       [ 87., 134.,  58.],
       [102.,  43.,  37.],
       [ 69.,  96.,  70.]], dtype=float32)
In [10]:
#Targets (apples, oranges)
targets = np.array([
    [56, 70],
    [81, 101],
    [119, 133],
    [22, 37],
    [103, 119],
], dtype='float32')
targets
Out[10]:
array([[ 56.,  70.],
       [ 81., 101.],
       [119., 133.],
       [ 22.,  37.],
       [103., 119.]], dtype=float32)
In [11]:
#Convert inputs
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
print(inputs)
print(targets)
tensor([[ 73., 67., 43.], [ 91., 88., 64.], [ 87., 134., 58.], [102., 43., 37.], [ 69., 96., 70.]]) tensor([[ 56., 70.], [ 81., 101.], [119., 133.], [ 22., 37.], [103., 119.]])
In [13]:
# Weights and biases
w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(2, requires_grad=True)
print(w)
print(b)
tensor([[-1.8065, -0.0513, -1.3134], [ 0.0892, -1.7629, -1.5939]], requires_grad=True) tensor([-1.1512, 0.4946], requires_grad=True)
In [12]:
inputs.shape
Out[12]:
torch.Size([5, 3])
In [15]:
b.shape
Out[15]:
torch.Size([2])
In [16]:
w.shape
Out[16]:
torch.Size([2, 3])
In [17]:
def model(x):
    return x @ w.t() + b
# @ 表示PyTorch中的矩阵乘法
In [18]:
#Generate predictions
preds = model(inputs)
print(preds)
tensor([[-192.9458, -179.6504], [-254.1234, -248.5386], [-241.3785, -320.4274], [-236.2229, -125.1912], [-222.6706, -274.1667]], grad_fn=<AddBackward0>)
In [19]:
print(targets)
tensor([[ 56., 70.], [ 81., 101.], [119., 133.], [ 22., 37.], [103., 119.]])
In [20]:
#MSE Loss
def mse(t1, t2):
    diff = t1 - t2
    return torch.sum(diff * diff) / diff.numel()
#numel() 返回张量中元素的数量
In [21]:
#Compute loss
loss = mse(preds, targets)
print(loss)
tensor(104787.9844, grad_fn=<DivBackward0>)
In [22]:
#Compute gradients
loss.backward()
In [23]:
#Gradients for weights
print(w)
print(w.grad)
tensor([[-1.8065, -0.0513, -1.3134], [ 0.0892, -1.7629, -1.5939]], requires_grad=True) tensor([[-25766.4434, -27365.7832, -17081.1426], [-26630.5352, -30592.6953, -18585.3945]])
In [24]:
w.grad.zero_()
b.grad.zero_()
print(w.grad)
print(b.grad)
# PyTorch 会累计梯度,需要讲梯度置0
tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([0., 0.])
In [25]:
loss
Out[25]:
tensor(104787.9844, grad_fn=<DivBackward0>)
In [28]:
loss = mse(preds, targets)
In [29]:
loss.backward()
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-29-52a0569421b1> in <module> ----> 1 loss.backward() ~/anaconda3/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph) 105 products. Defaults to ``False``. 106 """ --> 107 torch.autograd.backward(self, gradient, retain_graph, create_graph) 108 109 def register_hook(self, hook): ~/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables) 91 Variable._execution_engine.run_backward( 92 tensors, grad_tensors, retain_graph, create_graph, ---> 93 allow_unreachable=True) # allow_unreachable flag 94 95 RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
In [30]:
w.grad
Out[30]:
tensor([[0., 0., 0.],
        [0., 0., 0.]])
In [31]:
preds = model(inputs)
loss = mse(preds, targets)
loss.backward()
In [32]:
w.grad
Out[32]:
tensor([[-25766.4434, -27365.7832, -17081.1426],
        [-26630.5352, -30592.6953, -18585.3945]])
In [33]:
w
Out[33]:
tensor([[-1.8065, -0.0513, -1.3134],
        [ 0.0892, -1.7629, -1.5939]], requires_grad=True)
In [34]:
#Adjust weights & reset gradients
with torch.no_grad():
    w -= w.grad * 1e-5
    b -= b.grad * 1e-5
    w.grad.zero_()
    b.grad.zero_()
# torch.no_grad()指示PyTorch更新权重和偏置时不跟踪,计算或修改梯度
In [35]:
print(w)
print(b)
tensor([[-1.5489, 0.2223, -1.1426], [ 0.3555, -1.4570, -1.4080]], requires_grad=True) tensor([-1.1451, 0.5011], requires_grad=True)
In [36]:
#Calculate loss
preds = model(inputs)
loss = mse(preds, targets)
print(loss)
tensor(71140.9766, grad_fn=<DivBackward0>)
In [37]:
# Train for 100 epochs
for i in range(100):
    preds = model(inputs)
    loss = mse(preds, targets)
    loss.backward()
    with torch.no_grad():
        w -= w.grad * 1e-5
        b -= b.grad * 1e-5
        w.grad.zero_()
        b.grad.zero_()
    print(loss)
tensor(71140.9766, grad_fn=<DivBackward0>) tensor(48462.2656, grad_fn=<DivBackward0>) tensor(33173.3984, grad_fn=<DivBackward0>) tensor(22864.5156, grad_fn=<DivBackward0>) tensor(15911.6436, grad_fn=<DivBackward0>) tensor(11220.4121, grad_fn=<DivBackward0>) tensor(8053.3408, grad_fn=<DivBackward0>) tensor(5913.4502, grad_fn=<DivBackward0>) tensor(4465.8311, grad_fn=<DivBackward0>) tensor(3484.7944, grad_fn=<DivBackward0>) tensor(2818.2488, grad_fn=<DivBackward0>) tensor(2363.7021, grad_fn=<DivBackward0>) tensor(2052.0852, grad_fn=<DivBackward0>) tensor(1836.8523, grad_fn=<DivBackward0>) tensor(1686.6364, grad_fn=<DivBackward0>) tensor(1580.2994, grad_fn=<DivBackward0>) tensor(1503.5941, grad_fn=<DivBackward0>) tensor(1446.9209, grad_fn=<DivBackward0>) tensor(1403.8086, grad_fn=<DivBackward0>) tensor(1369.8958, grad_fn=<DivBackward0>) tensor(1342.2424, grad_fn=<DivBackward0>) tensor(1318.8674, grad_fn=<DivBackward0>) tensor(1298.4343, grad_fn=<DivBackward0>) tensor(1280.0411, grad_fn=<DivBackward0>) tensor(1263.0803, grad_fn=<DivBackward0>) tensor(1247.1420, grad_fn=<DivBackward0>) tensor(1231.9475, grad_fn=<DivBackward0>) tensor(1217.3105, grad_fn=<DivBackward0>) tensor(1203.1034, grad_fn=<DivBackward0>) tensor(1189.2397, grad_fn=<DivBackward0>) tensor(1175.6603, grad_fn=<DivBackward0>) tensor(1162.3256, grad_fn=<DivBackward0>) tensor(1149.2073, grad_fn=<DivBackward0>) tensor(1136.2859, grad_fn=<DivBackward0>) tensor(1123.5476, grad_fn=<DivBackward0>) tensor(1110.9834, grad_fn=<DivBackward0>) tensor(1098.5847, grad_fn=<DivBackward0>) tensor(1086.3470, grad_fn=<DivBackward0>) tensor(1074.2654, grad_fn=<DivBackward0>) tensor(1062.3367, grad_fn=<DivBackward0>) tensor(1050.5574, grad_fn=<DivBackward0>) tensor(1038.9255, grad_fn=<DivBackward0>) tensor(1027.4382, grad_fn=<DivBackward0>) tensor(1016.0939, grad_fn=<DivBackward0>) tensor(1004.8900, grad_fn=<DivBackward0>) tensor(993.8254, grad_fn=<DivBackward0>) tensor(982.8971, grad_fn=<DivBackward0>) tensor(972.1041, grad_fn=<DivBackward0>) tensor(961.4445, grad_fn=<DivBackward0>) tensor(950.9164, grad_fn=<DivBackward0>) tensor(940.5187, grad_fn=<DivBackward0>) tensor(930.2490, grad_fn=<DivBackward0>) tensor(920.1061, grad_fn=<DivBackward0>) tensor(910.0883, grad_fn=<DivBackward0>) tensor(900.1940, grad_fn=<DivBackward0>) tensor(890.4218, grad_fn=<DivBackward0>) tensor(880.7700, grad_fn=<DivBackward0>) tensor(871.2368, grad_fn=<DivBackward0>) tensor(861.8214, grad_fn=<DivBackward0>) tensor(852.5216, grad_fn=<DivBackward0>) tensor(843.3366, grad_fn=<DivBackward0>) tensor(834.2646, grad_fn=<DivBackward0>) tensor(825.3044, grad_fn=<DivBackward0>) tensor(816.4542, grad_fn=<DivBackward0>) tensor(807.7127, grad_fn=<DivBackward0>) tensor(799.0790, grad_fn=<DivBackward0>) tensor(790.5511, grad_fn=<DivBackward0>) tensor(782.1284, grad_fn=<DivBackward0>) tensor(773.8087, grad_fn=<DivBackward0>) tensor(765.5916, grad_fn=<DivBackward0>) tensor(757.4748, grad_fn=<DivBackward0>) tensor(749.4580, grad_fn=<DivBackward0>) tensor(741.5395, grad_fn=<DivBackward0>) tensor(733.7183, grad_fn=<DivBackward0>) tensor(725.9926, grad_fn=<DivBackward0>) tensor(718.3619, grad_fn=<DivBackward0>) tensor(710.8245, grad_fn=<DivBackward0>) tensor(703.3796, grad_fn=<DivBackward0>) tensor(696.0258, grad_fn=<DivBackward0>) tensor(688.7620, grad_fn=<DivBackward0>) tensor(681.5871, grad_fn=<DivBackward0>) tensor(674.5001, grad_fn=<DivBackward0>) tensor(667.4996, grad_fn=<DivBackward0>) tensor(660.5845, grad_fn=<DivBackward0>) tensor(653.7544, grad_fn=<DivBackward0>) tensor(647.0074, grad_fn=<DivBackward0>) tensor(640.3429, grad_fn=<DivBackward0>) tensor(633.7597, grad_fn=<DivBackward0>) tensor(627.2569, grad_fn=<DivBackward0>) tensor(620.8336, grad_fn=<DivBackward0>) tensor(614.4883, grad_fn=<DivBackward0>) tensor(608.2206, grad_fn=<DivBackward0>) tensor(602.0291, grad_fn=<DivBackward0>) tensor(595.9129, grad_fn=<DivBackward0>) tensor(589.8712, grad_fn=<DivBackward0>) tensor(583.9033, grad_fn=<DivBackward0>) tensor(578.0078, grad_fn=<DivBackward0>) tensor(572.1842, grad_fn=<DivBackward0>) tensor(566.4313, grad_fn=<DivBackward0>) tensor(560.7484, grad_fn=<DivBackward0>)
In [38]:
preds = model(inputs)
preds
Out[38]:
tensor([[ 56.5189,  80.4242],
        [ 73.8740,  98.5922],
        [138.6800, 121.4243],
        [ 17.6291,  94.4109],
        [ 89.2157,  82.1298]], grad_fn=<AddBackward0>)
In [39]:
targets
Out[39]:
tensor([[ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.]])
In [40]:
# Train for another 100 epochs
for i in range(100):
    preds = model(inputs)
    loss = mse(preds, targets)
    loss.backward()
    with torch.no_grad():
        w -= w.grad * 1e-5
        b -= b.grad * 1e-5
        w.grad.zero_()
        b.grad.zero_()
    print(loss)
tensor(555.1344, grad_fn=<DivBackward0>) tensor(549.5885, grad_fn=<DivBackward0>) tensor(544.1100, grad_fn=<DivBackward0>) tensor(538.6979, grad_fn=<DivBackward0>) tensor(533.3516, grad_fn=<DivBackward0>) tensor(528.0699, grad_fn=<DivBackward0>) tensor(522.8524, grad_fn=<DivBackward0>) tensor(517.6979, grad_fn=<DivBackward0>) tensor(512.6060, grad_fn=<DivBackward0>) tensor(507.5756, grad_fn=<DivBackward0>) tensor(502.6060, grad_fn=<DivBackward0>) tensor(497.6964, grad_fn=<DivBackward0>) tensor(492.8463, grad_fn=<DivBackward0>) tensor(488.0546, grad_fn=<DivBackward0>) tensor(483.3210, grad_fn=<DivBackward0>) tensor(478.6444, grad_fn=<DivBackward0>) tensor(474.0242, grad_fn=<DivBackward0>) tensor(469.4597, grad_fn=<DivBackward0>) tensor(464.9500, grad_fn=<DivBackward0>) tensor(460.4949, grad_fn=<DivBackward0>) tensor(456.0934, grad_fn=<DivBackward0>) tensor(451.7448, grad_fn=<DivBackward0>) tensor(447.4483, grad_fn=<DivBackward0>) tensor(443.2037, grad_fn=<DivBackward0>) tensor(439.0100, grad_fn=<DivBackward0>) tensor(434.8667, grad_fn=<DivBackward0>) tensor(430.7731, grad_fn=<DivBackward0>) tensor(426.7283, grad_fn=<DivBackward0>) tensor(422.7323, grad_fn=<DivBackward0>) tensor(418.7843, grad_fn=<DivBackward0>) tensor(414.8832, grad_fn=<DivBackward0>) tensor(411.0291, grad_fn=<DivBackward0>) tensor(407.2209, grad_fn=<DivBackward0>) tensor(403.4583, grad_fn=<DivBackward0>) tensor(399.7406, grad_fn=<DivBackward0>) tensor(396.0674, grad_fn=<DivBackward0>) tensor(392.4380, grad_fn=<DivBackward0>) tensor(388.8520, grad_fn=<DivBackward0>) tensor(385.3084, grad_fn=<DivBackward0>) tensor(381.8072, grad_fn=<DivBackward0>) tensor(378.3478, grad_fn=<DivBackward0>) tensor(374.9294, grad_fn=<DivBackward0>) tensor(371.5516, grad_fn=<DivBackward0>) tensor(368.2141, grad_fn=<DivBackward0>) tensor(364.9159, grad_fn=<DivBackward0>) tensor(361.6572, grad_fn=<DivBackward0>) tensor(358.4370, grad_fn=<DivBackward0>) tensor(355.2549, grad_fn=<DivBackward0>) tensor(352.1107, grad_fn=<DivBackward0>) tensor(349.0036, grad_fn=<DivBackward0>) tensor(345.9332, grad_fn=<DivBackward0>) tensor(342.8991, grad_fn=<DivBackward0>) tensor(339.9009, grad_fn=<DivBackward0>) tensor(336.9380, grad_fn=<DivBackward0>) tensor(334.0101, grad_fn=<DivBackward0>) tensor(331.1166, grad_fn=<DivBackward0>) tensor(328.2573, grad_fn=<DivBackward0>) tensor(325.4315, grad_fn=<DivBackward0>) tensor(322.6393, grad_fn=<DivBackward0>) tensor(319.8795, grad_fn=<DivBackward0>) tensor(317.1523, grad_fn=<DivBackward0>) tensor(314.4571, grad_fn=<DivBackward0>) tensor(311.7936, grad_fn=<DivBackward0>) tensor(309.1610, grad_fn=<DivBackward0>) tensor(306.5593, grad_fn=<DivBackward0>) tensor(303.9884, grad_fn=<DivBackward0>) tensor(301.4473, grad_fn=<DivBackward0>) tensor(298.9359, grad_fn=<DivBackward0>) tensor(296.4539, grad_fn=<DivBackward0>) tensor(294.0005, grad_fn=<DivBackward0>) tensor(291.5760, grad_fn=<DivBackward0>) tensor(289.1795, grad_fn=<DivBackward0>) tensor(286.8110, grad_fn=<DivBackward0>) tensor(284.4702, grad_fn=<DivBackward0>) tensor(282.1564, grad_fn=<DivBackward0>) tensor(279.8694, grad_fn=<DivBackward0>) tensor(277.6089, grad_fn=<DivBackward0>) tensor(275.3747, grad_fn=<DivBackward0>) tensor(273.1664, grad_fn=<DivBackward0>) tensor(270.9835, grad_fn=<DivBackward0>) tensor(268.8258, grad_fn=<DivBackward0>) tensor(266.6930, grad_fn=<DivBackward0>) tensor(264.5848, grad_fn=<DivBackward0>) tensor(262.5010, grad_fn=<DivBackward0>) tensor(260.4409, grad_fn=<DivBackward0>) tensor(258.4048, grad_fn=<DivBackward0>) tensor(256.3920, grad_fn=<DivBackward0>) tensor(254.4023, grad_fn=<DivBackward0>) tensor(252.4353, grad_fn=<DivBackward0>) tensor(250.4910, grad_fn=<DivBackward0>) tensor(248.5687, grad_fn=<DivBackward0>) tensor(246.6686, grad_fn=<DivBackward0>) tensor(244.7901, grad_fn=<DivBackward0>) tensor(242.9329, grad_fn=<DivBackward0>) tensor(241.0970, grad_fn=<DivBackward0>) tensor(239.2821, grad_fn=<DivBackward0>) tensor(237.4877, grad_fn=<DivBackward0>) tensor(235.7137, grad_fn=<DivBackward0>) tensor(233.9598, grad_fn=<DivBackward0>) tensor(232.2258, grad_fn=<DivBackward0>)
In [41]:
preds
Out[41]:
tensor([[ 57.0668,  76.1448],
        [ 75.0127,  96.1474],
        [135.2096, 133.8645],
        [ 20.8900,  69.5860],
        [ 89.2938,  92.3630]], grad_fn=<AddBackward0>)
In [42]:
targets
Out[42]:
tensor([[ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.]])
In [43]:
# PyTorch torch.nn
import torch.nn as nn
In [44]:
inputs
Out[44]:
tensor([[ 73.,  67.,  43.],
        [ 91.,  88.,  64.],
        [ 87., 134.,  58.],
        [102.,  43.,  37.],
        [ 69.,  96.,  70.]])
In [45]:
targets
Out[45]:
tensor([[ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.]])
In [46]:
# Input(temp, rainfall, humidity)
inputs1 = np.array([
    [73, 67, 43],
    [91, 88, 64],
    [87, 134, 58],
    [102, 43, 37],
    [69, 96, 70],
    [73, 67, 43],
    [91, 88, 64],
    [87, 134, 43],
    [102, 43, 37],
    [69, 96, 70],
    [73, 67, 43],
    [91, 88, 64],
    [87, 134, 58],
    [102, 43, 37],
    [69, 96, 70]
], dtype='float32')
In [47]:
# Targets (apples, oranges)
targets1 = np.array([
    [ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.],
    [ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.],[ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.]
], dtype='float32')
In [48]:
inputs1 = torch.from_numpy(inputs1)
targets1 = torch.from_numpy(targets1)
In [49]:
from torch.utils.data import TensorDataset
In [50]:
#Define dataset
train_ds = TensorDataset(inputs1, targets1)
train_ds[0:3]
Out[50]:
(tensor([[ 73.,  67.,  43.],
         [ 91.,  88.,  64.],
         [ 87., 134.,  58.]]), tensor([[ 56.,  70.],
         [ 81., 101.],
         [119., 133.]]))
In [51]:
train_ds[0]
Out[51]:
(tensor([73., 67., 43.]), tensor([56., 70.]))
In [52]:
#创建一个 DataLoader,它可以在训练时将数据分成预定义大小的批次。它还能提供其它效用程序,如数据的混洗和随机采样。
from torch.utils.data import DataLoader
In [53]:
#Define data loader
batch_size = 5
train_dl = DataLoader(train_ds, batch_size, shuffle=True)
In [54]:
#数据加载器
for xb, yb in train_dl:
    print(xb)
    print(yb)
    break
tensor([[102., 43., 37.], [ 91., 88., 64.], [ 69., 96., 70.], [ 91., 88., 64.], [ 91., 88., 64.]]) tensor([[ 22., 37.], [ 81., 101.], [103., 119.], [ 81., 101.], [ 81., 101.]])
In [55]:
#除了人工地实现权重和偏置的初始化,我们还可以使用 PyTorch 的 nn.Linear 类来定义模型,这能自动完成初始化。
# Define model
model = nn.Linear(3, 2)
print (model.weight)
print (model.bias)
Parameter containing: tensor([[-0.5320, -0.2509, -0.0440], [-0.2685, -0.5676, 0.0055]], requires_grad=True) Parameter containing: tensor([-0.1220, 0.5465], requires_grad=True)
In [56]:
list(model.parameters())
Out[56]:
[Parameter containing:
 tensor([[-0.5320, -0.2509, -0.0440],
         [-0.2685, -0.5676,  0.0055]], requires_grad=True),
 Parameter containing:
 tensor([-0.1220,  0.5465], requires_grad=True)]
In [57]:
inputs
Out[57]:
tensor([[ 73.,  67.,  43.],
        [ 91.,  88.,  64.],
        [ 87., 134.,  58.],
        [102.,  43.,  37.],
        [ 69.,  96.,  70.]])
In [58]:
inputs1
Out[58]:
tensor([[ 73.,  67.,  43.],
        [ 91.,  88.,  64.],
        [ 87., 134.,  58.],
        [102.,  43.,  37.],
        [ 69.,  96.,  70.],
        [ 73.,  67.,  43.],
        [ 91.,  88.,  64.],
        [ 87., 134.,  43.],
        [102.,  43.,  37.],
        [ 69.,  96.,  70.],
        [ 73.,  67.,  43.],
        [ 91.,  88.,  64.],
        [ 87., 134.,  58.],
        [102.,  43.,  37.],
        [ 69.,  96.,  70.]])
In [59]:
preds = model(inputs1)
preds
Out[59]:
tensor([[-57.6600, -56.8433],
        [-73.4290, -73.4793],
        [-82.5803, -98.5484],
        [-66.8001, -51.0393],
        [-63.9976, -72.0808],
        [-57.6600, -56.8433],
        [-73.4290, -73.4793],
        [-81.9202, -98.6312],
        [-66.8001, -51.0393],
        [-63.9976, -72.0808],
        [-57.6600, -56.8433],
        [-73.4290, -73.4793],
        [-82.5803, -98.5484],
        [-66.8001, -51.0393],
        [-63.9976, -72.0808]], grad_fn=<AddmmBackward>)
In [60]:
#除了手动定义损失函数,我们也可使用内置的损失函数 mse_loss
# Import nn.functional
import torch.nn.functional as F
In [61]:
#Define loss function
loss_fn = F.mse_loss
In [62]:
loss = loss_fn(model(inputs1), targets)
print(loss)
/home/xjskfc/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: UserWarning: Using a target size (torch.Size([5, 2])) that is different to the input size (torch.Size([15, 2])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size. """Entry point for launching an IPython kernel.
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-62-87143f653e83> in <module> ----> 1 loss = loss_fn(model(inputs1), targets) 2 print(loss) ~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction) 2254 ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret) 2255 else: -> 2256 expanded_input, expanded_target = torch.broadcast_tensors(input, target) 2257 ret = torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction)) 2258 return ret ~/anaconda3/lib/python3.7/site-packages/torch/functional.py in broadcast_tensors(*tensors) 60 [0, 1, 2]]) 61 """ ---> 62 return torch._C._VariableFunctions.broadcast_tensors(tensors) 63 64 RuntimeError: The size of tensor a (15) must match the size of tensor b (5) at non-singleton dimension 0
In [63]:
loss = loss_fn(model(inputs1), targets1)
print(loss)
tensor(25750.9121, grad_fn=<MseLossBackward>)
In [64]:
# 优化器 SGD 随机梯度下降   之所以是「随机」,原因是样本是以批的形式选择(通常会用到随机混洗),而不是作为单独一个数据组
# Define optimizer
opt = torch.optim.SGD(model.parameters(), lr=1e-5)
In [65]:
# 生成预测

#计算损失

#根据权重和偏置计算梯度

#按比例减去少量梯度来调整权重

#将梯度重置为零

# Utility function to train model
def fit(num_epochs, model, loss_fn, opt):
    
    # Repeat for given number of epochs
    for epoch in range(num_epochs):
        
        # Train with batches of data
        for xb, yb in train_dl:
            
            # 1. Generate predictions
            pred = model(xb)
            
            # 2. Calculate loss
            loss = loss_fn(pred, yb)
            
            # 3. Compute gradients
            loss.backward()
            
            # 4. Update parameters using gradients
            opt.step()
            
            # 5. Reset the gradients to zero
            opt.zero_grad()
            
        # print the progress
        if (epoch + 1) % 10 == 0:
            print ('Epoch [{} / {}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
            
In [66]:
fit(100, model, loss_fn, opt)
Epoch [10 / 100], Loss: 376.1974 Epoch [20 / 100], Loss: 227.1646 Epoch [30 / 100], Loss: 175.1957 Epoch [40 / 100], Loss: 104.9177 Epoch [50 / 100], Loss: 79.8791 Epoch [60 / 100], Loss: 98.6772 Epoch [70 / 100], Loss: 50.5110 Epoch [80 / 100], Loss: 36.8641 Epoch [90 / 100], Loss: 22.9128 Epoch [100 / 100], Loss: 14.9912
In [67]:
preds = model(inputs1)
preds
Out[67]:
tensor([[ 58.3943,  72.1866],
        [ 81.6864,  99.9217],
        [120.3652, 134.9600],
        [ 26.2468,  44.5568],
        [ 97.7991, 113.3033],
        [ 58.3943,  72.1866],
        [ 81.6864,  99.9217],
        [113.2504, 125.7890],
        [ 26.2468,  44.5568],
        [ 97.7991, 113.3033],
        [ 58.3943,  72.1866],
        [ 81.6864,  99.9217],
        [120.3652, 134.9600],
        [ 26.2468,  44.5568],
        [ 97.7991, 113.3033]], grad_fn=<AddmmBackward>)
In [68]:
targets1
Out[68]:
tensor([[ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.],
        [ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.],
        [ 56.,  70.],
        [ 81., 101.],
        [119., 133.],
        [ 22.,  37.],
        [103., 119.]])
In [71]:
import jovian
jovian.commit()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-71-598d04295944> in <module> 1 import jovian ----> 2 jovian.commit(key='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmYzJkY2MwNi0xYzBiLTQ1ZmMtOWNhYi05ZWJkNjZlZjgwZDciLCJ0eXBlIjoiYWNjZXNzIiwiaWF0IjoxNTY2MTk1MzE5LCJpZGVudGl0eSI6eyJ1c2VybmFtZSI6ImtyYWtlbjAwMCIsImlkIjo2NTd9LCJuYmYiOjE1NjYxOTUzMTksImV4cCI6MTU3MDA4MzMxOSwiZnJlc2giOmZhbHNlfQ.VJY_gaYkuAvwmdbXhKx5ntFjuZYLwO4igXyEjcneTH4') TypeError: commit() got an unexpected keyword argument 'key'
In [ ]: