1

I have the following Python code line:

x = layers.Dense(64, activation="relu")(x)

What does the (x) mean?

6
  • 5
    layers.Dense(64, activation="relu") returns something callable, which is then called with a single argument x. Commented Sep 21, 2020 at 13:04
  • 2
    Looks as if layers.Dense(64, activation="relu") returns a function that is then called with (x). Commented Sep 21, 2020 at 13:04
  • 1
    keras.io/api/layers/core_layers/dense Commented Sep 21, 2020 at 13:09
  • Thank you very much! If anybody wants some rep points, please write an answer. Commented Sep 21, 2020 at 13:09
  • VTC because there isn't a clear question here. Specifically understanding the effect of the code would depend on context. If it's generally about the syntax, then the question doesn't make sense to ask: putting (foo) after something treats the "something" as a callable, and calls it. In the example code, the (x) has the same relationship to layers.Dense(64, activation="relu"), that (64, activation="relu") does to layers.Dense. It's like asking what the [z] in x[y][z] means. Commented Jan 25, 2023 at 5:21

1 Answer 1

2

It looks like you're specifically reading code written using the Keras library's Functional API (as opposed to the Sequential API). What this means is each neural network layer upon creation returns a function that must be called. To create a simple feed-forward neural network in this manner (with no skip-connections),

a. Create a layer that takes in your input. This yields a function that takes your input.
b. Create another layer and pass the previous layer in as input.
...
n. Create an output layer that takes the second-to-last layer as input.

Or

x_in = layers.Input(...)
x_1 = layers.Dense(...)(x_in)
x_2 = layers.Dense(...)(x_1)
x_out = layers.Dense(...)(x_2)

You don't need to assign each layer its own variable name, though, so the previous example could be rewritten as (and is commonly in tutorials as such):

x = layers.Input(...)
x = layers.Dense(...)(x)
x = layers.Dense(...)(x)
x = layers.Dense(...)(x)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.