Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions 05_linear_regression.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import torch
from torch.autograd import Variable

Expand Down Expand Up @@ -41,7 +40,7 @@ def forward(self, x):

# Compute and print loss
loss = criterion(y_pred, y_data)
print(epoch, loss.data[0])
print(epoch, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
Expand Down
2 changes: 1 addition & 1 deletion 06_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def forward(self, x):

# Compute and print loss
loss = criterion(y_pred, y_data)
print(epoch, loss.data[0])
print(epoch, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
Expand Down
4 changes: 2 additions & 2 deletions 07_diabets_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from torch.autograd import Variable
import numpy as np

xy = np.loadtxt('./data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
xy = np.loadtxt('./data/diabetes.csv', delimiter=',', dtype=np.float32)
x_data = Variable(torch.from_numpy(xy[:, 0:-1]))
y_data = Variable(torch.from_numpy(xy[:, [-1]]))

Expand Down Expand Up @@ -52,7 +52,7 @@ def forward(self, x):

# Compute and print loss
loss = criterion(y_pred, y_data)
print(epoch, loss.data[0])
print(epoch, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
Expand Down
2 changes: 1 addition & 1 deletion 08_1_dataset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __len__(self):
train_loader = DataLoader(dataset=dataset,
batch_size=32,
shuffle=True,
num_workers=2)
num_workers=2) #num_workers=0 in cpu version

for epoch in range(2):
for i, data in enumerate(train_loader, 0):
Expand Down
6 changes: 3 additions & 3 deletions 08_2_dataset_loade_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DiabetesDataset(Dataset):

# Initialize your data, download, etc.
def __init__(self):
xy = np.loadtxt('./data/diabetes.csv.gz',
xy = np.loadtxt('./data/diabetes.csv',
delimiter=',', dtype=np.float32)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 0:-1])
Expand All @@ -29,7 +29,7 @@ def __len__(self):
train_loader = DataLoader(dataset=dataset,
batch_size=32,
shuffle=True,
num_workers=2)
num_workers=2) #num_workers=0 in cpu version


class Model(torch.nn.Module):
Expand Down Expand Up @@ -80,7 +80,7 @@ def forward(self, x):

# Compute and print loss
loss = criterion(y_pred, labels)
print(epoch, i, loss.data[0])
print(epoch, i, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
Expand Down
7 changes: 4 additions & 3 deletions 09_2_softmax_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ def train(epoch):
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
100. * batch_idx / len(train_loader), loss.item()))


def test():
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data, volatile=True), Variable(target)
with torch.no_grad():
data, target = Variable(data), Variable(target)
output = model(data)
# sum up batch loss
test_loss += criterion(output, target).data[0]
test_loss += criterion(output, target).item()
# get the index of the max
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
Expand Down
3 changes: 2 additions & 1 deletion 10_1_cnn_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def test():
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data, volatile=True), Variable(target)
with torch.no_grad():
data, target = Variable(data), Variable(target)
output = model(data)
# sum up batch loss
test_loss += F.nll_loss(output, target, size_average=False).data[0]
Expand Down
7 changes: 4 additions & 3 deletions 11_1_toy_inception_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,19 @@ def train(epoch):
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
100. * batch_idx / len(train_loader), loss.item()))


def test():
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data, volatile=True), Variable(target)
with torch.no_grad():
data, target = Variable(data), Variable(target)
output = model(data)
# sum up batch loss
test_loss += F.nll_loss(output, target, size_average=False).data[0]
test_loss += F.nll_loss(output, target, size_average=False).item()
# get the index of the max log-probability
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
Expand Down
1 change: 1 addition & 0 deletions 12_2_hello_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def init_hidden(self):
sys.stdout.write("predicted string: ")
for input, label in zip(inputs, labels):
# print(input.size(), label.size())
label = label.unsqueeze(0)
hidden, output = model(hidden, input)
val, idx = output.max(1)
sys.stdout.write(idx2char[idx.data[0]])
Expand Down
17 changes: 12 additions & 5 deletions 12_4_hello_rnn_emb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@

class Model(nn.Module):

def __init__(self):
def __init__(self, num_classes, input_size, hidden_size, num_layers, embedding_size):
super(Model, self).__init__()
self.embedding = nn.Embedding(input_size, embedding_size)
self.rnn = nn.RNN(input_size=embedding_size,

self.num_classes = num_classes
self.num_layers = num_layers
self.input_size = input_size
self.hidden_size = hidden_size
self.embedding_size = embedding_size

self.embedding = nn.Embedding(self.input_size, self.embedding_size)
self.rnn = nn.RNN(input_size=self.embedding_size,
hidden_size=5, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
self.fc = nn.Linear(self.hidden_size, self.num_classes)

def forward(self, x):
# Initialize hidden and cell states
Expand All @@ -51,7 +58,7 @@ def forward(self, x):


# Instantiate RNN model
model = Model()
model = Model(num_classes, input_size, hidden_size, num_layers, embedding_size)
print(model)

# Set loss and optimizer function
Expand Down