paddle线性回归

◇◆丶佛笑我妖孽 提交于 2020-03-06 06:13:09

1.构建输入变量和输出变量

x = fluid.data(name='x', shape=[None, 1], dtype='float32')
y = fluid.data(name='y', shape=[None, 1], dtype='float32')

 

2.建立神经网络

y_predict = fluid.layers.fc(input=x, size=1, act=None)

 

3.初始化程序

main_program = fluid.default_main_program()
startup_program = fluid.default_startup_program()

place = fluid.CPUPlace()

exe = fluid.Executor(place)

 

4.构建损失函数和优化器

cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(cost)

sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
sgd_optimizer.minimize(avg_loss)

 

5.启动程序

exe.run(startup_program)
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])

 

6.训练模型

import numpy as np

a = np.random.rand(60)

b = 2*a + 1

c=np.column_stack((a,b))

for i in range(20):
    avg_loss_value, = exe.run(main_program,feed=feeder.feed(c),fetch_list=[avg_loss])
    print(avg_loss_value)
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!