I write a simple code with the tensorflow toolkit as follow:
import tensorflow as tf
import numpy as np
if __name__=="__main__":
inp = np.random.randint(1,3,(1,20,300,1))
inputs = tf.convert_to_tensor(inp,dtype=tf.float32)
with tf.variable_scope('convpool1') as scope:
kernel = tf.get_variable('weights',[1,300,1,1],initializer=tf.truncated_normal_initializer(stddev=5e-2,dtype=tf.float32))
conv1 = tf.nn.conv2d(inputs,kernel,[1,1,1,1],padding='VALID')
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
with tf.variable_scope('convpool1'):
k_ = sess.run(kernel)
c1_ =sess.run(conv1)
It works when I run this code first time,but when I run the same again,it raises an Error:
Variable convpool1/weights already exists, disallowed. Did you mean to set reuse=True in VarScope?
And I restart the IDE(spyder), run this code,it works again.What could be the origin of this error and how i can resolve it? Thanks for your time!
Using
tf.reset_default_graph()
at the beginning should resolve your problem. I am not 100% sure but suspect spyder still have the Graph object (and thus the var scope is already created).
Related
I face problems when trying to get a numpy array from a tensorflow tensor. I use a tensorflow hub module but I don't want to use tensorflow in downstream tasks but rather need a numpy array.
I know that I have to call the 'eval()' method on the tensor from within a tensorflow session. But unfortuantely I cannot get it to work... :( It tells me that the "tables are not initialized". I tried to add 'sess.run(tf.tables_initializer())' but then I get the error: 'NotFoundError: Resource localhost/module_1/embeddings_morph_specialized/class tensorflow::Var does not exist'. I am not sure what to try next. I have also tried 'sess.run()' but have also been unsuccessful.
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
embed = hub.Module("https://public.ukp.informatik.tu-darmstadt.de/arxiv2018-xling-sentence-embeddings/tf-hub/monolingual/1")
X = embed(["This is a test."])
# I tried:
#with tf.Session() as sess:
# sess.run(tf.tables_initializer())
# X.eval()
'X' is the tensor which I would like to convert to a numpy array.
Any help is appreciated. :)
Thank you.
Unfortunately, tf_hub modules are not yet supported in eager mode except in tf 2 (which is still in beta and I think needs slightly different hub modules anyway).
Therefore you'll need to run this in a session.
Something like:
embed = hub.Module("https://public.ukp.informatik.tu-darmstadt.de/arxiv2018-xling-sentence-embeddings/tf-hub/monolingual/1")
X = embed(["This is a test."])
with tf.Session() as session:
session.run([tf.global_variables_initializer(), tf.tables_initializer()])
numpy_arr = session.run(X)
So I'm trying to run a training session, and when I do I get this error when trying to run my algorithm (when I use tf.train.get_global_step()):
ValueError: global_step is required for exponential_decay.
For some reason, tf.train.get_or_create_global_step() doesn't exist for me, I'm not sure if that's because it's a removed method or what. I updated TensorFlow and everything I'm up to date.
I've dug around the documentation and there's nothing about it. To run I'm using tf.app.run() with a main function.
Is there another way to initialize the global step variable?
Although tf.train.get_or_create_step() is perfectly fine, here is another solution:
g_step = tf.get_variable('global_step', trainable=False, initializer=0)
learning_rate = tf.train.exponential_decay(0.1, g_step)
tf.train.AdamOptimizer(learning_rate).minimize(loss=loss, global_step=g_step)
Create an untrainable variable that initializes with zero and passes it to the Optimizer.
If you need global_step later use tf.train.global_step():
sess = tf.Session()
# Initialize the variable
sess.run(g_step.initializer)
print('global_step: %s' % tf.train.global_step(sess, g_step))
So, the reason this function wasn't showing up was because I actually hadn't been on the newest version of TensorFlow even though it was telling me I was completely up to date.
Seen Here:
So all I did to fix it was uninstall tensorflow, then install from the actual link I don't have it anymore, but a quick google search would suffice.
In the following code, "a" works perfectly fine, and "c" also works. But "b" causes an error. Could someone explain the reason?
#!/usr/bin/python
import tensorflow as tf
import numpy as np
a = tf.Variable(tf.random_normal((10, 4)))
b = tf.constant(tf.random_normal((10, 4)))
c = tf.constant(np.random.randn(10, 4))
I am also a new one who start using tensorflow. I believe that there is something wrong with your variable type. According to the tensorflow API, you should feed a constant or list of value to 'tf.constant()'. However, in you code, before you initialize the variables and run this session, 'tf.random_normal()' is something like a placeholder without any real meaning. You can try to run this code. I am not sure if I understand this problem and I would like to discuss with you.
import tensorflow as tf
a = tf.random_normal((10, 4))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
b = tf.constant(sess.run(a))
print(sess.run(b))
Today I noticed some strange behaviour in Tensorflow and thought I'd ask here to understand what's happening. My problem revolves around tf.control_dependencies not making the specified operator being run before the operators I define inside the with block. What I am asking here is not how to compute the performance metrics (I coded that manually), but rather where my misconception lies.
So, to set the scene. Today, I made some code to log performance metrics during training of a CNN, and I was using the tensorflow.metrics module for this. However, the operators in this module cumulate the previous results (so performance metrics can be computed for very large datasets). I want to log how the metrics evolve over time as the network train, so I don't want this behaviour. Therefore, I wrapped the creation of these performance metrics nodes in a tf.control_dependencies, forcing (or so I thought) a tf.local_variables_initialiser to be evaluated before my performance metrics is computed. Thus, my code could look like this
import tensorflow as tf
import numpy as np
labels = tf.convert_to_tensor(np.arange(10))
out = tf.convert_to_tensor(np.random.randn(10, 1))
with tf.control_dependencies([tf.local_variables_initializer()]):
_, precision = tf.metrics.precision(labels, out)
with tf.Session() as sess:
#sess.run(tf.local_variables_initializer())
print(sess.run(precision))
but when I try to run the above code, I get the following error
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value precision_4/true_positives/count
[[Node: precision_4/true_positives/AssignAdd = AssignAdd[T=DT_FLOAT, _class=["loc:#precision_4/true_positives/count"], use_locking=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](precision_4/true_positives/count, precision_4/true_positives/Sum)]]
now, I have encountered this error many times while I tried to understand the metrics module and the reason for it is that I have not initialised my variables properly. Therefore, I tested this code
import tensorflow as tf
import numpy as np
labels = tf.convert_to_tensor(np.arange(10))
out = tf.convert_to_tensor(np.random.randn(10, 1))
with tf.control_dependencies([tf.local_variables_initializer()]):
_, precision = tf.metrics.precision(labels, out)
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
print(sess.run(precision))
which does indeed work.
So my question remains. Why is the tf.local_variables_initializer() node not ran before the performance metrics are computed in my first code example?
This is indeed really strange. I guess you need to place
_, precision = tf.metrics.precision(labels, out)
before the control_dependencies like
import tensorflow as tf
import numpy as np
labels = tf.convert_to_tensor(np.arange(10))
out = tf.convert_to_tensor(np.random.randn(10, 1))
_, _precision = tf.metrics.precision(labels, out)
with tf.control_dependencies([tf.local_variables_initializer()]):
# precision = tf.identity(_precision)
precision = 1 * _precision
with tf.Session() as sess:
print(sess.run(precision))
This works like expected as the local variables of tf.metrics.precision do exist before calling tf.local_variables_initializer. In your code, the tf.local_variables_initializer is executed before the node precision. Hence, precision_4/true_positives/count cannot exists and therefore not initialized, simply because the graph is not existing.
To make it even more strange (which seems to be a bug):
Placing precision = 1 * precision in the body of control_dependencies works. But precision = tf.identity(precision) does not.
This is a good candidate for a bug in TensorFlow.
I was trying to understand the mechanism of tensorflow for reading images using queues. I was using the code found here, whom basic parts are:
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once('D:/Dataset/*.jpg'))
image_reader = tf.WholeFileReader()
image_name, image_file = image_reader.read(filename_queue)
image = tf.image.decode_jpeg(image_file)
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
image_tensor = sess.run([image])
print(image_tensor)
which in reality does nothing special. I was getting an error:
OutOfRangeError (see above for traceback): FIFOQueue
'_0_input_producer' is closed and has insufficient elements (requested
1, current size 0)
which lead to search for missing images, wrong folder, wrong glob pattern etc until I discovered that tensorflow basically meant this:
"You need to initialize local variables also"!
Besides the fact that the code seemed to work in the original gist with just this substitution:
tf.initialize_all_variables().run()
instead of
tf.global_variables_initializer().run()
in my code it does not work. It produces the same error. I guess it has changed the implementation of initialize_all_variables() with tensorflow development (I am using 1.3.0), since in here it mentions that it initialize local variables also.
So, the final conclusion I came with was that I should initialize local variables also. And my code worked. The error message is awfully misleading (which did not help at all) but anyway to the main part I am a bit confused why am I getting a local variable by match_filenames_once. In documentation there is no reference about this (I am not sure it should though).
Am I always going to get local from this match_filenames_once? Can I control it somehow?