diff --git a/1_linear_regression.py b/1_linear_regression.py index 9c3dc59..b10aacd 100644 --- a/1_linear_regression.py +++ b/1_linear_regression.py @@ -41,7 +41,7 @@ def main(): for i in range(100): cost = 0. - num_batches = len(X) / batch_size + num_batches = len(X) // batch_size for k in range(num_batches): start, end = k * batch_size, (k + 1) * batch_size cost += train(model, loss, optimizer, X[start:end], Y[start:end]) diff --git a/2_logistic_regression.py b/2_logistic_regression.py index 7a0a4d5..65fbf09 100644 --- a/2_logistic_regression.py +++ b/2_logistic_regression.py @@ -8,9 +8,11 @@ def build_model(input_dim, output_dim): - # We don't need the softmax layer here since CrossEntropyLoss already uses it internally. + # We don't need the softmax layer here since CrossEntropyLoss already + # uses it internally. model = torch.nn.Sequential() - model.add_module("linear", torch.nn.Linear(input_dim, output_dim, bias=False)) + model.add_module("linear", + torch.nn.Linear(input_dim, output_dim, bias=False)) return model @@ -56,10 +58,11 @@ def main(): for i in range(100): cost = 0. - num_batches = n_examples / batch_size + num_batches = n_examples // batch_size for k in range(num_batches): start, end = k * batch_size, (k + 1) * batch_size - cost += train(model, loss, optimizer, trX[start:end], trY[start:end]) + cost += train(model, loss, optimizer, + trX[start:end], trY[start:end]) predY = predict(model, teX) print("Epoch %d, cost = %f, acc = %.2f%%" % (i + 1, cost / num_batches, 100. * np.mean(predY == teY))) diff --git a/3_neural_net.py b/3_neural_net.py index 7624764..74d7ac9 100644 --- a/3_neural_net.py +++ b/3_neural_net.py @@ -57,7 +57,7 @@ def main(): for i in range(100): cost = 0. - num_batches = n_examples / batch_size + num_batches = n_examples // batch_size for k in range(num_batches): start, end = k * batch_size, (k + 1) * batch_size cost += train(model, loss, optimizer, trX[start:end], trY[start:end]) diff --git a/4_modern_neural_net.py b/4_modern_neural_net.py index b3b5af0..db77291 100644 --- a/4_modern_neural_net.py +++ b/4_modern_neural_net.py @@ -61,7 +61,7 @@ def main(): for i in range(100): cost = 0. - num_batches = n_examples / batch_size + num_batches = n_examples // batch_size for k in range(num_batches): start, end = k * batch_size, (k + 1) * batch_size cost += train(model, loss, optimizer, trX[start:end], trY[start:end]) diff --git a/5_convolutional_net.py b/5_convolutional_net.py index d83f40d..7386da1 100644 --- a/5_convolutional_net.py +++ b/5_convolutional_net.py @@ -78,7 +78,7 @@ def main(): for i in range(20): cost = 0. - num_batches = n_examples / batch_size + num_batches = n_examples // batch_size for k in range(num_batches): start, end = k * batch_size, (k + 1) * batch_size cost += train(model, loss, optimizer, trX[start:end], trY[start:end]) diff --git a/data_util.py b/data_util.py index bb6ff6e..6400fbf 100644 --- a/data_util.py +++ b/data_util.py @@ -1,11 +1,20 @@ import gzip import os from os import path -import urllib import numpy as np +import sys +if sys.version_info.major < 3: + import urllib +else: + import urllib.request as request + + DATASET_DIR = 'datasets/' +MNIST_FILES = ["train-images-idx3-ubyte.gz", "train-labels-idx1-ubyte.gz", + "t10k-images-idx3-ubyte.gz", "t10k-labels-idx1-ubyte.gz"] + def download_file(url, local_path): dir_path = path.dirname(local_path) @@ -14,13 +23,15 @@ def download_file(url, local_path): os.makedirs(dir_path) print("Downloading from '%s' ..." % url) - urllib.URLopener().retrieve(url, local_path) + if sys.version_info.major < 3: + urllib.URLopener().retrieve(url, local_path) + else: + request.urlretrieve(url, local_path) def download_mnist(local_path): url_root = "http://yann.lecun.com/exdb/mnist/" - for f_name in ["train-images-idx3-ubyte.gz", "train-labels-idx1-ubyte.gz", - "t10k-images-idx3-ubyte.gz", "t10k-labels-idx1-ubyte.gz"]: + for f_name in MNIST_FILES: f_path = os.path.join(local_path, f_name) if not path.exists(f_path): download_file(url_root + f_name, f_path) @@ -39,6 +50,11 @@ def load_mnist(ntrain=60000, ntest=10000, onehot=True): data_dir = os.path.join(DATASET_DIR, 'mnist/') if not path.exists(data_dir): download_mnist(data_dir) + else: + # check all files + checks = [path.exists(os.path.join(data_dir, f)) for f in MNIST_FILES] + if not np.all(checks): + download_mnist(data_dir) with gzip.open(os.path.join(data_dir, 'train-images-idx3-ubyte.gz')) as fd: buf = fd.read()