Skip to content

Commit 27450fc

Browse files
authored
refactor: PEP8 FLAKE8 (#127)
1. Format to PEP8 2. Remove unused var/package
1 parent f69c95f commit 27450fc

28 files changed

+47
-52
lines changed

Chainer/chlab-02-1-linear_regression.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
import numpy as np
55
import chainer
6-
from chainer import training, Variable
7-
from chainer import datasets, iterators, optimizers
8-
from chainer import Chain
6+
from chainer import training
7+
from chainer import datasets
98
from chainer.training import extensions
109

1110
import chainer.functions as F

Keras/klab-10-1-mnist_softmax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
model.summary()
5050

5151
model.compile(loss='categorical_crossentropy',
52-
optimizer='adam', learning_rate = learning_rate,
52+
optimizer='adam', learning_rate=learning_rate,
5353
metrics=['accuracy'])
5454

5555
model.fit(X_train, Y_train, batch_size=batch_size, epochs=training_epochs)

Keras/klab-12-1-softmax_hello_char.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from keras.models import Sequential
3-
from keras.layers import Dense, TimeDistributed, Activation, LSTM
3+
from keras.layers import Activation
4+
from keras.layers import Dense
45
from keras.utils import np_utils
56

67
import os

Keras/klab-12-3-rnn_stock_prediction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
22
import numpy as np
33
from keras.models import Sequential
4-
from keras.layers import Dense, LSTM, Activation
4+
from keras.layers import Activation
5+
from keras.layers import LSTM
56
from sklearn.preprocessing import MinMaxScaler
67
import os
78

PyTorch/lab-10-3-mnist_nn_xavier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
66
import random
7-
from torch.nn import init
7+
import torch.nn.init
88

99
torch.manual_seed(777) # reproducibility
1010

PyTorch/lab-10-4-mnist_nn_deep.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
66
import random
7-
from torch.nn import init
7+
import torch.nn.init
88

99
torch.manual_seed(777) # reproducibility
1010

PyTorch/lab-10-5-mnist_nn_dropout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
66
import random
7-
from torch.nn import init
7+
import torch.nn.init
88

99
torch.manual_seed(777) # reproducibility
1010

PyTorch/lab-11-1-mnist_cnn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from torch.autograd import Variable
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
6-
from torch.nn import init
6+
import torch.nn.init
77

88
torch.manual_seed(777) # reproducibility
99

@@ -32,6 +32,7 @@
3232

3333

3434
class CNN(torch.nn.Module):
35+
3536
def __init__(self):
3637
super(CNN, self).__init__()
3738
# L1 ImgIn shape=(?, 28, 28, 1)

PyTorch/lab-11-2-mnist_deep_cnn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from torch.autograd import Variable
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
6-
from torch.nn import init
6+
import torch.nn.init
77

88
torch.manual_seed(777) # reproducibility
99

@@ -33,6 +33,7 @@
3333

3434

3535
class CNN(torch.nn.Module):
36+
3637
def __init__(self):
3738
super(CNN, self).__init__()
3839
# L1 ImgIn shape=(?, 28, 28, 1)

PyTorch/lab-11-3-mnist_cnn_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from torch.autograd import Variable
44
import torchvision.datasets as dsets
55
import torchvision.transforms as transforms
6-
from torch.nn import init
6+
import torch.nn.init
77

88
torch.manual_seed(777) # reproducibility
99

@@ -32,6 +32,7 @@
3232

3333

3434
class CNN(torch.nn.Module):
35+
3536
def __init__(self):
3637
super(CNN, self).__init__()
3738
self._build_net()

PyTorch/lab-12-1-hello-rnn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import torch
33
import torch.nn as nn
44
from torch.autograd import Variable
5-
import numpy as np
65

76
torch.manual_seed(777) # reproducibility
87

@@ -39,6 +38,7 @@
3938

4039

4140
class RNN(nn.Module):
41+
4242
def __init__(self, num_classes, input_size, hidden_size, num_layers):
4343
super(RNN, self).__init__()
4444
self.num_classes = num_classes

PyTorch/lab-12-2-char-seq-rnn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import torch
33
import torch.nn as nn
44
from torch.autograd import Variable
5-
import numpy as np
65

76
torch.manual_seed(777) # reproducibility
87

@@ -46,6 +45,7 @@ def one_hot(x, num_classes):
4645

4746

4847
class LSTM(nn.Module):
48+
4949
def __init__(self, num_classes, input_size, hidden_size, num_layers):
5050
super(LSTM, self).__init__()
5151
self.num_classes = num_classes

PyTorch/lab-12-4-rnn-long_char.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import torch
33
import torch.nn as nn
44
from torch.autograd import Variable
5-
import numpy as np
65

76
torch.manual_seed(777) # reproducibility
87

@@ -59,6 +58,7 @@ def one_hot(x, num_classes):
5958

6059

6160
class LSTM(nn.Module):
61+
6262
def __init__(self, num_classes, input_size, hidden_size, num_layers):
6363
super(LSTM, self).__init__()
6464
self.num_classes = num_classes

PyTorch/lab-12-5-stock_prediction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def MinMaxScaler(data):
8383

8484

8585
class LSTM(nn.Module):
86+
8687
def __init__(self, num_classes, input_size, hidden_size, num_layers):
8788
super(LSTM, self).__init__()
8889
self.num_classes = num_classes

lab-07-1-learning_rate_and_evaluation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Lab 7 Learning rate and Evaluation
22
import tensorflow as tf
3-
import numpy as np
43
tf.set_random_seed(777) # for reproducibility
54

65
x_data = [[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5],

lab-09-4-xor_tensorboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
# tensorboard --logdir=./logs/xor_logs
5050
merged_summary = tf.summary.merge_all()
5151
writer = tf.summary.FileWriter("./logs/xor_logs_r0_01")
52-
writer.add_graph(sess.graph) # Show the graph
52+
writer.add_graph(sess.graph) # Show the graph
5353

5454
# Initialize TensorFlow variables
5555
sess.run(tf.global_variables_initializer())

lab-10-7-mnist_nn_higher_level_API.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
c = sess.run(cost, feed_dict=feed_dict_cost)
7878
avg_cost += c / total_batch
7979

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

8383
print('Learning Finished!')

lab-11-3-mnist_cnn_class.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Lab 11 MNIST and Deep learning CNN
22
import tensorflow as tf
3-
import random
43
# import matplotlib.pyplot as plt
54

65
from tensorflow.examples.tutorials.mnist import input_data

lab-11-4-mnist_cnn_layers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Lab 11 MNIST and Deep learning CNN
22
import tensorflow as tf
3-
import random
43
# import matplotlib.pyplot as plt
54

65
from tensorflow.examples.tutorials.mnist import input_data

lab-13-1-mnist_using_scope.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Lab 7 Learning rate and Evaluation
22
import tensorflow as tf
3-
import numpy as np
43
import random
5-
import matplotlib.pyplot as plt
64

75
from tensorflow.examples.tutorials.mnist import input_data
86

lab-13-2-mnist_tensorboard.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Lab 7 Learning rate and Evaluation
22
import tensorflow as tf
3-
import numpy as np
43
import random
5-
import matplotlib.pyplot as plt
64

75
from tensorflow.examples.tutorials.mnist import input_data
86

mxnet/mxlab-04-3-file_input_linear_regression.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
data_names=['data'],
3333
label_names=['target'],
3434
context=mx.gpu(0))
35-
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
36-
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
35+
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
36+
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
3737
net.init_params(initializer=mx.init.Normal(sigma=0.01))
3838
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-4, 'momentum': 0.9})
3939

