AttributeError:'SparseCategoricalCrossentropy'对象没有属性'__name__'

尼古拉斯·格维斯(Nicolas Gervais)

我在基本的CNN中遇到此错误:

AttributeError:'SparseCategoricalCrossentropy'对象没有属性' name '

我以为你可以loss=tf.metrics.SparseCategoricalCrossentropy()进入model.compile()

import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
tf.random.set_seed(42)

train, test = tfds.load('fashion_mnist', split=['train', 'test'], as_supervised=True)

train = train.map(lambda x, y: (tf.divide(x, 255), y)).batch(8)
test = test.map(lambda x, y: (tf.divide(x, 255), y)).batch(8)

custom_model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')])

custom_model.compile(loss=tf.metrics.SparseCategoricalCrossentropy(),
                     optimizer=tf.optimizers.Adam(),
                     metrics=[tf.metrics.SparseCategoricalAccuracy()])

early_stopping = [tf.keras.callbacks.EarlyStopping(patience=5)]

conv_dropout_history = custom_model.fit(train, validation_data=test,
                                        epochs=100, callbacks=early_stopping)

有什么方法可以使用此对象,model.compile()还是我应该只使用tf.metrics.sparse_categorical_crossentropy(或字符串形式)?

小鸭

您正在使用度量作为损失函数。

尝试替换此:

tf.metrics.SparseCategoricalCrossentropy()

有了这个:

tf.keras.losses.SparseCategoricalCrossentropy()

指标不能由Keras优化最小化,所以你必须使用损失函数

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AttributeError:“ str”对象没有属性“ __name__”

动态更改函数__name__会引发AttributeError:'method'对象没有属性'__name__'

AttributeError:“ str”对象没有属性“ name”

AttributeError:'Doctype'对象没有属性'name'

轻松应用:AttributeError:'DataFrame'对象没有属性'name'

AttributeError:“ LGBMRegressor”对象没有属性“ feature_name_”

Keras:AttributeError:'Adam'对象没有属性'_name'

AttributeError: 'str' 对象没有属性 'name' PySpark

AttributeError: 'DataFrame' 对象在使用 SMOTE 时没有属性 'name'

AttributeError: 'Animal' 对象没有属性 'get_name'

AttributeError: 'Seekerskillset' 对象没有属性 'skill_name'

Python装饰器-<Classname>对象没有属性'__name__'

尽管没有`__name__`属性,“类对象”仍使用`.__ name__`打印其名称。

AttributeError : 'RMSProp' 没有属性 'name'

AttributeError: '' 对象没有属性 ''

AttributeError: '...' 对象没有属性 '...'

AttributeError 对象没有属性

收到错误AttributeError:'FlashDisplayPage'对象没有属性'label_name'

Selenium w / Python3-AttributeError:“ str”对象没有属性“ tag_name”

AttributeError:'ManyToManyField'对象没有属性'_m2m_reverse_name_cache'

Python Selenium Webdriver:AttributeError'list'对象没有属性'tag_name'

如何修复AttributeError:'NoneType'对象的CNN没有属性'original_name_scope'?

flask-mongoengine AttributeError:“ BaseQuerySet”对象没有属性“ service_name”

AttributeError:“ str”对象在定义类和调用它时没有属性“ name”

AttributeError:“函数”对象没有属性“ func_name”和python 3

AttributeError:输入层级联期间,“模型”对象没有属性“ _name”

Python 3导入错误AttributeError:从C ++调用脚本时,'_ModuleLock_'对象没有属性'name'

AttributeError: 'Person' 对象没有属性 'name' 错误请帮帮我

AttributeError:“列表”对象没有属性“对象”