当前位置: 代码迷 >> 综合 >> TensorFlow2 中加载模型后若predict问题 WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.stat
  详细解决方案

TensorFlow2 中加载模型后若predict问题 WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.stat

热度:26   发布时间:2024-02-04 18:36:25.0

RNN 简单预测

源码

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN
import osinput_word = "abcde"w_to_id = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}  # 单词映射到数值id的词典
id_to_onehot = {0: [1., 0., 0., 0., 0.], 1: [0., 1., 0., 0., 0.], 2: [0., 0., 1., 0., 0.], 3: [0., 0., 0., 1., 0.],
4: [0., 0., 0., 0., 1.]}  # id编码为one-hotx_train = [id_to_onehot[w_to_id['a']], id_to_onehot[w_to_id['b']], id_to_onehot[w_to_id['c']],id_to_onehot[w_to_id['d']], id_to_onehot[w_to_id['e']]]
y_train = [w_to_id['b'], w_to_id['c'], w_to_id['d'], w_to_id['e'], w_to_id['a']]x_train = np.reshape(x_train, (len(x_train), 1, 5))
y_train = np.array(y_train)def evaluate(target_model):_, acc = target_model.evaluate(x_train, y_train)print("Restore model, accuracy: {:5.2f}%".format(100*acc))checkpoint_save_path = "./checkpoint/rnn_onehot_1pre1.ckpt"latest = tf.train.latest_checkpoint('./checkpoint')model = tf.keras.Sequential([SimpleRNN(3),Dense(5, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])if os.path.exists(checkpoint_save_path + '.index'):print('-------------load the model-----------------')model.load_weights(checkpoint_save_path)
else:print('no model , exit!\n')# evaluate(model)
# model.summary()preNum = int(input("input the number of test alphabet:"))
for i in range(preNum):alphabet1 = input("input test alphabet:")alphabet = [id_to_onehot[w_to_id[alphabet1]]]# 使alphabet符合SimpleRNN输入要求:[送入样本数, 循环核时间展开步数, 每个时间步输入特征个数]。# 此处验证效果送入了1个样本,送入样本数为1;输入1个字母出结果,所以循环核时间展开步数为1; 表示为独热码有5个输入特征,每个时间步输入特征个数为5alphabet = np.reshape(alphabet, (1, 1, 5))result = model.predict([alphabet])pred = tf.argmax(result, axis=1)pred = int(pred)tf.print(alphabet1 + '->' + input_word[pred])

保存ckpt文件,然后进行预测,没有问题。

单独写一个加载模型的代码

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN
import osinput_word = "abcde"w_to_id = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}  # 单词映射到数值id的词典
id_to_onehot = {0: [1., 0., 0., 0., 0.], 1: [0., 1., 0., 0., 0.], 2: [0., 0., 1., 0., 0.], 3: [0., 0., 0., 1., 0.],
4: [0., 0., 0., 0., 1.]}  # id编码为one-hotx_train = [id_to_onehot[w_to_id['a']], id_to_onehot[w_to_id['b']], id_to_onehot[w_to_id['c']],id_to_onehot[w_to_id['d']], id_to_onehot[w_to_id['e']]]
y_train = [w_to_id['b'], w_to_id['c'], w_to_id['d'], w_to_id['e'], w_to_id['a']]x_train = np.reshape(x_train, (len(x_train), 1, 5))
y_train = np.array(y_train)def evaluate(target_model):_, acc = target_model.evaluate(x_train, y_train)print("Restore model, accuracy: {:5.2f}%".format(100*acc))checkpoint_save_path = "./checkpoint/rnn_onehot_1pre1.ckpt"latest = tf.train.latest_checkpoint('./checkpoint')model = tf.keras.Sequential([SimpleRNN(3),Dense(5, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])if os.path.exists(checkpoint_save_path + '.index'):print('-------------load the model-----------------')model.load_weights(checkpoint_save_path)
else:print('no model , exit!\n')# evaluate(model)
# model.summary()preNum = int(input("input the number of test alphabet:"))
for i in range(preNum):alphabet1 = input("input test alphabet:")alphabet = [id_to_onehot[w_to_id[alphabet1]]]# 使alphabet符合SimpleRNN输入要求:[送入样本数, 循环核时间展开步数, 每个时间步输入特征个数]。# 此处验证效果送入了1个样本,送入样本数为1;输入1个字母出结果,所以循环核时间展开步数为1; 表示为独热码有5个输入特征,每个时间步输入特征个数为5alphabet = np.reshape(alphabet, (1, 1, 5))result = model.predict([alphabet])pred = tf.argmax(result, axis=1)pred = int(pred)tf.print(alphabet1 + '->' + input_word[pred])

在这个代码里不执行evaluate(model)函数,python报错

(tf2) F:\MyFile\ProgramCode\Python\TensorFlow2\RNN_recognize>python model_load_test.py
-------------load the model-----------------
2020-07-31 19:15:09.804273: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
input the number of test alphabet:1
input test alphabet:a
Traceback (most recent call last):File "model_load_test.py", line 39, in <module>result = model.predict([alphabet])File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1013, in predictuse_multiprocessing=use_multiprocessing)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 498, in predictworkers=workers, use_multiprocessing=use_multiprocessing, **kwargs)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 426, in _model_iterationuse_multiprocessing=use_multiprocessing)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 646, in _process_inputsx, y, sample_weight=sample_weights)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2346, in _standardize_user_dataall_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2572, in _build_model_with_inputsself._set_inputs(cast_inputs)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2647, in _set_inputsinputs = self._set_input_attrs(inputs)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapperresult = method(self, *args, **kwargs)File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2686, in _set_input_attrsinput_shape = (None,) + tuple(inputs.shape[1:])
AttributeError: 'list' object has no attribute 'shape'
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.state_spec
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-1.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-1.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.cell.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.cell.recurrent_kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).layer-0.cell.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer-1.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer-1.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer-0.cell.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer-0.cell.recurrent_kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer-0.cell.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer-1.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer-1.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer-0.cell.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer-0.cell.recurrent_kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer-0.cell.bias
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.

运行evaluate(model) ,即进行一次model.evaluate 预测后,代码可以正常运行model.predict

(tf2) F:\MyFile\ProgramCode\Python\TensorFlow2\RNN_recognize>python model_load_test.py
-------------load the model-----------------
2020-07-31 19:19:32.563967: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
5/5 [==============================] - 0s 28ms/sample - loss: 0.4219 - sparse_categorical_accuracy: 1.0000
Restore model, accuracy: 100.00%
input the number of test alphabet:1
input test alphabet:b
b->c

不知道造成这个问题原因是啥…
但可以用model.evaluate执行后再使用predict
。。。。。。。。。。。。。。。。。。。。。。。

  相关解决方案