我正在尝试编译Keras模型以训练和测试数据集。但是在编译过程中,将显示以下错误消息。谁能帮我解决这个问题?我一直在检查其他页面,并遵循了他们的建议,但是它们都没有真正帮助我解决该问题。
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"), # Rectified Linear Unit.
tf.keras.layers.Dense(10, activation="softmax")
model.compile(optimizer="adam", loss="sparse_categorial_crossentropy", metrics=["accuracy"])
当我尝试编译和运行时,出现以下几行。
Traceback (most recent call last):
File "/home/eaindra/PycharmProjects/NeuralNetwork/Tensorflow1.py", line 42, in <module>
model.compile(optimizer="adam", loss="sparse_categorial_crossentropy", metrics=["accuracy"])
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 336, in compile
self.loss, self.output_names)
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1351, in prepare_loss_functions
loss_functions = [get_loss_function(loss) for _ in output_names]
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1351, in <listcomp>
loss_functions = [get_loss_function(loss) for _ in output_names]
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1087, in get_loss_function
loss_fn = losses.get(loss)
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/losses.py", line 1183, in get
return deserialize(identifier)
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/losses.py", line 1174, in deserialize
printable_module_name='loss function')
File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 210, in deserialize_keras_object
raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
ValueError: Unknown loss function:sparse_categorial_crossentropy
答案 0 :(得分:1)
loss function
上似乎有错字。您写了categorial
而不是categorical
,并且错过了模型定义中的方括号。
固定的代码段附在下面;
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"), # Rectified Linear Unit.
tf.keras.layers.Dense(10, activation="softmax")])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])