gpt4 book ai didi

python - Keras后端json定义为tensorflow,但是Keras仍然找不到tensorflow

转载 作者:行者123 更新时间:2023-11-30 08:54:15 25 4
gpt4 key购买 nike

我已将 keras 的 json 文件更改为以下内容:

{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}

但是当我为神经网络运行以下简单的 Keras 教程时:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, input_dim=20, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(10, init='uniform'))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])

model.fit(X_train, y_train,
nb_epoch=20,
batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)

取自:https://keras.io/getting-started/sequential-model-guide/

我仍然收到以下错误:

Using TensorFlow backend.
Traceback (most recent call last):
File "./keras_test", line 3, in <module>
from keras.models import Sequential
File "/usr/local/lib/python2.7/dist-packages/keras/__init__.py", line 2, in <module>
from . import backend
File "/usr/local/lib/python2.7/dist-packages/keras/backend/__init__.py", line 67, in <module>
from .tensorflow_backend import *
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 1, in <module>
import tensorflow as tf
ImportError: No module named tensorflow

我不知道问题可能是什么,所以我们将不胜感激。

最初我认为这可能是 python 版本控制问题。由于我对 python 编码和 Linux 有点陌生,所以我一直在安装/升级所有 python 版本而没有真正思考,我只希望 Keras 能够使用与我的 Tensorflow 后端相同的 python 版本(这应该是 anaconda3 使用的那个)。事后看来,我真的应该使用 virtualenv,但我不确定这是否是我的问题(只是试图提供尽可能多的信息)。

我使用以下方式安装了 Keras:

 sudo pip install keras

如果我再次运行它,我会得到以下输出:

Requirement already satisfied (use --upgrade to upgrade): keras in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): theano in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): pyyaml in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.11 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

此外,我 100% 确定我的 Tensorflow 安装有效,因为我已经运行(并编写了)一些 GPU-Cuda 示例。

谢谢!

最佳答案

我认为你忘记了最明显的事情,TensorFlow 没有安装,它不是 Keras 依赖项。我建议您安装 TensorFlow:

pip install --user tensorflow

这会将 TensorFlow 安装在我们的用户文件夹 (~/.local) 中,并且不需要 root 权限。

关于python - Keras后端json定义为tensorflow,但是Keras仍然找不到tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com