Tensorflow中问题总结

1)ValueError: Variable bar/v does not exist, or was not created with tf.get_variab
le(). Did you mean to set reuse=None in VarScope?

import tensorflow as tf


with tf.variable_scope("foo"):
    v=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))

with tf.variable_scope("bar", reuse=True):
    v1=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))

注意第二个变量在创建的时候,由于tf.variable_scope设置的为reuse=True,即指定为共享变量,所以会找以前创建的这个变量,然而该变量并没有存在,因此报错!!!
注意reuse=True的变量作用域或者变量,必须在之前已经存在过!!!

PS:
tf.get_variable()和tf.Variable()的区别:
tf.Variable()每次都在创建新的变量,所有的reuse=True与它并没有什么关系;
tf.get_variable()则

2)ValueError: Variable bar/v already exists, disallowed. Did you mean to set reuse
=True in VarScope? Originally defined at:

import tensorflow as tf

with tf.variable_scope("foo"):
    v=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))

with tf.variable_scope("bar"):
    v1=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))
    v2=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))

这是因为tf.get_variable()并不会处理命名冲突,

完整的代码实现:

import tensorflow as tf
def conv_relu(input, kernel_shape, bias_shape):
    # Create variable named "weights".
    with tf.variable_scope("h1") as scope:
        weights = tf.get_variable("weights", kernel_shape,
            initializer=tf.random_normal_initializer())
        # Create variable named "biases".
        biases = tf.get_variable("biases", bias_shape,
            initializer=tf.constant_initializer(0.0))
        conv = tf.nn.conv2d(input, weights,
            strides=[1, 1, 1, 1], padding='SAME')
        print(weights.name)
        return tf.nn.relu(conv + biases)

input1 = tf.random_normal([1,10,10,32])
input2 = tf.random_normal([1,20,20,32])
with tf.variable_scope("hello") as scope:
    x = conv_relu(input1, kernel_shape=[5, 5, 32, 32], bias_shape=[32])
    #scope.reuse_variables()
    tf.get_variable_scope().reuse_variables()
    x = conv_relu(x, kernel_shape=[5, 5, 32, 32], bias_shape = [32])

PS:
tf.get_variable的策略如下:当变量作用域没有指定reuse时候,尝试创建变量,然鹅并不会处理命名冲突;当指定reuse=True的时候,尝试重用变量(不会再创建对应的变量),如果在reuse=True的作用域内,该变量还没有被创建则会报变量不存在的问题!

Tensorflow的主要讲解:
1)Graph管理Tensor,Tensor是在Graph中保存的,不用显式创建;Variable需要显式创建,由Session进行管理,sess.run(tf.global_variables_initializer())时创建成功,sess.close()时释放;
2)Graph是Tensor和Operation的集合,所以Import_graph_def()中没有任何变量相关的信息;
MetaGraph中包括CollectionDef,SaverDef和GraphDef以及MetaInfoDef,其中GraphDef上面结果,SaverDef用于保存图的节点信息,CollectionDef中存放了key:value的dict对,而key中有tf.GraphKeys.Trainable_VARIABLES等等,方便import_meta_graph后取回。所以这样我们就可以获取到Tensor,Operation和Variable等信息,但是注意对应的权值并没有恢复,是随机的;
saver.save则同时保留了图的信息和权值,Index,ckpt,meta等等,但是saver.restore的时候只restore了权值信息,不会restore对应的图的相关信息!
3)Graph或者Operation或者Variable等都会转变为protocol buffer组成的nodeDef或者GraphDef(多个NodeDef组成)!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值