Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'set_random_seed' from 'tensorflow' (C:\Users\polon\Anaconda3\lib\site-packages\tensorflow\__init__.py)

Good day,

Here is the error. Can somebody help how can i solve it?

ImportError                               Traceback (most recent call last)
<ipython-input-18-c29f17706012> in <module>
      7 import numpy as np
      8 import numpy.random as nr
----> 9 from tensorflow import set_random_seed
     10 import matplotlib.pyplot as plt
     11 get_ipython().run_line_magic('matplotlib', 'inline')

ImportError: cannot import name 'set_random_seed' from 'tensorflow' (C:\Users\polon\Anaconda3\lib\site-packages\tensorflow\__init__.py)

Looked for similar problems on Stack, but nothing worked for me.

like image 900
poloniki Avatar asked Oct 31 '19 07:10

poloniki


3 Answers

In Tensoflow2 there is no need to perform

from tensorflow import set_random_seed

in order to run

set_random_seed(x)

(as it was in older version)

Only have to run

import tensorflow
tensorflow.random.set_seed(x)

Thanks to @David Buck

like image 135
poloniki Avatar answered Oct 06 '22 07:10

poloniki


I too faced same error but instead of from tensorflow import set_random_seed, I've used

import tensorflow as tf
tf.random.set_seed() 

And it worked I think that method is useful for version 1 and the above snippet is useful for version 2

like image 5
kamma rahul Avatar answered Oct 06 '22 06:10

kamma rahul


This code works for me:

from numpy.random import seed
seed(1)
from tensorflow import random
random.set_seed(1)

I got the same result of my neural network model every time.

like image 2
vcaquilpan Avatar answered Oct 06 '22 05:10

vcaquilpan