[吃药深度学习随笔] 张量 计算图 会话

张量(tensor):即多为数组/列表

阶:即为张量的维数

张量可以表示0阶到n阶的多维数组

例子:

import tensorflow as tf
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])

result = a+b
print(result)

得到结果

Tensor("add:0", shape=(2,), dtype=float32)

计算图(Graph):搭建神经网络的计算过程,只搭建,不运算,运算在会话(Session)里

(计算图,其中X1和X2分别乘上权重得到Y)

例子:

import tensorflow as tf
a = tf.constant([[1.0, 2.0]])
b = tf.constant([[3.0], [4.0]])

y = tf.matmul(a, b)
print(y)

其中 tf.matmul 是矩阵相乘的函数

得到结果:Tensor("MatMul:0", shape=(1, 1), dtype=float32)

 意思为(矩阵输出号:0,形状(1x1的矩阵),数据类型:32位浮点)

会话(Session):执行一个图中的节点运算

例子:

import tensorflow as tf
a = tf.constant([[1.0, 2.0]])
b = tf.constant([[3.0], [4.0]])

y = tf.matmul(a, b)
print(y)

with tf.Session() as sess:
    print (sess.run(y))

其中

with tf.Session() as sess:
    print (sess.run(y))

等价于

sess = tf.Session()
print (sess.run(y))
sess.close

with的作用在于在使用完会话后自动Close

得到结果:[[11.]]

意思是:一个1x1矩阵 元素为11

验算:1*3+2*4 = 11 计算正确 为矩阵乘法

猜你喜欢

转载自www.cnblogs.com/EatMedicine/p/9029209.html