tensotflow代码实战2_Session()会话控制_简单样例

说明:资料来源于莫烦老师教程:

https://www.youtube.com/watch?v=HhjtJ73AwIY&list=PLXO45tsB95cKI5AIlf5TxxFPzb-0zeVZ8&index=10

一、代码 

import tensorflow as tf

matrix1 = tf.constant([[3,3]])#一行两列的矩阵 ,即1x2的矩阵
matrix2 = tf.constant([[2],
                       [2]])#两行一列的矩阵   ,即2x1的矩阵
product = tf.matmul(matrix1,matrix2)#matrix multiply ;与numpy中的np.dot(m1,m2)一样的作用

#method 1
sess = tf.Session()
result = sess.run(product)#每run一下,tensorflow就执行一下括号里的结构
print(result)#打印出:[[12]]
sess.close()

#method 2
with tf.Session() as sess:#这种写法就不用主动将sess进行close了,会自动关闭
	result2 = sess.run(product)
	print(result2)
#do something #运行到这里时候,其实就已经把上面的Sess关闭了 




二、运行结果

猜你喜欢

转载自blog.csdn.net/Johnisjohn/article/details/90139346