本文介绍了model.fit()Keras分类多输入-单输出给出错误:AttributeError:'NoneType'对象没有属性'fit'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建具有多个输入(实际上是3个)的Keras分类模型,以预测一个输出.具体来说,我的3个输入是:

I am constructing a Keras Classification model with Multiple Inputs (3 actually) to predict one single output. Specifically, my 3 inputs are:

  1. 演员
  2. 图摘要
  3. 相关电影功能

输出:

  1. 流派标签

以上所有输入和单个输出都与10,000个IMDB电影有关.

All the above inputs and the single output are related to 10,000 IMDB Movies.

即使模型创建成功,当我尝试在三个不同的X_train上拟合模型时,也会出现Attribute错误.对于演员,我有一个X_train和X_test;对于情节摘要,我有一个不同的X_train和X_test;对于电影功能,我有一个不同的X_train和X_test.对于所有输入,我的y_train和y_test都相同.

Even though the creation of the model is successful, when I try to fit the model on my three different X_train's I get an Attribute error. I have one X_train and X_test for actors, a different one X_train and X_test for plot summary and a different one X_train and X_test for movie features. My y_train and y_test are the same for all the inputs.

Python代码(创建多个输入keras)

def kera_multy_classification_model():

    sentenceLength_actors = 15
    vocab_size_frequent_words_actors = 20001

    sentenceLength_plot = 23
    vocab_size_frequent_words_plot = 17501

    sentenceLength_features = 69
    vocab_size_frequent_words_features = 20001

    model = keras.Sequential(name='Multy-Input Keras Classification model')

    actors = keras.Input(shape=(sentenceLength_actors,), name='actors_input')
    plot = keras.Input(shape=(sentenceLength_plot,), name='plot_input')
    features = keras.Input(shape=(sentenceLength_features,), name='features_input')

    emb1 = layers.Embedding(input_dim = vocab_size_frequent_words_actors + 1,
                            # based on keras documentation input_dim: int > 0. Size of the vocabulary, i.e. maximum integer index + 1.
                            output_dim = Keras_Configurations_model1.EMB_DIMENSIONS,
                            # int >= 0. Dimension of the dense embedding
                            embeddings_initializer = 'uniform', 
                            # Initializer for the embeddings matrix.
                            mask_zero = False,
                            input_length = sentenceLength_actors,
                            name="actors_embedding_layer")(actors)
    encoded_layer1 = layers.LSTM(100)(emb1)

    emb2 = layers.Embedding(input_dim = vocab_size_frequent_words_plot + 1,
                            output_dim = Keras_Configurations_model2.EMB_DIMENSIONS,
                            embeddings_initializer = 'uniform',
                            mask_zero = False,
                            input_length = sentenceLength_plot,
                            name="plot_embedding_layer")(plot)
    encoded_layer2 = layers.LSTM(100)(emb2)

    emb3 = layers.Embedding(input_dim = vocab_size_frequent_words_features + 1,
                            output_dim = Keras_Configurations_model3.EMB_DIMENSIONS,
                            embeddings_initializer = 'uniform',
                            mask_zero = False,
                            input_length = sentenceLength_features,
                            name="features_embedding_layer")(features)
    encoded_layer3 = layers.LSTM(100)(emb3)

    merged = layers.concatenate([encoded_layer1, encoded_layer2, encoded_layer3])

    layer_1 = layers.Dense(Keras_Configurations_model1.BATCH_SIZE, activation='relu')(merged)

    output_layer = layers.Dense(Keras_Configurations_model1.TARGET_LABELS, activation='softmax')(layer_1)

    model = keras.Model(inputs=[actors, plot, features], outputs=output_layer)

    print(model.output_shape)

    print(model.summary())

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['sparse_categorical_accuracy'])

Python代码(适合我的输入的多个输入keras)

def fit_keras_multy_input(model, x_train_seq_actors, x_train_seq_plot, x_train_seq_features, x_test_seq_actors, x_test_seq_plot, x_test_seq_features, y_train, y_test):

    s = time()

    fit_model = model.fit([x_train_seq_actors, x_train_seq_plot, x_train_seq_features], y_train, 
                          epochs=Keras_Configurations_model1.NB_EPOCHS,
                          verbose = Keras_Configurations_model1.VERBOSE,
                          batch_size=Keras_Configurations_model1.BATCH_SIZE,
                          validation_data=([x_test_seq_actors, x_test_seq_plot, x_test_seq_features], y_test),
                          callbacks=callbacks)

    duration = time() - s
    print("\nTraining time finished. Duration {} secs".format(duration))

模型的结构

产生错误

注意:请不要X_train和X_test都是数字序列. (已被标记化的文本)

进行了一些研究后,问题开始于model.compile()函数.虽然,我不确定应该在模型的编译功能中进行哪些更改以解决此问题.

Having done a little research, the problem starts in the model.compile() function. Although, I am not sure what should be changed in my model's compile function so as to fix this.

在此问题上,谢谢您的任何建议或帮助.请随时在评论中询问我可能错过的任何其他信息,以使此问题更完整.

Thank you in advance for any advice or help on this matter. Feel free to ask on the comments any additional information that I may have missed, to make this question more complete.

推荐答案

您的函数kera_multy_classification_model()不返回任何内容,因此在model = kera_multy_classification_model()之后,您得到model == None,因为您的函数不返回任何内容.而None的类型是NoneType,它实际上没有名为fit()的方法.

Your function kera_multy_classification_model() doesn't return anything, so after model = kera_multy_classification_model(), you get model == None as your function returns nothing. And None's type is NoneType and it really doesn't have a method called fit().

只需在kera_multy_classification_model()的末尾添加返回模型.

Just add return model in the end of kera_multy_classification_model().

这篇关于model.fit()Keras分类多输入-单输出给出错误:AttributeError:'NoneType'对象没有属性'fit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:50