Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d0fe57

Browse files
committed
Add py files
1 parent 5a2a896 commit 3d0fe57

14 files changed

+867
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
x_data = [1.0, 2.0, 3.0]
5+
y_data = [2.0, 4.0, 6.0]
6+
7+
8+
# our model for the forward pass
9+
def forward(x):
10+
return x * w
11+
12+
13+
# Loss function
14+
def loss(x, y):
15+
y_pred = forward(x)
16+
return (y_pred - y) * (y_pred - y)
17+
18+
19+
w_list = []
20+
mse_list = []
21+
22+
for w in np.arange(0.0, 4.1, 0.1):
23+
print("w=", w)
24+
l_sum = 0
25+
for x_val, y_val in zip(x_data, y_data):
26+
y_pred_val = forward(x_val)
27+
l = loss(x_val, y_val)
28+
l_sum += l
29+
print("\t", x_val, y_val, y_pred_val, l)
30+
print("MSE=", l_sum / 3)
31+
w_list.append(w)
32+
mse_list.append(l_sum / 3)
33+
34+
plt.plot(w_list, mse_list)
35+
plt.ylabel('Loss')
36+
plt.xlabel('w')
37+
plt.show()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
x_data = [1.0, 2.0, 3.0]
2+
y_data = [2.0, 4.0, 6.0]
3+
4+
w = 1.0 # a random guess: random value
5+
6+
# our model forward pass
7+
8+
9+
def forward(x):
10+
return x * w
11+
12+
13+
# Loss function
14+
def loss(x, y):
15+
y_pred = forward(x)
16+
return (y_pred - y) * (y_pred - y)
17+
18+
19+
# compute gradient
20+
def gradient(x, y): # d_loss/d_w
21+
return 2 * x * (x * w - y)
22+
23+
# Before training
24+
print("predict (before training)", 4, forward(4))
25+
26+
# Training loop
27+
for epoch in range(10):
28+
for x_val, y_val in zip(x_data, y_data):
29+
grad = gradient(x_val, y_val)
30+
w = w - 0.01 * grad
31+
print("\tgrad: ", x_val, y_val, round(grad, 2))
32+
l = loss(x_val, y_val)
33+
34+
print("progress:", epoch, "w=", round(w, 2), "loss=", round(l, 2))
35+
36+
# After training
37+
print("predict (after training)", "4 hours", forward(4))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import torch
2+
3+
x_data = [1.0, 2.0, 3.0]
4+
y_data = [2.0, 4.0, 6.0]
5+
6+
w = torch.tensor([1.0], requires_grad = True) # Any random value
7+
# our model forward pass
8+
9+
10+
def forward(x):
11+
return x * w
12+
13+
# Loss function
14+
15+
16+
def loss(x, y):
17+
y_pred = forward(x)
18+
return (y_pred - y) * (y_pred - y)
19+
20+
# Before training
21+
print("predict (before training)", 4, forward(4).item())
22+
23+
# Training loop
24+
for epoch in range(10):
25+
for x_val, y_val in zip(x_data, y_data):
26+
l = loss(x_val, y_val)
27+
l.backward()
28+
print("\tgrad: ", x_val, y_val, w.grad.item())
29+
w.data = w.data - 0.01 * w.grad.data
30+
31+
# Manually zero the gradients after updating weights
32+
w.grad.data.zero_()
33+
34+
print("progress:", epoch, l.item())
35+
36+
# After training
37+
print("predict (after training)", 4, forward(4).item())
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#%%
2+
import torch
3+
4+
x_data = torch.tensor([[1.0], [2.0], [3.0]])
5+
y_data = torch.tensor([[2.0], [4.0], [6.0]])
6+
7+
8+
class Model(torch.nn.Module):
9+
10+
def __init__(self):
11+
"""
12+
In the constructor we instantiate two nn.Linear module
13+
"""
14+
super(Model, self).__init__()
15+
self.linear = torch.nn.Linear(1, 1) # One in and one out
16+
17+
def forward(self, x):
18+
"""
19+
In the forward function we accept a Variable of input data and we must return
20+
a Variable of output data. We can use Modules defined in the constructor as
21+
well as arbitrary operators on Variables.
22+
"""
23+
y_pred = self.linear(x)
24+
return y_pred
25+
26+
# our model
27+
model = Model()
28+
29+
30+
# Construct our loss function and an Optimizer. The call to model.parameters()
31+
# in the SGD constructor will contain the learnable parameters of the two
32+
# nn.Linear modules which are members of the model.
33+
criterion = torch.nn.MSELoss(reduction='sum')
34+
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
35+
36+
# Training loop
37+
for epoch in range(500):
38+
# Forward pass: Compute predicted y by passing x to the model
39+
y_pred = model(x_data)
40+
41+
# Compute and print loss
42+
loss = criterion(y_pred, y_data)
43+
print(epoch, loss.item())
44+
45+
# Zero gradients, perform a backward pass, and update the weights.
46+
optimizer.zero_grad()
47+
loss.backward()
48+
optimizer.step()
49+
50+
#%%
51+
# After training
52+
hour_var = torch.tensor([[4.0]])
53+
y_pred = model(hour_var)
54+
print("predict (after training)", 4, model(hour_var).detach().squeeze())
55+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import torch
3+
import torch.nn.functional as F
4+
5+
x_data = torch.tensor([[1.0], [2.0], [3.0], [4.0]])
6+
y_data = torch.tensor([[0.], [0.], [1.], [1.]])
7+
8+
9+
class Model(torch.nn.Module):
10+
11+
def __init__(self):
12+
"""
13+
In the constructor we instantiate nn.Linear module
14+
"""
15+
super(Model, self).__init__()
16+
self.linear = torch.nn.Linear(1, 1) # One in and one out
17+
18+
def forward(self, x):
19+
"""
20+
In the forward function we accept a Variable of input data and we must return
21+
a Variable of output data.
22+
"""
23+
y_pred = torch.sigmoid(self.linear(x))
24+
return y_pred
25+
26+
# our model
27+
model = Model()
28+
29+
30+
# Construct our loss function and an Optimizer. The call to model.parameters()
31+
# in the SGD constructor will contain the learnable parameters of the two
32+
# nn.Linear modules which are members of the model.
33+
criterion = torch.nn.BCELoss(reduction='mean')
34+
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
35+
36+
# Training loop
37+
for epoch in range(1000):
38+
# Forward pass: Compute predicted y by passing x to the model
39+
y_pred = model(x_data)
40+
41+
# Compute and print loss
42+
loss = criterion(y_pred, y_data)
43+
print(epoch, loss.item())
44+
45+
# Zero gradients, perform a backward pass, and update the weights.
46+
optimizer.zero_grad()
47+
loss.backward()
48+
optimizer.step()
49+
50+
# After training
51+
hour_var = torch.tensor([[1.0]])
52+
print("predict 1 hour ", 1.0, model(hour_var).data[0][0] > 0.5)
53+
hour_var = torch.tensor([[7.0]])
54+
print("predict 7 hours", 7.0, model(hour_var).data[0][0] > 0.5)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
import torch
3+
import numpy as np
4+
5+
xy = np.loadtxt('./data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
6+
x_data = torch.from_numpy(xy[:, 0:-1])
7+
y_data = torch.from_numpy(xy[:, [-1]])
8+
9+
print(x_data.data.shape)
10+
print(y_data.data.shape)
11+
12+
13+
class Model(torch.nn.Module):
14+
15+
def __init__(self):
16+
"""
17+
In the constructor we instantiate two nn.Linear module
18+
"""
19+
super(Model, self).__init__()
20+
self.l1 = torch.nn.Linear(8, 6)
21+
self.l2 = torch.nn.Linear(6, 4)
22+
self.l3 = torch.nn.Linear(4, 1)
23+
24+
self.sigmoid = torch.nn.Sigmoid()
25+
26+
def forward(self, x):
27+
"""
28+
In the forward function we accept a Variable of input data and we must return
29+
a Variable of output data. We can use Modules defined in the constructor as
30+
well as arbitrary operators on Variables.
31+
"""
32+
out1 = self.sigmoid(self.l1(x))
33+
out2 = self.sigmoid(self.l2(out1))
34+
y_pred = self.sigmoid(self.l3(out2))
35+
return y_pred
36+
37+
# our model
38+
model = Model()
39+
40+
41+
# Construct our loss function and an Optimizer. The call to model.parameters()
42+
# in the SGD constructor will contain the learnable parameters of the two
43+
# nn.Linear modules which are members of the model.
44+
criterion = torch.nn.BCELoss(reduction='mean')
45+
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
46+
47+
# Training loop
48+
for epoch in range(100):
49+
# Forward pass: Compute predicted y by passing x to the model
50+
y_pred = model(x_data)
51+
52+
# Compute and print loss
53+
loss = criterion(y_pred, y_data)
54+
print(epoch, loss.item())
55+
56+
# Zero gradients, perform a backward pass, and update the weights.
57+
optimizer.zero_grad()
58+
loss.backward()
59+
optimizer.step()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# References
2+
# https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/01-basics/pytorch_basics/main.py
3+
# http://pytorch.org/tutorials/beginner/data_loading_tutorial.html#dataset-class
4+
import torch
5+
import numpy as np
6+
from torch.utils.data import Dataset, DataLoader
7+
8+
9+
class DiabetesDataset(Dataset):
10+
""" Diabetes dataset."""
11+
12+
# Initialize your data, download, etc.
13+
def __init__(self):
14+
xy = np.loadtxt('./data/diabetes.csv.gz',
15+
delimiter=',', dtype=np.float32)
16+
self.len = xy.shape[0]
17+
self.x_data = torch.from_numpy(xy[:, 0:-1])
18+
self.y_data = torch.from_numpy(xy[:, [-1]])
19+
20+
def __getitem__(self, index):
21+
return self.x_data[index], self.y_data[index]
22+
23+
def __len__(self):
24+
return self.len
25+
26+
27+
dataset = DiabetesDataset()
28+
train_loader = DataLoader(dataset=dataset,
29+
batch_size=32,
30+
shuffle=True,
31+
num_workers=2)
32+
33+
for epoch in range(2):
34+
for i, data in enumerate(train_loader, 0):
35+
# get the inputs
36+
inputs, labels = data
37+
38+
# Run your training process
39+
print(epoch, i, "inputs", inputs.data, "labels", labels.data)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /