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

yizhuo liu picture yizhuo liu · Apr 3, 2018 · Viewed 21.2k times · Source

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 :(

Answer

Schorsch picture Schorsch · Apr 3, 2018

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.