我正在尝试从Datalab实例中的TensorFlow contrib包导入Keras(我使用的是Tensorflow 1.1.0版本和Python 2.7)
from tensorflow.contrib.keras.python.keras.regularizers import l1,l2
from tensorflow.contrib.keras.python.keras.models import Sequential, load_model
from tensorflow.contrib.keras.python.keras.layers import Dense, Dropout, Activation
from tensorflow.contrib.keras.python.keras.constraints import max_norm
from tensorflow.contrib.keras.python.keras.optimizers import RMSprop, Adam
from tensorflow.contrib.keras.python.keras.layers.normalization import BatchNormalization
但是我收到以下错误:
ImportErrorTraceback (most recent call last)
<ipython-input-52-adf3061b6b33> in <module>()
1 #from tensorflow.contrib.keras import *
----> 2 from tensorflow.contrib.keras.python.keras.regularizers import l1,l2
3 from tensorflow.contrib.keras.python.keras.models import Sequential, load_model
4 from tensorflow.contrib.keras.python.keras.layers import Dense, Dropout, Activation
5 from tensorflow.contrib.keras.python.keras.constraints import max_norm
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/__init__.py in <module>()
24
25 # pylint: disable=wildcard-import
---> 26 from tensorflow.contrib.keras.api.keras import *
27
28 try:
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/api/keras/__init__.py in <module>()
23 from __future__ import print_function
24
---> 25 from tensorflow.contrib.keras.api.keras import activations
26 from tensorflow.contrib.keras.api.keras import applications
27 from tensorflow.contrib.keras.api.keras import backend
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/api/keras/activations/__init__.py in <module>()
20
21 # Activation functions.
---> 22 from tensorflow.contrib.keras.python.keras.activations import elu
23 from tensorflow.contrib.keras.python.keras.activations import hard_sigmoid
24 from tensorflow.contrib.keras.python.keras.activations import linear
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/keras/python/keras/__init__.py in <module>()
19 from __future__ import print_function
20
---> 21 from tensorflow.contrib.keras.python.keras import activations
22 from tensorflow.contrib.keras.python.keras import applications
23 from tensorflow.contrib.keras.python.keras import backend
ImportError: cannot import name activations
我尝试将Tensorflow升级到1.2.1版本,但它仍无效。
为什么我收到此错误的任何想法?谢谢!
答案 0 :(得分:1)
我将回答我自己的问题:似乎每次重新启动实例时,安装在Datalab实例中的python包都被重置为默认包(我还没有发现为什么会发生这种情况),所以我正在执行代替1.0.0 tensorflow版本的代码。我刚刚在一个单元格中写道:
!pip install tensorflow==1.1.0
我重新启动了Datalab内核(重启会话按钮)。之后,一切正常,我可以从Tensorflow contrib包中导入Keras。