How do I perform multiple operations together on one line in a tensor session?

I am researching tensorflow and I have two first questions: does every session need to do every session? for example, if I am creating a simple program that has three operations: add and subtract and multiply a matrix, then do I need to run all these operations in the 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 every operation (add, pod and matmul) in the session ??

If yes I need to run, can I run all these operations together? I tried, but I got the 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, after which 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 can I run the whole operation once or should I run each separately?

second while writing this code i tried to multiply two matrices with form [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 to multiply a = ([1,2,3], [4,5,6]) b = ([9,8,7], [6,4,3]) ??

+3


source to share


1 answer


To answer the first question, yes, everything works in session. You define a graph and then a session - this is when the graph is compiled, loaded into the internals of the tensorflow and the state of all variables is executed, persists as long as the session is open.

Your code is a little weird. You define one of your inputs as a constant and the other as a variable, but the variable never changes. But at the most basic level, you can run multiple operations in a session by passing multiple operations as a list. Here's 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.]
 [-five. -1. 1.]]

Your matrix multiplication won't work because the inner dimensions must match. Does this answer your question, or are you trying to do something else?

0


source







All Articles