In [8]:
import numpy as np
from importlib import reload
import time
import nnmath
import ann
import cnn
In [9]:
training_input = np.load('training_input.npy')
training_output = np.load('training_output.npy')
test_input = np.load('test_input.npy')
test_output = np.load('test_output.npy')
validation_input = np.load('validation_input.npy')
validation_output = np.load('validation_output.npy')
weights0 = np.load('weights0.npy')
weights1 = np.load('weights1.npy')
biases0 = np.load('biases0.npy')
biases1 = np.load('biases1.npy')
In [20]:
ndim_input = 784
image_shape = (1, 28, 28)
ndim_output = 10
n_training = training_input.shape[0]
n_test = test_input.shape[0]
n_validation = validation_input.shape[0]
training_data = (training_input.reshape(np.r_[n_training, np.asarray(image_shape)]), training_output)
test_data = (test_input.reshape(np.r_[n_test, np.asarray(image_shape)]), test_output)
validation_data = (validation_input.reshape(np.r_[n_validation, np.asarray(image_shape)]), validation_output)
In [1057]:
n = 5000
small_training = (training_input[0:n].reshape(np.r_[n, np.asarray(image_shape)]), training_output[0:n])
n = 1000
small_test = (test_input[0:n].reshape(np.r_[n, np.asarray(image_shape)]), test_output[0:n])
In [962]:
reload(nnmath)
reload(nnmath)
reload(cnn)
reload(cnn)
Out[962]:
In [1050]:
feature_shape = (6, 5, 5)
cplayer = cnn.ConvPoolLayer(image_shape, feature_shape)
cplayer.set_mini_batch_size(10)
#feature_shape2 = (12, 5, 5)
#cplayer2 = cnn.ConvPoolLayer((4,12,12), feature_shape2)
fcplayer = cnn.FullyConnectedLayer(6*12*12, 10)
net = cnn.Network((cplayer, fcplayer), cfunc='crossentropy')
#net = cnn.Network((cplayer, cplayer2, fcplayer), cfunc='crossentropy')
#outlayer = cnn.FullyConnectedLayer(20, 10)
#net = cnn.Network((cplayer, fcplayer, outlayer), cfunc='crossentropy')
In [1059]:
net.set_parameters(stepsize=0.5, overfit_lambda=0E-0/50000.)
net.set_mini_batch_size(13)
net.max_epochs = 10
t0 = time.time()
net.SGD(small_training, test_data=small_test)
#net.SGD(training_data, test_data=test_data)
t1 = time.time()
print("Time lapsed {0:6.3f} secs".format(t1-t0))
In [1026]:
f_fcplayer = cnn.FullyConnectedLayer(1*28*28, 100)
f_outlayer = cnn.FullyConnectedLayer(100, 10)
fnet = cnn.Network((f_fcplayer, f_outlayer), cfunc='crossentropy')
In [1031]:
fnet.set_parameters(stepsize=0.125, overfit_lambda=2E-2/50000.)
fnet.set_mini_batch_size(12)
fnet.max_epochs = 10
t0 = time.time()
#fnet.SGD(small_training, test_data=small_test)
fnet.SGD(training_data, test_data=test_data)
t1 = time.time()
print("Time lapsed {0:6.3f} secs".format(t1-t0))
In [1032]:
test_results = np.argmax(fnet.feedforward(validation_data[0]), axis=1)
#tmp_test_data = np.argmax(test_data[1], axis=1)
test_results_random = np.random.choice(np.arange(10), size=(test_results.shape))
In [1046]:
fig = plt.figure(figsize=(20,4))
#fig.subplots_adjust(left=0, bottom=0, right=1, top=1,
fig.subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=0, hspace=0)
axnum = 0
#plt.subplots(figsize=(20, 4))
select_number = 7
index = np.where(test_results==select_number)[0]
tmp_image = validation_data[0][index][np.random.choice(np.arange(index.size), size=(20))].reshape(20, 28, 28)
output = np.zeros((28*2, 28*10))
for i in np.arange(2):
for j in np.arange(10):
axnum += 1
ax = fig.add_subplot(2, 10, axnum)
ax.imshow(tmp_image[i*10+j, ...], cmap=plt.cm.Greys)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# output[i*28:(i+1)*28, j*28:(j+1)*28] = tmp_image[i*10+j,:,:]
#ax.imshow(output,cmap = plt.cm.Greys)
#random_images = test_data[0][np.random.randint(0, test_results.size, size=20)].shape
In [1045]:
fig = plt.figure(figsize=(20,4))
#fig.subplots_adjust(left=0, bottom=0, right=1, top=1,
fig.subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=0, hspace=0)
axnum = 0
#plt.subplots(figsize=(20, 4))
index = np.random.choice(np.arange(test_results.size), size=(20))
tmp_image = validation_data[0][index].reshape(20, 28, 28)
tmp_results = test_results[index]
output = np.zeros((28*2, 28*10))
for i in np.arange(2):
for j in np.arange(10):
axnum += 1
ax = fig.add_subplot(2, 10, axnum)
ax.imshow(tmp_image[i*10+j, ...], cmap=plt.cm.Greys)
ax.text(0.8, 8.0,tmp_results[i*10+j],fontsize=30)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
In [486]:
layer.activation_deriv(layer.z)
Out[486]:
In [172]:
convolved = net.layers[0].feedforward(test_data[0])
fullyconnected = net.layers[1].feedforward(convolved)
allout = net.layers[2].feedforward(fullyconnected)
In [173]:
test_results = np.argmax(allout, axis=1)
tmp_test_data = np.argmax(test_data[1], axis=1)
print(np.count_nonzero(np.equal(test_results, tmp_test_data)))
In [183]:
tmprandom = np.random.choice(np.arange(10), size=10000)
print(np.count_nonzero(np.equal(tmprandom, tmp_test_data)))
In [522]:
864/12/12
Out[522]:
In [203]:
training_data[0].shape
Out[203]:
In [215]:
tt = np.zeros((10,1,28,28))
In [348]:
np.arange(2,3)
Out[348]:
In [445]:
3*12*12
Out[445]:
In [566]:
x = np.random.random((1000, 4))
In [568]:
xx = np.amax(x, axis=1)
In [569]:
xx.mean()
Out[569]:
In [623]:
#atmp = np.arange(100).reshape(10,10)
atmp = np.random.random(8*8).reshape(8,8)
In [624]:
atmp
Out[624]:
In [640]:
mask = np.zeros((8,8), dtype=bool)
aa = nnmath.maxpooling22_down(atmp, mask=mask)
In [651]:
reload(nnmath)
reload(nnmath)
Out[651]:
In [641]:
mask
Out[641]:
In [642]:
atmp[mask]
Out[642]:
In [658]:
btmp = np.arange(4*4).reshape(4,4)+10
In [659]:
nnmath.maxpooling22_up(btmp, mask=mask)
Out[659]:
In [656]:
mask
Out[656]:
In [798]:
btmp
Out[798]:
In [799]:
btmp[::-1, ::-1]
Out[799]:
In [986]:
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]: