ValueError:变量vgg_block1 / conv_1 / Conv / weights已经存在,不允许使用。您是要在VarScope中设置“ reuse = True”还是“ reuse = tf.AUTO_REUSE”?

时间:2019-03-28 18:06:06

标签: tensorflow

def vgg_conv_block(filter_size, inputs):
k_size = 3
with (tf.variable_scope("conv_1")):
    conv_1 = convolution2d(
        inputs=inputs,
        num_outputs=filter_size,
        kernel_size=k_size,
        stride=1,
        padding='SAME',
        rate=1,
        activation_fn=tf.nn.relu,
        weights_initializer=tf.truncated_normal_initializer(dtype=tf.float32, stddev=sigma),
        biases_initializer=tf.zeros_initializer,
    )
with (tf.variable_scope("conv_2")):
    conv_2 = layers.convolution2d(
        inputs=conv_1,
        num_outputs=filter_size,
        kernel_size=k_size,
        stride=1,
        padding='SAME',
        rate=1,
        activation_fn=tf.nn.relu,
        weights_initializer=tf.truncated_normal_initializer(dtype=tf.float32, stddev=sigma),
        biases_initializer=tf.zeros_initializer,
    )
return max_pool2d(conv_2, [2,2], [2,2], 'SAME')


x = tf.placeholder(tf.float32, (None, 32, 32))
x_reshaped = tf.reshape(x, (-1, 32, 32, 1))
y = tf.placeholder(tf.float32, (None, classes))

#convolutions
with(tf.variable_scope("vgg_block1")):
    vgg_block_1 = vgg_conv_block(32, x_reshaped)
with(tf.variable_scope("vgg_block2")):
    conv_output = vgg_conv_block(64, vgg_block_1)

#fully connected
fc0 = layers.flatten(conv_output)
fc1 = fully_connected(
    inputs=fc0,
    num_outputs=hidden,
    weights_initializer=tf.truncated_normal_initializer(dtype=tf.float32, stddev=sigma),
    biases_initializer=tf.zeros_initializer,
    activation_fn=tf.nn.relu
)
keep_prob= tf.placeholder(tf.float32)
fc_dropout = tf.nn.dropout(fc1, keep_prob=keep_prob)
# classifier_head
y_ = fully_connected(
inputs=fc_dropout,
num_outputs=classes,
weights_initializer=tf.truncated_normal_initializer(dtype=tf.float32, stddev=sigma),
biases_initializer=tf.zeros_initializer,
activation_fn=None
)

# loss, optimizer and training
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = lr)
training_operation = optimizer.minimize(loss_operation)

correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()

我正在使用Python 3.6.3运行此笔记本。我正在尝试运行python笔记本。在此之前的错误是我必须更改以下内容的地方:

`cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_,y)` 

cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y)

现在我得到以下错误: 我在下面给出的链接中添加了当前错误的图像。 Current Error

有人可以帮助解决此错误吗?

0 个答案:

没有答案