前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >paddlepaddle关于使用dropout小案例

paddlepaddle关于使用dropout小案例

作者头像
Gxjun
发布2020-01-15 17:11:38
9790
发布2020-01-15 17:11:38
举报
文章被收录于专栏:mlml
代码语言:javascript
复制
 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}))

结果:

代码语言:javascript
复制
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)]
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档