【问题标题】:Tensorflow model TypeError: 'NoneType' object is not callableTensorflow 模型 TypeError:“NoneType”对象不可调用
【发布时间】:2021-10-05 17:01:07
【问题描述】:

我构建了一个自定义的 Tensorflow 2.0 模型(意味着是一个 Wide and Deep 模型)。使用以下层成功编译模型:


    from keras.layers.merge import concatenate
    from keras.models import Model, Sequential
    from keras.layers import Dense, Input, Embedding, GlobalMaxPool1D

    #wide model

    model = Sequential()
    model.add(layers.Embedding(vocab_size, embedding_dim, 
                               weights=[embedding_matrix], 
                               input_length=maxlen, 
                               trainable=True))
    model.add(layers.GlobalMaxPool1D())
    model.add(layers.Dense(10, activation='relu'))
    model.add(layers.Dense(1, activation='sigmoid'))
    
    #deepmodel

    deep_model = Sequential()
    deep_model.add(layers.Input(shape=X_train_deep_numpy.shape[1:]))
    deep_model.add(layers.Dense(64, activation='relu'))
    deep_model.add(layers.Dense(64, activation='relu'))
    deep_model.add(layers.Dense(1, activation='sigmoid'))
    
    model_concat = concatenate([model.output, deep_model.output], axis=-1)
    model_concat = Dense(1, activation='softmax')(model_concat)
    merged_model = Model(inputs=[model.input, deep_model.input], outputs=model_concat)
    merged_model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
    
    merged_model.summary()

传递了一个 Numpy 数组列表来拟合模型,其中 X_train_numpy.shape = (26907, 100), X_train_deep_numpy.shape = (26907, 8), y_train.shape = (26907,),但遇到了TypeError: 'NoneType' object is not callable


    TypeError                                 Traceback (most recent call last)
    <ipython-input-120-760d94bc9f56> in <module>()
    ----> 1 merged_model.fit(x=[X_train_numpy, X_train_deep_numpy], y=y_train, epochs=50, batch_size=1000)
    
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
       1182                 _r=1):
       1183               callbacks.on_train_batch_begin(step)
    -> 1184               tmp_logs = self.train_function(iterator)
       1185               if data_handler.should_sync:
       1186                 context.async_wait()
    
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
        883 
        884       with OptionalXlaContext(self._jit_compile):
    --> 885         result = self._call(*args, **kwds)
        886 
        887       new_tracing_count = self.experimental_get_tracing_count()
    
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
        915       # In this case we have created variables on the first call, so we run the
        916       # defunned version which is guaranteed to never create variables.
    --> 917       return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
        918     elif self._stateful_fn is not None:
        919       # Release the lock early so that multiple threads can perform the call
    
    TypeError: 'NoneType' object is not callable

错误信息很笼统,因此无法确定出了什么问题 - 检查了拼写错误和多余的空格,但无济于事。谁能帮助查明我的 TF 模型出了什么问题?

【问题讨论】:

    标签: tensorflow keras deep-learning


    【解决方案1】:

    由于您有二元分类问题,您需要在两个输出上计算 softmax + categorical_crossentropy 以选择最可能的一个。

    因此,您的最后一层应该是:

    model_concat = Dense(2, activation='softmax')(model_concat)
    model.compile(...)
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2012-04-28
      • 2016-10-10
      • 1970-01-01
      相关资源
      最近更新 更多