简体   繁体   English

在Tensorflow中将tf.get_variable用作tf.Variable的替代时出错

[英]Error when using tf.get_variable as alternativ for tf.Variable in Tensorflow

Hi I'm new to neural networks and I'm currently working on Tensoflow. 嗨,我是神经网络的新手,我目前正在研究Tensoflow。 First I did the MNIST tutorial which worked quite well. 首先,我做了MNIST教程,效果很好。 Now I wanted to deepen the whole by means of an own network for Cifar10 in Google Colab. 现在,我想通过Google Colab中自己的Cifar10网络来加深整体。 For this purpose I wrote the following code: 为此,我编写了以下代码:

def conv2d(input, size, inputDim, outputCount):
  with tf.variable_scope("conv2d"): 
    ## -> This area causes problems <- ##
    ##########variant1
    weight = tf.Variable(tf.truncated_normal([size, size, inputDim, outputCount], stddev=0.1),name="weight")
    bias = tf.Variable( tf.constant(0.1, shape=[outputCount]),name="bias")
    ##########variant2
    weight = tf.get_variable("weight", tf.truncated_normal([size, size, inputDim, outputCount], stddev=0.1))
    bias = tf.get_variable("bias", tf.constant(0.1, shape=[outputCount]))
    ##################
    conv = tf.nn.relu(tf.nn.conv2d(input, weight, strides=[1, 1, 1, 1], padding='SAME') + bias)    
  return conv

def maxPool(conv2d):....

def fullyConnect(input, inputSize, outputCount, relu):
  with tf.variable_scope("fullyConnect"):
    ## -> This area causes problems <- ##
    ##########variant1
    weight = tf.Variable( tf.truncated_normal([inputSize, outputCount], stddev=0.1),name="weight")
    bias = tf.Variable( tf.constant(0.1, shape=[outputCount]),name="bias")
    ##########variant2
    weight = tf.get_variable("weight", tf.truncated_normal([inputSize, outputCount], stddev=0.1))
    bias = tf.get_variable("bias", tf.constant(0.1, shape=[outputCount]))
    ##################            
    fullyIn = tf.reshape(input, [-1, inputSize])        
    fullyCon = fullyIn
    if relu:
      fullyCon = tf.nn.relu(tf.matmul(fullyIn, weight) + bias)
  return fullyCon

#Model Def.
def getVGG16A(grafic,width,height,dim):
  with tf.name_scope("VGG16A"):    
    img = tf.reshape(grafic, [-1,width,height,dim])
    with tf.name_scope("Layer1"): 
      with tf.variable_scope("Layer1"):   
        with tf.variable_scope("conv1"):
          l1_c = conv2d(img,3, dim, 64)        
        with tf.variable_scope("mp1"):
          l1_mp = maxPool(l1_c) #32 > 16

    with tf.name_scope("Layer2"):
      with tf.variable_scope("Layer2"):  
        with tf.variable_scope("conv1"):
          l2_c = conv2d(l1_mp,3, 64, 128)
        with tf.variable_scope("mp1"):
          l2_mp = maxPool(l2_c) #16 > 8

    with tf.name_scope("Layer6"):
      with tf.variable_scope("Layer6"):  
        with tf.variable_scope("fully1"):
          L6_fc1 = fullyConnect(l2_mp, 8*8*128 , 1024, True)
        with tf.variable_scope("fully2"):
          L6_fc2 = fullyConnect(L6_fc1, 1024, 1024, True)         

        keep_prob = tf.placeholder(tf.float32)
        drop = tf.nn.dropout(L6_fc2, keep_prob)

        with tf.variable_scope("fully3"):
          L6_fc3 = fullyConnect(drop,1024, 3, False)
  return L6_fc3, keep_prob

x = tf.placeholder(tf.float32, [None, 3072]) #input
y_ = tf.placeholder(tf.float32, [None, 3])   #output

# Build the graph for the deep net
y_conv, keep_prob = getVGG16A(x,32,32,3) #create Model

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())   

  for batch in getBatchData(prep_filter_dataBatch1,2): #a self-written method for custom batch return

    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.8})

  print('test accuracy %g' % accuracy.eval(feed_dict={
      x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

For the definition of the tensorflow variables I first used variant1 (tf.variable). 为了定义张量流变量,我首先使用了variant1(tf.variable)。 This caused an overflow of the graphics memory after repeated execution. 重复执行后,这会导致图形内存溢出。 Then I used variant2 (tf.get_variable). 然后,我使用了variant2(tf.get_variable)。 If I have understood the documentation correctly, this should use already existing variables if they exist. 如果我正确理解了文档,则应该使用已经存在的变量(如果存在)。

But as soon as I do this I get the following error message: 但是,一旦我这样做,我会收到以下错误消息:

TypeError: Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.

I've been looking the hole day, but I haven't found an explanation for this. 我一直在寻找黑洞日,但没有找到任何解释。

Now I hope that there is someone here who can explain to me why this is not possible, or where I can find further information. 现在,我希望这里有人可以向我解释为什么这是不可能的,或者在哪里可以找到更多信息。 The error message is getting me nowhere. 错误消息使我无处可去。 I don't want a solution because I want to and have to understand this, because I want to write my bachelor thesis in the field of CNN. 我不需要解决方案,因为我想要并且必须了解这一点,因为我想写CNN领域的学士论文。

Why can I use tf.variable but not tf.get_variable which should do the same? 为什么我可以使用tf.variable但不能使用tf.get_variable呢?

Thanks for the help, best regards, Pascal :) 感谢您的帮助,最好的问候,帕斯卡尔:)

I found my mistake. 我发现了我的错误。 I forgot the keyword initializer . 我忘记了关键字initializer

the correct line looks like this: 正确的行如下所示:

weight = tf.get_variable("weight",initializer=tf.truncated_normal([size, size, inputDim, outputCount], stddev=anpassung))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM