简体   繁体   English

AttributeError: 'Model' object 没有属性 '_output_tensor_cache'

[英]AttributeError: 'Model' object has no attribute '_output_tensor_cache'

import keras
from keras.layers import Input, Dense
from keras.models import Model
from keras_adamw import AdamW

mlp = Model([
        Dense(10, activation='relu', input_shape=trainX_scaled.shape), #input shape
        Dense(10,  activation='relu'),  #Hiddin layer
        Dense(10, activation='relu') #output layer
])

optimizer = AdamW(lr=0.001,model=mlp)
mlp.compile(optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = mlp.fit(trainX_scaled, train_y, epochs=500, validation_data=(valX_scaled, val_y), batch_size=1)

The error is错误是

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-66-46d3a15c03c4> in <module>()
     19 optimizer = AdamW(lr=0.001,model=mlp)
     20 mlp.compile(optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
---> 21 history = mlp.fit(trainX_scaled, train_y, epochs=500, validation_data=(valX_scaled, val_y), batch_size=1)

3 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/network.py in call(self, inputs, mask)
    578         cache_key = object_list_uid(inputs)
    579         cache_key += '_' + object_list_uid(masks)
--> 580         if cache_key in self._output_tensor_cache:
    581             return self._output_tensor_cache[cache_key]
    582         else:

AttributeError: 'Model' object has no attribute '_output_tensor_cache'

The error occurs when running model.fit .运行model.fit时出现错误。

Which tensorflow version have you installed in your system?你的系统安装了哪个tensorflow版本? Please import the keras libraries from tensorflow.keras and re-execute the above code.请从tensorflow.keras导入keras库,重新执行以上代码。

from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

AdamW api is part of Tensorflow Addons package. To import AdamW optimizer, you need to use below code: AdamW api 是Tensorflow Addons package 的一部分。要导入AdamW优化器,您需要使用以下代码:

!pip install tensorflow-addons
import tensorflow_addons as tfa
from tensorflow_addons.optimizers import AdamW

Or can simply use Adam optimizer instead of using Adamw as below:或者可以简单地使用Adam优化器而不是使用Adamw ,如下所示:

from tensorflow.keras.optimizers import Adam

Let us know if the issue still persists.让我们知道问题是否仍然存在。 Thank You.谢谢你。

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

相关问题 AttributeError: object 没有属性 '_output_tensor_cache' - AttributeError: object has no attribute '_output_tensor_cache' AttributeError:“张量”object 在自定义 model 中没有属性“numpy” - AttributeError: 'Tensor' object has no attribute 'numpy' in a custom model model.summary() - AttributeError: 'Tensor' object 没有属性 'summary' - model.summary() - AttributeError: 'Tensor' object has no attribute 'summary' AttributeError: &#39;Tensor&#39; 对象在尝试使用 &#39;Model&#39; 时没有属性 &#39;input&#39; - AttributeError: 'Tensor' object has no attribute 'input' when trying to use 'Model' AttributeError:&#39;Tensor&#39;对象没有属性&#39;append&#39; - AttributeError: 'Tensor' object has no attribute 'append' TensorFlow:AttributeError:&#39;Tensor&#39;对象没有属性&#39;shape&#39; - TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Medpy AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;astype&#39; - Medpy AttributeError: 'Tensor' object has no attribute 'astype' AttributeError:&#39;Tensor&#39;对象没有属性&#39;compile&#39; - AttributeError: 'Tensor' object has no attribute 'compile' AttributeError:“张量”object 没有属性“to_sparse” - AttributeError: 'Tensor' object has no attribute 'to_sparse' AttributeError:&#39;Tensor&#39;对象没有属性&#39;reshape&#39; - AttributeError: 'Tensor' object has no attribute 'reshape'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM