Python 为多标签文本分类神经网络在Keras中创建一个关注的LSTM层

Python 为多标签文本分类神经网络在Keras中创建一个关注的LSTM层,python,tensorflow,machine-learning,keras,neural-network,Python,Tensorflow,Machine Learning,Keras,Neural Network,问候亲爱的社区成员。我正在创建一个神经网络来预测多标签y。具体来说,神经网络需要5个输入(演员列表、情节摘要、电影特征、电影评论、标题),并尝试预测电影类型的顺序。在神经网络中,我使用嵌入层和全局最大池层 然而,我最近注意到了反复出现的层次,这在机器学习翻译中是一个非常有趣的话题。因此,我想知道我是否可以使用其中一个图层,但只能使用绘图摘要输入。注意,我不做ml翻译,而是做文本分类 我的神经网络处于当前状态 def create_fit_keras_model(hparams, 版本\数据\控制

问候亲爱的社区成员。我正在创建一个神经网络来预测多标签y。具体来说,神经网络需要5个输入(演员列表、情节摘要、电影特征、电影评论、标题),并尝试预测电影类型的顺序。在神经网络中,我使用嵌入层和全局最大池层

然而,我最近注意到了反复出现的层次,这在机器学习翻译中是一个非常有趣的话题。因此,我想知道我是否可以使用其中一个图层,但只能使用绘图摘要输入。注意,我不做ml翻译,而是做文本分类

我的神经网络处于当前状态

def create_fit_keras_model(hparams,
版本\数据\控制,
优化器名称,
验证方法,
回调,
优化器(版本=无):
句子长度演员=训练演员。形状[1]
vocab\u size\u frequency\u words\u actors=len(actors\u tokenizer.word\u index)
句子长度图=X列图。形状[1]
vocab\u size\u frequency\u words\u plot=len(plot\u tokenizer.word\u index)
句子长度特征=X序列特征。形状[1]
vocab\u size\u frequency\u words\u features=len(features\u tokenizer.word\u index)
句子长度\u评论=X\u序列\u评论。形状[1]
vocab\u size\u frequency\u words\u reviews=len(reviews\u tokenizer.word\u index)
句子长度\标题=X\序列\标题。形状[1]
vocab\u size\u frequency\u words\u title=len(title\u tokenizer.word\u index)
model=keras.Sequential(name='{0}{1}dim{2}batchsize{3}lr{4}decaymultiplier{5}'。格式(Sequential_model_name,
str(hparams[HP\u嵌入\u DIM]),
str(HP参数[HP_隐藏单位],
str(HP参数[HP学习率],
str(hparams[HP\u衰减\u步数\u乘数],
版本(数据(控制))
actors=keras.Input(shape=(sentenceLength\u actors,),name='actors\u Input')
plot=keras.Input(shape=(语句长度→plot,),batch\u size=hparams[HP\u HIDDEN\u UNITS],name='plot\u Input')
features=keras.Input(shape=(句子长度)\u features,),name='features\u Input')
reviews=keras.Input(shape=(句子长度_reviews,),name='reviews_Input')
title=keras.Input(shape=(sentenceLength\u title,),name='title\u Input')
emb1=层。嵌入(输入尺寸=声音大小\u频繁词\u演员+2,
输出尺寸=16,#hparams[HP#u嵌入尺寸],超参数或固定尺寸。
embeddings_初始值设定项='uniform',
掩码_zero=真,
输入长度=句子长度,
name=“actors\u嵌入层”)(actors)
#encoded_layer1=layers.globalaveragepoolg1d(name=“globalaveragepooling_actors_layer”)(emb1)
encoded_layer1=layers.globalMapooling1d(name=“globalMapooling_actors_layer”)(emb1)
emb2=层。嵌入(输入尺寸=声音大小频繁单词绘图+2,
输出尺寸=HP参数[HP尺寸],
embeddings_初始值设定项='uniform',
掩码_zero=真,
输入长度=句子长度图,
name=“plot\u嵌入层”)(plot)
#(备选方案1)
#encoded_layer2=layers.globalMapooling1d(name=“globalMapooling_plot_summary_Layer”)(emb2)
#(备选方案2)
emb2=layers.bidirective(layers.LSTM(hparams[HP\u EMBEDDING\u DIM],return\u sequences=True))(emb2)
平均池=layers.globalaveragepoolg1d()(emb2)
max_pool=layers.globalExpooling1d()(emb2)
conc=layers.concatenate([avg\u pool,max\u pool])
#(备选方案3)
#emb2=layers.bidirective(layers.LSTM(hparams[HP\u EMBEDDING\u DIM],return\u sequences=True))(emb2)
#emb2=layers.bidirective(layers.LSTM(hparams[HP\u EMBEDDING\u DIM],return\u sequences=True))(emb2)
#emb2=AttentionWithContext()(emb2)
emb3=层。嵌入(输入尺寸=声音大小\u频繁词\u特征+2,
输出尺寸=HP参数[HP尺寸],
embeddings_初始值设定项='uniform',
掩码_zero=真,
输入长度=句子长度特征,
name=“features\u嵌入层”)(features)
#encoded_layer3=layers.GlobalAveragePoolg1d(name=“globalaveragepooling_movie_features_layer”)(emb3)
encoded_layer3=layers.globalExpooling1d(name=“globalExpooling_movie_features_layer”)(emb3)
emb4=层。嵌入(输入尺寸=声音大小\u频繁词\u评论+2,
输出尺寸=HP参数[HP尺寸],
embeddings_初始值设定项='uniform',
掩码_zero=真,
输入长度=句子长度,
name=“审查\嵌入\层”)(审查)
#encoded_layer4=layers.GlobalAveragePoolg1d(name=“globalaveragepooling_user_reviews_layer”)(emb4)
encoded_layer4=layers.globalExpooling1d(name=“globalExpooling_user_reviews_layer”)(emb4)
emb5=层。嵌入(输入尺寸=声音大小频繁单词标题+2,
输出尺寸=HP参数[HP尺寸],
embeddings_初始值设定项='uniform',
    def __init__(self):    
        ##Nothing special to be done here
        super(peel_the_layer, self).__init__()
        
    def build(self, input_shape):
        ##Define the shape of the weights and bias in this layer
        ##This is a 1 unit layer. 
        units=1
        ##last index of the input_shape is the number of dimensions of the prev
        ##RNN layer. last but 1 index is the num of timesteps
        self.w=self.add_weight(name="att_weights", shape=(input_shape[-1], units), initializer="normal") #name property is useful for avoiding RuntimeError: Unable to create link.
        self.b=self.add_weight(name="att_bias", shape=(input_shape[-2], units), initializer="zeros")
        super(peel_the_layer,self).build(input_shape)
        
    def call(self, x):
        ##x is the input tensor..each word that needs to be attended to
        ##Below is the main processing done during training
        ##K is the Keras Backend import
        e = K.tanh(K.dot(x,self.w)+self.b)
        a = K.softmax(e, axis=1)
        output = x*a
        
        ##return the ouputs. 'a' is the set of attention weights
        ##the second variable is the 'attention adjusted o/p state' or context
        return a, K.sum(output, axis=1)
        a, context = peel_the_layer()(lstm_out)
        ##context is the o/p which be the input to your classification layer
        ##a is the set of attention weights and you may want to route them to a display