Skip to content

Commit

Permalink
refactor: PEP8 FLAKE8 (hunkim#127)
Browse files Browse the repository at this point in the history
1. Format to PEP8
2. Remove unused var/package
  • Loading branch information
kkweon authored Apr 15, 2017
1 parent f69c95f commit 27450fc
Show file tree
Hide file tree
Showing 28 changed files with 47 additions and 52 deletions.
5 changes: 2 additions & 3 deletions Chainer/chlab-02-1-linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import numpy as np
import chainer
from chainer import training, Variable
from chainer import datasets, iterators, optimizers
from chainer import Chain
from chainer import training
from chainer import datasets
from chainer.training import extensions

import chainer.functions as F
Expand Down
2 changes: 1 addition & 1 deletion Keras/klab-10-1-mnist_softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
model.summary()

model.compile(loss='categorical_crossentropy',
optimizer='adam', learning_rate = learning_rate,
optimizer='adam', learning_rate=learning_rate,
metrics=['accuracy'])

model.fit(X_train, Y_train, batch_size=batch_size, epochs=training_epochs)
Expand Down
3 changes: 2 additions & 1 deletion Keras/klab-12-1-softmax_hello_char.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed, Activation, LSTM
from keras.layers import Activation
from keras.layers import Dense
from keras.utils import np_utils

import os
Expand Down
3 changes: 2 additions & 1 deletion Keras/klab-12-3-rnn_stock_prediction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM, Activation
from keras.layers import Activation
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
import os

Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-10-3-mnist_nn_xavier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import random
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-10-4-mnist_nn_deep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import random
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-10-5-mnist_nn_dropout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import random
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down
3 changes: 2 additions & 1 deletion PyTorch/lab-11-1-mnist_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -32,6 +32,7 @@


class CNN(torch.nn.Module):

def __init__(self):
super(CNN, self).__init__()
# L1 ImgIn shape=(?, 28, 28, 1)
Expand Down
3 changes: 2 additions & 1 deletion PyTorch/lab-11-2-mnist_deep_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -33,6 +33,7 @@


class CNN(torch.nn.Module):

def __init__(self):
super(CNN, self).__init__()
# L1 ImgIn shape=(?, 28, 28, 1)
Expand Down
3 changes: 2 additions & 1 deletion PyTorch/lab-11-3-mnist_cnn_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.nn import init
import torch.nn.init

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -32,6 +32,7 @@


class CNN(torch.nn.Module):

def __init__(self):
super(CNN, self).__init__()
self._build_net()
Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-12-1-hello-rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -39,6 +38,7 @@


class RNN(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
super(RNN, self).__init__()
self.num_classes = num_classes
Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-12-2-char-seq-rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -46,6 +45,7 @@ def one_hot(x, num_classes):


class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
super(LSTM, self).__init__()
self.num_classes = num_classes
Expand Down
2 changes: 1 addition & 1 deletion PyTorch/lab-12-4-rnn-long_char.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np

torch.manual_seed(777) # reproducibility

Expand Down Expand Up @@ -59,6 +58,7 @@ def one_hot(x, num_classes):


class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
super(LSTM, self).__init__()
self.num_classes = num_classes
Expand Down
1 change: 1 addition & 0 deletions PyTorch/lab-12-5-stock_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def MinMaxScaler(data):


class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
super(LSTM, self).__init__()
self.num_classes = num_classes
Expand Down
1 change: 0 additions & 1 deletion lab-07-1-learning_rate_and_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Lab 7 Learning rate and Evaluation
import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility

x_data = [[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5],
Expand Down
2 changes: 1 addition & 1 deletion lab-09-4-xor_tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
# tensorboard --logdir=./logs/xor_logs
merged_summary = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/xor_logs_r0_01")
writer.add_graph(sess.graph) # Show the graph
writer.add_graph(sess.graph) # Show the graph

# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
Expand Down
2 changes: 1 addition & 1 deletion lab-10-7-mnist_nn_higher_level_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
c = sess.run(cost, feed_dict=feed_dict_cost)
avg_cost += c / total_batch

print("[Epoch: {:>4}] cost = {:>.9}".format(epoch+1, avg_cost))
print("[Epoch: {:>4}] cost = {:>.9}".format(epoch + 1, avg_cost))
#print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))

print('Learning Finished!')
Expand Down
1 change: 0 additions & 1 deletion lab-11-3-mnist_cnn_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Lab 11 MNIST and Deep learning CNN
import tensorflow as tf
import random
# import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data
Expand Down
1 change: 0 additions & 1 deletion lab-11-4-mnist_cnn_layers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Lab 11 MNIST and Deep learning CNN
import tensorflow as tf
import random
# import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data
Expand Down
2 changes: 0 additions & 2 deletions lab-13-1-mnist_using_scope.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Lab 7 Learning rate and Evaluation
import tensorflow as tf
import numpy as np
import random
import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

Expand Down
2 changes: 0 additions & 2 deletions lab-13-2-mnist_tensorboard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Lab 7 Learning rate and Evaluation
import tensorflow as tf
import numpy as np
import random
import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

Expand Down
6 changes: 3 additions & 3 deletions mxnet/mxlab-04-3-file_input_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
data_names=['data'],
label_names=['target'],
context=mx.gpu(0))
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
net.init_params(initializer=mx.init.Normal(sigma=0.01))
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-4, 'momentum': 0.9})

