Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import statments when using Tensorflow contrib keras

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..

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.

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.

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.

like image 327
bivouac0 Avatar asked Oct 18 '22 04:10

bivouac0


2 Answers

Try this with recent versions of tensorflow:

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, TimeDistributed, Dense, ...
like image 69
Soren Avatar answered Nov 15 '22 07:11

Soren


Try with tensorflow.contrib.keras.python.keras:

from tensorflow.contrib.keras.python.keras.models import Sequential
like image 38
Julio Daniel Reyes Avatar answered Nov 15 '22 05:11

Julio Daniel Reyes