Keras:在一个简单的网络中“操作没有梯度”

时间:2018-10-07 18:25:32

标签: python tensorflow keras

我开始使用Keras,并试图实现一个简单的网络以在CIFAR10(32x32图像,10类)上对其进行测试,但是该模型未能fit数据集。

型号代码:

import keras, keras.layers as L

input_shape = [32, 32, 3]
n_inputs = 32 * 32 * 3

model = keras.models.Sequential()
model.add(L.InputLayer(input_shape))
model.add(L.Flatten())
model.add(L.Dense(n_inputs, activation='relu'))
model.add(L.Dense(10, activation='softmax'))

model.compile(optimizer='adam', 
              loss=keras.metrics.sparse_categorical_accuracy,
              metrics=['accuracy'])
model.fit(x=X_train, y=y_train,
          epochs=10)

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-16-be14b685c358> in <module>
      4 model.fit(x=X_train, y=y_train,
      5 #           validation_data=[X_val, y_val],
----> 6           epochs=10)

...\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, **kwargs)
-> 1010         self._make_train_function()

...\keras\engine\training.py in _make_train_function(self)
--> 509                         loss=self.total_loss)

...\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
---> 91             return func(*args, **kwargs)

...\keras\optimizers.py in get_updates(self, loss, params)
    473     @interfaces.legacy_get_updates_support
    474     def get_updates(self, loss, params):
--> 475         grads = self.get_gradients(loss, params)
    476         self.updates = [K.update_add(self.iterations, 1)]
    477 

...\keras\optimizers.py in get_gradients(self, loss, params)
---> 91             raise ValueError('An operation has `None` for gradient. '

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

我在教程中看到类似的片段可以正常工作,并且找不到错误。此模型有什么问题?

0 个答案:

没有答案