Tensorflow之Session

Tensorflow有两种Session的运行方式,案例如下:

import tensorflow as tf
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],
                      [2]])
product = tf.matmul(matrix1,matrix2)#matrix multiply 类似numpy中的点乘np.dot(m1,m2)
#method 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close() #close最好加上
[[12]]
# method 2
with tf.Session() as sess: #循环内部的语句会自动close
    result2 = sess.run(product)
print(result2)
[[12]]

猜你喜欢

转载自blog.csdn.net/gracejpw/article/details/104108595