PaddlePaddle在训练用的特征和电影特征之间的分数是出现张量类型错误

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/PaddlePaddle/article/details/87967471
  • 关键字:张量数据类型

  • 问题描述:在训练用的特征和电影特征之间的分数,定义的fluid.layers.data的数量类型为int64,最后在训练的是就出现张量类型错误。

  • 报错信息:

<ipython-input-8-71a7f986f7ba> in train(use_cuda, train_program, params_dirname)
     39         event_handler=event_handler,
     40         reader=train_reader,
---> 41         feed_order=feed_order)

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/contrib/trainer.py in train(self, num_epochs, event_handler, reader, feed_order)
    403         else:
    404             self._train_by_executor(num_epochs, event_handler, reader,
--> 405                                     feed_order)
    406 
    407     def test(self, reader, feed_order):

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/contrib/trainer.py in _train_by_executor(self, num_epochs, event_handler, reader, feed_order)
    481             exe = executor.Executor(self.place)
    482             reader = feeder.decorate_reader(reader, multi_devices=False)
--> 483             self._train_by_any_executor(event_handler, exe, num_epochs, reader)
    484 
    485     def _train_by_any_executor(self, event_handler, exe, num_epochs, reader):

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/contrib/trainer.py in _train_by_any_executor(self, event_handler, exe, num_epochs, reader)
    510                                       fetch_list=[
    511                                           var.name
--> 512                                           for var in self.train_func_outputs
    513                                       ])
    514                 else:

/opt/conda/envs/py35-paddle1.0.0/lib/python3.5/site-packages/paddle/fluid/executor.py in run(self, program, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache)
    468 
    469         self._feed_data(program, feed, feed_var_name, scope)
--> 470         self.executor.run(program.desc, scope, 0, True, True)
    471         outs = self._fetch_data(fetch_list, fetch_var_name, scope)
    472         if return_numpy:

EnforceNotMet: Tensor holds the wrong type, it holds l at [/paddle/paddle/fluid/framework/tensor_impl.h:29]
PaddlePaddle Call Stacks: 
  • 问题复现:获取到预测程序之后,再通过fluid.layers.data接口定义一个label输入,dtype参数的值设置为int64,作为用户与电影之间的得分,然后使用这个label和预测程序创建一个损失和函数,在最后的训练时出现以上的错误。错误代码如下:
def train_program():
    scale_infer = inference_program()
    label = layers.data(name='score', shape=[1], dtype='int64')
    square_cost = layers.square_error_cost(input=scale_infer, label=label)
    avg_cost = layers.mean(square_cost)
    return [avg_cost, scale_infer]
  • 解决问题:在数据集中,用户与电影之间的分数是整数,但是使用的是平方误差损失函数,所以输出的结果应该是浮点类型的。在定义label的时候,fluid.layers.data设置的类型应该是float32。正确代码如下:
def train_program():
    scale_infer = inference_program()
    label = layers.data(name='score', shape=[1], dtype='float32')
    square_cost = layers.square_error_cost(input=scale_infer, label=label)
    avg_cost = layers.mean(square_cost)
    return [avg_cost, scale_infer]
展开阅读全文

没有更多推荐了,返回首页