# rainy-rain/basic-ipynb-26291

2 years ago

## 基础

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.)
y = w * x + b
y
Out[12]:
In [14]:
y.backward() #计算导数
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]:

# 模型 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

# Train for 100 epochs
for i in range(100):
preds = model(inputs)
loss = mse(preds, targets)
print(loss)
loss.backward()

## 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
import torch.nn.functional as F

train_ds = TensorDataset(inputs, targets)

batch_size = 5

# 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)

loss.backward()

# 4. Update parameters using gradients
opt.step()

# 5. Reset the gradients to zero

# 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 [ ]: