Tensorflow使用初体验:Session

Tensorflow使用初体验:Session

Session会话是tensorflow里面的重要机制,tensorflow构建的计算图必须通过Session会话才能执行,如果只是在计算图中定义了图的节点但没有使用Session会话的话,就不能运行该节点。

TensorFlow中使用会话的模式一般有两种: 

  1. 明确调用会话生成函数和关闭会话函数。
  2. 通过Python的上下文管理器来使用会话
# -*- coding: utf-8 -*-

import tensorflow as tf

matrix1 = tf.constant([[5,3]])
matrix2 =tf.constant([[6],[2]])

product = tf.matmul(matrix1 ,matrix2)
 
#method 1
sess = tf.Session()
result =sess.run(product)

print(result)
sess.close()
 
# method 2
with tf.Session() as sess:
    result2 =sess.run(product)
    print(result2)
    
    
    

运行结果如下:做矩阵的点乘运算:5*6+3*2 =36

[[36]]
[[36]]

猜你喜欢

转载自blog.csdn.net/duan_zhihua/article/details/81022676
今日推荐