首页 > 其他 > 详细

[tf] tensorflow中dropout小坑记录

时间:2017-11-28 21:37:59      阅读:437      评论:0      收藏:0      [点我收藏+]

tensorflow中dropout小坑记录

几天看别人写的代码,有几行总觉得没什么用,自己写了小程序测试了下,果然。
虽然平时这么写的人不多,但是还是记录下吧。
对tensorflow使用时要转变下思维,和平时写的C++不太一样,只是建立了一个静态图。

  1. 在list中进行for循环,内部操作是局部变量操作,与原list无关。
  2. tf.nn.dropout操作,在随机舍掉部分节点的同时为了保证输出值的平稳会将保留下的节点数据除以keep_prob进行扩大。
  3. 赋值操作即使赋值给原数据,也是两个op节点,空间变量名是不相同的。

code

import tensorflow as tf
a1 = tf.get_variable(name=‘a1‘, shape=[2,3],
        initializer=tf.random_normal_initializer(mean=0, stddev=1,seed = 1))  
a3 = tf.get_variable(name=‘a3‘, shape=[2,3], initializer=tf.ones_initializer())  
d = [a1,a3]
for i in d:
    i = tf.nn.dropout(i,keep_prob=0.4)
super_d = tf.concat(d,1)
super_d2 = super_d
print("haha1")
print(super_d)
super_d = tf.nn.dropout(super_d,keep_prob = 0.5)
print("haha2")
print(super_d)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(‘*******d*********‘)
    print(sess.run(d))
    print(‘*******super_d*********‘)
    print(sess.run(super_d))
    print(‘*******super d2*********‘)
    print(sess.run(super_d2))

output

haha1
Tensor("concat:0", shape=(2, 6), dtype=float32)
haha2
Tensor("dropout_2/mul:0", shape=(2, 6), dtype=float32)
*******d*********
[array([[-0.81131822,  1.48459876,  0.06532937],
       [-2.4427042 ,  0.0992484 ,  0.59122431]], dtype=float32), array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)]
*******super_d*********
[[-1.62263644  2.96919751  0.13065875  0.          0.          0.        ]
 [-0.          0.          1.18244863  0.          2.          2.        ]]
*******super d2*********
[[-0.81131822  1.48459876  0.06532937  1.          1.          1.        ]
 [-2.4427042   0.0992484   0.59122431  1.          1.          1.        ]]

[tf] tensorflow中dropout小坑记录

原文:http://www.cnblogs.com/zhanxiage1994/p/7912063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!