tensorflow基本组件
- 使用图 (graph) 来表示计算任务
- 在被称之为会话 (Session) 的上下文 (context) 中执行图
- 使用 tensor 表示数据
- 通过变量 (Variable) 维护状态
- 使用 feed 和 fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据
import tensorflow as tf a=tf.constant(2) b=tf.constant(3) with tf.Session() as sess: print("a:%i" % sess.run(a) , "b:%i" % sess.run(b)) print("Addition with constants: %i" % sess.run(a+b)) print("Multiplication with constant: %i" % sess.run(a*b)) print ("使用变量Variable构造计算图a,b") a = tf.placeholder(tf.int16) b = tf.placeholder(tf.int16) add = tf.add(a,b) mul = tf.multiply(a,b) with tf.Session() as sess: print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})) print("Addition with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})) print("\n" + "构造计算图") matrix1=tf.constant([[3.,3.]]) matrix2=tf.constant([[2.],[2.]]) product=tf.matmul(matrix1,matrix2) with tf.Session() as sess: result = sess.run(product) print(result)