【发布时间】:2018-02-05 15:58:40
【问题描述】:
我正在尝试在 eeg 信号上实现 1d CNN,但我收到一个错误,上面写着
ValueError: 两个形状的维度 1 必须相等,但分别是 492 和 1 将形状 0 与其他形状合并。对于具有 > 输入形状的“MaxPool/input”(操作:“Pack”):[?,492,64], [50,1,64]。
[?, 492, 64] (batchsize, in_width, channels) 我相信这是第一个 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)
【问题讨论】:
标签: python tensorflow