tensorflow sequential model outputting nan

Viewed 17

Why is my code outputting nan? I'm using a sequential model with a 30x1 input vector and a single value output. I'm using tensorflow and python. This is one of my firs

While True:

 # Define a simple sequential model
 def create_model():
   model = tf.keras.Sequential([
     keras.layers.Dense(30, activation='relu',input_shape=(30,)),
     keras.layers.Dense(12, activation='relu'),
     keras.layers.Dropout(0.2),
     keras.layers.Dense(7, activation='relu'),
     keras.layers.Dense(1, activation = 'sigmoid')
   ])

   model.compile(optimizer='adam',
                 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                 metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

   return model

 # Create a basic model instance
 model = create_model()

 # Display the model's architecture
 model.summary()

 train_labels=[1]
 test_labels=[1]

 train_images= [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]]
 test_images=[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]]

 model.fit(train_images, 
           train_labels,  
           epochs=10,
           validation_data=(test_images, test_labels),
           verbose=1)  
            
 print('predicted:',model.predict(train_images))
1 Answers

You are using SparseCategoricalCrossentropy. It expects labels to be integers starting from 0. So, you have only one label 1, but it means you have at least two categories - 0 and 1. So you need at least two neurons in the last layer:

keras.layers.Dense(2, activation = 'sigmoid')

( If your goal is classification, you should maybe consider to use softmax instead of sigmoid, without from_logits=True )

Related