TensorFlow维度问题:预测与标签张量的形状不相容

时间:2016-07-22 23:08:44

标签: python nlp tensorflow conv-neural-network

我正在尝试为关系提取代码运行CNN(找到here),但我遇到了与logits和标签的张量不兼容的形状问题。对于批量大小= 10,我得到:

InvalidArgumentError: Incompatible shapes: [10,2] vs. [270,2]

模型初始化如下所示:

def weight_variable(shape, name):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, name=name)

@staticmethod
def bias_variable(shape, name):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial, name=name)

def max_pool_16x1(x):
    return tf.nn.max_pool(x, ksize=[1, 16, 1, 1],
                          strides=[1, 1, 1, 1], padding='VALID')

def conv2d_valid(x, W):
    # by choosing [1,1,1,1] and "same" the output dimension == input dimension
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')

def net_work_diy(self, input_shape, classes):
    # assume x : [ batch x 20 x 320 x c ]
    self.m, self.n, self.c = input_shape
    self.classes = classes

    self.sess = tf.InteractiveSession()

    # 4 dimensional  datasize x seqwidth x veclength x channels
    self.x = tf.placeholder(tf.float32,  [None, self.m, self.n, self.c], name="x-input")
    self.y_ = tf.placeholder(tf.float32, [None, len(self.classes)], name="y-input")

    W_conv1 = CNN.weight_variable([5, self.n, self.c, 150], name="w_conv1")
    b_conv1 = CNN.bias_variable([150], name="b_conv1")

    with tf.name_scope("conv_1") as scope:
        h_conv1 = tf.nn.relu(CNN.conv2d_valid(self.x, W_conv1) + b_conv1)
        h_relu1 = tf.nn.relu(h_conv1)
        h_pool1 = CNN.max_pool_16x1(h_relu1)

    with tf.name_scope("fully_connected") as scope:
        self.keep_prob = tf.placeholder("float")
        h_fc1_drop = tf.nn.dropout(h_pool1, self.keep_prob)
        h_fc1_drop_flat = tf.reshape(h_fc1_drop, [-1, 150])
        W_fc1 = CNN.weight_variable([150, len(self.classes)], name="w_fc1")
        b_fc1 = CNN.bias_variable([len(self.classes)], name="b_fc1")
        h_fc1 = tf.matmul(h_fc1_drop_flat, W_fc1) + b_fc1

        self.y_conv = tf.nn.softmax(h_fc1)

    # Add summary ops to collect data
    _ = tf.histogram_summary("weights", W_conv1)
    _ = tf.histogram_summary("biases", b_conv1)
    _ = tf.histogram_summary("y", self.y_conv)

    with tf.name_scope("xent") as scope:
        cross_entropy = -tf.reduce_sum(self.y_ * tf.log(self.y_conv))
        _ = tf.scalar_summary('cross entropy', cross_entropy)

    with tf.name_scope("train") as scope:
        self.train_step = tf.train.AdamOptimizer(1e-5).minimize(cross_entropy)

    with tf.name_scope("test") as scope:
        # self.predict = tf.argmax(self.y_conv, 1)
        self.correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y_, 1))
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))

我看到一些答案建议更改conv2d和max_pool中的stride参数,因此我尝试使用以下函数替换conv2d_validmax_pool_16x1

def conv2d_same(x, W):
    # by choosing [1,1,1,1] and "same" the output dimension == input dimension
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1], padding='SAME')

但它没有解决问题,我收到以下错误:

InvalidArgumentError: Incompatible shapes: [10,2] vs. [34500,2]

非常感谢您帮助我们理解这个问题!

0 个答案:

没有答案