Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tensorflow中将tf.get_变量用作tf.variable的替代变量时出错_Python_Tensorflow_Conv Neural Network_Google Colaboratory - Fatal编程技术网

Python 在Tensorflow中将tf.get_变量用作tf.variable的替代变量时出错

Python 在Tensorflow中将tf.get_变量用作tf.variable的替代变量时出错,python,tensorflow,conv-neural-network,google-colaboratory,Python,Tensorflow,Conv Neural Network,Google Colaboratory,嗨,我是神经网络新手,目前正在研究Tensoflow。 首先,我做了MNIST教程,效果很好。现在我想通过在Google Colab中为Cifar10建立自己的网络来深化整个过程。为此,我编写了以下代码: def conv2d(input, size, inputDim, outputCount): with tf.variable_scope("conv2d"): ## -> This area causes problems <- ## ##########

嗨,我是神经网络新手,目前正在研究Tensoflow。 首先,我做了MNIST教程,效果很好。现在我想通过在Google Colab中为Cifar10建立自己的网络来深化整个过程。为此,我编写了以下代码:

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}))
我今天一直在找这个洞,但我还没有找到解释

现在我希望这里有人能向我解释为什么这是不可能的,或者我可以在哪里找到进一步的信息。这个错误消息对我毫无帮助。我不想要一个解决方案,因为我想并且必须理解这一点,因为我想在CNN领域写我的学士论文

为什么我可以使用tf.variable而不使用tf.get_变量,这应该是相同的

谢谢你的帮助, 向你问好,帕斯卡:)

我发现了我的错误。 我忘记了关键字
初始值设定项

正确的行如下所示:

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