D
D
Dplll2018-02-24 17:08:52
Python
Dplll, 2018-02-24 17:08:52

What is wrong with my python code?

def dot(v,w):
    return sum(v_i*w_i for v_i, w_i in zip(v,w))

def feed_forward(neural_network, input_vector):   
    outputs = []

    for layer in neural_network:

        input_with_bias = input_vector + [1]             # add a bias input
        output = [dot(neuron, input_with_bias) # compute the output
                  for neuron in layer]                                 # for this layer
        outputs.append(output)                           # and remember it

        # the input to the next layer is the output of this one
        input_vector = output

    return outputs
random.seed(0)
num_layer = 2
num_neuron = 3 
count_weight = 3    
input_layer = [[random.random() for _ in range(count_weight)]
             for __ in range(num_neuron)]
output_layer = [random.random() for _ in range(count_weight+1)]
neurolink = [input_layer, output_layer]          

input_vector = [1,0,1]


feed_forward(neurolink, input_vector)

It gives the following error
Traceback (most recent call last):
File "nlone.py", line 37, in
feed_forward(neurolink, input_vector)
File "nlone.py", line 17, in feed_forward
for neuron in layer]
File "nlone. py", line 17, in
for neuron in layer]
File "nlone.py", line 8, in dot
return sum(v_i*w_i for v_i, w_i in zip(v,w))
TypeError: zip argument #1 must support iteration

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander, 2018-02-24
@adelshin23

Function zip()

v = [1, 2]
w = [6, 7]
def dot(v, w):
    return sum(v_i*w_i for v_i, w_i in zip(v, w))

print(dot(v, w)) # 20

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question