0
点赞
收藏
分享

微信扫一扫

pytorch代码笔记

/watch?v=c36lUUr864M&t=936s
未完待续

基本操作

import torch
import numpy as np

device = torch.device("cpu")

if torch.cuda.is_available():
    device = torch.device("cuda")

x = torch.ones(2,2,dtype=torch.double)
x.size()
x = torch.rand(2,2,requires_grad=True,device=device)
y = torch.rand(2,2,device=device)
z = x+y #+-*/
z = torch.add(x,y)#add sub mul
y.add_(x)# sub_ div_ 原地操作
x=torch.rand(4,4)
x[1][1].item()

y=x.view(-1,2) #插眼

a = torch.ones(5)
a.to("cuda")
a.add_(13)
b = a.numpy()

a = np.ones(6)
b = torch.from_numpy(a)
b.to(device)

print(a)
print(b)

Gradients

import torch
import numpy as np

x = torch.rand(3,requires_grad=True)#***
print(x)
y = x+2
z = y*y*2
#z = z.mean()

print(z)

v = torch.tensor([0.1,1.0,0.001],dtype=torch.float32)

z.backward(v)# 插眼:如果不是标量则必须给vecor ***
print(x.grad)

x = torch.rand(3,requires_grad=True)

#dont calculate 插眼
#x.requires_grad_(False)
#y = x.detach()
#with torch.no_grad():
#    y = x + 2
#    print(y)

weights = torch.ones(4,requires_grad=True)

for epoch in range(3):
    model_output = (weights*3).sum()
    model_output.backward()
    print(weights.grad)

    weights.grad.zero_() #将积累的计算清零 插眼,需要深入理解 ***

#optimizer
#optimizer = torch.optim.SGD([weights], lr=0.01)
#optimizer.step()
#optimizer.zero_grad()

Backpropagation

import torch
import numpy as np

x = torch.tensor(1.0)
y = torch.tensor(2.0)

w = torch.tensor(1.0,requires_grad=True)

y_hat = w * x
loss = (y_hat-y)**2

print(loss)

loss.backward()
print(w.grad)

Gradient Descent

image-20220316151942503

import torch
import numpy as np


# f = w * x
# f = 2 * x
x = np.array([1,2,3,4],dtype=np.float32)
y = np.array([2,4,6,8],dtype=np.float32)
w = 0.0


# model prediction
def forward(x):
    return w*x

# loss MSE
def loss(y,y_predicted):
    return ((y_predicted-y)**2).mean()

# gradient
# MSE = 1/N * (w*x -y)**2
def gradient(x,y,y_predicted):
    return np.dot(2*x,y_predicted-y).mean()

print(f'prediction before training: f(5) = {forward(5):.3f}')

# Training
learning_rate = 0.01
n_iters = 10

for epoch in range(n_iters):
    # prediction = forward pass
    y_pred = forward(x)

    #loss
    l = loss(y,y_pred)

    # gradients
    dw = gradient(x,y,y_pred)

    #update weights
    w-=learning_rate * dw

    if epoch%1==0:
        print(f'epoch {epoch+1}:w = {w:.3f},loss = {l:.8f}')
        
print(f'Prediction after training: f(5) = {forward(5):.3f}')

image-20220316145349626

import torch


# f = w * x
# f = 2 * x
x = torch.tensor([1,2,3,4],dtype=torch.float32)
y = torch.tensor([2,4,6,8],dtype=torch.float32)
w = torch.tensor(0.0,dtype=torch.float32,requires_grad=True)


# model prediction
def forward(x):
    return w*x

# loss MSE
def loss(y,y_predicted):
    return ((y_predicted-y)**2).mean()

# gradient
# MSE = 1/N * (w*x -y)**2
def gradient(x,y,y_predicted):
    return np.dot(2*x,y_predicted-y).mean()

print(f'prediction before training: f(5) = {forward(5):.3f}')

# Training
learning_rate = 0.01
n_iters = 10

for epoch in range(n_iters):
    # prediction = forward pass
    y_pred = forward(x)

    #loss
    l = loss(y,y_pred)

    # gradients
    #dw = gradient(x,y,y_pred)
    l.backward()

    #update weights
    #w-=learning_rate * dw
    with torch.no_grad():
        w -= learning_rate * w.grad
    
    #zero gradients
    w.grad.zero_()        

    if epoch%1==0:
        print(f'epoch {epoch+1}:w = {w:.3f},loss = {l:.8f}')
        
print(f'Prediction after training: f(5) = {forward(5):.3f}')

Training pipeline

  1. Design model (input,output size,forward pass)
  2. Construct loss and optimizer
  3. Training loop
    • forward pass: compute and prediction
    • backward pass: gradients
    • update weights

image-20220316153456524

import torch
import torch.nn as nn

# f = w * x
# f = 2 * x
x = torch.tensor([[1],[2],[3],[4]],dtype=torch.float32)
y = torch.tensor([[2],[4],[6],[8]],dtype=torch.float32)
w = torch.tensor(0.0,dtype=torch.float32,requires_grad=True)

x_test = torch.tensor([5],dtype=torch.float32)


n_samples,n_features = x.shape
print(n_samples,n_features)

input_size = n_features
output_size = n_features

class LinearRegression(nn.Module):

    def __init__(self,input_dim,output_dim):
        super(LinearRegression,self).__init__()
        self.lin = nn.Linear(input_dim,output_dim)

    def forward(self,x):
        return self.lin(x)
    
#model = nn.Linear(input_size,output_size)
model = LinearRegression(input_size,output_size)


# model prediction
def forward(x):
    return w*x

# loss MSE
def loss(y,y_predicted):
    return ((y_predicted-y)**2).mean()

# gradient
# MSE = 1/N * (w*x -y)**2
def gradient(x,y,y_predicted):
    return np.dot(2*x,y_predicted-y).mean()

print(f'Prediction before training: f(5) = {model(x_test).item():.3f}')

# Training
learning_rate = 0.1
n_iters = 300
loss = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)

for epoch in range(n_iters):
    # prediction = forward pass
    y_pred = forward(x)
    #y_pred = model(x)
    
    #loss
    l = loss(y,y_pred)

    # gradients
    #dw = gradient(x,y,y_pred)
    l.backward()

    #update weights
    #w-=learning_rate * dw
    #with torch.no_grad():
    #    w -= learning_rate * w.grad
    optimizer.step()
    
    #zero gradients
    #w.grad.zero_()
    optimizer.zero_grad()

    if epoch%1==0:
        [w,b] = model.parameters()
        print(f'epoch {epoch+1}:w = {w[0][0].item():.3f},loss = {l:.8f}')
        
print(f'Prediction after training: f(5) = {model(x_test).item():.3f}')

image-20220316153507880

import torch
import torch.nn as nn



# f = w * x
# f = 2 * x
x = torch.tensor([[1],[2],[3],[4]],dtype=torch.float32)
y = torch.tensor([[2],[4],[6],[8]],dtype=torch.float32)
#w = torch.tensor(0.0,dtype=torch.float32,requires_grad=True)

x_test = torch.tensor([5],dtype=torch.float32)


n_samples,n_features = x.shape
print(n_samples,n_features)

input_size = n_features
output_size = n_features

class LinearRegression(nn.Module):

    def __init__(self,input_dim,output_dim):
        super(LinearRegression,self).__init__()
        self.lin = nn.Linear(input_dim,output_dim)

    def forward(self,x):
        return self.lin(x)
    
#model = nn.Linear(input_size,output_size)
model = LinearRegression(input_size,output_size)


# model prediction
def forward(x):
    return w*x

# loss MSE
def loss(y,y_predicted):
    return ((y_predicted-y)**2).mean()

# gradient
# MSE = 1/N * (w*x -y)**2
def gradient(x,y,y_predicted):
    return np.dot(2*x,y_predicted-y).mean()

print(f'Prediction before training: f(5) = {model(x_test).item():.3f}')

# Training
learning_rate = 0.1
n_iters = 300
loss = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)

for epoch in range(n_iters):
    # prediction = forward pass
    #y_pred = forward(x)
    y_pred = model(x)
    
    #loss
    l = loss(y,y_pred)

    # gradients
    #dw = gradient(x,y,y_pred)
    l.backward()

    #update weights
    #w-=learning_rate * dw
    #with torch.no_grad():
    #    w -= learning_rate * w.grad
    optimizer.step()
    
    #zero gradients
    #w.grad.zero_()
    optimizer.zero_grad()

    if epoch%1==0:
        [w,b] = model.parameters()
        print(f'epoch {epoch+1}:w = {w[0][0].item():.3f},loss = {l:.8f}')
        
print(f'Prediction after training: f(5) = {model(x_test).item():.3f}')

Linear Regression

  1. Design model (input,output size,forward pass)
  2. Construct loss and optimizer
  3. Training loop
    • forward pass: compute and prediction
    • backward pass: gradients
    • update weights
import torch
import torch.nn as nn
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt


# 0)prepare data
x_numpy,y_numpy = datasets.make_regression(n_samples=100,n_features=1,noise=20,random_state=1)

x = torch.from_numpy(x_numpy.astype(np.float32))
y = torch.from_numpy(y_numpy.astype(np.float32))
y = y.view(y.shape[0],1)# 插眼


n_samples,n_features = x.shape

# 1)model

input_size = n_features
output_size = 1

model = nn.Linear(input_size,output_size)
pr = model(x).detach().numpy()
# 2)loss and optimizer
learning_rate = 0.01
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)

# 3)training loop
num_epochs = 200
for epoch in range(num_epochs):
    # forward pss and loss
    y_predicted = model(x)
    loss = criterion(y_predicted,y)

    # backward pass
    loss.backward()

    #update
    optimizer.step()

    optimizer.zero_grad()

    if(epoch+1)%10 ==0:
        print(f'epoch: {epoch+1},loss = {loss.item():.4f}')

#plot
predicted = model(x).detach().numpy()

plt.plot(x_numpy,y_numpy,'ro')
plt.plot(x_numpy,predicted,'b')
plt.show()

Logistic Regression

import torch
import torch.nn as nn
import numpy as np
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split


# 0)prepare data
bc = datasets.load_breast_cancer()
x,y = bc.data,bc.target

n_samples,n_features = x.shape

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1234)
#sclae
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

x_train = torch.from_numpy(x_train.astype(np.float32))
x_test = torch.from_numpy(x_test.astype(np.float32))
y_train = torch.from_numpy(y_train.astype(np.float32))
y_test = torch.from_numpy(y_test.astype(np.float32))

y_train = y_train.view(y_train.shape[0],1)
y_test = y_test.view(y_test.shape[0],1)

# 1)model
# f = wx+b, sigmod at the end
class LogisticRegression(nn.Module):
   def __init__(self,n_input_features):
       super(LogisticRegression,self).__init__()
       self.linear = nn.Linear(n_input_features,1)

   def forward(self,x):
       y_predicted = torch.sigmoid(self.linear(x))
       return y_predicted
model = LogisticRegression(n_features)


# 2)loss and optimizer

learning_rate = 0.03
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)


# 3)training loop
num_epoch = 10000
for epoch in range(num_epoch):
   #forward pass and loss
   y_predicted = model(x_train)
   loss = criterion(y_predicted,y_train)

   #backward pass
   loss.backward()

   #update
   optimizer.step()

   #zero gradients
   optimizer.zero_grad()

   if (epoch+1)%10==0:
       print(f'epoch:{epoch+1},loss = {loss.item():.4f}')

with torch.no_grad():
   y_predicted = model(x_test)
   y_predicted_cls = y_predicted.round()
   acc = y_predicted_cls.eq(y_test).sum()/float(y_test.shape[0])
   print(f'accuracy = {acc:.4f}')
举报

相关推荐

0 条评论