简 介:
关键词
: Paddle,静态,动态
新手入门第四课——PaddlePaddle快速入门.
§01 定义张量
1.1 两种方法对比
下面两种方法都可以定义出相同的张量。
x1 = fluid.layers.fill_constant(shape=[2,2], value=1, dtype='int64')
x2 = fluid.layers.fill_constant(shape=[2,2], value=1, dtype='int64')
y1 = fluid.layers.sum(x=[x1,x2])
print(type(x1), x1, type(y1),y1)
z1 = paddle.ones([2,2], dtype='int64')
z2 = paddle.ones([2,2], dtype='int64')
w = z1+z2
print(type(w), w)
运行结果
<class 'paddle.Tensor'>
Tensor(shape=[2, 2], dtype=int64, place=CPUPlace, stop_gradient=True,
[[1, 1],
[1, 1]])
<class 'paddle.Tensor'>
Tensor(shape=[2, 2], dtype=int64, place=CPUPlace, stop_gradient=True,
[[2, 2],
[2, 2]])
<class 'paddle.Tensor'>
Tensor(shape=[2, 2], dtype=int64, place=CPUPlace, stop_gradient=True,
[[2, 2],
[2, 2]])
可以看到他们的执行结果是相同的。
1.2 为什么这样定义无法允许?
下面的程序是按照Paddle中的示例程序来运行,反向在运行过程中无法执行。
place = fluid.CPUPlace()
exe = fluid.executor.Executor(place)
exe.run(fluid.default_startup_program())
§02 回归网络
2.1 第一种方法:静态图
这是一种定义静态图的方法,定义回归网络。
from headm import * # =
import paddle
import paddle.fluid as fluid
paddle.enable_static()
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
hidden = fluid.layers.fc(input=x, size=100, act='relu')
net = fluid.layers.fc(input=hidden, size=1, act=None)
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
cost = fluid.layers.square_error_cost(input=net, label=y)
avg_cost = fluid.layers.mean(cost)
test_program = fluid.default_main_program().clone(for_test=True)
optimizer = fluid.optimizer.SGDOptimizer(learning_rate=0.01)
opts = optimizer.minimize(avg_cost)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
x_data = array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]).astype('float32')
y_data = array([[3.0], [5.0], [7.0], [9.0], [11.0]]).astype('float32')
test_data = array([[6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]).astype('float32')
for pass_id in range(20):
train_cost = exe.run(program=fluid.default_main_program(),
feed={'x':x_data, 'y':y_data},
fetch_list=[avg_cost])
print("pass:%d, Cost:%0.5f"%(pass_id, train_cost[0]))
result = exe.run(program=test_program,
feed={'x':test_data, 'y':array([[0.0]]).astype('float32')},
fetch_list=[net])
printt(result)
2.2 第二种方法:动态图
动态图的方法进行预测是在 Paddle下的Tensor运算以及简单回归问题 给出了测试方案。
from headm import * # =
import paddle
import paddle.nn.functional as F
from paddle import to_tensor as TT
from paddle.nn.functional import square_error_cost as SQRC
from paddle.nn.functional import softmax as softmax
paddle.disable_static()
net = paddle.nn.Sequential(
paddle.nn.Linear(13,100),
paddle.nn.ReLU(),
paddle.nn.Linear(100,1),
)
optimizer = paddle.optimizer.SGD(learning_rate=0.01, parameters=net.parameters())
x_data = array([[i]+[0]*12 for i in range(1,6)]).astype('float32')
y_data = array([[i*2+1] for i in range(1,6)]).astype('float32')
test_data = array([[6]+[0]*12]).astype('float32')
inputs = paddle.to_tensor(x_data)
labels = paddle.to_tensor(y_data)
for pass_id in range(10):
out = net(inputs)
loss = paddle.mean(SQRC(out,labels))
loss.backward()
optimizer.step()
optimizer.clear_grad()
if pass_id %1==0:
print("Pass:{}, Loss:{}".format(pass_id,loss.numpy()))
dir(train_data)
§03 回归UCI房价
3.1 静态图方法
from headm import * # =
import paddle
import paddle.fluid as fluid
train_data = paddle.dataset.uci_housing.train()
test_data = paddle.dataset.uci_housing.test()
BUF_SIZE = 500
BATCH_SIZE = 20
train_reader = paddle.batch(
paddle.reader.shuffle(train_data, buf_size=BUF_SIZE),
batch_size = BATCH_SIZE,
)
test_reader = paddle.batch(
paddle.reader.shuffle(test_data, buf_size=BUF_SIZE),
batch_size = BATCH_SIZE,
)
paddle.enable_static()
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_cost = fluid.layers.mean(cost)
optimizer = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
opts = optimizer.minimize(avg_cost)
test_program = fluid.default_main_program().clone(for_test=True)
use_cude = False
place = fluid.CUDAPlace(0) if use_cude else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder(place=place, feed_list=[x,y])
iter = 0
iters = []
train_costs = []
def draw_train_progress(iters, train_costs):
title = 'Train Cost'
plt.figure(figsize=(10, 8))
plt.title(title, fontsize=24)
plt.xlabel('Iter', fontsize=14)
plt.ylabel('Cost', fontsize=14)
plt.plot(iters, train_costs, color='red', label='Train Cost')
plt.grid()
plt.show()
EPOCH_NUM = 50
model_save_dir = '/home/aistudio/work/fit_a_line.inference.model'
for pass_id in range(EPOCH_NUM):
train_cost = 0
for batch_id,data in enumerate(train_reader()):
train_cost = exe.run(program=fluid.default_main_program(),
feed=feeder.feed(data),
fetch_list=[avg_cost])
if batch_id%40 ==0:
print("Pass:%d, Cost:%0.5f"%(pass_id, train_cost[0][0]))
iter = iter+BATCH_SIZE
iters.append(iter)
train_costs.append(train_cost[0][0])
test_cost = 0
for batch_id,data in enumerate(test_reader()):
test_cost = exe.run(program=test_program,
feed=feeder.feed(data),
fetch_list=[avg_cost])
print("Test:%d, Cost:%.5f"%(pass_id, test_cost[0][0]))
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
print("Save models to %s"%model_save_dir)
fluid.io.save_inference_model(model_save_dir,
['x'],
[y_predict],
exe)
draw_train_program(iters, train_costs)
▲ 图3.1.1 Train Cost
3.2 动态图方法
▲ 图3.2.1 Train Error
■ 相关文献链接:
● 相关图表链接: