Session

import tensorflow as tf 
import numpy as np 

# 去掉警告信息
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# create two matrixes
matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1, matrix2) 

# method 1
sess = tf.Session()
result = sess.run(product)
print(result)  # [[12]]

# method 2
with tf.Session() as sess:
    print(sess.run(product)) # [[12]]


# 扩展
# numpy 中的矩阵相乘
matrix3 = np.mat([[3, 3]])
matrix4 = np.mat([[2], [2]])
print(np.dot(matrix3, matrix4)) # [[12]]

你可能感兴趣的:(Session)