Jovian
⭐️
Sign In

基础

In [27]:
import torch
In [6]:
t1 = torch.tensor(4.0)
t1
Out[6]:
tensor(4.)
In [7]:
t2 = torch.tensor([[1,2],[3.,5.1]])
t2
Out[7]:
tensor([[1.0000, 2.0000],
        [3.0000, 5.1000]])
In [12]:
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad=True)
b = torch.tensor(5., requires_grad=True)
y = w * x + b
y
Out[12]:
tensor(17., grad_fn=<AddBackward0>)
In [14]:
y.backward() #计算导数
print('dy/dx', x.grad)
print('dy/dw', w.grad)
print('dy/db', b.grad)
dy/dx None dy/dw tensor(3.) dy/db tensor(1.)
In [15]:
import numpy as np
In [18]:
x = np.array([[1,3],[9,7]], dtype='int64')
x
Out[18]:
array([[1, 3],
       [9, 7]], dtype=int64)
In [20]:
x1 = torch.from_numpy(x)
x1
Out[20]:
tensor([[1, 3],
        [9, 7]])
In [21]:
x1.dtype
Out[21]:
torch.int64
In [26]:
x2 = x1.numpy()
x2
Out[26]:
array([[1, 3],
       [9, 7]], dtype=int64)

线性回归

In [29]:
import numpy as np
import torch
In [30]:
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [87, 134, 58], 
                   [102, 43, 37], 
                   [69, 96, 70]], dtype='float32')
# Targets (apples, oranges)
targets = np.array([[56, 70], 
                    [81, 101], 
                    [119, 133], 
                    [22, 37], 
                    [103, 119]], dtype='float32')
# Convert inputs and targets to tensors
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 [33]:
w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(1, 2, requires_grad=True)

# 模型 function
def model(x):
    return x @ w.t() + b

# Generate predictions
preds = model(inputs)
print(preds)
tensor([[123.2537, 101.3748], [159.6577, 115.4846], [230.4895, 137.9375], [ 94.8721, 146.7149], [165.2026, 74.4557]], grad_fn=<AddBackward0>)
In [35]:
# Loss function
def mse(t1, t2):
    diff = t1 - t2
    return torch.sum(diff * diff) / diff.numel()

# Train for 100 epochs
for i in range(100):
    preds = model(inputs)
    loss = mse(preds, targets)
    print(loss)
    loss.backward()
    with torch.no_grad():
        w -= w.grad * 1e-5
        b -= b.grad * 1e-5
        w.grad.zero_()
        b.grad.zero_()
