1. 程式人生 > >tensorflow中 session() 和 graph的關係

tensorflow中 session() 和 graph的關係

自己的理解

一般我們都是先建立graph ,然後在建立一個 session,將圖載入進去
最近自己在學習tensorflow過程中,發現我們將圖載入到會話裡面後,可以繼續基於載入的圖 繼續構造圖

例子

import tensorflow as tf
import numpy as np
a=tf.constant([[1,2],[3,5]])
b=tf.constant([[1,2],[3,4]])
c=tf.add(a,b)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer(
)) print(sess.run(c)) d=tf.matmul(a,b) print(sess.run(d)) 輸出: [[2 4] [6 9]] [[ 7 10] [18 26]]