我正在尝试将2个堆栈的字符级CNN添加到一个较大的神经网络系统中,但是在输入维数方面出现了ValueError。
我要实现的是通过替换字符(根据大写字母,数字或字母)并将输入的字符输入CNN中来获得输入字的正交表示形式。我知道可以使用LSTM / RNN来实现,但是要求表明使用CNN,因此使用其他NN并不是可选的。
那里的大多数示例自然使用图像数据集(MNIST等),而不使用文本数据集。因此,我很困惑,不确定如何对字符嵌入进行“重塑”,以使它们可以成为CNN的有效输入。
因此,这是我要运行的代码的一部分:
# ...
# shape = (batch size, max length of sentence, max length of word)
self.char_ids = tf.placeholder(tf.int32, shape=[None, None, None],
name="char_ids")
# ...
# Char embedding lookup
_char_embeddings = tf.get_variable(
name="_char_embeddings",
dtype=tf.float32,
shape=[self.config.nchars, self.config.dim_char])
char_embeddings = tf.nn.embedding_lookup(_char_embeddings,
self.char_ids, name="char_embeddings")
# Reshape for CNN?
s = tf.shape(char_embeddings)
char_embeddings = tf.reshape(char_embeddings, shape=[s[0]*s[1], self.config.dim_char, s[2]])
# Conv #1
conv1 = tf.layers.conv1d(
inputs=char_embeddings,
filters=64,
kernel_size=3,
padding="valid",
activation=tf.nn.relu)
# Conv #2
conv2 = tf.layers.conv1d(
inputs=conv1,
filters=64,
kernel_size=3,
padding="valid",
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=2, strides=2)
# Dense Layer
output = tf.layers.dense(inputs=pool2, units=32, activation=tf.nn.relu)
# ...
这是我得到的错误:
File "/home/emre/blstm-crf-ner/model/ner_model.py", line 159, in add_word_embeddings_op activation=tf.nn.relu)
File "/home/emre/blstm-crf-ner/virtner/lib/python3.4/site-packages/tensorflow/python/layers/convolutional.py", line 411, in conv1d return layer.apply(inputs)
File "/home/emre/blstm-crf-ner/virtner/lib/python3.4/site-packages/tensorflow/python/layers/base.py", line 809, in apply return self.__call__(inputs, *args, **kwargs)
File "/home/emre/blstm-crf-ner/virtner/lib/python3.4/site-packages/tensorflow/python/layers/base.py", line 680, in __call__ self.build(input_shapes)
File "/home/emre/blstm-crf-ner/virtner/lib/python3.4/site-packages/tensorflow/python/layers/convolutional.py", line 132, in build raise ValueError('The channel dimension of the inputs '
ValueError: The channel dimension of the inputs should be defined. Found `None`.
任何帮助,将不胜感激。
谢谢。
更新
所以通过一些博客文章下面后1,2,感谢维杰男,我明白,我们必须提供输入尺寸事先(与提供sequence_length
s的RNN / LSTM)。所以这是最终的代码片段:
# Char embedding lookup
_char_embeddings = tf.get_variable(
name="_char_embeddings",
dtype=tf.float32,
shape=[self.config.nchars, self.config.dim_char])
char_embeddings = tf.nn.embedding_lookup(_char_embeddings,
self.char_ids, name="char_embeddings")
# max_len_of_word: 20
# Just pad shorter words and truncate the longer ones.
s = tf.shape(char_embeddings)
char_embeddings = tf.reshape(char_embeddings, shape=[-1, self.config.dim_char, self.config.max_len_of_word])
# Conv #1
conv1 = tf.layers.conv1d(
inputs=char_embeddings,
filters=64,
kernel_size=3,
padding="valid",
activation=tf.nn.relu)
# Conv #2
conv2 = tf.layers.conv1d(
inputs=conv1,
filters=64,
kernel_size=3,
padding="valid",
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling1d(inputs=conv2, pool_size=2, strides=2)
# Dense Layer
output = tf.layers.dense(inputs=pool2, units=32, activation=tf.nn.relu)
conv1d
期望在图形创建期间定义通道尺寸。因此,您无法将尺寸传递为None
。
您需要进行以下更改:
char_ids = tf.placeholder(tf.int32, shape=[None, max_len_sen, max_len_word],
name="char_ids")
#max_len_sen and max_len_word has to be set.
#Reshapping for CNN, should be
s = char_embeddings.get_shape()
char_embeddings = tf.reshape(char_embeddings, shape=[-1, dim_char, s[2]])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句