Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: <lambda>() missing 1 required positional argument: 'w'

Code is here

return self.activator(reduce(lambda a, b: a+b, map(lambda x, w: x*w, zip(input_vec, self.weights)), 0.0) + self.bias)

The python2.7-version code is like lambda (x, w)

But now the Tuple parameter unpacking was removed so I dont know how to figure it :(

like image 347
yizhuo liu Avatar asked Apr 03 '18 09:04

yizhuo liu


2 Answers

It is a good thing to make a small running example which shows the problem. In your case, that is not the fact since we are missing some variables. Like i said in the other comment, your list you map over is made of tuples. Like you already know, you cannot unpack the tuples anymore, but you can use indices like you would use on an array. A simple working example:

val = reduce(lambda a, b: a+b, map(lambda t: t[0]*t[1], zip([10, 20, 30], [0.1, 0.3, 0.7])), 0.0)
print(val)

Like you see, the lambda function passed to the map function just has one parameter now. I called it t to make clear that this is a tuple, you can call it x if you want. In the function-body i use indices to get the first and second element of the tuple. If you run this code, you see that it works. So next step is to adept that to your code:

return self.activator(reduce(lambda a, b: a+b, map(lambda t: t[0]*t[1], zip(input_vec, self.weights)), 0.0) + self.bias)

And that should do the trick.

like image 153
Schorsch Avatar answered Sep 28 '22 11:09

Schorsch


you can't unpack anymore. But, you can just take tuple as it is, and use indexing in the formula:

map(lambda t: t[0]*t[1], zip(input_vec, self.weights))

using map with lambda has little interest, generator comprehension equivalent:

(x*w for x,w in zip(input_vec, self.weights))    

(and unpacking works)

The same construct needs to be repeated with the outer lambda and even reduce, which can be completely eliminated with sum which performs sums of the elements:

return self.activator(sum(x*w for x,w in zip(input_vec, self.weights)) + self.bias)

no more lambdas, shorter and clearer

like image 27
Jean-François Fabre Avatar answered Sep 28 '22 09:09

Jean-François Fabre