def train(): mnn = MultilayerNN() mnn.add(FullConnectLayer([784, 300], 0.003)) mnn.add(FullConnectLayer([300, 100], 0.003)) mnn.add(FullConnectLayer([100, 10], 0.003)) mnn.add(SoftmaxLayer()) from util.mnist import mnist_train_data, \ mnist_train_label, image_to_binary, \ label_to_one_hot, mnist_test_data, mnist_test_label test_data = image_to_binary(mnist_test_data) test_label = label_to_one_hot(mnist_test_label) batch_size = 32 for i in range(1000): for j in range(60000 // batch_size): train_data = image_to_binary(mnist_train_data) train_label = label_to_one_hot(mnist_train_label) input_tensor = train_data[j * batch_size:(j + 1) * batch_size].reshape(-1, 1, 28, 28) label_tensor = train_label[j * batch_size:(j + 1) * batch_size] input_tensor = input_tensor.reshape([-1, 1, 28, 28]) mnn.train(input_tensor, label_tensor) if j % 10 == 0: pred = mnn.predict(test_data) accuracy = np.mean(np.equal(np.argmax(pred, axis=1), np.argmax(test_label, axis=1))) print(mnn.loss(), mnn.accuracy(), accuracy) if j % 100 == 0: mnn.save_model_as_json()
def train_from_exist(): mnn = MultilayerNN() mnn.load_model_from_json("./model-1513582360.model") from util.mnist import mnist_train_data, \ mnist_train_label, image_to_binary, \ label_to_one_hot, mnist_test_data, mnist_test_label test_data = image_to_binary(mnist_test_data) test_label = label_to_one_hot(mnist_test_label) batch_size = 32 for i in range(1000): for j in range(60000 // batch_size): train_data = image_to_binary(mnist_train_data) train_label = label_to_one_hot(mnist_train_label) input_tensor = train_data[j * batch_size:(j + 1) * batch_size].reshape(-1, 1, 28, 28) label_tensor = train_label[j * batch_size:(j + 1) * batch_size] input_tensor = input_tensor.reshape([-1, 1, 28, 28]) mnn.train(input_tensor, label_tensor) if j % 10 == 0: pred = mnn.predict(test_data) accuracy = np.mean(np.equal(np.argmax(pred, axis=1), np.argmax(test_label, axis=1))) print(mnn.loss(), mnn.accuracy(), accuracy) if j % 100 == 0: mnn.save_model_as_json()
def mnist_test(): from util.mnist import mnist_train_data, \ mnist_train_label, mnist_test_data, mnist_test_label, image_to_binary, \ label_to_one_hot, one_hot_to_label train_data = image_to_binary(mnist_train_data) train_label = label_to_one_hot(mnist_train_label) test_data = image_to_binary(mnist_test_data) test_label = mnist_test_label sm = Softmax(train_data, train_label) sm.train() pred = one_hot_to_label(sm.predict(test_data)).reshape(-1) print(np.sum(pred == test_label))
def test(): mnn = MultilayerNN() mnn.load_model_from_json("./model-1514368830.model") from util.mnist import mnist_test_data, mnist_test_label, image_to_binary, \ label_to_one_hot test_data = image_to_binary(mnist_test_data) test_label = label_to_one_hot(mnist_test_label) pred = mnn.predict(test_data) accuracy = np.mean(np.equal(np.argmax(pred, axis=1), np.argmax(test_label, axis=1))) print(accuracy)
from sklearn.neighbors import NearestNeighbors from util.mnist import mnist_train_data, \ mnist_train_label, mnist_test_data, mnist_test_label, image_to_binary, \ label_to_one_hot, one_hot_to_label nn = NearestNeighbors(n_neighbors=3, metric="l2", n_jobs=-1, algorithm='kd_tree') train_data = image_to_binary(mnist_train_data) train_label = label_to_one_hot(mnist_train_label) test_data = image_to_binary(mnist_test_data) test_label = mnist_test_label nn.fit(train_data) print(nn.kneighbors(test_data))