问题描述
我有一堆使用Keras编写的代码,这些代码是作为单独的pip安装安装的,并且导入语句的编写方式类似于from keras.models import Sequential
等.
I have a bunch of code written using Keras that was installed as a separate pip install and the import statements are written like from keras.models import Sequential
, etc..
在新机器上,我安装了Tensorflow,现在在 contrib 目录中包括Keras.为了保持版本的一致性,我认为最好使用 contrib 中的功能,而不是单独安装Keras,但这会导致一些导入问题.
On a new machine, I have Tensorflow installed which now includes Keras inside the contrib directory. In order to keep the versions consistent I thought it would be best to use what's in contrib instead of installing Keras separately, however this causes some import issues.
我可以使用import tensorflow.contrib.keras as keras
导入Keras,但是做类似from tensorflow.contrib.keras.models import Sequential
的操作会给出 ImportError:没有名为模型的模块,而from keras.models import Sequential
提供了类似的 ImportError:没有名为keras的模块.型号.
I can import Keras using import tensorflow.contrib.keras as keras
but doing something like from tensorflow.contrib.keras.models import Sequential
gives ImportError: No module named models, and from keras.models import Sequential
gives a similar ImportError: No module named keras.models.
是否有一种简单的方法来使from x.y import z
语句起作用?如果不是这样,则意味着更改所有实例以使用冗长的命名(即m1 = keras.models.Sequential()
),这不是我的首选语法,但是是可行的.
Is there a simple method to get the from x.y import z
statements to work? If not it means changing all the instances to use the verbose naming (ie.. m1 = keras.models.Sequential()
) which isn't my preferred syntax but is do-able.
推荐答案
尝试使用最新版本的tensorflow:
Try this with recent versions of tensorflow:
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, TimeDistributed, Dense, ...
这篇关于使用Tensorflow contrib keras时的导入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!