I am learning tensorflow and i have two questions first is , is it necessery to run each opeation in session ? like if i create a simple program in which there are three operations addition and substraction and matrix multiply then is it necessary to run all those operation in session ?
import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6],[5,6,7]],dtype="float32")
b=np.array([[7,8,9],[9,6,5],[6,7,8]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)
mul=tf.matmul(a,b)
with tf.Session() as rr:
rr.run(inn)
rr.run(ab)
res=rr.run(mul)
print(res)
so now i have to run each operation (add,sub and matmul) in session ??
if "yes" i have to run then can i run all those operation together ? i tried but i got error :
first i tried this :
import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6],[5,6,7]],dtype="float32")
b=np.array([[7,8,9],[9,6,5],[6,7,8]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)
mul=tf.matmul(a,b)
with tf.Session() as rr:
rr.run(inn,ab,add,sub,mul)
rr.run(ab)
res=rr.run(mul)
print(res)
i got this error :
TypeError: run() takes from 2 to 5 positional arguments but 6 were given
so i removed one argument (mul) from run then i got this error :
raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
how i can run all operation once or i have to run each individually ?
second during writing this code i tried to multiply two matrix with shape [2,3] my program was :
import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6]],dtype="float32")
b=np.array([[7,8,9],[9,6,5]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)
mul=tf.matmul(a,b)
with tf.Session() as rr:
rr.run(inn)
rr.run(ab)
res=rr.run(mul)
print(res)
i got this error :
ValueError: Dimensions must be equal, but are 3 and 2 for 'MatMul' (op: 'MatMul') with input shapes: [2,3], [2,3].
how multiply a=([1,2,3],[4,5,6]) b= ([9,8,7],[6,4,3]) ??
To answer the first question, yes, everything runs in a session. You define a graph and then the session is when the graph gets compiled, loaded into tensorflow internals and executed the state of all the variables is kept as long as the session is open.
Your code is a bit strange though. You define one of your inputs as a constant, but the other one as a variable but the variable one never changes. But at the very basic level, you can run multiple operations in a session by passing multiple operations as a list. Here is how:
a=np.array([[1,2,3],[4,5,6]],dtype="float32")
b=np.array([[7,8,9],[9,6,5]],dtype="float32")
tfa = tf.constant(a)
tfb = tf.constant(b)
add = tfa + tfb
sub = tfa - tfb
with tf.Session() as s:
res_add, res_sub = s.run([add, sub])
print(res_add)
print(res_sub)
and the output is
[[ 8. 10. 12.]
[ 13. 11. 11.]]
[[-6. -6. -6.]
[-5. -1. 1.]]
Your matrix multiplication is not going to work, because inner dimensions have to match. Does this answer your question, or were you trying to do something else?
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)
I just installed TensorFlow using conda install tensorflowfrom the anaconda prompt. I'm using Python 3.6 on Windows 10.
I thought I'd try it out with something simple, like
rnd_ints = tf.random_normal([10], dtype=tf.float64)
When I call rnd_ints all I get is this:
<tf.Tensor 'random_normal:0' shape=(10,) dtype=float64>
I thought I was supposed to be getting an array object of some sort?
From the documentation:
A Tensor is a symbolic handle to one of the outputs of an Operation.
It does not hold the values of that operation's output, but instead
provides a means of computing those values in a TensorFlow tf.Session.
This class has two primary purposes:
A Tensor can be passed as an input to another Operation. This builds a
dataflow connection between operations, which enables TensorFlow to
execute an entire Graph that represents a large, multi-step
computation.
After the graph has been launched in a session, the value of the
Tensor can be computed by passing it to tf.Session.run. t.eval() is a
shortcut for calling tf.get_default_session().run(t).
The answer to your question: when you call tf.random_normal() you create a Tensor object, which does not have actual values stored. In order to get an output you'll need to run it inside a session. Here's how you can get an actual output:
import tensorflow as tf
rnd_ints = tf.random_normal([10], dtype=tf.float64)
with tf.Session() as sess:
rnd = sess.run(rnd_ints)
print(rnd)
# [-1.59628093 0.62648824 0.18566968 0.2274149 1.27171951 -0.18103614
# -2.05964716 0.37477217 0.3355942 -1.57350681]
I'm currently running in a problem where tensorflow doesn't produce the results I intended to get. When I tried to debug the problem I noticed that up to a division everything seems to work out fine.
https://imgur.com/a/DT4RWiS
Can someone enlighten me what might be happening here?
There might be some stochasticity involved. Consider the following example:
import tensorflow as tf
a = tf.random_uniform(shape=(4,), minval=1, maxval=2)
b = tf.random_uniform(shape=(4,), minval=1, maxval=2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(a.eval()/b.eval())
print((a/b).eval())
When a.eval() and (a/b).eval() are called, tf.random_uniform is executed and tensor a is filled with different random numbers.
This is because the / operator in tensorflow follows the Python 2 syntax and performs an integer division (see https://www.tensorflow.org/api_docs/python/tf/div). You should use tf.divide instead. In general for any mathematical operation between tensors you should use tf.operation_name
I'm new at Tensorflow. I am having a litte trouble at understanding its constants. I have this simple code mentioned below:
import tensorflow as tf
vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
with tf.Session() as sess:
v = sess.run(vector)
argm = tf.argmax(v,1)
print(argm)
I expect this to return something like [4,7,8], as I understood from the documentation. Instead, I get this:
Tensor("ArgMax:0", shape=(3,), dtype=int64).
So, i don't know what am I doing wrong.
Alternatively to the answer of #James, you might want to use tensorflow's eager execution, which behaves more like "standard" python: operations are executed as you type them, no more graphs and Session.
import tensorflow as tf
tf.enable_eager_execution()
vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
argm = tf.argmax(vector,1)
print(argm)
Tensorflow operations like tf.argmax somewhat unintuitively don't perform the operation that they're stating, but add an operation to a graph that will be performed. When you run argm = tf.argmax(v,1), the return value is tensor that isn't evaluated yet.
If you want the result of the argmax operation, you can run something like this:
import tensorflow as tf
vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
argm = tf.argmax(vector,1)
with tf.Session() as sess:
a = sess.run(argm)
print(a)
With this code, we're explicitly asking Tensorflow to run the computations to compute the result of the tf.argmax operation. With your previous code, we ran the computation to compute v (which is a constant, so that's pretty quick), then define a new graph operation to compute argmax on that - but never actually do the computation.
I cannot understand Tensorflow system.
First,I wrote
#coding:UTF-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1,const2)
with tf.Session() as sess:
result = sess.run(add_op)
print(result)
and it print out 5.
Second,I wrote
#coding:UTF-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1,const2)
print(add_op)
and it print out Tensor("Add:0", shape=(), dtype=int32).
I cannot understand this system.
I use Python and other languages, so I think tf.add() method is add method.However,in the case of Tensorflow,it seems different.
why is this part
with tf.Session() as sess:
result = sess.run(add_op)
print(result)
necessary?
What functions does this part have?
I would suggest to read the official Getting Started with TensorFlow guide of TensorFlow to get to know the core concepts of the library, such as the one which seems to be the problem here:
Every TensorFlow program consists of two parts:
Building the computational graph.
Running the computational graph.
Now, what is a "computational graph"? In TensorFlow, you specify a series of operations which are executed on your input. This series of operations is your "computational graph". To understand that, lets look at some examples:
Simple addition: let's look at your example, your code is
const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1,const2)
This creates two constant nodes in the graph, and creates a second node which adds them. Graphically, this looks like:
To make it a little bit more complex, lets say you have an input x and want to add a constant 3 to it. Then your code would be:
const1 = tf.constant(2)
x = tf.placeholder(tf.float32)
add_op = tf.add(const1,x)
and your graph is
In both examples, this was the first part of the program. So far, we only defined how our computational graph should look, i.e. what inputs we have, what outputs, and all calculations needed.
But: no calculations have been done so far! In the second example, you don't even know what your input x is - only that it will be a float32.
If you have a GPU, you'll notice that TensorFlow hasn't even touched the GPU yet. Even if you have a huge neural network with millions of training images, this step runs in milliseconds, as no "real" work has to be done.
Now comes part two: running the graph we defined above. Here's where the work happens!
We fire up TensorFlow by creating a tf.Session, and then we can run anything by calling sess.run().
with tf.Session() as sess:
result = sess.run(add_op)
print(result)
In the second example, we now have to tell TensorFlow what our value x should be:
with tf.Session() as sess:
result = sess.run(add_op, {x: 5.0})
print(result)
tl;dr: every TensorFlow program has two parts: 1. building a computational graph, and 2. running this graph. With tf.add you only define the graph, but no addition is performed yet. To run this graph, use sess.run() as in your first piece code.