【发布时间】:2019-06-27 23:38:44
【问题描述】:
我刚开始在 spyder 中使用 tensorflow 和 keras。我试图运行一个张量流示例:https://www.tensorflow.org/tutorials/keras/basic_classification。但是当我杀死控制台并再次运行我的代码时,似乎找不到 keras 模块并显示下面的错误
ImportError: cannot import name 'keras'
我已经在我的 anaconda 上安装了 keras 和 tensorflow。我在 spyder 上使用 Windows 10 运行它。我在stackoverflow上看到的另一个答案是安装我已经完成的keras。我尝试安装并重新安装它,它可以工作,但是在我杀死内核后,错误再次出现。
我尝试删除并重新安装 tensorflow 和 keras,它可以工作,但同样的问题不断出现。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(300, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
#-----------------MAKING PREDICTIONS
predictions = model.predict(test_images)
predictions[0]
我希望每次我杀死内核或控制台时控制台都会找到 keras 模块。我的模型也卡在了 0.1 的精度,这可能与错误无关,但示例显示精度高于 0.8
【问题讨论】:
-
(此处是 Spyder 维护者)请阅读our guide,其中解释了如何解决此问题。
-
您使用的是哪个 Python 版本?一定有某种 Python 解释器错误。
标签: tensorflow module keras spyder