【问题标题】:Tensorflow character-level CNN - input shapeTensorflow 字符级 CNN - 输入形状
【发布时间】:2018-05-11 11:28:18
【问题描述】:

我正在尝试将 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`.

任何帮助将不胜感激。
谢谢。

更新

因此,在阅读了一些博客文章 12 并感谢 vijay m 之后,我了解到我们必须事先提供输入尺寸(与使用 RNN/LSTM 提供 sequence_lengths 不同)。所以这里是最终的代码sn-p:

# 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)

【问题讨论】:

    标签: python tensorflow embedding convolutional-neural-network


    【解决方案1】:

    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]])
    

    【讨论】:

    • 我想我忘了只有 lstm/rnn 我们可以使用可变长度输入或动态馈送序列长度等。在我按照你的建议定义之后,它完美地工作。谢谢!
    【解决方案2】:

    Conv1d 中输入的默认格式是形状(batch、length、channels),也许 char_embeddings 应该是这样的:

    s = char_embeddings.get_shape()
    char_embeddings = tf.reshape(char_embeddings, shape=[-1, s[2], dim_char])
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 2017-10-25
      • 2020-10-05
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多