paddlepaddle关于使用dropout小案例

 1 import numpy as np
 2 import paddle.fluid as fluid
 3 import paddle.fluid.layers as layers
 4 
 5 debug = True
 6 bs = 1
 7 c = 1
 8 h, w = 3, 1
 9 
10 main_program = fluid.Program()
11 startup_program = fluid.Program()
12 with fluid.program_guard(main_program, startup_program):
13     x = fluid.layers.data(name='x', shape=[-1, c, w, h], dtype='float32')
14     # hidden = fluid.layers.fc(input=x, size=32, act='relu')
15     hidden = fluid.layers.dropout(x, dropout_prob=0.5)
16     # loss = fluid.layers.cross_entropy(
17     #     input=fluid.layers.fc(hidden, size=10, act='softmax'),
18     #     label=fluid.layers.data(name='label', shape=[1], dtype='int64'))
19 
20 if debug:
21     test_program = main_program.clone(for_test=True)
22 else:
23     test_program = main_program.clone()
24 
25 place = fluid.CPUPlace()
26 exe = fluid.Executor(place)
27 exe.run(startup_program)
28 x_data = np.random.rand(bs, c, w, h).astype(np.float32)
29 print(exe.run(main_program, fetch_list=[x,hidden], feed={'x': x_data}))
30 
31 print(x_data)
32 print(exe.run(test_program, fetch_list=[x,hidden], feed={'x': x_data}))

结果:

1 [array([[[[0.5775851 , 0.78441525, 0.4060972 ]]]], dtype=float32), array([[[[0.        , 0.78441525, 0.        ]]]], dtype=float32)]
2 [[[[0.5775851  0.78441525 0.4060972 ]]]]
3 [array([[[[0.5775851 , 0.78441525, 0.4060972 ]]]], dtype=float32), array([[[[0.28879255, 0.39220762, 0.2030486 ]]]], dtype=float32)]

 

上一篇:numpy学习(一)


下一篇:numpy笔记