TensorFlow Session使用的两种方法

xiaoxiao2021-02-28  78

TensorFlow Session

在TensorFlow中是通过session进行交互的,使用session有两种方法。下面通过一个简单的例子(两个矩阵相乘)说一下 {[3,1] 与{[5,2] 相乘 [1,2]} [2,4]}

代码

#encoding=utf-8 import tensorflow as tf matrix1 = tf.constant([[3,1],[1,2]]) matrix2 = tf.constant([[5,2],[2,4]]) product = tf.matmul(matrix1,matrix2) #矩阵乘法 #session method 1 sess = tf.Session() result = sess.run(product) print(result) sess.close() #session method 2 with tf.Session() as sess2: result2 = sess2.run(product) print(result2)

结果

参考

本文通过学习莫烦的教程写的,用于自己学习mark一下

转载请注明原文地址: https://www.6miu.com/read-31178.html

最新回复(0)