小编Shu*_*aai的帖子

Keras使用Tensorflow后端 - 屏蔽丢失功能

我正在尝试使用Keras和Tensorflow后端使用LSTM实现序列到序列任务.输入是具有可变长度的英语句子.为了构建具有二维形状[batch_number,max_sentence_length]的数据集,我在行尾添加EOF并用足够的占位符填充每个句子,例如"#".然后将句子中的每个字符转换为单热矢量,现在数据集具有3-D形状[batch_number,max_sentence_length,character_number].在LSTM编码器和解码器层之后,计算输出和目标之间的softmax交叉熵.

为了消除模型训练中的填充效应,可以在输入和丢失功能上使用掩蔽.Keras中的掩码输入可以通过使用"layers.core.Masking"来完成.在Tensorflow中,可以按如下方式 屏蔽损失函数:Tensorflow中的自定义屏蔽损失函数

但是,我没有找到在Keras中实现它的方法,因为keras中使用定义的损失函数只接受参数y_true和y_pred.那么如何将真正的sequence_lengths输入到丢失函数和掩码?

此外,我在\ keras\engine\training.py中找到了一个函数"_weighted_masked_objective(fn)".它的定义是"为目标函数添加对屏蔽和样本加权的支持."但似乎该函数只能接受fn(y_true,y_pred).有没有办法使用这个函数来解决我的问题?

具体来说,我修改了余阳的例子.

from keras.models import Model
from keras.layers import Input, Masking, LSTM, Dense, RepeatVector, TimeDistributed, Activation
import numpy as np
from numpy.random import seed as random_seed
random_seed(123)

max_sentence_length = 5
character_number = 3 # valid character 'a, b' and placeholder '#'

input_tensor = Input(shape=(max_sentence_length, character_number))
masked_input = Masking(mask_value=0)(input_tensor)
encoder_output = LSTM(10, return_sequences=False)(masked_input)
repeat_output = RepeatVector(max_sentence_length)(encoder_output)
decoder_output = LSTM(10, return_sequences=True)(repeat_output)
output = Dense(3, activation='softmax')(decoder_output)

model = Model(input_tensor, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.summary()

X = np.array([[[0, 0, 0], …
Run Code Online (Sandbox Code Playgroud)

masking lstm keras loss-function

22
推荐指数
1
解决办法
1万
查看次数

标签 统计

keras ×1

loss-function ×1

lstm ×1

masking ×1