嗨,我一直在尝试遵循以下两个示例:
但是,对于示例1,我一直遇到以下错误
Input 0 of layer conv2d_1 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 20]
以及以下错误示例2。
ValueError: Layer conv2d_1 expects 1 inputs, but it received 100 input tensors. Inputs received: [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]
我已经更改了代码,而不是使用word_list = tf.unstack(word_vectors, axis=1),我已更改为以下实现来解决错误,但是我不知道实现是否正确。
word_vectors = tf.contrib.layers.embed_sequence(
x, vocab_size=n_words, embed_dim=EMBEDDING_SIZE)
word_list = tf.reshape( word_vectors, [-1, MAX_DOCUMENT_LENGTH, EMBEDDING_SIZE, 1])
conv1, pool1 = cnn(word_list, FILTER_SHAPE1, 'CNN_Layer1')
函数cnn和其他参数已定义
MAX_DOCUMENT_LENGTH = 100
N_FILTERS = 10
FILTER_SHAPE1 = [20, 20]
POOLING_WINDOW = 4
POOLING_STRIDE = 2
MAX_LABEL = 15
EMBEDDING_SIZE = 20
def cnn(x, filter_shape, name):
with tf.variable_scope(name):
conv1 = tf.layers.conv2d(
x,
filters=N_FILTERS,
kernel_size=filter_shape,
padding='VALID',
activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(
conv1,
pool_size=POOLING_WINDOW,
strides=POOLING_STRIDE,
padding='SAME')
return conv1, pool1