keras中module ‘keras.utils‘ has no attribute ‘to_categorical‘解决方案

项目场景:

在运行环境为:python 3.9.7,tensorflow 2.6.0,keras 2.6.0模型训练中报错

问题描述

当代码编辑如下时,运行中总会报出module ‘keras.utils’ has no attribute ‘to_categorical’的错误

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

keras中module ‘keras.utils‘ has no attribute ‘to_categorical‘解决方案

原因分析:

to_categorical的依赖包引用格式错误导致无法加载运行

解决方案:

import tensorflow as tf

y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年4月6日 下午6:22
下一篇 2022年4月6日 下午6:28

相关推荐