我正在根据文章数据设计用于分类的keras模型。
我有如下4维数据
[batch, article_num, word_num, word embedding size]
我想将每个(word_num,单词嵌入)数据馈送到keras双向层
为了获得3维结果,如下所示。
[batch, article_num, bidirectional layer output size]
当我尝试提供4维数据以进行这样的测试
inp = Input(shape=(article_num, word_num, ))
# dims = [batch, article_num, word_num]
x = Reshape((article_num * word_num, ), input_shape = (article_num, word_num))(inp)
# dims = [batch, article_num * word_num]
x = Embedding(word_num, word_embedding_size, input_length = article_num * word_num)(x)
# dims = [batch, article_num * word_num, word_embedding_size]
x = Reshape((article_num , word_num, word_embedding_size),
input_shape = (article_num * word_num, word_embedding_size))(x)
# dims = [batch, article_num, word_num, word_embedding_size]
x = Bidirectional(CuDNNLSTM(50, return_sequences = True),
input_shape=(article_num , word_num, word_embedding_size))(x)
我得到了错误
ValueError: Input 0 is incompatible with layer bidirectional_12: expected ndim=3, found ndim=4
我该如何实现?
答案 0 :(得分:0)
如果您不希望它碰到constexpr
维度,则可以尝试使用article_num
包装器。但是我不确定它是否可以与双向和其他东西兼容。
TimeDistributed
提示:
inp = Input(shape=(article_num, word_num))
x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))
#option 1
#x1 shape : (batch, article_num, word_num, 50)
x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)
#option 2
#x2 shape : (batch, article_num, 50)
x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)
,只需要在input_shape
张量上使用即可。 Input
,则可能不需要任何重塑。 TimeDistributed
,请使用word_num
。