当我运行这段代码时:
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
行也会给予错误。
1条答案
按热度按时间pvabu6sv1#
执行
from tensorflow import keras
并尝试运行print(keras.__version__)
。如果它不起作用,您可能应该卸载Keras和TensorFlow,然后重新安装这两个库的更新版本。