close

How to import keras from tf.keras in Tensorflow?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to import keras from tf.keras in Tensorflow in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to import keras from tf.keras in Tensorflow?

  1. How to import keras from tf.keras in Tensorflow?

    You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3

  2. import keras from tf.keras in Tensorflow

    You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3

Method 1

Use the keras module from tensorflow like this:

import tensorflow as tf

Import classes

from tensorflow.python.keras.layers import Input, Dense

or use directly

dense = tf.keras.layers.Dense(...)

EDIT Tensorflow 2

from tensorflow.keras.layers import Input, Dense

and the rest stays the same.

Method 2

Try from tensorflow.python import keras

with this, you can easily change keras dependent code to tensorflow in one line change.

You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3

Edited: for tensorflow 1.10 and above you can use import tensorflow.keras as keras to get keras in tensorflow.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read