Skip to content

Commit d28debf

Browse files
committed
initial commit
0 parents  commit d28debf

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

recurrent_net.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
5+
class RecurrentNet(torch.nn.Module):
6+
def __init__(self, D_in, H, D_out):
7+
super(RecurrentNet, self).__init__()
8+
self.linear1 = torch.nn.Linear(D_in, H)
9+
self.recurrent_linear = torch.nn.Linear(H, H)
10+
self.linear2 = torch.nn.Linear(H, D_out)
11+
12+
def forward(self, x):
13+
h_relu = self.linear1(x).clamp(min=0)
14+
for _ in range(3):
15+
h_relu = self.recurrent_linear(h_relu).clamp(min=0)
16+
y_pred = self.linear2(h_relu)
17+
return y_pred
18+
19+
20+
N, D_in, H, D_out = 64, 1000, 100, 10
21+
22+
x = Variable(torch.randn(N, D_in))
23+
y = Variable(torch.randn(N, D_out), requires_grad=False)
24+
25+
model = RecurrentNet(D_in, H, D_out)
26+
criterion = torch.nn.MSELoss(size_average=False)
27+
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
28+
for t in range(500):
29+
y_pred = model(x)
30+
loss = criterion(y_pred, y)
31+
print(t, loss.data[0])
32+
33+
model.zero_grad()
34+
loss.backward()
35+
optimizer.step()
36+

tf_two_layer_net.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
4+
N, D_in, H, D_out = 64, 1000, 100, 10
5+
6+
x = tf.placeholder(tf.float32, shape=(None, D_in))
7+
y = tf.placeholder(tf.float32, shape=(None, D_out))
8+
9+
w1 = tf.Variable(tf.random_normal((D_in, H)))
10+
w2 = tf.Variable(tf.random_normal((H, D_out)))
11+
12+
h = tf.matmul(x, w1)
13+
h_relu = tf.maximum(h, tf.zeros(1))
14+
y_pred = tf.matmul(h_relu, w2)
15+
loss = tf.reduce_sum((y - y_pred) ** 2.0)
16+
17+
learning_rate = 1e-6
18+
grad_w1, grad_w2 = tf.gradients(loss, [w1, w2])
19+
new_w1 = w1.assign(w1 - learning_rate * grad_w1)
20+
new_w2 = w2.assign(w2 - learning_rate * grad_w2)
21+
22+
23+
with tf.Session() as sess:
24+
sess.run(tf.global_variables_initializer())
25+
26+
x_value = np.random.randn(N, D_in)
27+
y_value = np.random.randn(N, D_out)
28+
for _ in range(500):
29+
loss_value, _, _ = sess.run(
30+
[loss, new_w1, new_w2],
31+
feed_dict={x: x_value, y: y_value})
32+
print(loss_value)

two_layer_net_autograd.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
N, D_in, H, D_out = 64, 1000, 100, 10
5+
6+
x = Variable(torch.randn(N, D_in))
7+
y = Variable(torch.randn(N, D_out))
8+
9+
w1 = Variable(torch.randn(D_in, H), requires_grad=True)
10+
w2 = Variable(torch.randn(H, D_out), requires_grad=True)
11+
12+
learning_rate = 1e-6
13+
for t in range(500):
14+
y_pred = x.mm(w1).clamp(min=0).mm(w2)
15+
loss = (y_pred - y).pow(2).sum()
16+
17+
w1.grad.data.zero_()
18+
w2.grad.data.zero_()
19+
loss.backward()
20+
21+
print(t, loss.data[0])
22+
w1.data -= learning_rate * w1.grad.data
23+
w2.data -= learning_rate * w2.grad.data

two_layer_net_module.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
5+
class TwoLayerNet(torch.nn.Module):
6+
def __init__(self, D_in, H, D_out):
7+
super(TwoLayerNet, self).__init__()
8+
self.linear1 = torch.nn.Linear(D_in, H)
9+
self.linear2 = torch.nn.Linear(H, D_out)
10+
11+
def forward(self, x):
12+
h_relu = self.linear1(x).clamp(min=0)
13+
y_pred = self.linear2(h_relu)
14+
return y_pred
15+
16+
17+
N, D_in, H, D_out = 64, 1000, 100, 10
18+
19+
x = Variable(torch.randn(N, D_in))
20+
y = Variable(torch.randn(N, D_out), requires_grad=False)
21+
22+
model = TwoLayerNet(D_in, H, D_out)
23+
criterion = torch.nn.MSELoss(size_average=False)
24+
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
25+
for t in range(500):
26+
y_pred = model(x)
27+
loss = criterion(y_pred, y)
28+
print(t, loss.data[0])
29+
30+
model.zero_grad()
31+
loss.backward()
32+
optimizer.step()
33+

two_layer_net_nn.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
N, D_in, H, D_out = 64, 1000, 100, 10
5+
6+
x = Variable(torch.randn(N, D_in))
7+
y = Variable(torch.randn(N, D_out), requires_grad=False)
8+
9+
model = torch.nn.Sequential(
10+
torch.nn.Linear(D_in, H),
11+
torch.nn.ReLU(),
12+
torch.nn.Linear(H, D_out),
13+
)
14+
criterion = torch.nn.MSELoss(size_average=False)
15+
16+
learning_rate = 1e-4
17+
for t in range(500):
18+
y_pred = model(x)
19+
loss = criterion(y_pred, y)
20+
print(t, loss.data[0])
21+
22+
model.zero_grad()
23+
loss.backward()
24+
for param in model.parameters():
25+
param.data -= learning_rate * param.grad.data

two_layer_net_optim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
N, D_in, H, D_out = 64, 1000, 100, 10
5+
6+
x = Variable(torch.randn(N, D_in))
7+
y = Variable(torch.randn(N, D_out), requires_grad=False)
8+
9+
model = torch.nn.Sequential(
10+
torch.nn.Linear(D_in, H),
11+
torch.nn.ReLU(),
12+
torch.nn.Linear(H, D_out),
13+
)
14+
criterion = torch.nn.MSELoss(size_average=False)
15+
16+
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
17+
for t in range(500):
18+
y_pred = model(x)
19+
loss = criterion(y_pred, y)
20+
print(t, loss.data[0])
21+
22+
model.zero_grad()
23+
loss.backward()
24+
optimizer.step()
25+

two_layer_net_tensor.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import torch
2+
3+
N, D_in, H, D_out = 64, 1000, 100, 10
4+
5+
x = torch.randn(N, D_in)
6+
y = torch.randn(N, D_out)
7+
8+
w1 = torch.randn(D_in, H)
9+
w2 = torch.randn(H, D_out)
10+
11+
learning_rate = 1e-6
12+
for t in range(500):
13+
h = x.mm(w1)
14+
h_relu = h.clamp(min=0)
15+
y_pred = h_relu.mm(w2)
16+
17+
loss = (y_pred - y).pow(2).sum()
18+
print(t, loss)
19+
20+
grad_y_pred = 2.0 * (y_pred - y)
21+
grad_w2 = h_relu.t().mm(grad_y_pred)
22+
grad_h_relu = grad_y_pred.mm(w2.t())
23+
grad_h = grad_h_relu.clone()
24+
grad_h[h < 0] = 0
25+
grad_w1 = x.t().mm(grad_h)
26+
27+
w1 -= learning_rate * grad_w1
28+
w2 -= learning_rate * grad_w2

0 commit comments

Comments
 (0)