Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with setting TensorFlow as the session in Keras

Performing the following:

from keras import backend as K
sess = tf.Session()
K.set_session(sess)

Even though I've imported Keras and TensorFlow correctly, I get the following:

module 'keras.backend' has no attribute 'set_session'

Any help would be appreciated!

like image 667
MaxR Avatar asked Dec 19 '16 15:12

MaxR


3 Answers

Requires .compat.v1. after the tf

Example:

tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config));

(Tested in Anaconda, Python 3.7, tensorflow 2.0.0)

like image 182
leenremm Avatar answered Oct 14 '22 03:10

leenremm


I think you need this :

from keras.backend.tensorflow_backend import set_session

Use it like this :

from keras import backend as K
K.tensorflow_backend.set_session(sess)

Works fine for me

like image 21
Pusheen_the_dev Avatar answered Oct 14 '22 03:10

Pusheen_the_dev


Try this,

import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.callbacks import ModelCheckpoint

# Set the config values 
config = tf.ConfigProto(intra_op_parallelism_threads=<NUM_PARALLEL_EXEC_UNITS>, 
inter_op_parallelism_threads=2, allow_soft_placement=True, device_count = {'CPU': 
<NUM_PARALLEL_EXEC_UNITS> })

#Create the session
session = tf.Session(config=config)
tf.keras.backend.set_session(session)
like image 1
Ratheesh A - Intel Avatar answered Oct 14 '22 05:10

Ratheesh A - Intel