import tensorflow as tf
# 一行两列
matrix1 = tf.constant([[1, 0]])
# 两行一列
matrix2 = tf.constant([[1], [0]])
# 矩阵相乘
matmul = tf.matmul(matrix1, matrix2)
# 打印结果 (Tensor而不是结果 2)
print(matmul)
Tensor("MatMul:0", shape=(1, 1), dtype=int32)
# 定义会话
sess = tf.Session()
# 真正开始执行
print(sess.run(matmul))
sess.close()
[[1]]
# with ... as ...的写法,可以保证 session会自动关闭
with tf.Session() as sess:
print(sess.run(matmul))
[[1]]