TensorFlow既是一个实现机器学习算法的接口,同时也是执行机器学习算法的框架。
其中Session、Variable和placehold最为基础的知识,今天就来总结一下。

1.会话控制Session

会话层是专门用来计算的,可以用一下两种方式去激活计算图来计算。

import tensorflow as tf
matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],
[2]])
product = tf.matmul(matrix1, matrix2)
# methods 1
sess = tf.Session()
resulit = sess.run(product)
print(resulit)
sess.close

结果为:[[12]]

import tensorflow as tf
matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],
[2]])
product = tf.matmul(matrix1, matrix2)
# methods 2
with tf.Session() as sess:
result = sess.run(product)
print(result)

结果为:[[12]]

Variable变量

下面是在anaconda中的一个demo

TensorFlow:Session、Variable和placehold_会话层

TensorFlow:Session、Variable和placehold_TensorFlow_02


要点

  • 只要使用Variable方法就要初始化,一般用语句​​init_op = tf.initialize_all_variables()​​来初始化所有变量
  • 要在会话层激活初始化的变量,一般为​​sess.run(init_op)​

placeholder占位

TensorFlow:Session、Variable和placehold_Session_03