首先我们载入tensorflow模块,如果需要,可以查看一下当前tf的版本
import tensorflow as tf
print(tf.__version__)
tensorflow是一种图计算框架,所有的计算操作被声明为图(graph)中的节点(Node)
即使只是声明一个变量或者常量,也并不执行实际的操作,而是向图中增加节点
a = tf.constant(5)
b = tf.constant(7)
print(a)
print(a)
c = tf.add(a, b)
print(c)
d = a + b
e = a*b
print(d)
print(e)
sess = tf.Session()
a,b,c,d,e = sess.run([a,b,c,d,e])
print("a=",a," b=",b," c=",c," d=",d," e=",e)
mat_a = tf.constant([[1,1,1],[3,3,3]])
mat_b = tf.constant([[2,2,2],[5,5,5]],name='mat_b')
mul_a_b = mat_a * mat_b
tf_mul_a_b = tf.multiply(mat_a, mat_b)
tf_matmul_a_b = tf.matmul(mat_a, tf.transpose(mat_b),name='matmul_with_name')
sess2 = tf.Session()
mul_a_b,tf_mul_a_b,tf_matmul_a_b = sess2.run([mul_a_b,tf_mul_a_b,tf_matmul_a_b])
print(" mul_a_b=",mul_a_b)
print(" tf_mul_a_b=",tf_mul_a_b)
print(" tf_matmul_a_b=",tf_matmul_a_b)
graph是一张有向无环图,在tensorflow中,通常用protobuf进行持久化
this_graph = tf.get_default_graph()
this_graph_def = this_graph.as_graph_def()
print(this_graph_def)
tensorflow constant
最新推荐文章于 2023-10-12 10:26:12 发布