paddle线性回归
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)