Пример #1
0
def main():
    # Parameters
    sizes = [784, 16, 16, 10]
    eta = 3
    mini_batch_size = 50
    epochs = 30

    nn = NN(sizes)
    emnistTrainData, validation_data, test_data = mnist_loader.load_data_wrapper(
    )
    results = nn.stochastic_gradient_descent(training_data=emnistTrainData,
                                             mini_batch_size=mini_batch_size,
                                             epochs=epochs,
                                             learning_rate=eta)

    epochResults = [item[0] for item in results]

    # Outputs image and what the trained network thinks it should be
    count = 0
    stop = 0  # int(input("How many images would you like to see?: "))
    while count < stop:
        example = emnistTrainData[count]
        pixels = example[0].reshape((28, 28))
        expectedLabel = np.argmax(example[1])
        predictedLabel = np.argmax(nn.feed_forward(example[0]))
        # Plot
        plt.title(
            'Expected: {expectedLabel}, Predicted: {predictedLabel}: '.format(
                expectedLabel=expectedLabel, predictedLabel=predictedLabel))
        plt.imshow(pixels, cmap='gray')
        plt.show()
        count += 1

    # Plot learning progress
    plt.plot(epochResults)
    plt.ylabel("# Correct")
    plt.xlabel("Epoch")
    plt.title("Learning Rate = " + str(eta))
    plt.show()

    # test network on test data set
    total = len(test_data)
    correct = 0
    for example in test_data:
        inputLayer = example[0]
        output = np.argmax((nn.feed_forward(inputLayer)))
        if output == example[1]:
            correct += 1
    print("Model Correctly Identified %d out of %d examples" %
          (correct, total))
Пример #2
0
def test_feed_forward():
    config = {
        "learning rate": .0001,
        "debug": False,
        "layer node count": [2, 2, 2],
        "first moment decay rate": .9,
        "second moment decay rate": .999,
        "experience replay size": 8,
        "replay batches": 4,
        "model size": 50000,
        "report frequency": 10,
        "number episodes": 300000000,
        "initial temp": 1,
        "adam epsilon": 10**-8
    }
    num_inputs = config["layer node count"][0]
    num_hidden = config["layer node count"][1]
    num_output = config["layer node count"][2]
    config["weights"] = [
        np.ones((num_inputs, num_hidden)),
        np.ones((num_hidden, num_output)) * 2
    ]
    config["bias"] = [np.ones((num_hidden)), np.ones((num_output)) * 2]

    nn = NN(config)

    state = np.ones(num_inputs) * 1
    state2 = np.ones(num_inputs) * 2
    state3 = np.ones(num_inputs) * 3
    states = np.row_stack((state, state2, state3))
    zs, activations = nn.feed_forward(states)
    for i in range(3):
        if not np.allclose(activations[-2][i], num_inputs * (i + 1) + 1):
            raise ValueError("hidden outputs are off")
    for j in range(3):
        if not np.allclose(activations[-1][j],
                           ((num_inputs * (j + 1) + 1) * num_hidden * 2) + 2):
            raise ValueError("hidden outputs are off")

    # Test RELU

    state = np.ones(num_inputs) * -2
    states = state.reshape((1, 2))
    zs, activations = nn.feed_forward(states)
    if not np.allclose(activations[-2][:], 0):
        raise ValueError("hidden outputs are off")

    if not np.allclose(activations[-1][:], 2):  # only bias..
        raise ValueError("hidden outputs are off")
Пример #3
0
import numpy as np
from nn import NN, Layer

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

Y = np.array([[0], [1], [1], [0]])

nn = NN(2)
nn.add(Layer(3))
nn.add(Layer(1))

print("Predictions before training")
print(nn.feed_forward(X))
print()

nn.train(X, Y)

print()
print("Predictions after training")
print(nn.feed_forward(X))