SkalskiP / ILearnDeepLearning.py

This repository contains small projects related to Neural Networks and Deep Learning in general. Subjects are closely linekd with articles I publish on Medium. I encourage you both to read as well as to check how the code works in the action.
https://medium.com/@skalskip
MIT License
1.33k stars 466 forks source link

Prediction TypeError in Let’s code a Neural Network in plain NumPy #4

Closed CreatureOX closed 5 years ago

CreatureOX commented 5 years ago

I could use

params_values = train(np.transpose(X_train), np.transpose(y_train.reshape((y_train.shape[0], 1))), NN_ARCHITECTURE, 10000, 0.01)

but there is an error when I implement next step

Y_test_hat, _ = full_forward_propagation(np.transpose(X_test), params_values, NN_ARCHITECTURE)

Here is it

TypeError Traceback (most recent call last) 1 # Prediction ----> 2 Y_testhat, = full_forward_propagation(np.transpose(X_test), params_values, NN_ARCHITECTURE)

in full_forward_propagation(X, params_values, nn_architecture) 8 9 activ_function_curr = layer["activation"] ---> 10 W_curr = params_values["W" + str(layer_idx)] 11 b_curr = params_values["b" + str(layer_idx)] 12

TypeError: tuple indices must be integers or slices, not str

But I think params_values is a dictionary, Why is it? I have copied all the necessary code. btw, I use Juypter 6.7.0 in Anaconda3 & python 3.6.7

SkalskiP commented 5 years ago

Would you have a chance to share your notebook somewhere on GitHub? It's hard for me to debug your problem when I can't see the source code you're using.

CreatureOX commented 5 years ago

Here is my repository NumpyNN

I've tried many times, but the error still occurs.

SkalskiP commented 5 years ago

Super. I will try to find out what may be the problem.

SkalskiP commented 5 years ago

@CreatureOX I think I already know what your problem is. The error occurred because some of the code you used was taken from an article on Medium and some from the repository. More specifically, the train function you are using returns a three-piece tuple.

def train(X, Y, nn_architecture, epochs, learning_rate):
    params_values = init_layers(nn_architecture, 2)
    cost_history = []
    accuracy_history = []

    for i in range(epochs):
        Y_hat, cache = full_forward_propagation(X, params_values, nn_architecture)

        cost = get_cost_value(Y_hat, Y)
        cost_history.append(cost)
        accuracy = get_accuracy_value(Y_hat, Y)
        accuracy_history.append(accuracy)

        grads_values = full_backward_propagation(Y_hat, Y, cache, params_values, nn_architecture)
        params_values = update(params_values, grads_values, nn_architecture, learning_rate)

    return params_values, cost_history, accuracy_history # <---TUPLE

Note that in the code version used in the repository, this function returns only one of these elements.

return params_values

How can you solve this problem? In two ways:

  1. Correct the return of the train function so that it returns only one item as it is in the repository. OR
  2. Correct the code at the point of use of the train function. Unpack the tuple in place, like this:
    params_values, _, _ = train(np.transpose(X_train), np.transpose(y_train.reshape((y_train.shape[0], 1))), NN_ARCHITECTURE, 10000, 0.01)

    I hope that everything is clear. Let us know if any further problems have occurred. Greetings.

CreatureOX commented 5 years ago

thx for your detailed reply, It works!

SkalskiP commented 5 years ago

Great! It's very nice to hear that.