Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
CreatureOX opened this issue Oct 30, 2018 · 6 comments
Closed
Labels
question Further information is requested

Comments

@CreatureOX
Copy link

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_test_hat, _ = 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
Copy link
Owner

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
Copy link
Author

Here is my repository NumpyNN

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

@SkalskiP SkalskiP added the question Further information is requested label Oct 31, 2018
@SkalskiP
Copy link
Owner

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

@SkalskiP
Copy link
Owner

@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
Copy link
Author

thx for your detailed reply, It works!

@SkalskiP
Copy link
Owner

Great! It's very nice to hear that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants