0

I study tensorflow and below error occurs.

keras version is 2.2.4-tf, Python is 3.7.4

And OS is window 10.

I made tensorflow model and error occur when model is learning.

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import datasets
(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()

inputs = layers.Input((28, 28, 1))
net = layers.Conv2D(32, (3, 3), padding='SAME')(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(10)(net)  # num_classes
net = layers.Activation('softmax')(net)

model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')


model.compile(optimizer=tf.keras.optimizers.Adam(), 
              loss='sparse_categorical_crossentropy', 
              metrics=[tf.keras.metrics.Accuracy()])

train_x = train_x[..., tf.newaxis]
test_x = test_x[..., tf.newaxis]

num_epochs = 1
batch_size = 32

model.fit(train_x, train_y, 
          batch_size=batch_size, 
          shuffle=True, 
          epochs=num_epochs) 

below is error when model.fit is run.

It seems that learning can't be done completely.

What is wrong with above code?

Train on 60000 samples
   32/60000 [..............................] - ETA: 11s
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-fea17f92bc8b> in <module>
      2           batch_size=batch_size,
      3           shuffle=True,
----> 4           epochs=1) 

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py in 
fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, 
class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, 
max_queue_size, workers, use_multiprocessing, **kwargs)
    817         max_queue_size=max_queue_size,
    818         workers=workers,
--> 819         use_multiprocessing=use_multiprocessing)
    820 
    821   def evaluate(self,

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in 
fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, 
shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, 
validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    340                 mode=ModeKeys.TRAIN,
    341                 training_context=training_context,
--> 342                 total_epochs=epochs)
    343             cbks.make_logs(model, epoch_logs, training_result, ModeKeys.TRAIN)
    344


TypeError: 'NoneType' object is not callable
6
  • "What is wrong with above code?" Nothing. The problem is somewhere above, where you define your model and/or the training data. Show us that code. Commented Mar 2, 2020 at 13:05
  • sorry this is my first stack overflow question. So, I made mistake. I edited my question. Thank you . Commented Mar 2, 2020 at 13:15
  • Can you check that (train_x, train_y), (test_x, test_y) actually contain data after the line (train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()? And also, what are their shapes? Commented Mar 2, 2020 at 13:28
  • Right, those actually contain data and train_x.shape is (60000, 28, 28, 1), train_y.shape is (60000,), test_x.shape is (10000, 28, 28, 1), test_y.shape is (10000,) Commented Mar 2, 2020 at 13:39
  • Ok! Could you then detail why the train_x = train_x[..., tf.newaxis]? The image data seems to have already the right shape (although for the labels, I think you need to one-hot encode them), I see no need to append an extra dimension at the end of the images Commented Mar 2, 2020 at 13:42

1 Answer 1

1

I believe that you are messing up the reshaping your Input example points.

Try to do something like in the below code:

Your model:

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import datasets
(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()

inputs = layers.Input((28, 28, 1))
net = layers.Conv2D(32, (3, 3), padding='SAME')(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(10)(net)  # num_classes
net = layers.Activation('softmax')(net)

model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')


model.compile(optimizer=tf.keras.optimizers.Adam(), 
              loss='sparse_categorical_crossentropy', 
              metrics=[tf.keras.metrics.Accuracy()])

Reshaping your input:

X = train_x.reshape([-1,28,28,1])#reshaping as per your model input dimensions

Also one hot encoding the output (if not done):

Y= tf.keras.utils.to_categorical(train_y, 10)

Training your model:

num_epochs = 1
batch_size = 32

model.fit(X, Y, 
          batch_size=batch_size, 
          shuffle=True, 
          epochs=num_epochs) 

I believe this will work.

Sign up to request clarification or add additional context in comments.

7 Comments

First, thank you for your answer. And I'm so sorry but similar error occurs which says " ValueError: Shapes (32, 10) and (32, 1) are incompatible " Is there a possibility that environment setting is wrong?? Um...
@박남선 can you please share the complete stack trace, and also do let me know that if the dataset that you are trying on is mnist and you are not using some other one whith different dimensions of input
@Tayyab OP is not one-hot encoding the labels (which should be done). That's the source of the second error
@GPhilo I have also added the part to one hot encode the labels, thanks for pointing out.
@박남선 as per pointed by GPhillo your other error will also be now solved once you one hot encode the labels
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.