2017-08-28 91 views
1

我想实现的EEG信号的1D CNN,和我得到它说1d conv错误 - 输入和过滤器大小需要相同?

ValueError: Dimension 1 in both shapes must be equal, but are 492 and 1 From merging shape 0 with other shapes. for 'MaxPool/input' (op: 'Pack') with > input shapes: [?,492,64], [50,1,64].

错误[?,492,64](BATCHSIZE,in_width,频道)我相信这是输出张量对于第一个Conv1d层

[50,1,64](filter_width,in_channels,out_channels)是第一个Conv1d权重的形状。

为什么492和1必须相等?我不理解这个阻止我找到问题的错误。我的第一周张量流和任何帮助将不胜感激。谢谢。下面导致错误的代码。

# Convolutional Layer 1s 
filter_size_1s = 50 
num_filters_1s = 64 
stride_1s = 6 
# Convolutional Layer 2s , 3s , 4s 
filter_size_s = 8 
num_filters_s = 128 
stride_s = 1 

#weights and biases 
# filter tensor of shape [filter_width, in_channels, out_channels] 
W_1s = tf.Variable(tf.truncated_normal([50, 1, 64], stddev=0.1)) 
B_1s = tf.Variable(tf.constant(0.1, tf.float32, [64])) 
W_2s = tf.Variable(tf.truncated_normal([8, 64, 128], stddev=0.1)) 
B_2s = tf.Variable(tf.constant(0.1, tf.float32, [128])) 
W_3s = tf.Variable(tf.truncated_normal([8, 128, 128], stddev=0.1)) 
B_3s = tf.Variable(tf.constant(0.1, tf.float32, [128])) 
W_4s = tf.Variable(tf.truncated_normal([8, 128, 128], stddev=0.1)) 
B_4s = tf.Variable(tf.constant(0.1, tf.float32, [128])) 


def CNN_small(input, phase_test, iteration): 

    conv1s = new_conv_layer(input, W_1s, B_1s, stride_1s, phase_test, iteration) 
    max_pool1s = tf.nn.max_pool(conv1s, 
          ksize=[1, pool_size_1s, 1, 1], 
          strides=[1, pool_stride_1s, 1, 1], 
          padding='VALID') 

    dropout_s = tf.nn.dropout(max_pool1s, dropout_prob)  
    conv2s = new_conv_layer(dropout_s, W_2s, B_2s, stride_s, phase_test, iteration)  
    conv3s = new_conv_layer(conv2s, W_3s, B_3s, stride_s, phase_test, iteration)  
    conv4s = new_conv_layer(conv3s, W_4s, B_4s, stride_s, phase_test, iteration) 
    max_pool2s = tf.nn.max_pool(conv4s, 
          ksize=[1, pool_size_2s, 1, 1], 
          strides=[1, pool_stride_2s, 1, 1], 
          padding='VALID') 
    return max_pool2s 

def new_conv_layer(input, weights, bias, stride, phase_test, iteration): 

    conv = tf.nn.conv1d(value=input, filters=weights, stride=stride, padding='VALID') + bias 
    #bn = batch_norm(conv, biases, phase_test, iteration) #biases added into batch_norm 
    activation = tf.nn.relu(conv) 
    return activation, weights 

x = tf.placeholder(tf.float32, shape=[None, stage_length], name='x') 
x_stage = tf.reshape(x, [-1, stage_length, num_channels]) #batch, in_width, channels 

#And the line which is giving the error 
#cnn layer 
max_pool2s = CNN_small(x_stage, phase_test, iteration) 

回答

0

new_conv_layer函数返回2张量,和你想使用它,如果它只有一个返回。如果你不层后,使用权,只是改变return声明

return activation 

您也可以找到tf.layers API清洁。

+0

谢谢!这是问题所在。感谢你的帮助。 –

相关问题