Expand All @@ -58,4 +58,4 @@
'''
input = [60, 70, 110], score = [[ 182.48858643]]
input = [90, 100, 80], score = [[ 175.24279785]]
'''
'''
9 changes: 4 additions & 5 deletions mxnet/mxlab-05-2-logistic_regression_diabetes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Lab 5 Logistic Regression Classifier
import mxnet as mx
import mxnet.ndarray as nd
import numpy as np
import logging
import sys
Expand Down Expand Up @@ -30,10 +29,10 @@
data_names=['data'],
label_names=['target'],
context=mx.gpu(0))
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
net.init_params(initializer=mx.init.Normal(sigma=0.01))
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-3, 'momentum':0.9})
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-3, 'momentum': 0.9})

# 4. Train the model
# First constructing the training iterator and then fit the model
Expand Down Expand Up @@ -84,4 +83,4 @@
[ 1.]
[ 1.]
[ 1.]]
'''
'''
9 changes: 4 additions & 5 deletions mxnet/mxlab-06-2-softmax_zoo_classifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Lab 6 Softmax Classifier
import mxnet as mx
import mxnet.ndarray as nd
import numpy as np
import logging
import sys
Expand Down Expand Up @@ -32,10 +31,10 @@
data_names=['data'],
label_names=['target'],
context=mx.gpu(0))
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size,), layout='NC')])
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size,), layout='NC')])
net.init_params(initializer=mx.init.Normal(sigma=0.01))
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-1, 'momentum':0.9})
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-1, 'momentum': 0.9})

# 4. Train the model
# First constructing the training iterator and then fit the model
Expand Down Expand Up @@ -91,4 +90,4 @@
0. 0. 1. 1. 1. 1. 3. 3. 2. 0. 0. 0. 0. 0. 0. 0. 0. 1.
6. 3. 0. 0. 2. 6. 1. 1. 2. 6. 3. 1. 0. 6. 3. 1. 5. 4.
2. 2. 3. 0. 0. 1. 0. 5. 0. 6. 1.]
'''
'''
10 changes: 5 additions & 5 deletions mxnet/mxlab-11-2-mnist_deep_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
X, y = mnist.data, mnist.target
X = X.astype(np.float32) / 255.0
X_train, X_valid, X_test = X[:55000].reshape((-1, 1, 28, 28)),\
X[55000:60000].reshape((-1, 1, 28, 28)),\
X[60000:].reshape((-1, 1, 28, 28))
X[55000:60000].reshape((-1, 1, 28, 28)),\
X[60000:].reshape((-1, 1, 28, 28))
y_train, y_valid, y_test = y[:55000], y[55000:60000], y[60000:]

# hyper parameters
Expand Down Expand Up @@ -92,7 +92,7 @@
# Slice the data batch and label batch.
# Note that we use np.take to ensure that the batch will be padded correctly.
data_npy = np.take(X_train,
indices=np.arange(i * batch_size, (i+1) * batch_size),
indices=np.arange(i * batch_size, (i + 1) * batch_size),
axis=0,
mode="clip")
label_npy = np.take(y_train,
Expand All @@ -115,7 +115,7 @@
total_num = 0
for i in range(total_batch):
num_valid = batch_size if (i + 1) * batch_size <= X_test.shape[0]\
else X_test.shape[0] - i * batch_size
else X_test.shape[0] - i * batch_size
data_npy = np.take(X_test,
indices=np.arange(i * batch_size, (i + 1) * batch_size),
axis=0,
Expand All @@ -137,7 +137,7 @@
test_net.reshape(data_shapes=[mx.io.DataDesc(name='data', shape=(1, 1, 28, 28), layout='NCHW')],
label_shapes=None)
r = np.random.randint(0, X_test.shape[0])
test_net.forward(data_batch=mx.io.DataBatch(data=[nd.array(X_test[r:r+1])],
test_net.forward(data_batch=mx.io.DataBatch(data=[nd.array(X_test[r:r + 1])],
label=None))
logits_nd = test_net.get_outputs()[0]
print("Label: ", int(y_test[r]))
Expand Down
7 changes: 4 additions & 3 deletions mxnet/mxlab-11-5-mnist_cnn_ensemble_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
X, y = mnist.data, mnist.target
X = X.astype(np.float32) / 255.0
X_train, X_valid, X_test = X[:55000].reshape((-1, 1, 28, 28)),\
X[55000:60000].reshape((-1, 1, 28, 28)),\
X[60000:].reshape((-1, 1, 28, 28))
X[55000:60000].reshape((-1, 1, 28, 28)),\
X[60000:].reshape((-1, 1, 28, 28))
y_train, y_valid, y_test = y[:55000], y[55000:60000], y[60000:]

# hyper parameters
Expand All @@ -26,6 +26,7 @@
batch_size = 100
num_models = 2


def build_symbol():
data = mx.sym.var(name="data")
label = mx.sym.var(name="label")
Expand Down Expand Up @@ -134,7 +135,7 @@ def get_batch(p, batch_size, X, y):
total_num = 0
for i in range(total_batch):
num_valid = batch_size if (i + 1) * batch_size <= X_test.shape[0]\
else X_test.shape[0] - i * batch_size
else X_test.shape[0] - i * batch_size
data_npy, label_npy, num_valid = get_batch(i, batch_size, X_test, y_test)
prob_ensemble = nd.zeros(shape=(label_npy.shape[0], 10), ctx=mx.gpu())
for i, test_net in enumerate(test_nets):
Expand Down
7 changes: 3 additions & 4 deletions mxnet/mxlab-12-4-rnn_deep_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# brew install graphviz
# pip3 install graphviz
# pip3 install pydot-ng
from mxnet.visualization import plot_network
import matplotlib.pyplot as plt

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) # Config the logging
Expand Down Expand Up @@ -125,8 +124,8 @@ def train_eval_net(use_cudnn):
print("Done!")


print("CUDNN time spent: %g, test mse: %g" %(cudnn_time_spent, cudnn_mse))
print("NoCUDNN time spent: %g, test mse: %g" %(normal_time_spent, normal_mse))
print("CUDNN time spent: %g, test mse: %g" % (cudnn_time_spent, cudnn_mse))
print("NoCUDNN time spent: %g, test mse: %g" % (normal_time_spent, normal_mse))

plt.close('all')
fig = plt.figure()
Expand All @@ -138,4 +137,4 @@ def train_eval_net(use_cudnn):
'''
CUDNN time spent: 10.0955, test mse: 0.00721571
NoCUDNN time spent: 38.9882, test mse: 0.00565724
'''
'''
Loading

0 comments on commit 27450fc

Please sign in to comment.