简体   繁体   English

model.fit() Keras 分类多输入-单输出给出错误:AttributeError: 'NoneType' 对象没有属性 'fit'

[英]model.fit() Keras Classification Multiple Inputs-Single Output gives error: AttributeError: 'NoneType' object has no attribute 'fit'

I am constructing a Keras Classification model with Multiple Inputs (3 actually) to predict one single output.我正在构建一个具有多个输入(实际上是 3 个)的 Keras 分类模型来预测一个输出。 Specifically, my 3 inputs are:具体来说,我的 3 个输入是:

  1. Actors演员
  2. Plot Summary情节概要
  3. Relevant Movie Features相关电影特色

Output :输出

  1. Genre tags流派标签

All the above inputs and the single output are related to 10,000 IMDB Movies.以上所有输入和单个输出都与 10,000 部 IMDB 电影有关。

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.即使模型创建成功,当我尝试在三个不同的 X_train 上拟合模型时,我也会收到属性错误。 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.我有一个 X_train 和 X_test 用于演员,一个不同的 X_train 和 X_test 用于情节摘要,另外一个 X_train 和 X_test 用于电影功能。 My y_train and y_test are the same for all the inputs.我的 y_train 和 y_test 对于所有输入都是相同的。

Python Code (create the multiple input keras) 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 Code (fit the multiple input keras on my inputs) 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))

Model's Structure模型结构

在此处输入图片说明

Error produced产生的错误

在此处输入图片说明

Note: Please not that each of the X_train and X_test are sequences of numbers.注意:请注意 X_train 和 X_test 都是数字序列。 (Text that have been tokenized) (已被标记的文本)

Having done a little research, the problem starts in the model.compile() function.做了一些研究后,问题开始于 model.compile() 函数。 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.请随时在评论中询问我可能遗漏的任何其他信息,以使这个问题更完整。

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.您的函数kera_multy_classification_model()不返回任何内容,因此在model = kera_multy_classification_model() ,您会得到model == None因为您的函数不返回任何内容。 And None 's type is NoneType and it really doesn't have a method called fit() . None的类型是NoneType并且它确实没有称为fit()的方法。

Just add return model in the end of kera_multy_classification_model() .只需在kera_multy_classification_model()的末尾添加返回模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras `model.fit` 期间的运行时错误“AttributeError: 'tuple' object has no attribute '_keras_mask'” - Run-time error during Keras `model.fit` “AttributeError: 'tuple' object has no attribute '_keras_mask'” Keras: AttributeError: 'int' 对象在使用 model.fit 时没有属性 'ndim' - Keras : AttributeError: 'int' object has no attribute 'ndim' when using model.fit AttributeError: 'Dimension' 对象在使用 Keras Sequential Model.fit 时没有属性 'log10' - AttributeError: 'Dimension' object has no attribute 'log10' while using Keras Sequential Model.fit model.fit AttributeError: 'tuple' object 没有属性 'shape' - model.fit AttributeError: 'tuple' object has no attribute 'shape' Keras model.fit() 发出 TypeError: 'NoneType' 对象不可调用 - Keras model.fit() giving out TypeError: 'NoneType' object is not callable 如何将 Keras 中的多个输入的标签赋予 model.fit() function? - How to give labels of multiple inputs in Keras to model.fit() function? Keras:model.fit()在siamese_model中出现多个输入错误 - Keras: model.fit() getting error for multiple inputs in siamese_model keras model.load_weights 错误 NoneType' object 没有属性 'fit' - keras model.load_weights error NoneType' object has no attribute 'fit' AttributeError: 'function' object 在 Keras 中没有属性 'fit' - AttributeError: 'function' object has no attribute 'fit' in Keras AttributeError:“ NoneType”对象没有属性“ fit_generator” - AttributeError: 'NoneType' object has no attribute 'fit_generator'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM