示例#1
0
nn = NeuralNetMLP(n_output=10,
        n_features= X_train.shape[1],
        n_hidden = 50,
        l2 = 0.1,
        l1 = 0.0,
        epochs=1000,
        eta=0.001,
        alpha=0.001,
        decrease_const=0.00001,
        shuffle=True,
        minibatches=50,
        random_state= 1)

start_time = time.time()

nn.fit(X_train,y_train, print_progress=True)

y_train_pred = nn.predict(X_train)

y_test_pred = nn.predict(X_test)

end_time = time.time()

total_time = start_time - end_time

f = open('out.txt', 'w')

for val in y_train_pred:
     f.write(str(val) + '\n')
X_test, y_test = load_mnist('mnist', kind='t10k')
# print('Rows: %d, columns: %d' % (X_test.shape[0], X_test.shape[1]))

nn = NeuralNetMLP(n_hidden=100,
                  l2=0.01,
                  epochs=200,
                  eta=0.0005,
                  minibatch_size=100,
                  shuffle=True,
                  seed=1)
nn.fit(X_train=X_train[:55000],
       y_train=y_train[:55000],
       X_valid=X_train[55000:],
       y_valid=y_train[55000:])

y_test_pred = nn.predict(X_test)
miscl_img = X_test[y_test != y_test_pred][25:50]
correct_lab = y_test[y_test != y_test_pred][25:50]
miscl_lab = y_test_pred[y_test != y_test_pred][25:50]
fig, ax = plt.subplots(nrows=5, ncols=5, sharex=True, sharey=True)
ax = ax.flatten()
for i in range(25):
    img = miscl_img[i].reshape(28, 28)
    ax[i].imshow(img, cmap='Greys', interpolation='nearest')
    ax[i].set_title('%d t: %d p: %d' % (i + 1, correct_lab[i], miscl_lab[i]))
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.show()

with open('minst.nn', 'wb') as f:
    return images, labels


X_train, y_train = load_mnist('/Users/chandu/Documents/UTD S2/ML Projects/',
                              kind='train')
X_test, y_test = load_mnist('/Users/chandu/Documents/UTD S2/ML Projects/',
                            kind='t10k')

nn = NeuralNetMLP(n_output=10,
                  n_features=X_train.shape[1],
                  n_hidden=50,
                  l2=0.1,
                  l1=0.0,
                  epochs=1000,
                  eta=0.001,
                  alpha=0.001,
                  decrease_const=0.00001,
                  shuffle=True,
                  minibatches=50,
                  random_state=1)

nn.fit(X_train, y_train, print_progress=True)

y_train_pred = nn.predict(X_train)
acc = np.sum(y_train == y_train_pred, axis=0) / X_train.shape[0]
print('Training accuracy: %.2f%%' % (acc * 100))

y_test_pred = nn.predict(X_test)
acc = np.sum(y_test == y_test_pred, axis=0) / X_test.shape[0]
print('Test accuracy: %.2f%%' % (acc * 100))
        magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16))
        images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784)

        return images, labels


X_train, y_train = load_mnist('data/', kind='train')
print('Rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1]))

X_test, y_test = load_mnist('data/', kind='t10k')
print('Rows: %d, columns: %d' % (X_test.shape[0], X_test.shape[1]))

nn = NeuralNetMLP(n_output=10,
                  n_features=X_train.shape[1],
                  n_hidden=50,
                  l2=0.1,
                  l1=0.0,
                  epochs=1000,
                  eta=0.001,
                  alpha=0.001,
                  decrease_const=0.00001,
                  minibatches=50,
                  shuffle=True,
                  random_state=1)

nn.fit(X_train, y_train, print_progress=True)
image = Image.open('./kawase_20170814.png').convert('L')
image = image.resize((28, 28))
image_array = np.asarray(ImageOps.invert(image), dtype='uint8')
nn.predict(image_array.reshape(1, 28 * 28))
示例#5
0
	fig = plt.figure()
	plt.plot(range(nn.epochs), nn.eval_['cost'])
	plt.ylabel('Cost')
	plt.xlabel('Epochs')
	fig.savefig('NeuralNetMLP_cost.pdf')

	fig = plt.figure()
	plt.plot(range(nn.epochs), nn.eval_['train_acc'], label='training')
	plt.plot(range(nn.epochs), nn.eval_['valid_acc'], label='validation', linestyle='--')
	plt.ylabel('Accuracy')
	plt.xlabel('Epochs')
	fig.savefig('NeuralNetMLP_acc.pdf')


	y_test_pred = nn.predict(X_test)
	acc = (np.sum(y_test == y_test_pred).astype(np.float)/X_test.shape[0])
	print('Training accuracy: %.2f%%' % (acc*100))
	miscl_img = X_test[y_test != y_test_pred][:25]
	correct_lab = y_test[y_test != y_test_pred][:25]
	miscl_lab = y_test_pred[y_test != y_test_pred][:25]
	fig, ax =plt.subplots(nrows=5, ncols=5, sharex=True, sharey=True)
	ax = ax.flatten()
	for i in range(25):
		img = miscl_img[i].reshape(28,28)
		ax[i].imshow(img, cmap='Greys', interpolation='nearest')
		ax[i].set_title('%d) t: %d p: %d' % (i+1, correct_lab[i], miscl_lab[i]))
	ax[0].set_xticks([])
	ax[0].set_yticks([])
	plt.tight_layout()
	fig.savefig('NeuralNetMLP_miscl.pdf')
示例#6
0
                  shuffle=True,               # データのシャッフル
                  random_state=3)             # 乱数シードの状態
                  
nn.fit(X_trn, y_trn, print_progress=True)

plt.figure(0)
plt.plot(range(len(nn.cost_)), nn.cost_)
plt.ylim([0, 1000])
plt.ylabel('Cost')
plt.xlabel('Epochs * 50')
plt.tight_layout()

# =====================================================================
# 課題3(b): 学習用データ,検証用データおよびテストデータに対するaccuracyの算出を追加する

y_trn_pred = nn.predict(X_trn)
acc = np.sum(y_trn == y_trn_pred, axis=0) / X_trn.shape[0]
print('accuracy for trn data: %.2f%%' % (acc * 100))

y_vld_pred = nn.predict(X_vld)
acc = np.sum(y_vld == y_vld_pred, axis=0) / X_vld.shape[0]
print('accuracy for vld data: %.2f%%' % (acc * 100))

y_tst_pred = nn.predict(X_tst)
acc = np.sum(y_tst == y_tst_pred, axis=0) / X_tst.shape[0]
print('accuracy for tst data: %.2f%%' % (acc * 100))

# =====================================================================

plt.show()