求助Variable layer1-conv1/weight already exists, disallowed
tensorflow吧
全部回复
仅看楼主
level 2
神气呐你 楼主
2017年08月07日 09点08分 1
level 2
神气呐你 楼主
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 7 11:06:23 2017
@author: wht
"""
import tensorflow as tf
import numpy as np
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
BATCH_SIZE = 100
TRAIN_STEP = 100000
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500
IMAGE_SIZE = 28
NUM_CHANNELS = 1
NUM_LABELS = 10
# 第一层卷积层的尺寸和深度
CONV1_DEEP = 32
CONV1_SIZE = 5
# 第二层卷积层的尺寸和深度
CONV2_DEEP = 64
CONV2_SIZE = 5
# 全连接层的节点个数
FC_SIZE = 512
x = tf.placeholder(tf.float32,shape=[BATCH_SIZE,28,28,1])
y_ = tf.placeholder(tf.float32,shape=[None,10])
regularizer = tf.contrib.layers.l2_regularizer(0.001)
def reshaped_x(xs):
return np.reshape(xs,(BATCH_SIZE,28,28,1))
def createNet(input_tensor,regularizer):
with tf.variable_scope('layer1-conv1'):
# 这里使用tf.get_variable或tf.Variable没有本质区别,因为在训练或是测试中没有在同一个程序中多次调用这个函数。
# 如果在同一个程序中多次调用,在第一次调用之后需要将reuse参数置为True。
conv1_weights = tf.get_variable(
"weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP],
initializer = tf.truncated_normal_initializer(stddev=0.1)
)
conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0))
# 使用边长为5,深度为32的过滤器,过滤器移动的步长为1,且使用全0填充
conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
# 实现第二层池化层的前向传播过程。
# 这里选用最大池化层,池化层过滤器的边长为2,使用全0填充且移动的步长为2。
# 这一层的输入是上一层的输出,也就是28*28*32的矩阵。输出为14*14*32的矩阵。
with tf.name_scope('layer2-pool'):
pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 声明第三层卷积层的变量并实现前向传播过程。
# 这一层的输入为14*14*32的矩阵,输出为14*14*64的矩阵。
with tf.variable_scope('layer3-conv2'):
conv2_weights = tf.get_variable(
"weight", [CONV2_SIZE, CONV2_SIZE, CONV1_DEEP, CONV2_DEEP],
initializer = tf.truncated_normal_initializer(stddev=0.1)
)
conv2_biases = tf.get_variable("bias", [CONV2_DEEP], initializer=tf.constant_initializer(0.0))
# 使用边长为5,深度为64的过滤器,过滤器移动的步长为1,且使用全0填充
conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
# 实现第四层池化层的前向传播过程。
# 这一层和第二层的结构是一样的。这一层的输入为14*14*64的矩阵,输出为7*7*64的矩阵。
with tf.name_scope('layer4-poo2'):
pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
pool_shape = pool2.get_shape().as_list()
nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
reshaped = tf.reshape(pool2,[pool_shape[0],nodes])
with tf.variable_scope('layer5_fc1'):
fc1_weights = tf.get_variable('weight',[nodes,512],initializer=tf.truncated_normal_initializer(stddev=0.1))
if regularizer != None:
tf.add_to_collection('losses',regularizer(fc1_weights))
fc1_biases = tf.get_variable('bias',[512],initializer=tf.constant_initializer(0.1))
fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_weights) + fc1_biases )
#if train:
# fc1 = tf.nn.dropout(0.5)
with tf.variable_scope('layer6_fc2'):
fc2_weights = tf.get_variable('weigt',[512,10],initializer=tf.truncated_normal_initializer(stddev =0.1))
if regularizer != None:
tf.add_to_collection('losses',regularizer(fc2_weights))
fc2_biases = tf.get_variable('bias',[10],initializer=tf.constant_initializer(0.1))
#fc2 = tf.nn.softmax(tf.matmul(fc1,fc2_weights) + fc2_biases )
fc2 = tf.matmul(fc1,fc2_weights) + fc2_biases
return fc2
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=createNet(x,regularizer),labels=tf.argmax(y_,1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
tf.add_to_collection('losses',cross_entropy_mean)
loss = tf.add_n(tf.get_collection('losses'))
global_step = tf.Variable(0)
learning_rate = tf.train.exponential_decay(0.1,global_step,mnist.train.num_examples/100,0.96,staircase = True)
y = createNet(x,regularizer)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)
correct_pre = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accurcy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(TRAIN_STEP):
batch_x,batch_y = mnist.train.next_batch(BATCH_SIZE)
sess.run(accurcy,feed_dict={x:reshaped_x(batch_x),y_:batch_y})
if i%1000 == 0:
validat_acc = sess.run(accurcy,feed_dict={x:mnist.validation.images,y_:mnist.validation.labels})
print validat_acc
#print sess.run(accurcy,feed_dict={x:mnist.test.images,y_:mnist.test.labels})
2017年08月07日 09点08分 2
level 2
神气呐你 楼主
应该是我在同一个程序中多次调用了 createNet函数,才会导致变量重复使用了?可我的程序没多次调用啊有大神帮我一下吗
2017年08月07日 09点08分 3
求问楼主解决了吗?遇到相同的问题
2018年05月29日 09点05分
楼主解决了吗?
2018年06月27日 09点06分
level 8
应该是我在同一个程序中多次调用了 createNet函数,才会导致变量重复使用了?
2018年05月30日 10点05分 4
1