如何在Keras中使用张量的一部分?

时间:2018-09-20 09:53:49

标签: python keras

我想在lstm层的输出中使用张量的一部分,但不知道如何正确执行。     我的目的是告诉LSTM层其输入序列的“实际”长度。     这是我的尝试,但失败了。     有谁能帮助解决这个问题并解释细节,非常感谢〜

input_spectrogram = Input(shape=(64,500,1))
input_length = Input(shape=(1,))
cnn1 = Conv2D(filters = 64, kernel_size = (1,4),input_shape=(64,500, 1),padding = 'same',strides = 1,activation = 'relu',name='conv1')(input_spectrogram)
maxpooling1 = MaxPooling2D(pool_size = (1,4),name='maxpooling1')(cnn1)
bn1 = BatchNormalization(name='BN1')(maxpooling1)
cnn2 = Conv2D(filters = 128, kernel_size = (64,1),strides = 1,activation ='relu',name='conv2')(bn1)
maxpooling2 = MaxPooling2D(pool_size = (1,2),name='maxpooling2')(cnn2)
reshape = Reshape((62,128))(maxpooling2)
lstm1 = LSTM(128,return_sequences = True,recurrent_dropout=0.3,name='lstm1')(reshape)   #output:(None,62,128)
softmax_in = Lambda(lambda x:x[0][x[1],:])([lstm1,input_length])
softmax_ = Dense(10,activation='softmax',name='softmax_')(softmax_in)
seq = Model(inputs=input_spectrogram, outputs=[softmax_])
seq.compile(loss='categorical_crossentropy', optimizer='adadelta',metrics=['accuracy'])

2 个答案:

答案 0 :(得分:0)

似乎不完全支持使用张量索引(请参见此处的讨论:https://github.com/tensorflow/tensorflow/issues/206#issuecomment-158435464)。

使用常量代替索引是否对您有用?

答案 1 :(得分:0)

input_spectrogram = Input(shape=(64,500,1))
input_length = Input(shape=(1,))
cnn1 = Conv2D(filters = 64, kernel_size = (1,4),input_shape=(64,500, 1),padding = 'same',strides = 1,activation = 'relu',name='conv1')(input_spectrogram)
maxpooling1 = MaxPooling2D(pool_size = (1,4),name='maxpooling1')(cnn1)
bn1 = BatchNormalization(name='BN1')(maxpooling1)
cnn2 = Conv2D(filters = 128, kernel_size = (64,1),strides = 1,activation ='relu',name='conv2')(bn1)
maxpooling2 = MaxPooling2D(pool_size = (1,2),name='maxpooling2')(cnn2)
reshape = Reshape((62,128))(maxpooling2)
lstm1 = LSTM(128,return_sequences = True,recurrent_dropout=0.3,name='lstm1')(reshape)   #output:(None,62,128)
softmax_in = Lambda(lambda x:x[:,5])(lstm1)
softmax_ = Dense(10,activation='softmax',name='softmax_')(softmax_in)
seq = Model(inputs=input_spectrogram, outputs=[softmax_])
seq.compile(loss='categorical_crossentropy', optimizer='adadelta',metrics=['accuracy'])

现在可行,那么如何使用输入层中的“ real_length”?