码迷,mamicode.com
首页 > 其他好文 > 详细

[tf] tensorflow中dropout小坑记录

时间:2017-11-28 21:55:02      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:log   super   sha   部分   initial   建立   tensor   记录   dev   

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小坑记录

标签:log   super   sha   部分   initial   建立   tensor   记录   dev   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!