0
votes

I am using Keras for a project and I don't understand how Keras uses data input, that is to say how Keras reads our input data when creating the first layer.

For example:

  1. model = Sequential()
  2. model.add(Dense(10, activation='sigmoid', input_dim=3,name='layer1'))

In this model, what does it mean to have 10 neurons and an input with 3 dimensions? If the input data has 100 examples (number of lines in the matrix data), how does Keras use them?

Thank you.

2

2 Answers

1
votes

input_dim=3 means that your data have 3 features which will be used to determine final result eg. if you want to determine what animal data refer to you could put width, height and color as data.

100 examples of different animals widths, heights and colors combinations allow neural network to adjust its parameters (learn) what width, height and color refers to what kind of animal. Keras starts with random weights for neurons and goes one by one 100 times using provided samples to adjust network weights. Keras actually uses batches which means 100 samples are divided into smaller groups for better learning rate and general performance (less data to store in memory at once).

10 neurons are 10 'places' where network is able to store multiplications results between neuron weights and input data. You could imagine neuron as a bulb which lights a bit brighter or darker depending whether data shows some useful data feature eg. if animal is above 3 meters tall. Each neuron has its own set of weights which are changed just a bit when network examines next sample from your data. At the end you should have 10 neurons (bulbs) which react in more or less intense way depending on presence of different features in your data eg. if animal is very tall.

The more neurons you have the more possible features you can track eg. if animal is tall, hairy, orange, spotted etc. But the more neurons you have there is also the higher risk that your network will be too exact and will learn features which are unique for your training example (it's called overfitting) but do not help you to recognize animal samples which were not included in your training data (it's called ability to generalize and is most important point of actually training neural network). Selecting number of neurons is then a bit of practical exercise where you search for one which works for your need.

I hope it clarifies your doubts. If you would like to get deeper in this field there are a lot of good resources online explaining neural networks training process in details including features, neurons and learning.

3
votes

input_dim=3 means each of your input data has 3 dimensions. In your case, your input data with 100 examples should have a shape of (100,3). The number of examples doesn't really matter when you compile your model. But you should make sure the dimension of each of your input data matches the input_dim that you set in your first Dense layer.

A short example:

model = Sequential()
model.add(Dense(10, input_dim=3, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adamax')

model.fit(xTrain, yTrain, epochs=50, batch_size=10)