我要添加两个我认为是“重塑”在一起的向量,并得到一个二维矩阵.我期望这里出现某种类型的错误,但没有得到.我想我了解发生了什么,将它们视为水平和垂直都有两个以上的向量集,但我不明白为什么a和b的结果没有不同.如果不是要这么做,为什么还要这么做呢?
import tensorflow as tf
import numpy as np
start_vec = np.array((83,69,45))
a = tf.expand_dims(start_vec, 0)
b = tf.expand_dims(start_vec, 1)
ab_sum = a + b
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
a = sess.run(a)
b = sess.run(b)
ab_sum = sess.run(ab_sum)
print(a)
print(b)
print(ab_sum)
=================================================
[[83 69 45]]
[[83]
[69]
[45]]
[[166 152 128]
[152 138 114]
[128 114 90]]
解决方法:
实际上,这个问题更多地利用了张量流的广播特性,
与numpy(Broadcasting)相同.广播摆脱了张量之间的运算形状必须相同的要求.当然,它还必须满足某些条件.
General Broadcasting Rules:
When operating on two arrays, NumPy
compares their shapes element-wise. It starts with the trailing
dimensions, and works its way forward. Two dimensions are compatible
when1.they are equal, or
2.one of them is 1
一个简单的例子是一维张量乘以标量.
import tensorflow as tf
start_vec = tf.constant((83,69,45))
b = start_vec * 2
with tf.Session() as sess:
print(sess.run(b))
[166 138 90]
回到问题所在,tf.expand_dims()的功能是在指定轴位置将尺寸标注插入张量形状.您的原始数据形状为(3,).当设置轴= 0时,您将获得a = tf.expand_dims(start_vec,0)的形状为(1,3).您将获得b = tf.expand_dims(start_vec,1)的形状为(3,1 ),当您设置的轴= 1时.
通过比较广播规则,可以看到它们满足第二个条件.所以他们的实际操作是
83,83,83 83,69,45
69,69,69 + 83,69,45
45,45,45 83,69,45