@@ -58,4 +58,4 @@
5858
'''
5959
input = [60, 70, 110], score = [[ 182.48858643]]
6060
input = [90, 100, 80], score = [[ 175.24279785]]
61-
'''
61+
'''

mxnet/mxlab-05-2-logistic_regression_diabetes.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Lab 5 Logistic Regression Classifier
22
import mxnet as mx
3-
import mxnet.ndarray as nd
43
import numpy as np
54
import logging
65
import sys
@@ -30,10 +29,10 @@
3029
data_names=['data'],
3130
label_names=['target'],
3231
context=mx.gpu(0))
33-
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
34-
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
32+
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
33+
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size, 1), layout='NC')])
3534
net.init_params(initializer=mx.init.Normal(sigma=0.01))
36-
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-3, 'momentum':0.9})
35+
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-3, 'momentum': 0.9})
3736

3837
# 4. Train the model
3938
# First constructing the training iterator and then fit the model
@@ -84,4 +83,4 @@
8483
[ 1.]
8584
[ 1.]
8685
[ 1.]]
87-
'''
86+
'''

mxnet/mxlab-06-2-softmax_zoo_classifier.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Lab 6 Softmax Classifier
22
import mxnet as mx
3-
import mxnet.ndarray as nd
43
import numpy as np
54
import logging
65
import sys
@@ -32,10 +31,10 @@
3231
data_names=['data'],
3332
label_names=['target'],
3433
context=mx.gpu(0))
35-
net.bind(data_shapes = [mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
36-
label_shapes= [mx.io.DataDesc(name='target', shape=(batch_size,), layout='NC')])
34+
net.bind(data_shapes=[mx.io.DataDesc(name='data', shape=(batch_size, dimension), layout='NC')],
35+
label_shapes=[mx.io.DataDesc(name='target', shape=(batch_size,), layout='NC')])
3736
net.init_params(initializer=mx.init.Normal(sigma=0.01))
38-
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-1, 'momentum':0.9})
37+
net.init_optimizer(optimizer='sgd', optimizer_params={'learning_rate': 1E-1, 'momentum': 0.9})
3938

4039
# 4. Train the model
4140
# First constructing the training iterator and then fit the model
@@ -91,4 +90,4 @@
9190
0. 0. 1. 1. 1. 1. 3. 3. 2. 0. 0. 0. 0. 0. 0. 0. 0. 1.
9291
6. 3. 0. 0. 2. 6. 1. 1. 2. 6. 3. 1. 0. 6. 3. 1. 5. 4.
9392
2. 2. 3. 0. 0. 1. 0. 5. 0. 6. 1.]
94-
'''
93+
'''

mxnet/mxlab-11-2-mnist_deep_cnn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
X, y = mnist.data, mnist.target
1717
X = X.astype(np.float32) / 255.0
1818
X_train, X_valid, X_test = X[:55000].reshape((-1, 1, 28, 28)),\
19-
X[55000:60000].reshape((-1, 1, 28, 28)),\
20-
X[60000:].reshape((-1, 1, 28, 28))
19+
X[55000:60000].reshape((-1, 1, 28, 28)),\
20+
X[60000:].reshape((-1, 1, 28, 28))
2121
y_train, y_valid, y_test = y[:55000], y[55000:60000], y[60000:]
2222

2323
# hyper parameters
@@ -92,7 +92,7 @@
9292
# Slice the data batch and label batch.
9393
# Note that we use np.take to ensure that the batch will be padded correctly.
9494
data_npy = np.take(X_train,
95-
indices=np.arange(i * batch_size, (i+1) * batch_size),
95+
indices=np.arange(i * batch_size, (i + 1) * batch_size),
9696
axis=0,
9797
mode="clip")
9898
label_npy = np.take(y_train,
@@ -115,7 +115,7 @@
115115
total_num = 0
116116
for i in range(total_batch):
117117
num_valid = batch_size if (i + 1) * batch_size <= X_test.shape[0]\
118-
else X_test.shape[0] - i * batch_size
118+
else X_test.shape[0] - i * batch_size
119119
data_npy = np.take(X_test,
120120
indices=np.arange(i * batch_size, (i + 1) * batch_size),
121121
axis=0,
@@ -137,7 +137,7 @@
137137
test_net.reshape(data_shapes=[mx.io.DataDesc(name='data', shape=(1, 1, 28, 28), layout='NCHW')],
138138
label_shapes=None)
139139
r = np.random.randint(0, X_test.shape[0])
140-
test_net.forward(data_batch=mx.io.DataBatch(data=[nd.array(X_test[r:r+1])],
140+
test_net.forward(data_batch=mx.io.DataBatch(data=[nd.array(X_test[r:r + 1])],
141141
label=None))
142142
logits_nd = test_net.get_outputs()[0]
143143
print("Label: ", int(y_test[r]))

mxnet/mxlab-11-5-mnist_cnn_ensemble_layers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
X, y = mnist.data, mnist.target
1717
X = X.astype(np.float32) / 255.0
1818
X_train, X_valid, X_test = X[:55000].reshape((-1, 1, 28, 28)),\
19-
X[55000:60000].reshape((-1, 1, 28, 28)),\
20-
X[60000:].reshape((-1, 1, 28, 28))
19+
X[55000:60000].reshape((-1, 1, 28, 28)),\
20+
X[60000:].reshape((-1, 1, 28, 28))
2121
y_train, y_valid, y_test = y[:55000], y[55000:60000], y[60000:]
2222

2323
# hyper parameters
@@ -26,6 +26,7 @@
2626
batch_size = 100
2727
num_models = 2
2828

29+
2930
def build_symbol():
3031
data = mx.sym.var(name="data")
3132
label = mx.sym.var(name="label")
@@ -134,7 +135,7 @@ def get_batch(p, batch_size, X, y):
134135
total_num = 0
135136
for i in range(total_batch):
136137
num_valid = batch_size if (i + 1) * batch_size <= X_test.shape[0]\
137-
else X_test.shape[0] - i * batch_size
138+
else X_test.shape[0] - i * batch_size
138139
data_npy, label_npy, num_valid = get_batch(i, batch_size, X_test, y_test)
139140
prob_ensemble = nd.zeros(shape=(label_npy.shape[0], 10), ctx=mx.gpu())
140141
for i, test_net in enumerate(test_nets):

mxnet/mxlab-12-4-rnn_deep_prediction.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# brew install graphviz
1010
# pip3 install graphviz
1111
# pip3 install pydot-ng
12-
from mxnet.visualization import plot_network
1312
import matplotlib.pyplot as plt
1413

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

127126

128-
print("CUDNN time spent: %g, test mse: %g" %(cudnn_time_spent, cudnn_mse))
129-
print("NoCUDNN time spent: %g, test mse: %g" %(normal_time_spent, normal_mse))
127+
print("CUDNN time spent: %g, test mse: %g" % (cudnn_time_spent, cudnn_mse))
128+
print("NoCUDNN time spent: %g, test mse: %g" % (normal_time_spent, normal_mse))
130129

131130
plt.close('all')
132131
fig = plt.figure()
@@ -138,4 +137,4 @@ def train_eval_net(use_cudnn):
138137
'''
139138
CUDNN time spent: 10.0955, test mse: 0.00721571
140139
NoCUDNN time spent: 38.9882, test mse: 0.00565724
141-
'''
140+
'''

0 commit comments

Comments
 (0)