Skip to content

Commit 93985c4

Browse files
author
xuming06
committed
add pytorch demo.
1 parent 4a1dfc0 commit 93985c4

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

20pytorch/01.tensor.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief: http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html
4+
import torch
5+
6+
x = torch.Tensor(5, 2)
7+
print(x)
8+
9+
y = torch.rand(3, 4)
10+
print(y)
11+
print(len(y))
12+
print(y.size())
13+
14+
z = torch.rand(3, 1)
15+
print(z)
16+
o = torch.add(y, z)
17+
print(o)
18+
19+
k = torch.Tensor(3, 4)
20+
torch.add(y, z, out=k)
21+
print(k)
22+
23+
a = torch.ones(3)
24+
print(a)
25+
26+
b = torch.FloatTensor(3, 4)
27+
print(b)
28+
29+
b = a.add_(2)
30+
print(b)
31+
32+
import numpy as np
33+
34+
a = np.ones(3)
35+
b = torch.from_numpy(a)
36+
np.add(a, 1, out=a)
37+
print(a)
38+
print(b)

20pytorch/02.variable.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief: http://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#variable
4+
import torch
5+
from torch.autograd import Variable
6+
7+
a = Variable(torch.ones(3, 3), requires_grad=True)
8+
print(a)
9+
10+
b = a + 2
11+
print(b)
12+
13+
c = b * b * 2
14+
out = c.mean()
15+
print(c, out)
16+
17+
print('a.grad:')
18+
print(a.grad)
19+
20+
print('a.grad after backward:')
21+
out.backward()
22+
print(a.grad)
23+
24+
print('y = y * 2 result:')
25+
x = torch.randn(3)
26+
print('raw x:',x)
27+
x = Variable(x, requires_grad=True)
28+
y = x * 2
29+
while y.data.norm() < 1000:
30+
y = y * 2
31+
print(y)

20pytorch/03.neural_network.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief: http://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#define-the-network
4+
5+
# 1. define network
6+
import torch
7+
from torch.autograd import Variable
8+
import torch.nn as nn
9+
import torch.nn.functional as F
10+
11+
12+
class Network(nn.Module):
13+
def __init__(self):
14+
super(Network, self).__init__()
15+
self.conv1 = nn.Conv2d(1, 6, 5)
16+
self.conv2 = nn.Conv2d(6, 16, 5)
17+
self.fc1 = nn.Linear(16 * 5 * 5, 120)
18+
self.fc2 = nn.Linear(120, 84)
19+
self.fc3 = nn.Linear(84, 10)
20+
21+
def forward(self, x):
22+
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
23+
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
24+
x = x.view(-1, self.num_flat_features(x))
25+
x = F.relu(self.fc1(x))
26+
x = F.relu(self.fc2(x))
27+
x = self.fc3(x)
28+
return x
29+
30+
def num_flat_features(self, x):
31+
size = x.size()[1:]
32+
num_features = 1
33+
for s in size:
34+
num_features *= s
35+
return num_features
36+
37+
38+
network = Network()
39+
print(network)
40+
41+
params = list(network.parameters())
42+
print(len(params))
43+
print(params[0].size())
44+
45+
input = Variable(torch.randn(1, 1, 32, 32))
46+
out = network(input)
47+
print(out)
48+
49+
network.zero_grad()
50+
out.backward(torch.randn(1, 10))
51+
52+
# loss function
53+
output = network(input)
54+
target = Variable(torch.arange(1, 11))
55+
target = target.view(1, -1)
56+
criterion = nn.MSELoss()
57+
loss = criterion(output, target)
58+
print('loss:', loss)

20pytorch/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:

0 commit comments

Comments
 (0)