本文介绍了Tensorflow——keras model.save()引发NotImplementedError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))

model.compile(optimizer ='adam',
            loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])
model.fit(x_train, y_train, epochs=3)

当我尝试保存模型时

model.save('epic_num_reader.model')

我收到NotImplementedError:

I get a NotImplementedError:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-4-99efa4bdc06e> in <module>()
      1
----> 2 model.save('epic_num_reader.model')

NotImplementedError: Currently `save` requires model to be a graph network. Consider using `save_weights`, in order to save the weights of the model.

那么如何保存代码中定义的模型?

So how can I save the model defined in the code?

推荐答案

您忘记了第一层定义中的input_shape参数,这使模型变得不确定,并且尚未实现保存未定义模型,这将触发错误.

You forgot the input_shape argument in the definition of the first layer, which makes the model undefined, and saving undefined models has not been implemented yet, which triggers the error.

model.add(tf.keras.layers.Flatten(input_shape = (my, input, shape)))

只需将input_shape添加到第一层,它就可以正常工作.

Just add the input_shape to the first layer and it should work fine.

这篇关于Tensorflow——keras model.save()引发NotImplementedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:20