zoukankan      html  css  js  c++  java
  • 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)]
  • 相关阅读:
    线性表3 数据结构和算法08
    线性表3 数据结构和算法08
    线性表的链式存储结构
    OD使用教程9 调试篇09|解密系列
    线性表
    线性表
    线性表
    OD使用教程9 调试篇09|解密系列
    验证中英文数字和下划线中划线
    formEl.submit is not a function的原因
  • 原文地址:https://www.cnblogs.com/gongxijun/p/12166668.html
Copyright © 2011-2022 走看看