import tensorflow as tf
Creating graph
import tensorflow as tf
my_graph = tf.Graph()
with my_graph.as_default():
a = tf.constant([50], name = 'my_const_a')
Here we have added one constant. Now we will create session and see the result.
import tensorflow as tf
my_graph = tf.Graph()
with my_graph.as_default():
a = tf.constant([50], name = 'my_const_a')
my_sess = tf.Session(graph = my_graph)
result = my_sess.run(a)
print(result)
sess.close()
Output
[50]
import tensorflow as tf
my_graph = tf.Graph()
with my_graph.as_default():
a = tf.constant([12], name = 'my_const_a')
b = tf.constant([13], name = 'my_const_b')
c=tf.add(a,b)
sess = tf.Session(graph = my_graph)
result = sess.run(c)
print(result)
sess.close()
Output
[25]
We will get shape of different dimensions
import tensorflow as tf
my_graph = tf.Graph()
with my_graph.as_default():
my_scalar = tf.constant(5)
my_vector = tf.constant([1,2,3])
my_matrix = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
my_tensor = tf.constant( [ [[1,2,3],[4,5,6],[7,8,9]] ,
[[4,5,6],[1,2,3],[7,8,9]] , [[7,8,9],[4,5,6],[1,2,3]] ] )
print(my_scalar.shape)
print(my_vector.shape)
print(my_matrix.shape)
print(my_tensor.shape)
Output
()
(3,)
(3, 3)
(3, 3, 3)
Now let us multiply two matrix by using matmul()
import tensorflow as tf
my_graph = tf.Graph()
with my_graph.as_default():
my_matrix1=tf.constant([[1,2,3],[4,5,6],[7,8,9]])
my_matrix2=tf.constant([[11,12,13],[14,15,16],[17,18,19]])
my_result=tf.matmul(my_matrix1,my_matrix2)
with tf.Session(graph = my_graph) as my_sess:
result=my_sess.run(my_result)
print(result)
Output
[[ 90 96 102]
[216 231 246]
[342 366 390]]
conda create -n tensorflow_env tensorflow
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.