5 Replies - 525 Views - Last Post: 11 February 2021 - 08:58 AM Rate Topic: ***** 1 Votes

#1 klg52486   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 117
  • Joined: 01-December 16

Please help me understand this lambda function

Posted 10 February 2021 - 04:29 PM

I'm terrible at python, and none of the examples of lambda that I've found are helping me understand what these lambda functions are supposed to be doing. I'm trying to debug something left over from someone who isn't here anymore.

 shapes.update(input) 


Ok, so shapes just gives us some info about what the model is training on and how many classes there are. It looks like this:
{'images': (64, 1, 28, 28), 'labels': (64, 10)}

 shapes.update(dict(map(lambda x: (x.name, x.dims), inits))) 

This line doesn't appear to do anything. Printing shapes results in the same output:
{'images': (64, 1, 28, 28), 'labels': (64, 10)}

Is there something that could have been updated but wasn't in this case? I really don't understand the format of these lambda functions and what they are doing.

 tensors = set(reduce(lambda a,b: a+b, list(map(lambda x: list(x.input)+list(x.output), nodes)), [])) 

This line just creates an empty set. What is supposed to be happening to a and b? How is something supposed to be passed to a and b to be added together (a+b is what is supposed to happen, right? What is a and b?)

 unknownTensors = list(filter(lambda x: x not in shapes.keys(), tensors)) 

This is also just empty, which makes sense since there is nothing in tensors. shapes.keys() is just 'images' and 'labels'

Thanks again for any help... I'm so lost lol

This post has been edited by klg52486: 10 February 2021 - 04:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Please help me understand this lambda function

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12210
  • View blog
  • Posts: 20,670
  • Joined: 19-March 11

Re: Please help me understand this lambda function

Posted 10 February 2021 - 05:47 PM

I'm not sure I can give you insight into the big picture, but I can tell you what the lambdas are doing:

A lambda expression is simply a way of expressing a function definition inline.

A lambda expression of the form
lambda [var1, var2, var_n] : expr  


evaluates to a function object, which takes n arguments and uses them to evaluate the expression expr.

For example, your first lambda expression

lambda a,b: a+b


evaluates to a function which takes two arguments and returns their sum.

This is exactly equivalent to

def add(a,B)/>:
    return a+b




And in fact, you could add that definition to the code and then use the name add instead of the lambda expression, and it would work exactly the same.

This sum function is being used in a reduce, which you haven't asked about, so I'll assume this makes sense to you: it just means, we're reducing the list by summing its elements.

This one is a little more interesting:

lambda x: list(x.input)+list(x.output)



Here, we see the value of naming things. It's entirely unclear to me what this is actually doing - I can't name the function that this produces, because I can't tell you what it's for. All I know from looking at the code is that it expects its single argument to be the sort of thing that has an input and an output, and that those will be amenable to coercion to a list.


Ooops, gotta run to a meeting, will fill some thoughts on the final lambda later.
Was This Post Helpful? 2
  • +
  • -

#3 klg52486   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 117
  • Joined: 01-December 16

Re: Please help me understand this lambda function

Posted 10 February 2021 - 06:15 PM

It's unclear to me how things are being passed to the lambda function. In the example
	tensors = set(reduce(lambda a,b: a+b, list(map(lambda x: list(x.input)+list(x.output), nodes)), [])) 

Is the lambda function being named tensors to be used at a later date, or is something being computed and returned to the variable tensors?

It's only used once, and that's in the last lambda function
unknownTensors = list(filter(lambda x: x not in shapes.keys(), tensors))


This tells me that it's supposed to be returning something to tensors. So what is being passed to a and b? What is being passed to x? The syntax is so confusing to me. Let me go back to C++! :blink:

This post has been edited by klg52486: 10 February 2021 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12210
  • View blog
  • Posts: 20,670
  • Joined: 19-March 11

Re: Please help me understand this lambda function

Posted 10 February 2021 - 08:10 PM

Ah. tensors is the set created by reducing the list created by mapping the function lambda x: list(x.input)+list(x.output) across the list of nodes.

Taking it inside-out:

lambda x: list(x.input)+list(x.output) is a function. For the moment, let's call it f. What it does is this: it takes an argument x, which has an input and an output, and it gloms them together into one list.

map is a higher-level function that takes as its arguments an arbitrary function m and a sequence S, and returns the new sequence S' by applying m to each element s of the original sequence S. In general, map(m, S) is equivalent to [m(s) for s in S]. So

map(lambda x: list(x.input)+list(x.output), nodes)


is equivalent to
[f(node) for node in nodes]



and we can give it a name. Let's call it mapped_nodes:

mapped_nodes = [f(node) for node in nodes]


Recall that f is the name we're using for that lambda. We can do this explicitly as

f = lambda x: list(x.input)+list(x.output)


or

def f(x):
    return list(x.input) + list(x.output)




Again, these are equivalent statements, in that they each result in a function called f with the same behavior.

Okay, now we come to:
reduce(lambda a,b: a+b, list(map(lambda x: list(x.input)+list(x.output), nodes)), [])




As we saw, this is equivalent to
reduce(lambda a,b: a+b, mapped_nodes), [])



So what is reduce? Reduce is another higher-order function which takes a two-arg function fn, in this case our friend the add function, and a sequence, and an initial value, and it applies the function r to convert the sequence into a single entity.

In general, reduce(fn, seq, initial) is equivalent to something like the following:


def my_reduce(fn, seq, initial):
    current = initial
    for item in seq: 
        current = fn(current, item)
    return current



So in this case we are reducing the list of mapped_nodes by addition - but as you can see from the value for initial we're summing lists, so it's the overloaded + operator for lists that we're interested in. So we're glomming all of those lists into one long list.


And of course finally we're using the set constructor to de-duplicate that long list.

I agree that this syntax is extremely aggravating, and needlessly so. It's a good example of gratuitous brevity. However, based on what I've sketched out above you should be able to translate it into more verbose and readable code.
Was This Post Helpful? 2
  • +
  • -

#5 klg52486   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 117
  • Joined: 01-December 16

Re: Please help me understand this lambda function

Posted 10 February 2021 - 09:05 PM

Thank you so much for the time you put into this! This was so helpful- I wish I could give you more than one upvote lol. "Gratuitous brevity." Yes. It is really nice when code is readable by someone else who didn't write it. (Especially a newer developer who might be taking your place!!)
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12210
  • View blog
  • Posts: 20,670
  • Joined: 19-March 11

Re: Please help me understand this lambda function

Posted 11 February 2021 - 08:58 AM

Glad I could help!

Quote

It is really nice when code is readable by someone else who didn't write it


Exactly. It's not always easy to tell when you've hit the sweet spot on this, but in my experience, minimizing the number of keystrokes required to reproduce the code is almost never the way to get there.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1