Reputation:
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]) ??
Upvotes: 5
Views: 3689
Reputation: 15105
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?
Upvotes: 2