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

RNN 简单预测

源码

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN
import os


input_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-hot

x_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个输入特征,每个时间步输入特征个数为5
	alphabet = 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 os


input_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-hot

x_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个输入特征,每个时间步输入特征个数为5
	alphabet = 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 predict
    use_multiprocessing=use_multiprocessing)
  File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 498, in predict
    workers=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_iteration
    use_multiprocessing=use_multiprocessing)
  File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 646, in _process_inputs
    x, 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_data
    all_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_inputs
    self._set_inputs(cast_inputs)
  File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2647, in _set_inputs
    inputs = 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_wrapper
    result = 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_attrs
    input_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
。。。。。。。。。。。。。。。。。。。。。。。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值