Tensorflow 2D卷积层的尺寸误差

时间:2017-08-21 19:10:23

标签: python-3.x tensorflow deep-learning

我正在尝试使用SVHN数据集来开发深度数字识别应用程序

SVHN Dataset Link

我正在使用.mat文件,在我的代码中,pickle文件包含列车测试和测试集的字典,这些字典是在相应的.mat文件的帮助下创建的。

我正在尝试创建一个CNN模型,该模型具有~3个CNN图层以及最大池图层和具有丢失和衰减学习率的完全连接图层。我遇到了一些矩阵维度错误。

filename='SVHN.pickle'

with open(filename,'rb') as f:
other=pickle.load(f)


train_data=other['train_dataset']

del other


train_dataset=train_data['X'].astype(np.float32)
train_labels=train_data['y']
train_dataset=np.reshape(train_dataset,(32,32,73257,-1))
print(train_dataset.shape)
#print(train_labels)

classes=10

batch_size=32

num_steps = 1500

graph=tf.Graph()

'''Placeholder for the data'''
data_placeholder=tf.placeholder(tf.float32,shape=(None,batch_size,batch_size,3))
label_placeolder=tf.placeholder(tf.float32,shape=(classes))

'''Variable for Weights and Biases'''
#1024 is for the number of nodes in each hidden layer

layer1_weights=tf.Variable(tf.truncated_normal([batch_size,32,32,512]))
layer1_biases=tf.Variable(tf.zeros([512]))
#tf.cast(layer1_weights,tf.float64)
#tf.cast(layer1_biases,tf.float64)

layer2_weights=tf.Variable(tf.truncated_normal([batch_size,512]))
layer2_biases=tf.Variable(tf.zeros([512]))

layer3_weights=tf.Variable(tf.truncated_normal([batch_size,512]))
layer3_biases=tf.Variable(tf.zeros([512]))

layer4_weights=tf.Variable(tf.truncated_normal([batch_size,512]))
layer4_biases=tf.Variable(tf.zeros([512]))

def layer_multiplication(data_input_given):

    #Convolutional Layer 1

    CNN1=tf.nn.relu(tf.nn.conv2d(data_input_given,layer1_weights,strides=[1,1,1,1],padding='SAME')+layer1_biases)

    #Pooling Layer

    Pool1=tf.nn.max_pool(CNN1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

    #second Convolution layer

    CNN2=tf.nn.relu(tf.nn.conv2d(Pool1,layer2_weights,strides=[1,1,1,1],padding='SAME'))+layer2_biases

    #Second Pooling

    Pool2 = tf.nn.max_pool(CNN2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

    #Third Convolutional Layer

    CNN3 = tf.nn.relu(tf.nn.conv2d(Pool2, layer3_weights, strides=[1, 1, 1, 1], padding='SAME')) + layer3_biases

    #Third Pooling Layer

    Pool3 = tf.nn.max_pool(CNN3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

    #Fully Connected Layer

    FullyCon=tf.reshape(Pool3,[-1,32,32,512])

    FullyConProcess=tf.nn.relu(tf.matmul(FullyCon,layer4_weights)+layer4_biases)

    dropout = tf.nn.dropout(FullyConProcess, 0.4)

    return dropout

gloabl_step = tf.Variable(0, trainable=False)

decay_rate=tf.train.exponential_decay(0.1,gloabl_step,4000,0.96,staircase=False,)

train_input=layer_multiplication(train_dataset)

loss=(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label_placeolder,logits=train_input))
                                   + 0.01 * tf.nn.l2_loss(layer1_weights)
                                   + 0.01 * tf.nn.l2_loss(layer2_weights)
                                   + 0.01 * tf.nn.l2_loss(layer3_weights)
                                   + 0.01 * tf.nn.l2_loss(layer4_weights)
      )

optimizer = tf.train.GradientDescentOptimizer(name='Stochastic', learning_rate=decay_rate).minimize(loss,global_step=gloabl_step)

train_prediction = tf.nn.softmax(train_input)

with tf.Session(graph=graph) as session:
    tf.global_variables_initializer().run()
    print('Initialized')

    for step in range(num_steps):
        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        batch_data = train_dataset[offset:(offset + batch_size)]
        batch_labels = train_labels[offset:(offset + batch_size)]

        #print(batch_data)
        #print(batch_labels)

        feed_dict = {data_placeholder:batch_data, label_placeolder:batch_labels}
        _, l, predictions = session.run(
            [optimizer, loss, train_prediction], feed_dict=feed_dict)
        if (step % 500 == 0):
            print('Minibatch loss at step %d: %f' % (step, l))
            #print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
            #print('Validation accuracy: %.1f%%' % accuracy(
                #valid_prediction.eval(), valid_labels))
    #print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

以下是我遇到的错误

  

ValueError:尺寸必须相等,但对于' Conv2D'是1和32。 (op:' Conv2D')输入形状:[32,32,73257,1],[32,32,32,512]。

我试图摆弄尺寸,但无济于事。

如果有人能纠正我的错误并且告诉我如何在将来避免此错误,我将非常感激。

1 个答案:

答案 0 :(得分:1)

您需要正确定义权重形状,更改所有权重形状,如下所示。

# weights shape should be [filter_height, filter_width, in_channels, out_channels]
# suppose filter size (3, 3), in_channel = 3 (RGB image input)
layer1_weights=tf.Variable(tf.truncated_normal([3,3,3,512]))

# here for fully connected weights_shape [in_channels, out_channels]        
# in_channels last dimension of the previous layer's output
layer2_weights=tf.Variable(tf.truncated_normal([512, 512]))

# Also the placeholders
data_placeholder=tf.placeholder(tf.float32,shape=(None,input_height,input_width,3))
label_placeolder=tf.placeholder(tf.float32,shape=(None, classes))