C4W1A2 tip: TensorFlow sometimes uses "double" parentheses

In case anyone else is a TensorFlow learner (perhaps with a Matlab background) like me, here’s a tip on tf usage that was not obvious to me from the documentation. Some tf methods are called like this:
tfl.SomeMethod(methodParameters)(thingYouActuallyInput)
If a method has no parameters, I was (incorrectly) doing this and getting only unhelpful error messages; apparently they don’t think coders will do this but I did:
tfl.SomeMethod(thingYouActuallyInput)
In actuality, what you need to do this is:
tfl.SomeMethod()(thingYouActuallyInput)
I think this wound up mattering on Flatten and ReLU.

The method you’re using actually concatenates two separate steps into one line.

  • The first one creates the layer.
  • The second one passes data to it.

So a more obvious way to write the code would be:
a_layer = SomeTFLayer(…)
then
Y = a_layer(X)

Or you can do that all at once with:
Y = SomeTFLayer(…) (X)

1 Like

It’s definitely the case that the TF documentation and particularly what they tell us in the notebooks here is not as clear and complete as it could be. In addition to Tom’s explanation, here’s a nice thread that gives an overall introduction and explanation of both the Sequential and Functional APIs.

1 Like

TMosh - a big “thank you” for this. Your solution with two lines of code makes much more sense to me, and also aligns well with the documentation I was able to find by Googling. I think this is only the first or second assignment in the Specialization where the provided comments do not encourage us to use a specific number of lines in our code… however the layout given strongly hints that we should use one line.

Do you have access to the course staff? If so, I’d like to strongly encourage them to update the lab text to say something like “you’ll probably want to use two lines of code for this kind of operation and here’s an example of how to do that.” Otherwise, this lab quickly becomes an exercise in guessing tf syntax and Google searching rather than in CNNs.

Thanks again.

Paul, thanks for this! That thread has some quality content which frankly should be included in this lab or in the lectures for the course. Are you in touch with the course staff? Would be great to see that (or similar) included.

You can use as many lines of code as you like, the “one line of code” text are just hints.
The grader only looks at the values your functions returns - it doesn’t care how you write the code inside a function.

Agreed, those are just hints. But in approximately 100% of cases before this exercise, I found those hints to be helpful as they motivated me to write code which was more Pythonic. In this particular case, the format of the comments perhaps inadvertently led me to a solution that was probably less Pythonic. I liked your two lines at a time version better.

Hi Tmosh, I’m now on the C4W2A1 lab “Residual_Networks” which uses the “double” parentheses technique on almost every line of the first code block, seemingly as a way of simplifying the code and avoiding buffer variables. Given this, I now am strongly of the opinion that the course material should cover this technique ahead of both C4W1A2 and C4W2A1. I still like your approach, it’s just not what they seem to want us to use in the next lab.

The course has used this “two-in-one” syntax for a couple of years now, and the staff is unlikely to see any value in changing it now.

It’s a useful syntax, once you’re used to it.

Agreed. I am now seeing how “two in one” is also probably just as Pythonic if not more so, in contexts like this.

Given this, I’d like to request that they show us at least one example of this (and ideally, several) in the lab text, and ideally in the lectures too. I’m guessing it’s easier to update the labs than the lectures. Are you able to contact the course staff?

You keep asking if the mentors are in contact with the course staff. Why?

My goal is that the learnings and clarifications surfaced here be used to improve the course. If mentors can get in touch with course staff to facilitate improvement of lectures, labs and or quizzes, that’s a win. I haven’t had nearly as much engagement from course staff here as from mentors.

It’s clear to me that these courses are as good as they are due to the work of teachers and learners that came before me. So I hope to pay it forward by pointing out when something could be better, and having those changes implemented.