我在从tensorflow.keras.utils导入to_categorical时遇到问题

问题描述 投票:0回答:1

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.python import keras
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Conv2D, Flatten, MaxPooling2D
from tensorflow.python.keras.utils import to_categorical

我收到以下错误:无法在“init.py”中找到引用“to_categorical”`

我尝试了多种导入方式,但都没有成功。据我所知,我已经安装了所有必需的软件包,为什么它不起作用?

python tensorflow keras
1个回答
0
投票

该函数存在于

tensorflow.keras.utils
中,而不是
tensorflow.python.keras.utils
中。

尝试:

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.python import keras
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Conv2D, Flatten, MaxPooling2D
from tensorflow.keras.utils import to_categorical
© www.soinside.com 2019 - 2024. All rights reserved.