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)]
编程是一种快乐,享受代码带给我的乐趣!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2015-01-08 jsp-------------之分页技术(一)
2014-01-08 HDUOJ --2523
2014-01-08 HDUOJ---1195Open the Lock