- Codes:
import torch
from torch.autograd import Variable
tensor = torch.FloatTensor([[1,2],[3,4]])
variable = Variable(tensor, requires_grad=True)
t_out = torch.mean(tensor*tensor)
v_out = torch.mean(variable*variable)
print(t_out)
print(v_out)
v_out.backward()
print(variable.grad)
print(variable)
print(variable.data)
print(variable.data.numpy())
Output:
tensor(7.5000)
tensor(7.5000, grad_fn=)
tensor([[0.5000, 1.0000],
[1.5000, 2.0000]])
tensor([[1., 2.],
[3., 4.]], requires_grad=True)
tensor([[1., 2.],
[3., 4.]])
[[1. 2.]
[3. 4.]]
2.Codes:
import torch
from torch.autograd import Variable
import torch.nn.functional as F
# import matplotlib.pyplot as plt
# data
x = torch.linspace(-5, 5, 200)
x = Variable(x)
x_np = x.data.numpy()
y_relu = F.relu(x).data.numpy()
y_sigoid = F.sigmoid(x).data.numpy()
y_tanh = F.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()
# plt.figure(1, figsize = (8,6))
# plt.subplot(221)
# plt.plot(x_np, y_relu, c='red', label = 'relu')
# plt.ylim((-1,5))
# plt.legend(loc='best')
Output:
- Codes:
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
# data
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
y = x.pow(2)+0.2*torch.rand(x.size())
plt.scatter(x.data.numpy(), y.data.numpy())
plt.show()
Output:
- Codes:
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
# data
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
y = x.pow(2)+0.2*torch.rand(x.size())
x, y = Variable(x), Variable(y)
# plt.scatter(x.data.numpy(), y.data.numpy())
# plt.show()
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.predict = torch.nn.Linear(n_hidden, n_output)
def forward(self):
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
net = Net(1, 10, 1)
print(net)
Output:
Net(
(hidden): Linear(in_features=1, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=1, bias=True)
)
- Codes: Regression
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
#### Generate the data
# torch.manual_seed(1) # reproducible
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # x data (tensor), shape=(100, 1)
y = x.pow(2) + 0.2*torch.rand(x.size()) # noisy y data (tensor), shape=(100, 1)
# plt.scatter(x.data.numpy(), y.data.numpy())
# plt.show()
#### Define the network
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
net = Net(n_feature=1, n_hidden=10, n_output=1) # define the network
print(net) # net architecture
optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_func = torch.nn.MSELoss() # this is for regression mean squared loss
plt.ion() # something about plotting
for t in range(200):
prediction = net(x) # input x and predict based on x
loss = loss_func(prediction, y) # must be (nn output, target)
optimizer.zero_grad() # clear gradients of last iteration
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
if t % 5 == 0:
# plot and show learning process
plt.cla()
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
plt.text(0.5, 0, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
- Codes: Classification
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
n_data = torch.ones(100,2) # noisy y data (tensor), shape=(100, 1)
x0 = torch.normal(2*n_data, 1) # One group of data
y0 = torch.zeros(100) # Labeled as 0
x1 = torch.normal(-2*n_data, 1) # Another group of data
y1 = torch.ones(100) # Labeled as 1
x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # Combine all x
y = torch.cat((y0, y1), ).type(torch.LongTensor)
# plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
# plt.show()
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
net = Net(n_feature=2, n_hidden=10, n_output=2) # define the network
print(net) # net architecture
optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
loss_func = torch.nn.CrossEntropyLoss()
plt.ion() # something about plotting
for t in range(200):
out = net(x) # input x and predict based on x
loss = loss_func(out, y) # must be (nn output, target)
optimizer.zero_grad() # clear gradients of last iteration
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
if t % 2 == 0:
# plot and show learning process
plt.cla()
prediction = torch.max(out, 1)[1]
pred_y = prediction.data.numpy()
target_y = y.data.numpy()
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn')
accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size)
plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
- Quick
Codes:
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
n_data = torch.ones(100,2) # noisy y data (tensor), shape=(100, 1)
x0 = torch.normal(2*n_data, 1) # One group of data
y0 = torch.zeros(100) # Labeled as 0
x1 = torch.normal(-2*n_data, 1) # Another group of data
y1 = torch.ones(100) # Labeled as 1
x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # Combine all x
y = torch.cat((y0, y1), ).type(torch.LongTensor)
# plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
# plt.show()
# Method 1
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
# Method 2
net2 = torch.nn.Sequential(
torch.nn.Linear(2,10),
torch.nn.ReLU(),
torch.nn.Linear(10,2)
)
net = Net(n_feature=2, n_hidden=10, n_output=2) # define the network
print(net) # net architecture
print(net2)
Output:
Net(
(hidden): Linear(in_features=2, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=2, bias=True)
)
Sequential(
(0): Linear(in_features=2, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=2, bias=True)
)
- CNN
Codes:
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.nn.functional as F
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
# Hyper Parameters
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST, # Already Download
)
#### Plot one example
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(), cmap = 'gray')
# plt.title('%i' % train_data.train_labels[0])
# plt.show()
# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# pick 2000 samples to speed up testing
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labels[:2000]
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d( # ->(1, 28, 28)
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2,
), # ->(16, 28, 28)
nn.ReLU(),
nn.MaxPool2d(kernel_size=2), # ->(16, 14, 14)
)
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 5, 1, 2), # ->(32, 14, 14)
nn.ReLU(),
nn.MaxPool2d(2), # ->(32, 7, 7)
)
self.out = nn.Linear(32*7*7, 10)
def forward(selfs, x):
x = self.conv1(x)
x = self.conv2(x) # (batch, 32, 7, 7)
x = self.view(x.size(0), -1) # (batch, 32*7*7)
output = self.out(x)
return output
cnn = CNN()
print(cnn)
Output:
CNN(
(conv1): Sequential(
(0): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(conv2): Sequential(
(0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(out): Linear(in_features=1568, out_features=10, bias=True)
)
Whole Codes:
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import torch.nn.functional as F
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
# Hyper Parameters
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST, # Already Download
)
#### Plot one example
# print(train_data.train_data.size())
# print(train_data.train_labels.size())
# plt.imshow(train_data.train_data[0].numpy(), cmap = 'gray')
# plt.title('%i' % train_data.train_labels[0])
# plt.show()
# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# pick 2000 samples to speed up testing
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labels[:2000]
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d( # ->(1, 28, 28)
in_channels=1,
out_channels=16,
kernel_size=5,
stride=1,
padding=2,
), # ->(16, 28, 28)
nn.ReLU(),
nn.MaxPool2d(kernel_size=2), # ->(16, 14, 14)
)
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 5, 1, 2), # ->(32, 14, 14)
nn.ReLU(),
nn.MaxPool2d(2), # ->(32, 7, 7)
)
self.out = nn.Linear(32*7*7, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x) # (batch, 32, 7, 7)
x = x.view(x.size(0), -1) # (batch, 32*7*7)
output = self.out(x)
return output
cnn = CNN()
print(cnn)
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()
# Training
for epoch in range(EPOCH):
for step, (x,y) in enumerate(train_loader):
output = cnn(x)
loss = loss_func(output, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
test_output = cnn(test_x)
pred_y = torch.max(test_output, 1)[1].data.numpy()
accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
print('Step: ', step)
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
plt.ioff()
# print 10 predictions from test data
test_output = cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy()
print(pred_y, 'prediction number')
print(test_y[:10].numpy(), 'real number')
Output:
CNN(
(conv1): Sequential(
(0): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(conv2): Sequential(
(0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(out): Linear(in_features=1568, out_features=10, bias=True)
)
Step: 0
Epoch: 0 | train loss: 2.3130 | test accuracy: 0.30
Step: 50
Epoch: 0 | train loss: 0.5708 | test accuracy: 0.78
Step: 100
Epoch: 0 | train loss: 0.2126 | test accuracy: 0.89
Step: 150
Epoch: 0 | train loss: 0.1353 | test accuracy: 0.92
Step: 200
Epoch: 0 | train loss: 0.2118 | test accuracy: 0.92
Step: 250
Epoch: 0 | train loss: 0.0949 | test accuracy: 0.95
Step: 300
Epoch: 0 | train loss: 0.3224 | test accuracy: 0.94
Step: 350
Epoch: 0 | train loss: 0.1817 | test accuracy: 0.96
Step: 400
Epoch: 0 | train loss: 0.3055 | test accuracy: 0.95
Step: 450
Epoch: 0 | train loss: 0.1006 | test accuracy: 0.96
Step: 500
Epoch: 0 | train loss: 0.2240 | test accuracy: 0.96
Step: 550
Epoch: 0 | train loss: 0.1914 | test accuracy: 0.97
Step: 600
Epoch: 0 | train loss: 0.0508 | test accuracy: 0.97
Step: 650
Epoch: 0 | train loss: 0.1072 | test accuracy: 0.97
Step: 700
Epoch: 0 | train loss: 0.0204 | test accuracy: 0.97
Step: 750
Epoch: 0 | train loss: 0.0480 | test accuracy: 0.97
Step: 800
Epoch: 0 | train loss: 0.3094 | test accuracy: 0.97
Step: 850
Epoch: 0 | train loss: 0.0889 | test accuracy: 0.97
Step: 900
Epoch: 0 | train loss: 0.3791 | test accuracy: 0.98
Step: 950
Epoch: 0 | train loss: 0.0915 | test accuracy: 0.97
Step: 1000
Epoch: 0 | train loss: 0.0809 | test accuracy: 0.98
Step: 1050
Epoch: 0 | train loss: 0.0346 | test accuracy: 0.97
Step: 1100
Epoch: 0 | train loss: 0.0224 | test accuracy: 0.98
Step: 1150
Epoch: 0 | train loss: 0.0914 | test accuracy: 0.98
[7 2 1 0 4 1 4 9 5 9] prediction number
[7 2 1 0 4 1 4 9 5 9] real number