我正在使用远程GPU并在jupyter笔记本中编写代码。导入keras时,我遇到“没有名为'google'的模块的错误。我进行了大量搜索,但全部徒劳。以下是导入库的列表:
我已更新anaconda环境conda更新conda,但问题仍然存在。我在远程GPU服务器上使用UBUNTU LTS 16.04,而在本地计算机上使用UBUNTU LTS 18.04。
import numpy as np
import keras
from keras import backend as K
from keras.models import Model
from keras.models import Sequential
from keras.layers import Activation
from keras.layers import Dropout
from keras.layers.core import Dense, Flatten
from keras.optimizers import Adam
from keras.optimizers import Nadam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import *
from sklearn.metrics import confusion_matrix, roc_curve, auc
from scipy import interp
from itertools import cycle
from sklearn.datasets import make_classification
from sklearn.preprocessing import label_binarize
import itertools
import matplotlib.pyplot as plt
%matplotlib inline
以下是错误
--------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-aeb0232d91c1> in <module>
1 import numpy as np
----> 2 import keras
3
4 from keras import backend as K
5 from keras.models import Model
/usr/local/lib/python3.5/dist-packages/keras/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/usr/local/lib/python3.5/dist-packages/keras/utils/__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7
8 # Globally-importable utils.
/usr/local/lib/python3.5/dist-packages/keras/utils/conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py in <module>
87 elif _BACKEND == 'tensorflow':
88 sys.stderr.write('Using TensorFlow backend.\n')
---> 89 from .tensorflow_backend import *
90 else:
91 # Try and load external backend.
/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in <module>
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.framework import ops as tf_ops
7 from tensorflow.python.training import moving_averages
/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py in <module>
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25
26 try:
/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py in <module>
57
58 # Protocol buffers
---> 59 from tensorflow.core.framework.graph_pb2 import *
60 from tensorflow.core.framework.node_def_pb2 import *
61 from tensorflow.core.framework.summary_pb2 import *
/usr/local/lib/python3.5/dist-packages/tensorflow/core/framework/graph_pb2.py in <module>
4 import sys
5 _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
----> 6 from google.protobuf import descriptor as _descriptor
7 from google.protobuf import message as _message
8 from google.protobuf import reflection as _reflection
ImportError: No module named 'google'
寻找解决方案,如果有人可以帮助我,谢谢您
答案 0 :(得分:0)
我不确定为什么会出现此错误。我运行了您的代码。对我来说很好。如果您想以Jupyter以外的Python脚本运行它,只需注释“%matplotlib inline”。
答案 1 :(得分:0)
在Mac OS上-安装tensorflow 1.3-它会自动删除其他protobuf安装并安装protobuf 3.4。但是,这不起作用,也无法安装或降级到其他任何protobuf版本。
但是我找到了解决方案。 不知道为什么会这样-但是在Mac OS上就解决了。
pip install google
在Windows中检查是否已安装。
$ pip list | grep -i keras
Keras (1.1.0)
使用并污染系统范围的Python是一个坏主意。我建议使用virtualenv(请参阅本指南)。
最好的用法是创建一个virtualenv
目录(例如,在您的家中),并将您的虚拟环境存储在以下位置:
cd virtualenv/
virtualenv -p python3.5 py-keras
source py-keras/bin/activate
pip install -q -U pip setuptools wheel
然后安装Keras:
pip install keras
您得到:
$ pip list
Keras (1.1.0)
numpy (1.11.2)
pip (8.1.2)
PyYAML (3.12)
scipy (0.18.1)
setuptools (28.3.0)
six (1.10.0)
Theano (0.8.2)
wheel (0.30.0a0)
但是,您还需要安装额外的库,例如Tensorflow:
$ python -c "import keras"
Using TensorFlow backend.
Traceback (most recent call last):
...
ImportError: No module named 'tensorflow'
点击此链接以安装必要的软件包。.https://www.tensorflow.org/install