应用密集层后的尺寸误差

时间:2017-11-22 09:00:03

标签: python tensorflow deep-learning conv-neural-network

我想在将dropout应用到最大池化卷积层输出后添加一个密集层。

我有以下用Python编写的TensorFlow代码。过滤器数量为128,len(filter_sizes)为3

pooled_outputs = []
    for i, filter_size in enumerate(filter_sizes):
        with tf.name_scope("conv-maxpool-%s" % filter_size):

            # Convolution Layer
            filter_shape = [filter_size, embedding_size, 1, num_filters]
            W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
            b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
            conv = tf.nn.conv2d(
                self.embedded_chars_expanded,
                W,
                strides=[1, 1, 1, 1],
                padding="VALID",
                name="conv")

            # Applying batch normalization
            # h = tf.contrib.layers.batch_norm(conv, center=True, scale=True, is_training=True)

            # Apply nonlinearity
            h1 = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")

            # Maxpooling over the outputs
            pooled = tf.nn.max_pool(
                h1,
                ksize=[1, sequence_length - filter_size + 1, 1, 1],
                strides=[1, 1, 1, 1],
                padding='VALID',
                name="pool")
            pooled_outputs.append(pooled)

    # Combine all the pooled features
    num_filters_total = num_filters * len(filter_sizes)
    self.h_pool = tf.concat(pooled_outputs, 3)
    self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total])

    # Add dropout
    with tf.name_scope("dropout"):
        #self.h_drop = tf.nn.dropout(dense, self.dropout_keep_prob)
        self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)

        # Adding dense layer
    dense = tf.layers.dense(self.h_drop, units=num_classes, activation=tf.nn.relu)

应用密集层后面临问题。

以下是错误:

Dimensions must be equal, but are 11 and 384 for 'output/scores/MatMul' (op: 'MatMul') with input shapes: [?,11], [384,11]

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

错误与矩阵的索引有关。我正在使用tensorflow提供的xw_plus_b函数,并使用矩阵的维数进行乘法错误。