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
(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?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