提交 785f9d44 编写于 作者: G gongweibao

remove w,b input params in train.py and remove def main in README.md

上级 413ba962
...@@ -114,9 +114,8 @@ fit_a_line下trainer.py演示了训练的整体过程 ...@@ -114,9 +114,8 @@ fit_a_line下trainer.py演示了训练的整体过程
### 首先初始化paddle ### 首先初始化paddle
```python ```python
def main(): # init
# init paddle.init(use_gpu=False, trainer_count=1)
paddle.init(use_gpu=False, trainer_count=1)
``` ```
### 然后进行模型配置 ### 然后进行模型配置
...@@ -124,32 +123,30 @@ def main(): ...@@ -124,32 +123,30 @@ def main():
使用`fc_layer``LinearActivation`来表示线性回归的模型本身。 使用`fc_layer``LinearActivation`来表示线性回归的模型本身。
```python ```python
#输入数据,13维的房屋信息 #输入数据,13维的房屋信息
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
y_predict = paddle.layer.fc(input=x, y_predict = paddle.layer.fc(input=x,
param_attr=paddle.attr.Param(name='w'),
size=1, size=1,
act=paddle.activation.Linear(), act=paddle.activation.Linear())
bias_attr=paddle.attr.Param(name='b')) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.regression_cost(input=y_predict, label=y)
cost = paddle.layer.regression_cost(input=y_predict, label=y)
``` ```
### 接着创建参数和优化器 ### 接着创建参数和优化器
```python ```python
# create parameters # create parameters
parameters = paddle.parameters.create(cost) parameters = paddle.parameters.create(cost)
# create optimizer # create optimizer
optimizer = paddle.optimizer.Momentum(momentum=0) optimizer = paddle.optimizer.Momentum(momentum=0)
``` ```
### 创建trainer ### 创建trainer
```python ```python
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters, parameters=parameters,
update_equation=optimizer) update_equation=optimizer)
``` ```
### 读取数据且打印训练的中间信息 ### 读取数据且打印训练的中间信息
...@@ -157,38 +154,38 @@ def main(): ...@@ -157,38 +154,38 @@ def main():
reader_dict中设置了训练数据和测试数据的下标,reader通过下标区分训练和测试数据。 reader_dict中设置了训练数据和测试数据的下标,reader通过下标区分训练和测试数据。
```python ```python
reader_dict={'x': 0, reader_dict={'x': 0,
'y': 1} 'y': 1}
# event_handler to print training and testing info # event_handler to print training and testing info
def event_handler(event): def event_handler(event):
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0: if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % ( print "Pass %d, Batch %d, Cost %f" % (
event.pass_id, event.batch_id, event.cost) event.pass_id, event.batch_id, event.cost)
if isinstance(event, paddle.event.EndPass): if isinstance(event, paddle.event.EndPass):
result = trainer.test( result = trainer.test(
reader=paddle.reader.batched( reader=paddle.reader.batched(
uci_housing.test(), batch_size=2), uci_housing.test(), batch_size=2),
reader_dict=reader_dict) reader_dict=reader_dict)
print "Test %d, Cost %f" % (event.pass_id, result.cost) print "Test %d, Cost %f" % (event.pass_id, result.cost)
``` ```
### 开始训练 ### 开始训练
```python ```python
# training # training
trainer.train( trainer.train(
reader=paddle.reader.batched( reader=paddle.reader.batched(
paddle.reader.shuffle( paddle.reader.shuffle(
uci_housing.train(), buf_size=500), uci_housing.train(), buf_size=500),
batch_size=2), batch_size=2),
reader_dict=reader_dict, reader_dict=reader_dict,
event_handler=event_handler, event_handler=event_handler,
num_passes=30) num_passes=30)
``` ```
## 执行训练程序 ## bash中执行训练程序
**注意设置好paddle的安装包路径** **注意设置好paddle的安装包路径**
```bash ```bash
......
...@@ -9,10 +9,8 @@ def main(): ...@@ -9,10 +9,8 @@ def main():
# network config # network config
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13)) x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
y_predict = paddle.layer.fc(input=x, y_predict = paddle.layer.fc(input=x,
param_attr=paddle.attr.Param(name='w'),
size=1, size=1,
act=paddle.activation.Linear(), act=paddle.activation.Linear())
bias_attr=paddle.attr.Param(name='b'))
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.regression_cost(input=y_predict, label=y) cost = paddle.layer.regression_cost(input=y_predict, label=y)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册