Skip to content

Commit 8cbbfb0

Browse files
committed
Merge branch 'develop'
2 parents 3c21c12 + b57fd0b commit 8cbbfb0

6 files changed

Lines changed: 55 additions & 58 deletions

File tree

classes/conv_layer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
2-
from neural_layer import NeuralLayer
3-
import utils as u
2+
from classes.neural_layer import NeuralLayer
3+
import classes.utils as u
44

55

66
class ConvLayer(NeuralLayer):
@@ -69,7 +69,7 @@ def backward(self, d, need_d=True):
6969
d = d.reshape(self.w2, self.h2, self.k, -1).T
7070

7171
delta = d * u.relu_d(self.forward_result)
72-
padding = ((self.w - 1) * self.s + self.f - self.w2) / 2
72+
padding = ((self.w - 1) * self.s + self.f - self.w2) // 2
7373
cols = u.im2col_indices(delta, self.f, self.f, padding=padding, stride=self.s)
7474
sum_weights = []
7575
for index, n in enumerate(self.neurons):

classes/neural_layer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from classes.layer import Layer
2-
from neuron import Neuron
2+
from classes.neuron import Neuron
33
import numpy as np
4-
import utils as u
4+
import classes.utils as u
55

66

77
class NeuralLayer(Layer):
@@ -17,7 +17,7 @@ def __init__(self, input_size, k, u_type='adam', a_type='relu'):
1717
if isinstance(input_size, tuple):
1818
input_size = np.prod(input_size)
1919

20-
for n in xrange(k):
20+
for n in range(k):
2121
self.neurons.append(Neuron(input_size))
2222

2323
def predict(self, batch):

classes/neural_net.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from neural_layer import NeuralLayer
2-
from conv_layer import ConvLayer
3-
from pool_layer import PoolLayer
1+
from classes.neural_layer import NeuralLayer
2+
from classes.conv_layer import ConvLayer
3+
from classes.pool_layer import PoolLayer
4+
import classes.utils as utils
45
import numpy as np
5-
import utils
66

77
class NeuralNetwork(object):
88
def __init__(self, input_shape, layer_list, lr, l2_reg=0, dropout_p=1, loss='softmax'):
@@ -44,7 +44,6 @@ def __init__(self, input_shape, layer_list, lr, l2_reg=0, dropout_p=1, loss='sof
4444
self.layers.append(fc)
4545
next_input_size = fc.output_size()
4646

47-
4847
def predict(self, batch, label):
4948
next_input = batch
5049
for index, layer in enumerate(self.layers):
@@ -97,10 +96,8 @@ def epoch(self, batch, label):
9796
dropout_mask = self.dropout_masks.pop()
9897
back_input *= dropout_mask
9998

100-
10199
# update
102100
for index, layer in enumerate(self.layers):
103101
layer.update(self.lr, l2_reg=self.l2_reg, t=self.t)
104102

105-
106103
return loss + self.l2_reg * l2 / 2, correct_count / float(len(max_result)) * 100

classes/pool_layer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
2-
from layer import Layer
3-
import utils as u
2+
from classes.layer import Layer
3+
import classes.utils as u
44

55

66
# TODO only not overlapping f & s works...
@@ -30,16 +30,16 @@ def __init__(self, input_size, f=2, s=2):
3030

3131
offset = 0
3232
i_offset = 0
33-
for i in xrange(self.h):
33+
for i in range(self.h):
3434
if i % self.f == 0:
3535
start = self.w * i
3636
offset = start
3737
i_offset = i
3838
else:
3939
start = self.f * (i - i_offset) + offset
4040

41-
for j in xrange(self.w / self.f):
42-
self.indices += xrange(start, start + self.f)
41+
for j in range(int(self.w / self.f)):
42+
self.indices += range(start, start + self.f)
4343
start += field_size
4444

4545

classes/utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import numpy as np
22

3+
34
def softmax_loss(x, y):
45
x = x.T
56
probs = np.exp(x - np.max(x, axis=1, keepdims=True))
67
probs /= np.sum(probs, axis=1, keepdims=True)
78
N = x.shape[0]
8-
loss = -np.sum(np.log(probs[xrange(N), y])) / N
9+
loss = -np.sum(np.log(probs[range(N), y])) / N
910
dx = probs
10-
dx[xrange(N), y] -= 1
11+
dx[range(N), y] -= 1
1112
dx /= N
1213
return loss, dx
1314

@@ -18,12 +19,13 @@ def logistic_loss(x, y):
1819
dx = -(y - x)
1920
return loss, dx.T
2021

22+
2123
def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1):
2224
N, C, H, W = x_shape
2325
assert (H + 2 * padding - field_height) % stride == 0
2426
assert (W + 2 * padding - field_height) % stride == 0
25-
out_height = (H + 2 * padding - field_height) / stride + 1
26-
out_width = (W + 2 * padding - field_width) / stride + 1
27+
out_height = (H + 2 * padding - field_height) // stride + 1
28+
out_width = (W + 2 * padding - field_width) // stride + 1
2729

2830
i0 = np.repeat(np.arange(field_height), field_width)
2931
i0 = np.tile(i0, C)
@@ -78,6 +80,7 @@ def adam_update(neurons, lr, t, l2_reg=0, beta1=np.float32(0.9), beta2=np.float3
7880
n.weights -= lr * m / (np.sqrt(v) + 1e-8) + l2
7981
n.b -= lr * d_bias
8082

83+
8184
def nag_update(neurons, lr, l2_reg=0, mu=np.float32(0.9)):
8285
for n in neurons:
8386
l2 = l2_reg * n.weights
@@ -90,6 +93,7 @@ def nag_update(neurons, lr, l2_reg=0, mu=np.float32(0.9)):
9093
n.weights += -mu * n.v_prev + (1 + mu) * n.v - l2
9194
n.b -= lr * d_bias
9295

96+
9397
def momentum_update(neurons, lr, l2_reg=0, mu=np.float32(0.9)):
9498
for n in neurons:
9599
l2 = l2_reg * n.weights
@@ -111,14 +115,18 @@ def vanila_update(neurons, lr, l2_reg=0):
111115
n.weights -= lr * dx + l2
112116
n.b -= lr * d_bias
113117

118+
114119
def sigmoid(input):
115120
return 1/(1+np.exp(-input))
116121

122+
117123
def relu(input):
118124
return np.maximum(0, input)
119125

126+
120127
def sigmoid_d(input):
121128
return input * (1 - input)
122129

130+
123131
def relu_d(input):
124132
return input > 0
Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import sys, os
22
sys.path.insert(1, os.path.split(os.path.split(sys.path[0])[0])[0])
3-
import cPickle as pkl
3+
import pickle as pkl
44
import numpy as np
55
from sklearn import preprocessing
66
from sklearn.utils import shuffle
77
from classes.neural_net import NeuralNetwork
88

9+
910
def unpickle(file):
1011
fo = open(file, 'rb')
11-
dict = pkl.load(fo)
12+
dict = pkl.load(fo, encoding='latin1')
1213
fo.close()
1314
return dict
1415

@@ -20,7 +21,7 @@ def unpickle(file):
2021

2122
test_images = None
2223
test_labels = []
23-
for i in xrange(1, 6):
24+
for i in range(1, 6):
2425
data = unpickle(sys.path[0] + '/data_batch_'+str(i))
2526
if train_images is None:
2627
train_images = data['data']
@@ -29,75 +30,66 @@ def unpickle(file):
2930

3031
train_labels += data['labels']
3132

32-
# train_images = train_images[:100]
33-
# train_labels = train_labels[:100]
34-
3533
train_images = train_images.reshape(-1, 3, 32, 32)
3634
train_images = train_images.astype(np.float128)
37-
train_images /= 255.0
38-
39-
r_mean = np.average(train_images[:, 0])
40-
g_mean = np.average(train_images[:, 1])
41-
b_mean = np.average(train_images[:, 2])
4235

43-
train_images[:, 0] -= r_mean
44-
train_images[:, 1] -= g_mean
45-
train_images[:, 2] -= b_mean
36+
mean_image= np.mean(train_images, axis=0)
37+
train_images -= mean_image
38+
std = np.std(train_images, axis=0)
39+
train_images /= std
4640

4741
train_images = train_images.astype(np.float32)
4842

4943
data = unpickle(sys.path[0] + '/test_batch')
5044
test_images = data['data'].reshape(-1, 3, 32, 32)
5145
test_images = test_images.astype(np.float128)
52-
test_images /= 255.0
5346

54-
test_images[:, 0] -= r_mean
55-
test_images[:, 1] -= g_mean
56-
test_images[:, 2] -= b_mean
47+
test_images -= mean_image
48+
test_images /= std
5749

58-
test_images.astype(np.float32)
50+
test_images = test_images.astype(np.float32)
5951

6052
test_labels = data['labels']
6153

6254
lr = 1e-4
63-
dropout_percent = 0.5
64-
l2_reg = 4e-7
65-
learning_rate_decay = np.float32(99e-2)
66-
batch_size = 10
55+
dropout_percent = 0.4
56+
l2_reg = 3e-6
57+
learning_rate_decay = np.float32(100e-2)
58+
batch_size = 1
6759

6860
cnn = NeuralNetwork(train_images.shape[1:],
6961
[
70-
{'type': 'conv', 'k': 16, 'u_type': 'adam', 'f': 5, 's': 1, 'p': 2},
62+
{'type': 'conv', 'k': 16, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
7163
{'type': 'pool'},
72-
{'type': 'conv', 'k': 20, 'u_type': 'adam', 'f': 5, 's': 1, 'p': 2},
64+
{'type': 'conv', 'k': 20, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
7365
{'type': 'pool'},
74-
{'type': 'conv', 'k': 20, 'u_type': 'adam', 'f': 5, 's': 1, 'p': 2},
66+
{'type': 'conv', 'k': 20, 'u_type': 'nag', 'f': 5, 's': 1, 'p': 2},
7567
{'type': 'pool'},
7668
{'type': 'output', 'k': len(le.classes_), 'u_type': 'adam'}
7769
]
7870
, lr, l2_reg=l2_reg, dropout_p=dropout_percent)
7971

8072
cnn.epoch_count = 0
8173

82-
for i in xrange(60000000):
74+
for i in range(60000000):
8375
start = i * batch_size % len(train_images)
8476
end = start + batch_size
8577

8678
if start == 0 and i != 0:
8779
cnn.epoch_count += 1
8880
train_images, train_labels = shuffle(train_images, train_labels)
89-
print '{} epoch finish. learning rate is {}'.format(str(cnn.epoch_count), str(cnn.lr))
81+
print('{} epoch finish. learning rate is {}'.format(str(cnn.epoch_count), str(cnn.lr)))
9082
cnn.lr *= learning_rate_decay
9183

92-
loss, acc = cnn.predict(train_images[:4000], train_labels[:4000])
93-
print 'training acc:{}'.format(acc)
94-
print 'training loss:{}'.format(loss)
84+
loss, acc = cnn.predict(train_images[:2000], train_labels[:2000])
85+
print('training acc:{}'.format(acc))
86+
print('training loss:{}'.format(loss))
9587

96-
test_loss, test_acc = cnn.predict(test_images, test_labels)
97-
print 'test acc:{}'.format(test_acc)
98-
print 'test loss:{}'.format(test_loss)
88+
test_loss, test_acc = cnn.predict(test_images[:5000], test_labels[:5000])
89+
print('test acc:{}'.format(test_acc))
90+
print('test loss:{}'.format(test_loss))
9991

10092
cnn.t += 1
10193
loss, acc = cnn.epoch(train_images[start:end], train_labels[start:end])
102-
# print loss
103-
# print acc
94+
# print(loss)
95+
# print(acc)

0 commit comments

Comments
 (0)