Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

使用paddlepaddle中的疑惑 #366

Closed
wwlaoxi opened this issue Aug 29, 2017 · 4 comments
Closed

使用paddlepaddle中的疑惑 #366

wwlaoxi opened this issue Aug 29, 2017 · 4 comments
Assignees

Comments

@wwlaoxi
Copy link
Collaborator

wwlaoxi commented Aug 29, 2017

我试了 新手入门-基本使用概念-例子,
1、trainer.train(
reader = paddle.batch(train_reader(), batch_size = 1),
feeding = feeding,
event_handler = event_handler,
num_passes = 100)

feeding = feeding, 的作用?
2、训练结束时候cost是
Pass 99, Batch 0, Cost0.021706
Pass 99, Batch 1, Cost0.006622
Pass 99, Batch 2, Cost0.008095
Pass 99, Batch 3, Cost0.000031
我把训练时使用的数据(换了数组名称)又带到这个训练完的模型中预测输出结果:
test_data = [[1, 1], [1, 2], [3, 4],[5, 2]];
probs = paddle.infer(
output_layer=y_predict, parameters=parameters, input=test_data)
for data in probs:
print data
打印出来的结果如下
[-2.18957305]
[-2.18957305]
[-5.97472429]
[-9.7598753]
感觉和上面的cost不一样
3、训练结束后怎么参看parameters的内容?
4、训练时一般都是设置好次数,一直执行完。后续的模型中可以设置cost的目标值吗?既不设置执行次数,只要cost能比我设定的误差值就停止,这样达到我的目的了。

@pkuyym
Copy link

pkuyym commented Aug 30, 2017

  1. feeding提供了数据字段名称到位置的映射,通过reader产生数据时,不包含数据字段名,而在data layer取数据时,需指明字段名称,feeding参数可以把字段名与reader产生数据对应起来,PaddlePaddle具有一定推理功能,但是显式提供feeding参数是比较安全的做法。
  2. 如果训练过程leanring rate不为0或含有Batch Normalization层,那么示例给出的4个Cost值分别对应了4个模型,但是infer时使用的是最后的模型,这可能是差异的原因所在,请确保数据和模型严格对齐。
  3. 查看parameters参数,如果是v2版paddle,需要先解压模型,然后使用od -f -j 16或参考issue #2812查看每个layer的参数值。
  4. 目前PaddlePaddle不直接提供该项功能,不过你可以监控训练日志,然后自动停止模型训练

@pkuyym pkuyym self-assigned this Aug 30, 2017
@wwlaoxi
Copy link
Collaborator Author

wwlaoxi commented Aug 31, 2017

1、feeding = {'x': 0, 'y': 1} , 其中x和y就是数据字段名吗?0和1代表仕么? 以后是不是就这种固定写法即可代表数据对应关系。
2、leanring rate为0且不含有Batch Normalization层,数据和模型是对应的。代码如下(PADDLEPADDLE官网--新手入们--基本使用概念 里的代码):
import paddle.v2 as paddle
import numpy as np

init paddle

paddle.init(use_gpu=False)

network config

x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(2))
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.mse_cost(input=y_predict, label=y)

create parameters

parameters = paddle.parameters.create(cost)

create optimizer

optimizer = paddle.optimizer.Momentum(momentum=0)

create trainer

trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters,
update_equation=optimizer)

event_handler to print training info

def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 1 == 0:
print "Pass %d, Batch %d, Cost %f" % (event.pass_id, event.batch_id,
event.cost)

define training dataset reader

def train_reader():
train_x = np.array([[1, 1], [1, 2], [3, 4], [5, 2]])
train_y = np.array([-2, -3, -7, -7])

def reader():
    for i in xrange(train_y.shape[0]):
        yield train_x[i], train_y[i]

return reader

define feeding map

feeding = {'x': 0, 'y': 1}

training

trainer.train(
reader=paddle.batch(
train_reader(), batch_size=1),
feeding=feeding,
event_handler=event_handler,
num_passes=100)

训练结束时候cost是
Pass 99, Batch 0, Cost0.021706
Pass 99, Batch 1, Cost0.006622
Pass 99, Batch 2, Cost0.008095
Pass 99, Batch 3, Cost0.000031
我把训练时使用的数据(换了数组名称)又带到这个训练完的模型中预测输出结果:
test_data = [[1, 1], [1, 2], [3, 4],[5, 2]];
probs = paddle.infer(
output_layer=y_predict, parameters=parameters, input=test_data)
for data in probs:
print data
打印出来的结果如下
[-2.18957305]
[-2.18957305]
[-5.97472429]
[-9.7598753]
感觉和上面的cost不一样
3、我是在Jupyter Notebook里运行的这个程序,可以参考issue #2812中的方法parameters的内容。
4、你提到的监控训练日志,然后自动停止模型训练的具体怎么操作?有可以参考的例子吗?
@pkuyym

@cdliheng
Copy link

这个例子里面把训练数据的值修改大一点,就会报错,
比如把test_data = [[1, 1], [1, 2], [3, 4],[5, 2]]; 改为
test_data = [[1, 1], [1, 2], [3, 4],[502, 202]];
请问如何处理?

@shanyi15
Copy link
Collaborator

您好,此issue在近一个月内暂无更新,我们将于今天内关闭。若在关闭后您仍需跟进提问,可重新开启此问题,我们将在24小时内回复您。因关闭带来的不便我们深表歉意,请您谅解~感谢您对PaddlePaddle的支持!
Hello, this issue has not been updated in the past month. we will close it today for the sake of other user’s experience. If you still need to follow up on this question after closing, please feel free to reopen it. In that case, we will get back to you within 24 hours. We apologize for the inconvenience caused by the closure and thank you so much for your support of PaddlePaddle Group!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants