Ethan C.
Ethan C.

Reputation: 249

Understanding the difference between the arguments units and input_dim for Dense layer

Assuming I have a dataset of size (1000, 64) where 64 is the number of columns (i.e., features). In Keras, suppose I want to build a NN model as the following architecture:

  1. Input layer: Obviously should be one layer having a number of neurons equal to the number of features in my dataset. Hence, the number of neurons here = 64.
  2. Hidden Layer: I want to this hidden layer to have 100 neurons.
  3. Output Layer: This is one neuron because I am working with a binary classification problem.

Now, to build the aformentioned architecture in Keras, I usually do the following:

model = Sequential()
model.add(Dense(units=64, input_dim=64, activation='tanh'))  # Input layer
model.add(Dense(units=100, activation='tanh'))  # Hidden layer
model.add(Dense(units=1, activation='sigmoid')) # Output layer

The problem is that I cannot distinguish between the arguments units and input_dim for the Dense() layer (For the role as the first layer (Input Layer)). I do understand that input_dim is meant to specify the number of features in your dataset (which is in my case = 64), but I don't understand the role of units here for the input layer. Should units be 64? In another word, is the argument units supposed to be the same input_dim for the input layer?

Thank you very much.

Upvotes: 4

Views: 1817

Answers (1)

mujjiga
mujjiga

Reputation: 16906

Let looks at the pictorial representation first

enter image description here

In terms of keras sequential model notations, you will need two Dense layers (marked as dotted boxes in the figure)

  1. Dense Layer 1: It should have 100 neurons and each neuron takes as input 64 features so the definition for this dense layer will be Dense(units=100, input_dim=64)
  2. Dense Layer 2: You want one neuron here and it takes the output of the previous dense layer as the input. input_dim is not required for the dense layer because it is connected to the previous dense layer in the sequential model and whatever the previous dense layer outputs, it takes in as input (for each neuron) Dense(units=1)

So the model will be :

model = Sequential()
model.add(Dense(units=100, input_dim=64, activation='tanh'))  # First Hidden layer
model.add(Dense(units=1, activation='sigmoid')) # Output layer

You can think of Dense(units=m, input_dim=n) as a layer having m neurons, with each neuron having n inputs. This layer will output m outputs (one from each neuron).

Upvotes: 7

Related Questions