Problem with this code

Problem with this code
0

#1

Hello!!

I’m following the video Theory of Neural Networks - Deep Learning Without Frameworks and I’m trying to complete the second example and I write the code as follows:

import numpy as np
weights = np.array([0.7, 0.2, -0.5])
alpha = 0.1

streetlights = ([[0, 0, 1],
                [0, 1, 1],
                [0, 0, 1],
                [1, 1, 1],
                [0, 1, 1],
                [1, 0, 1]])
                
walk_vs_stop = np.array([0, 1, 0, 1, 1, 0])

for iteration in range(40):
    error_for_all_lights = 0
    for row_index in range (len(walk_vs_stop)):
        input = streetlights[row_index]
        goal_prediction = walk_vs_stop[row_index]
        
        prediction = input.dot(weights)
        error = (prediction - goal_prediction) ** 2
        error_for_all_lights += error
        
        delta = prediction - goal_prediction
        weights = weights - (alpha *(input * delta))
        
print ("Prediction:" + str(prediction))
print ("Weights:" + str(weights))
print ("Error:" + str(error))

I received the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-afbdd6a5aaed> in <module>()
     19         goal_prediction = walk_vs_stop[row_index]
     20 
---> 21         prediction = input.dot(weights)
     22         error = (prediction - goal_prediction) ** 2
     23         error_for_all_lights += error

AttributeError: 'list' object has no attribute 'dot'

I’m trying to solve it by my own but I’m not able to see where the problem is. Can you tell me, please, where is the mistake?

Thank you so much!!!


#2

Hey there,

I’ve been lost in JavaScript land for a while, so forgive me if I’m mistaken in these Numpy matters.

I believe the problem is that input is a Python List Object, which does not inherently have a function dot(). When you see the dot product function being called this way the preceding item needs to be a a Numpy Array object. The full function structure takes the form:

  • numpy.dot(arg1, arg2, optional_output_arg)

You could try:

...
prediction = np.dot(input, weights)
...

np.dot documentation

FYI:
When trying this myself I ran into a TypeError on the last line of the for loop. The code is attempting to multiply a sequence input by Numpy’s version of a floating point number. I might be totally wrong about this, but I think you might want this instead?

...
for row_index in range (len(walk_vs_stop)):
    input = np.array(streetlights[row_index])
    ...
    ...
    weights = weights - (alpha * (input * delta))
...

If you change the input object from a native Python List to a Numpy array prior to this the raw multiplication will play out because Numpy is smart like that and will multiply the value across the array instead of duplicating the list x (delta) number of times (which is what it would do if delta was an integer instead of a floating point)…assuming that’s what you want.

Example:

example_list = [1, 2, 3]
example_array = np.array([1, 2, 3])
delta = 2

print(example_list * delta)  # Prints: [1, 2, 3, 1, 2, 3]
print(example_array * delta)  # Prints: [2, 4, 6]

#3

Thank you so much Kylec!! Now it works.

Have a great day!

Juanma