几种Session的会话控制

191 阅读1分钟
import tensorflow as tf
matrix1 = tf.constant([[3,3]])
#恒量,一行两列
matrix2 = tf.constant([[2],[2]])
#恒量,两行一列
product = tf.matmul(matrix1,matrix2)
# 对matrix1 和matriax2 进行一个矩阵的乘法,matul是矩阵乘法函数
# matual 相当于numpy中的np.dot(m1,m2)

几种会话的控制:

  • method 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()
#注意这个close有没有都可以,原则上是应该有的因为这样整体结构更完整
  • method 2
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)
    #这种情况不用关闭Sessinon,因为到最后就自动关上了

输出结果如下: