Skip to content

Commit

Permalink
Remove support for Python 2. Misc fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhkhuc committed Sep 16, 2020
1 parent b0eb7eb commit bb96786
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 13 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ os:
- linux
language: python
python:
- 2.7
- 3.6
cache: bundler
install:
Expand Down
1 change: 1 addition & 0 deletions 1_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def build_model():


def train(model, loss, optimizer, x, y):
model.train()
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

Expand Down
2 changes: 2 additions & 0 deletions 2_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def build_model(input_dim, output_dim):


def train(model, loss, optimizer, x_val, y_val):
model.train()
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)

Expand All @@ -37,6 +38,7 @@ def train(model, loss, optimizer, x_val, y_val):


def predict(model, x_val):
model.eval()
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
Expand Down
2 changes: 2 additions & 0 deletions 3_neural_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def build_model(input_dim, output_dim):


def train(model, loss, optimizer, x_val, y_val):
model.train()
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)

Expand All @@ -36,6 +37,7 @@ def train(model, loss, optimizer, x_val, y_val):


def predict(model, x_val):
model.eval()
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
Expand Down
2 changes: 2 additions & 0 deletions 4_modern_neural_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def build_model(input_dim, output_dim):


def train(model, loss, optimizer, x_val, y_val):
model.train()
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)

Expand All @@ -40,6 +41,7 @@ def train(model, loss, optimizer, x_val, y_val):


def predict(model, x_val):
model.eval()
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
Expand Down
2 changes: 2 additions & 0 deletions 5_convolutional_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def forward(self, x):


def train(model, loss, optimizer, x_val, y_val):
model.train()
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)

Expand All @@ -54,6 +55,7 @@ def train(model, loss, optimizer, x_val, y_val):


def predict(model, x_val):
model.eval()
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
Expand Down
3 changes: 2 additions & 1 deletion 6_lstm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
import numpy as np

import torch
Expand All @@ -24,6 +23,7 @@ def forward(self, x):


def train(model, loss, optimizer, x_val, y_val):
model.train()
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)

Expand All @@ -44,6 +44,7 @@ def train(model, loss, optimizer, x_val, y_val):


def predict(model, x_val):
model.eval()
x = Variable(x_val, requires_grad=False)
output = model.forward(x)
return output.data.numpy().argmax(axis=1)
Expand Down
14 changes: 3 additions & 11 deletions data_util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import gzip
import os
import urllib.request as request
from os import path
import numpy as np

import sys
if sys.version_info.major < 3:
import urllib
else:
import urllib.request as request

import numpy as np

DATASET_DIR = 'datasets/'

Expand All @@ -23,10 +18,7 @@ def download_file(url, local_path):
os.makedirs(dir_path)

print("Downloading from '%s' ..." % url)
if sys.version_info.major < 3:
urllib.URLopener().retrieve(url, local_path)
else:
request.urlretrieve(url, local_path)
request.urlretrieve(url, local_path)


def download_mnist(local_path):
Expand Down

0 comments on commit bb96786

Please sign in to comment.