mirror of
https://github.com/yunjey/pytorch-tutorial.git
synced 2025-07-21 23:16:45 +08:00
add examples to pytorch basics
This commit is contained in:
@ -8,42 +8,104 @@ import torchvision.datasets as dsets
|
|||||||
from torch.autograd import Variable
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
# Create a torch tensor with random normal.
|
#========================== Table of Contents ==========================#
|
||||||
x = torch.randn(5, 3)
|
# 1. Basic autograd example 1 (Line 21 to 36)
|
||||||
print (x)
|
# 2. Basic autograd example 2 (Line 39 to 80)
|
||||||
|
# 3. Loading data from numpy (Line 83 to 86)
|
||||||
|
# 4. Implementing the input pipline (Line 90 to 117)
|
||||||
|
# 5. Input pipline for custom dataset (Line 119 to 139)
|
||||||
|
# 6. Using pretrained model (Line142 to 156)
|
||||||
|
# 7. Save and load model (Line 159 to L161)
|
||||||
|
|
||||||
# Build a layer.
|
|
||||||
|
#======================= Basic autograd example 1 =======================#
|
||||||
|
# Create tensors.
|
||||||
|
x = Variable(torch.Tensor([1]), requires_grad=True)
|
||||||
|
w = Variable(torch.Tensor([2]), requires_grad=True)
|
||||||
|
b = Variable(torch.Tensor([3]), requires_grad=True)
|
||||||
|
|
||||||
|
# Build a computational graph.
|
||||||
|
y = w * x + b # y = 2 * x + 3
|
||||||
|
|
||||||
|
# Compute gradients
|
||||||
|
y.backward()
|
||||||
|
|
||||||
|
# Print out the gradients
|
||||||
|
print(x.grad) # x.grad = 2
|
||||||
|
print(w.grad) # w.grad = 1
|
||||||
|
print(b.grad) # b.grad = 1
|
||||||
|
|
||||||
|
|
||||||
|
#======================== Basic autograd example 2 =======================#
|
||||||
|
# Create tensors.
|
||||||
|
x = Variable(torch.randn(5, 3))
|
||||||
|
y = Variable(torch.randn(5, 2))
|
||||||
|
print ('x: ', x)
|
||||||
|
print ('y: ', y)
|
||||||
|
|
||||||
|
# Build a linear layer.
|
||||||
linear = nn.Linear(3, 2)
|
linear = nn.Linear(3, 2)
|
||||||
print (linear.weight)
|
print ('w: ', linear.weight)
|
||||||
print (linear.bias)
|
print ('b: ', linear.bias)
|
||||||
|
|
||||||
# Forward propagate.
|
# Build Loss and Optimizer.
|
||||||
y = linear(Variable(x))
|
criterion = nn.MSELoss()
|
||||||
print (y)
|
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)
|
||||||
|
|
||||||
# Convert numpy array to torch tensor.
|
# Forward propagation.
|
||||||
|
pred = linear(x)
|
||||||
|
print('pred: ', pred)
|
||||||
|
|
||||||
|
# Compute loss.
|
||||||
|
loss = criterion(pred, y)
|
||||||
|
print('loss: ', loss.data[0])
|
||||||
|
|
||||||
|
# Backpropagation.
|
||||||
|
loss.backward()
|
||||||
|
|
||||||
|
# Print out the gradients.
|
||||||
|
print ('dL/dw: ', linear.weight.grad)
|
||||||
|
print ('dL/db: ', linear.bias.grad)
|
||||||
|
|
||||||
|
# 1-step Optimization (gradient descent).
|
||||||
|
optimizer.step()
|
||||||
|
print ('Optimized..!')
|
||||||
|
|
||||||
|
# You can also do optimization at the low level as shown below.
|
||||||
|
# linear.weight.data.sub_(0.01 * linear.weight.grad.data)
|
||||||
|
# linear.bias.data.sub_(0.01 * linear.bias.grad.data)
|
||||||
|
|
||||||
|
# Print out the loss after optimization.
|
||||||
|
loss = criterion(pred, y)
|
||||||
|
print('loss after 1 step optimization: ', loss.data[0])
|
||||||
|
|
||||||
|
|
||||||
|
#======================== Loading data from numpy ========================#
|
||||||
a = np.array([[1,2], [3,4]])
|
a = np.array([[1,2], [3,4]])
|
||||||
b = torch.from_numpy(a)
|
b = torch.from_numpy(a)
|
||||||
print (b)
|
print (b)
|
||||||
|
|
||||||
# Download and load cifar10 dataset .
|
|
||||||
train_dataset = dsets.CIFAR10(root='./data/',
|
|
||||||
|
#===================== Implementing the input pipline =====================#
|
||||||
|
# Download and construct dataset.
|
||||||
|
train_dataset = dsets.CIFAR10(root='../data/',
|
||||||
train=True,
|
train=True,
|
||||||
transform=transforms.ToTensor(),
|
transform=transforms.ToTensor(),
|
||||||
download=True)
|
download=True)
|
||||||
|
|
||||||
# Select one data pair.
|
# Select one data pair (read data from disk).
|
||||||
image, label = train_dataset[0]
|
image, label = train_dataset[0]
|
||||||
print (image.size())
|
print (image.size())
|
||||||
print (label)
|
print (label)
|
||||||
|
|
||||||
# Input pipeline (this provides queue and thread in a very simple way).
|
# Data Loader (this provides queue and thread in a very simple way).
|
||||||
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
batch_size=100,
|
batch_size=100,
|
||||||
shuffle=True,
|
shuffle=True,
|
||||||
num_workers=2)
|
num_workers=2)
|
||||||
|
|
||||||
# When iteration starts, queue and thread start to load dataset.
|
# When iteration starts, queue and thread start to load dataset from files.
|
||||||
data_iter = iter(train_loader)
|
data_iter = iter(train_loader)
|
||||||
|
|
||||||
# Mini-batch images and labels.
|
# Mini-batch images and labels.
|
||||||
@ -54,36 +116,46 @@ for images, labels in train_loader:
|
|||||||
# Your training code will be written here
|
# Your training code will be written here
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Build custom dataset.
|
#===================== Input pipline for custom dataset =====================#
|
||||||
|
# You should build custom dataset as below.
|
||||||
class CustomDataset(data.Dataset):
|
class CustomDataset(data.Dataset):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# TODO
|
||||||
|
# 1. Initialize file path or list of file names.
|
||||||
pass
|
pass
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
# TODO
|
# TODO
|
||||||
# 1. Read one data from file (e.g. using np.fromfile, PIL.Image.open).
|
# 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
|
||||||
# 2. Return a data pair (e.g. image and label).
|
# 2. Return a data pair (e.g. image and label).
|
||||||
pass
|
pass
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
# You should change 0 to the total size of your dataset.
|
# You should change 0 to the total size of your dataset.
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
# Then, you can just use prebuilt torch's data loader.
|
||||||
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
batch_size=100,
|
batch_size=100,
|
||||||
shuffle=True,
|
shuffle=True,
|
||||||
num_workers=2)
|
num_workers=2)
|
||||||
|
|
||||||
|
|
||||||
# Download and load pretrained model.
|
#========================== Using pretrained model ==========================#
|
||||||
|
# Download and load pretrained resnet.
|
||||||
resnet = torchvision.models.resnet18(pretrained=True)
|
resnet = torchvision.models.resnet18(pretrained=True)
|
||||||
|
|
||||||
# Detach top layer for finetuning.
|
# If you want to finetune only top layer of the model.
|
||||||
sub_model = nn.Sequential(*list(resnet.children())[:-1])
|
for param in resnet.parameters():
|
||||||
|
param.requires_grad = False
|
||||||
|
|
||||||
|
# Replace top layer for finetuning.
|
||||||
|
resnet.fc = nn.Linear(resnet.fc.in_features, 100) # 100 is for example.
|
||||||
|
|
||||||
# For test
|
# For test
|
||||||
images = Variable(torch.randn(10, 3, 256, 256))
|
images = Variable(torch.randn(10, 3, 256, 256))
|
||||||
print (resnet(images).size())
|
outputs = resnet(images)
|
||||||
print (sub_model(images).size())
|
print (outputs.size()) # (10, 100)
|
||||||
|
|
||||||
# Save and load the model.
|
|
||||||
torch.save(sub_model, 'model.pkl')
|
#============================ Save and load model ============================#
|
||||||
|
torch.save(resnet, 'model.pkl')
|
||||||
model = torch.load('model.pkl')
|
model = torch.load('model.pkl')
|
Reference in New Issue
Block a user