tensor(4755.9604, grad_fn=<DivBackward0>) tensor(3654.8313, grad_fn=<DivBackward0>) tensor(2908.1609, grad_fn=<DivBackward0>) tensor(2400.4128, grad_fn=<DivBackward0>) tensor(2053.7268, grad_fn=<DivBackward0>) tensor(1815.6326, grad_fn=<DivBackward0>) tensor(1650.7728, grad_fn=<DivBackward0>) tensor(1535.3171, grad_fn=<DivBackward0>) tensor(1453.2076, grad_fn=<DivBackward0>) tensor(1393.6228, grad_fn=<DivBackward0>) tensor(1349.2679, grad_fn=<DivBackward0>) tensor(1315.2279, grad_fn=<DivBackward0>) tensor(1288.1887, grad_fn=<DivBackward0>) tensor(1265.9180, grad_fn=<DivBackward0>) tensor(1246.9094, grad_fn=<DivBackward0>) tensor(1230.1481, grad_fn=<DivBackward0>) tensor(1214.9486, grad_fn=<DivBackward0>) tensor(1200.8495, grad_fn=<DivBackward0>) tensor(1187.5385, grad_fn=<DivBackward0>) tensor(1174.8054, grad_fn=<DivBackward0>) tensor(1162.5066, grad_fn=<DivBackward0>) tensor(1150.5454, grad_fn=<DivBackward0>) tensor(1138.8572, grad_fn=<DivBackward0>) tensor(1127.3961, grad_fn=<DivBackward0>) tensor(1116.1316, grad_fn=<DivBackward0>) tensor(1105.0427, grad_fn=<DivBackward0>) tensor(1094.1140, grad_fn=<DivBackward0>) tensor(1083.3357, grad_fn=<DivBackward0>) tensor(1072.6992, grad_fn=<DivBackward0>) tensor(1062.1996, grad_fn=<DivBackward0>) tensor(1051.8319, grad_fn=<DivBackward0>) tensor(1041.5934, grad_fn=<DivBackward0>) tensor(1031.4811, grad_fn=<DivBackward0>) tensor(1021.4922, grad_fn=<DivBackward0>) tensor(1011.6246, grad_fn=<DivBackward0>) tensor(1001.8773, grad_fn=<DivBackward0>) tensor(992.2477, grad_fn=<DivBackward0>) tensor(982.7342, grad_fn=<DivBackward0>) tensor(973.3358, grad_fn=<DivBackward0>) tensor(964.0506, grad_fn=<DivBackward0>) tensor(954.8768, grad_fn=<DivBackward0>) tensor(945.8136, grad_fn=<DivBackward0>) tensor(936.8594, grad_fn=<DivBackward0>) tensor(928.0128, grad_fn=<DivBackward0>) tensor(919.2720, grad_fn=<DivBackward0>) tensor(910.6362, grad_fn=<DivBackward0>) tensor(902.1041, grad_fn=<DivBackward0>) tensor(893.6742, grad_fn=<DivBackward0>) tensor(885.3451, grad_fn=<DivBackward0>) tensor(877.1154, grad_fn=<DivBackward0>) tensor(868.9847, grad_fn=<DivBackward0>) tensor(860.9506, grad_fn=<DivBackward0>) tensor(853.0131, grad_fn=<DivBackward0>) tensor(845.1702, grad_fn=<DivBackward0>) tensor(837.4207, grad_fn=<DivBackward0>) tensor(829.7635, grad_fn=<DivBackward0>) tensor(822.1981, grad_fn=<DivBackward0>) tensor(814.7223, grad_fn=<DivBackward0>) tensor(807.3356, grad_fn=<DivBackward0>) tensor(800.0367, grad_fn=<DivBackward0>) tensor(792.8247, grad_fn=<DivBackward0>) tensor(785.6981, grad_fn=<DivBackward0>) tensor(778.6566, grad_fn=<DivBackward0>) tensor(771.6984, grad_fn=<DivBackward0>) tensor(764.8228, grad_fn=<DivBackward0>) tensor(758.0285, grad_fn=<DivBackward0>) tensor(751.3149, grad_fn=<DivBackward0>) tensor(744.6808, grad_fn=<DivBackward0>) tensor(738.1248, grad_fn=<DivBackward0>) tensor(731.6467, grad_fn=<DivBackward0>) tensor(725.2446, grad_fn=<DivBackward0>) tensor(718.9183, grad_fn=<DivBackward0>) tensor(712.6667, grad_fn=<DivBackward0>) tensor(706.4889, grad_fn=<DivBackward0>) tensor(700.3837, grad_fn=<DivBackward0>) tensor(694.3503, grad_fn=<DivBackward0>) tensor(688.3878, grad_fn=<DivBackward0>) tensor(682.4955, grad_fn=<DivBackward0>) tensor(676.6725, grad_fn=<DivBackward0>) tensor(670.9177, grad_fn=<DivBackward0>) tensor(665.2305, grad_fn=<DivBackward0>) tensor(659.6095, grad_fn=<DivBackward0>) tensor(654.0548, grad_fn=<DivBackward0>) tensor(648.5651, grad_fn=<DivBackward0>) tensor(643.1392, grad_fn=<DivBackward0>) tensor(637.7767, grad_fn=<DivBackward0>) tensor(632.4772, grad_fn=<DivBackward0>) tensor(627.2390, grad_fn=<DivBackward0>) tensor(622.0622, grad_fn=<DivBackward0>) tensor(616.9454, grad_fn=<DivBackward0>) tensor(611.8882, grad_fn=<DivBackward0>) tensor(606.8898, grad_fn=<DivBackward0>) tensor(601.9493, grad_fn=<DivBackward0>) tensor(597.0663, grad_fn=<DivBackward0>) tensor(592.2399, grad_fn=<DivBackward0>) tensor(587.4691, grad_fn=<DivBackward0>) tensor(582.7541, grad_fn=<DivBackward0>) tensor(578.0932, grad_fn=<DivBackward0>) tensor(573.4862, grad_fn=<DivBackward0>) tensor(568.9323, grad_fn=<DivBackward0>)

PyTorch Built-in

In [40]:
import torch.nn as nn
# Input (temp, rainfall, humidity)
inputs = 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, 58], [102, 43, 37], 
                   [69, 96, 70], [73, 67, 43], [91, 88, 64], 
                   [87, 134, 58], [102, 43, 37], [69, 96, 70]], 
                  dtype='float32')

# Targets (apples, oranges)
targets = 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')

inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
In [41]:
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
import torch.nn.functional as F


train_ds = TensorDataset(inputs, targets)

# Define data loader
batch_size = 5
train_dl = DataLoader(train_ds, batch_size, shuffle=True)


# Define loss function
loss_fn = F.mse_loss

# Define model
model = nn.Linear(3, 2)
opt = torch.optim.SGD(model.parameters(), lr=1e-5)

# Utility function to train the 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()))

            
fit(100, model, loss_fn, opt)
Epoch [10/100], Loss: 493.9990 Epoch [20/100], Loss: 384.2797 Epoch [30/100], Loss: 252.3064 Epoch [40/100], Loss: 251.5859 Epoch [50/100], Loss: 172.6054 Epoch [60/100], Loss: 54.9546 Epoch [70/100], Loss: 87.8977 Epoch [80/100], Loss: 68.6918 Epoch [90/100], Loss: 56.7266 Epoch [100/100], Loss: 41.3089
In [ ]:
import jovian
jovian.commit()

[jovian] Saving notebook..
[jovian] Creating a new notebook on https://jvn.io [jovian] Please enter your API key (from https://jvn.io ):
In [ ]: