Tensorflow范围问题

时间:2018-06-19 22:25:46

标签: python python-3.x tensorflow

为什么这段代码不起作用并引发错误:

ValueError: Variable model/conv1/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?  

这是从https://www.tensorflow.org/programmers_guide/variables复制的

通常,每次我尝试使用范围运行代码时,总是会向我抛出此错误,并且我不了解发生了什么。我通读了示波器教程,但老实说,这确实让我感到困惑。如果有人可以阐明正在发生的事情,那就太好了。

import tensorflow as tf

def conv_relu(input, kernel_shape, bias_shape):
    # Create variable named "weights".
    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')
    return tf.nn.relu(conv + biases)

input1 = tf.random_normal([1,10,10,32])
input2 = tf.random_normal([1,20,20,32])

def my_image_filter(input_images):
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights", "conv1/biases".
        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights", "conv2/biases".
        return conv_relu(relu1, [5, 5, 32, 32], [32])

with tf.variable_scope("model"):
  output1 = my_image_filter(input1)
with tf.variable_scope("model", reuse=True):
  output2 = my_image_filter(input2)

0 个答案:

没有答案