How do I train a neural network with an array of list using keras in python

Issue

I’m trying to train a neural network using tensorflow.keras, but I don’t understand how do I train it with a numpy array of list (in python3).

I have tried to change the input shape of the layers, but I don’t really understand how it’s work.

import tensorflow as tf
from tensorflow import keras
import numpy as np

# Create the array of data

train_data = [[1.0,2.0,3.0],[4.0,5.0,6.0]]
train_data_np = np.asarray(train_data)
train_label = [[1,2,3],[4,5,6]]
train_label_np = np.asarray(train_data)

### Build the model

model = keras.Sequential([
    keras.layers.Dense(3,input_shape =(3,2)),
    keras.layers.Dense(3,activation=tf.nn.sigmoid)
])

model.compile(optimizer='sgd',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

#Train the model

model.fit(train_data_np,train_label_np,epochs=10)

The error is “Error when checking input: expected dense_input to have 3 dimensions, but got array with shape (2, 3)” when model.fit is called.

Solution

While defining a Keras model, you have to provide input shape to the first layer of the model.

For example, if your training data have n rows and m features ie shape : (n, m), You have to set the input_shape of the first Dense layer of the model to (m, ) ie the model should expect m features coming into it.

Now coming to your toy data,

train_data = [[1.0,2.0,3.0],[4.0,5.0,6.0]]
train_data_np = np.asarray(train_data)
train_label = [[1,2,3],[4,5,6]]
train_label_np = np.asarray(train_label)

Here, train_data_np.shape is (2, 3) ie 2 rows and 3 features, then you have to define the model like this,

model = keras.Sequential([
    keras.layers.Dense(3,input_shape =(3, )),
    keras.layers.Dense(3,activation=tf.nn.sigmoid)
])

Now, your labels are [[1,2,3],[4,5,6]]. In the normal 3 class classification task this will be a one-hot vector with 1 and 0s. But let’s leave that aside as this is a toy example to check Keras.

If the target label ie y_train is one-hot then you have to use categorical_crossentropy loss instead of sparse_categorical_crossentropy.

So you can compile and train the model like this

model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])

#Train the model

model.fit(train_data_np,train_label_np,epochs=10)


Epoch 1/10
2/2 [==============================] - 0s 61ms/step - loss: 11.5406 - acc: 0.0000e+00
Epoch 2/10
2/2 [==============================] - 0s 0us/step - loss: 11.4970 - acc: 0.5000
Epoch 3/10
2/2 [==============================] - 0s 0us/step - loss: 11.4664 - acc: 0.5000
Epoch 4/10
2/2 [==============================] - 0s 498us/step - loss: 11.4430 - acc: 0.5000
Epoch 5/10
2/2 [==============================] - 0s 496us/step - loss: 11.4243 - acc: 1.0000
Epoch 6/10
2/2 [==============================] - 0s 483us/step - loss: 11.4087 - acc: 1.0000
Epoch 7/10
2/2 [==============================] - 0s 1ms/step - loss: 11.3954 - acc: 1.0000
Epoch 8/10
2/2 [==============================] - 0s 997us/step - loss: 11.3840 - acc: 1.0000
Epoch 9/10
2/2 [==============================] - 0s 1ms/step - loss: 11.3740 - acc: 1.0000
Epoch 10/10
2/2 [==============================] - 0s 995us/step - loss: 11.3653 - acc: 1.0000

Answered By – Sreeram TP

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published