Beispiel #1
0
def ex2():
    x, y, x_train, x_val, x_test, y_train, y_val, y_test = generate_dateset()

    nn = NeuralNetwork(NN_SHAPE, LEARNING_RATE)
    pbar = tqdm(range(EPOCH))
    for e in pbar:
        shuffle(x_train, y_train)
        nn.train_sgd(x_train, y_train, batch_size=BATCH_SIZE)

        # compute train and validation accuracy
        train_accurracy = nn.evaluate(x_train, y_train)
        val_accuracy = nn.evaluate(x_val, y_val)
        pbar.set_description(
            f"Epoch {e:03}/{EPOCH} - Train {train_accurracy:.3f}% - Test {val_accuracy:.3f}% "
        )

    # compute test accuracy
    test_accuracy = nn.evaluate(x_test, y_test)
    print(f"Test {test_accuracy:.3f}%")

    # plot NN borders
    plot_contour(nn, x, y)
Beispiel #2
0
plt.xticks(list(range(10)))
plt.yticks(list(range(10)))
plt.ylim([-0.5, 9.5])
plt.save('mnist-cm.png')
plt.show()

# print(network.predict(X_test.T))
plt.plot(network.cost, label='loss')
plt.plot(network.val_cost, label='val_loss')
plt.plot(network.acc, label='acc')
plt.plot(network.val_acc, label='val_acc')
plt.legend()
plt.show()

pred = np.argmax(network.predict(X_test.T), axis=0)
cm = confusion_matrix(np.argmax(y_test.T, axis=0),
                      pred,
                      labels=list(range(10)))

print(cm)

plt.imshow(cm)
plt.xlabel("predicted")
plt.ylabel("true")
plt.xticks(list(range(10)))
plt.yticks(list(range(10)))
plt.ylim([-0.5, 9.5])
plt.show()

print(network.evaluate(X_test.T, y_test.T))
Beispiel #3
0
from nn import NeuralNetwork
from keras.datasets import mnist

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784, 1)
x_test = x_test.reshape(10000, 784, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_train = y_train.reshape(60000, 10, 1)
y_test = y_test.reshape(10000, 10, 1)

epoches = 3
nn = NeuralNetwork(784, 64, 10)
for epoch in range(int(epoches)):
    for i in range(len(x_train)):
        index = np.random.choice(len(x_train))
        x = x_train[index]
        y = y_train[index]
        nn.train(x, y)
        print(f"\rTraining: {i}/{len(x_train)}", end="")

    nn.evaluate(x_test, y_test)