def getAccuracy(var, begin, name, step, loop, path="./results/"):
    value = begin
    for i in range(loop):
        print(i)
        # create network
        if (var == 'hid'):
            nn = NeuralNetwork(hid=value, act=ACT)
        elif (var == 'lay'):
            nn = NeuralNetwork(lay=value, act=ACT)
        elif (var == 'lr'):
            nn = NeuralNetwork(lr=value, act=ACT)
        elif (var == 'epoch'):
            nn = NeuralNetwork(act=ACT)

        # train network
        if (var == 'epoch'):
            nn.train(ds.train_data, ds.train_labels_arr, value, DATA)
        else:
            nn.train(ds.train_data, ds.train_labels_arr, EPOCH, DATA)

        # counters
        correct = 0
        false = 0
        for i in range(TEST):
            output = nn.feedforward(ds.test_data[i],
                                    isRound=False,
                                    isSoftmax=True)
            output_digit = np.where(output == np.amax(output))
            if output_digit[0][0] == ds.test_labels[i]:
                correct += 1
            else:
                false += 1
        # calc accuracy
        accuracy = (correct * 100) / (correct + false)
        # log accuracy
        logResults(path, name, value, accuracy)
        # increment value
        value += step
if __name__ == "__main__":
    p = Problem("bdate2.txt")
    X_values = p.getX()
    Y_values = p.getY()
    number_of_instances = p.getNumberOfInstances()

    #X the array of inputs, Y the array of outputs

    X = np.array(X_values)
    Y = np.array(Y_values)
    neuralNetwork = NeuralNetwork(X, Y, 2)

    neuralNetwork.loss = []
    iterations = []
    for i in range(1000):  # noOfIterations
        neuralNetwork.feedforward()

        # learning rate = 0.0000001
        neuralNetwork.backprop(0.0000001)

        iterations.append(i)

    #In this plot we can see how the loss function decreases as iterations increase

    plt.pyplot.plot(iterations,
                    neuralNetwork.loss,
                    label='loss value vs iteration')
    plt.pyplot.xlabel('Iterations')
    plt.pyplot.ylabel('loss function')
    plt.pyplot.legend()
    plt.pyplot.show()
# Using the trained neural network to generate predictions on the test set.
# First, reading in the test set images, and doing the same pre-processing as on the training set.
MNIST_test_df = pd.read_csv('MNIST_test.csv')
MNIST_test = MNIST_test_df.values  # no labels in the test set
MNIST_test_inp_set_temp = MNIST_test / 255  # input matrix; X
MNIST_test_inp_set = np.ceil(MNIST_test_inp_set_temp)

# Transposing the input set so X is a collection of column vectors, each column representing an image.
MNIST_test_inp_set_transpose = np.transpose(MNIST_test_inp_set)

# Loop through and feed forward every image to generate the prediction.

# Use the decoding functions you wrote.
num_results = MNIST_test_inp_set.shape[0]
MNIST_results = np.empty(shape=(num_results, 2))

y_hats = MNIST_nn.feedforward(MNIST_test_inp_set_transpose)[3]
y_hats_transpose = np.transpose(y_hats)
y_hat_predictions = one_hot_decode(y_hats_transpose)
MNIST_results[:, 0] = np.arange(start=1, step=1, stop=num_results + 1)
MNIST_results[:, 1] = y_hat_predictions.flatten()

# Writing out results to a file named "MNIST_submission.csv". This is submitted to Kaggle.
MNIST_results_df = pd.DataFrame(data=MNIST_results,
                                columns=['ImageID', 'Label'])
MNIST_results_df.to_csv('MNIST_submission.csv', index=False)

# Printing the execution time
MNIST_end = time.time()
print('\n', 'Execution time (s): ', format(MNIST_end - MNIST_start, '.10f'),
      '\n')
Esempio n. 4
0
EPOCH   = 65
DATA    = 100
TEST    = 500

ds = Dataset()

nn = NeuralNetwork(INPUTS, OUTPUTS, HIDDEN, LAYERS, LR, ACT)

nn.train(ds.train_data, ds.train_labels_arr, EPOCH, DATA)

 # counters
correct = 0
false   = 0
for i in range(TEST):
    output = nn.feedforward(ds.test_data[i], isRound=False, isSoftmax=True)
    output_digit = np.where(output == np.amax(output))
    if output_digit[0][0] == ds.test_labels[i]:
        correct += 1
    else:
        false += 1
# calc accuracy
accuracy = (correct * 100) / (correct + false)
# log accuracy
# set file path
fpath = os.path.join('./results/', 'opt.csv')

with open(fpath, 'a') as fo:
    fo.write('{};{};{};{};{};{};{};{};{};{}\n'.format(
            INPUTS,
            OUTPUTS,