Reputation: 249
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:
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
Reputation: 16906
Let looks at the pictorial representation first
In terms of keras sequential model notations, you will need two Dense layers (marked as dotted boxes in the figure)
Dense(units=100, input_dim=64)
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