Home > Software design >  How to correctly arrange data for keras.model.fit()
How to correctly arrange data for keras.model.fit()

Time:12-31

I'm working in a neural network using keras (from tensorflow) for a college project. I'm pretty new to the library, so I don't really know how should I feed the data into the model in order for the training to work. I've been searching the internet for hours and I can't find a proper tutorial / documentation on how to do it.

Here's the model I'm using, one of the simplest possible ones:

model = keras.Sequential([
    keras.layers.Dense(20, input_dim=1,activation = activations.relu),
    keras.layers.Dense(10, activation=  activations.relu),
    keras.layers.Dense(8, activation= activations.sigmoid)
])

model.compile(optimizer = "adam", loss = "sparse_categorical_crossentropy",
              metrics = ["accuracy"])

The input to the network is a list of 20 floats, and the output a list of 8 floats ranged from 0 to 1 (confidence level), so I think this model is OK, please let me know if I'm wrong.

Here's a diagram of the model i'm trying to build and train: network

Let's say I have:

  • 10 input examples (10 lists of 20 floats) for the expected output [1,0,0,0,0,0,0,0]
  • 10 input examples for the expect output [0,1,0,0,0,0,0,0,0,0]
  • ...
  • 10 input examples for the expected output [0,0,0,0,0,0,0,1]

How should I prepare this data in order to use it with

model.fit(training_inputs,expected_outputs,epochs = NUM_EPOCHS) ?

What should training_inputs exactly be? and expected_outputs?

Any help will be appreciated. Thank you for your time!

CodePudding user response:

First of all, you have two issues in your model. According to your description, your input data is 20-dimensional, so in the first layer you should have input_dim=20. Then, you have a cross-entropy loss, so I'm assuming that you are training a 8-class classifier. If that's the case, then instead of keras.layers.Dense(8, activation= activations.sigmoid) you should use

keras.layers.Dense(8, activation=None),
keras.layers.Softmax()

as that ensures that you get a distribution over classes for each input data point.

Now regarding your input data question, training_inputs should a tensor (or numpy array, which will be readily converted) with shape (n_points, 20) in your case. Accordingly, expected_outputs should have shape (n_points, 8). So, just concatenate/stack your input data along the first dimension (axis=0), such that each row corresponds to your 20-dimensional data points. You do the same for expected_outputs, maybe something like,

expected_outputs = np.r_[
    np.tile([[1,0,0,0,0,0,0,0]], (10, 1)),
    np.tile([[0,1,0,0,0,0,0,0]], (10, 1)),
    ...
    np.tile([[0,0,0,0,0,0,0,1]], (10, 1)),
]

Remember to set batch_size and shuffle!

  • Related