tf.expand_dims对向量究竟有什么作用?即使矩阵形状不同,为什么也可以将结果相加? [英] What exactly does tf.expand_dims do to a vector and why can results be added together even when matrix shapes are different?

查看:129
本文介绍了tf.expand_dims对向量究竟有什么作用?即使矩阵形状不同,为什么也可以将结果相加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加两个我认为是重塑"在一起的向量,并得到一个二维矩阵.我期望这里出现某种类型的错误,但没有得到.我想我了解发生了什么,将它们视为水平和垂直都有两个以上的向量集,但是我不明白为什么a和b的结果没有不同.如果不是故意的话,为什么这样行不通?

I'm adding two vectors that I thought were 'reshaped' together and getting a 2d matrix as a result. I expect some type of error here, but didn't get it. I think I understand what's happening, it treated them as if there were two more sets of each vector horizontally and vertically, but I don't understand why the results of a and b aren't different. And if they're not meant to be, why does this work at all?

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消除了张量之间的运算形状必须相同的要求.当然,它还必须满足某些条件.

In fact, this question makes more use of broadcasting characteristics of tensorflow, which the same as numpy (Broadcasting). Broadcasting gets rid of the requirement that the operation shape between tensors must be the same. Of course, it must also meet certain conditions.

一般广播规则:

General Broadcasting Rules:

在两个数组上操作时,NumPy 比较它们的形状元素.它从尾随开始 尺寸,并向前发展.二维兼容 什么时候

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 when

1.它们相等,或者

2.其中一个是1

一个简单的例子是一维张量乘以标量.

A simple example is one-dimensional tensors multiplied by scalars.

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()的功能是在张量的形状中的指定axis位置处插入尺寸.您的原始数据形状为(3,).设置为axis=0时将得到a=tf.expand_dims(start_vec, 0)的形状为(1,3).设置为axis=1时将得到b=tf.expand_dims(start_vec, 1)的形状为(3,1)

Back to the question, the function of tf.expand_dims() is to insert a dimension into a tensor’s shape at the specified axis position. Your original data shape is (3,). You will get the shape of a=tf.expand_dims(start_vec, 0) is (1,3) when your set axis=0.You will get the shape of b=tf.expand_dims(start_vec, 1) is (3,1) when your set axis=1.

通过比较broadcasting的规则,您可以看到它们满足第二个条件.所以他们的实际操作是

By comparing the rules of broadcasting, you can see that they satisfy the second condition. So their actual operation is

83,83,83     83,69,45
69,69,69  +  83,69,45
45,45,45     83,69,45

这篇关于tf.expand_dims对向量究竟有什么作用?即使矩阵形状不同,为什么也可以将结果相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