即使导入了名为“keras”的模块,也不会发生错误

pprl5pva  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(131)

当我运行这段代码时:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 784).astype("float32") / 255.0
x_test = x_test.reshape(-1, 784).astype("float32") / 255.0

model = keras.Sequential(
    [
        layers.Dense(512, activation='relu'),
        layers.Dense(256, activation='relu'),
        layers.Dense(10),
    ]
)

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adam(lr=0.001),
    metrics=["accuracy"],

错误ModuleNotFoundError: No module named 'keras'是由于model = keras.Sequential...行而发生的,但是我认为loss=keras.losses.SparseCategoricalCrossentropy行也会给予错误。

pvabu6sv

pvabu6sv1#

执行from tensorflow import keras并尝试运行print(keras.__version__)。如果它不起作用,您可能应该卸载Keras和TensorFlow,然后重新安装这两个库的更新版本。

相关问题