windows使用样例保存model后,加载模型,预测出错。
Created by: skriser
在使用官方提供的demo直接训练预测没有问题。当把训练和预测分开执行,在加载模型预测时报错。
这是代码---
"""
usage: https://www.paddlepaddle.org.cn/documentation/docs/zh/beginners_guide/quick_start_cn.html
线性回归模型:借此属性paddle使用
"""
import paddle.fluid as fluid
import numpy as np
# 生成数据
np.random.seed(0)
outputs = np.random.randint(5, size=(10, 4))
res = []
# for i in range(10):
# # 假设方程式为 y=4a+6b+7c+2d
# y = 4 * outputs[i][0] + 6 * outputs[i][1] + 7 * outputs[i][2] + 2 * outputs[i][3]
# res.append([y])
# # 定义数据
# train_data = np.array(outputs).astype('float32')
# y_true = np.array(res).astype('float32')
#
# # 定义网络
# x = fluid.layers.data(name="x", shape=[4], dtype='float32')
# y = fluid.layers.data(name="y", shape=[1], dtype='float32')
# y_predict = fluid.layers.fc(input=x, size=1, act=None)
# # 定义损失函数
# cost = fluid.layers.square_error_cost(input=y_predict, label=y)
# avg_cost = fluid.layers.mean(cost)
# # 定义优化方法
# sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05)
# sgd_optimizer.minimize(avg_cost)
# # 参数初始化
# cpu = fluid.CPUPlace()
# exe = fluid.Executor(cpu)
# exe.run(fluid.default_startup_program())
# # #开始训练,迭代500次
# for i in range(500):
# outs = exe.run(
# feed={'x': train_data, 'y': y_true},
# fetch_list=[y_predict.name, avg_cost.name])
# if i % 50 == 0:
# print('iter={:.0f},cost={}'.format(i, outs[1][0]))
# # 存储训练结果
# params_dirname = "result"
# fluid.io.save_inference_model(params_dirname, ['x'], [y_predict], exe)
# 存储模型后,注释训练过程,使用加载模型方式执行报错
# 开始预测
params_dirname = "result"
cpu = fluid.CPUPlace()
infer_exe = fluid.Executor(cpu)
inference_scope = fluid.Scope()
# 加载训练好的模型
with fluid.scope_guard(inference_scope):
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(params_dirname, infer_exe)
# 生成测试数据
test = np.array([[[9], [5], [2], [10]]]).astype('float32')
# 进行预测
results = infer_exe.run(inference_program,
feed={"x": test},
fetch_list=fetch_targets)
# 给出题目为 【9,5,2,10】 输出y=4*9+6*5+7*2+10*2的值
print("9a+5b+2c+10d={}".format(results[0][0]))
=报错信息==========================================
C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py:774: UserWarning: The following exception is not an EOF exception.
"The following exception is not an EOF exception.")
Traceback (most recent call last):
File "C:/xgnews/text-to-text/paddle_learn/lineregression.py", line 63, in <module>
fetch_list=fetch_targets)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 775, in run
six.reraise(*sys.exc_info())
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 770, in run
use_program_cache=use_program_cache)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 817, in _run_impl
use_program_cache=use_program_cache)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 894, in _run_program
fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet:
--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
Windows not support stack backtrace yet.
------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\framework.py", line 2459, in append_op
attrs=kwargs.get("attrs", None))
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args, **kwargs)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\layers\nn.py", line 384, in fc
"y_num_col_dims": 1})
File "C:/xgnews/text-to-text/paddle_learn/lineregression.py", line 27, in <module>
y_predict = fluid.layers.fc(input=x, size=1, act=None)
----------------------
Error Message Summary:
----------------------
PaddleCheckError: Expected y_dims.size() > y_num_col_dims, but received y_dims.size():1 <= y_num_col_dims:1.
ShapeError: The input tensor Y's dimensions of MulOp should be larger than y_num_col_dims. But received Y's dimensions = 1, Y's shape = [0], y_num_col_dims = 1. at [D:\1.6.1\paddle\paddle\fluid\operators\mul_op.cc:59]
[operator < mul > error]