尝试在Keras中创建BLSTM网络时出现TypeError [英] TypeError when trying to create a BLSTM network in Keras

查看:370
本文介绍了尝试在Keras中创建BLSTM网络时出现TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Keras和深度学习有点陌生.我目前正在尝试复制此纸张,但是当我编译第二个模型时(与LSTM一起)出现以下错误:

I'm a bit new to Keras and deep learning. I'm currently trying to replicate this paper but when I'm compiling the second model (with the LSTMs) I get the following error:

"TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"

模型的描述是这样:

  1. 输入(长度T是特定于设备的窗口大小)
  2. 带有滤波器size 3、5和7的并行一维卷积 分别是stride=1number of filters=32activation type=linearborder mode=same
  3. 合并层,将输出合并 一维并行卷积
  4. 双向LSTM由正向LSTM组成 和向后的LSTM,output_dim=128
  5. 双向LSTM由正向LSTM组成 和向后的LSTM,output_dim=128
  6. 致密层,output_dim=128activation type=ReLU
  7. 致密层output_dim= Tactivation type=linear
  1. Input (length T is appliance specific window size)
  2. Parallel 1D convolution with filter size 3, 5, and 7 respectively, stride=1, number of filters=32, activation type=linear, border mode=same
  3. Merge layer which concatenates the output of parallel 1D convolutions
  4. Bidirectional LSTM consists of a forward LSTM and a backward LSTM, output_dim=128
  5. Bidirectional LSTM consists of a forward LSTM and a backward LSTM, output_dim=128
  6. Dense layer, output_dim=128, activation type=ReLU
  7. Dense layer, output_dim= T , activation type=linear

我的代码是这样的:

from keras import layers, Input
from keras.models import Model

def lstm_net(T):
    input_layer = Input(shape=(T,1))
    branch_a = layers.Conv1D(32, 3, activation='linear', padding='same', strides=1)(input_layer)
    branch_b = layers.Conv1D(32, 5, activation='linear', padding='same', strides=1)(input_layer)
    branch_c = layers.Conv1D(32, 7, activation='linear', padding='same', strides=1)(input_layer)

    merge_layer = layers.Concatenate(axis=-1)([branch_a, branch_b, branch_c])
    print(merge_layer.shape)
    BLSTM1 = layers.Bidirectional(layers.LSTM(128, input_shape=(8,40,96)))(merge_layer)
    print(BLSTM1.shape)
    BLSTM2 = layers.Bidirectional(layers.LSTM(128))(BLSTM1)
    dense_layer = layers.Dense(128, activation='relu')(BLSTM2)
    output_dense = layers.Dense(1, activation='linear')(dense_layer)
    model = Model(input_layer, output_dense)
    model.name = "lstm_net"
    return model

model = lstm_net(40)

之后,我得到了上面的错误.我的目标是作为输入提供一批长度为40的8个序列,也作为输出得到一批长度为40的8个序列.我在Keras Github上发现了这个问题在Flatten#818之后,LSTM层无法连接到Dense层然后@fchollet建议我应该在第一层中指定"input_shape",但可能不正确.我输入了两个打印语句,以查看形状是如何变化的,输出是:

After that I get the above error. My goal is to give as input a batch of 8 sequences of length 40 and get as output a batch of 8 sequences of length 40 too. I found this issue on Keras Github LSTM layer cannot connect to Dense layer after Flatten #818 and there @fchollet suggests that I should specify the 'input_shape' in the first layer which I did but probably not correctly. I put the two print statements to see how the shape is changing and the output is:

(?, 40, 96)
(?, 256)

错误发生在定义的BLSTM2行上,可以在此处

The error occurs on the line BLSTM2 is defined and can be seen in full here

推荐答案

您的问题出在以下三行中:

Your problem lies in these three lines:

BLSTM1 = layers.Bidirectional(layers.LSTM(128, input_shape=(8,40,96)))(merge_layer)
print(BLSTM1.shape)
BLSTM2 = layers.Bidirectional(layers.LSTM(128))(BLSTM1)

默认情况下,LSTM仅返回计算的最后一个元素-因此您的数据将失去其顺序性质.这就是前进层引发错误的原因.将此行更改为:

As a default, LSTM is returning only the last element of computations - so your data is losing its sequential nature. That's why the proceeding layer raises an error. Change this line to:

BLSTM1 = layers.Bidirectional(layers.LSTM(128, return_sequences=True))(merge_layer)
print(BLSTM1.shape)
BLSTM2 = layers.Bidirectional(layers.LSTM(128))(BLSTM1)

为了使第二个LSTM的输入也具有顺序性质.

In order to make the input to the second LSTM to have sequential nature also.

除此之外-我不想在中间模型层中使用input_shape,因为它是自动推断的.

Aside of this - I'd rather not use input_shape in middle model layer as it's automatically inferred.

这篇关于尝试在Keras中创建BLSTM网络时出现TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