scholarshipPytorch之扩充tensor的操作
我就废话不多说了,⼤家还是直接看代码吧~
b = s((3, 2, 6, 6))
a = s((3, 2, 1, 1))
Out[32]: torch.Size([3, 2, 6, 6])
a = s((3, 2, 2, 1))
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, lf.ur_global_ns, lf.ur_ns)
isolated system
File "<ipython-input-34-972575f79e92>", line 1, in <module>
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 2. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 1] a = s((3, 2, 1, 2))
新概念第一册a.expand_as(b).size()
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, lf.ur_global_ns, lf.ur_ns)
File "<ipython-input-36-972575f79e92>", line 1, in <module>
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton
dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 1, 2] a = s((3, 2, 2, 2))
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, lf.ur_global_ns, lf.ur_ns)
nostalgiaFile "<ipython-input-38-972575f79e92>", line 1, in <module>
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 2] a = s((3, 2, 6, 2))
nawaTraceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, lf.ur_global_ns, lf.ur_ns)
File "<ipython-input-40-972575f79e92>", line 1, in <module>
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 6, 2] a = s((3, 2, 6, 1))
Out[44]: torch.Size([3, 2, 6, 6])
a = s((3, 2, 1, 6))
Out[46]: torch.Size([3, 2, 6, 6])
假设⽬标形状为N, C, H, W,则要求tensor.size()=n, c, h, w(这⾥假设N,C不变):
1、h=w=1
2、h=1, w!=1
3、h!=1, w=1
补充:tensorflow 利⽤expand_dims和squeeze扩展和压缩tensor维度
在利⽤tensorflow进⾏⽂本挖掘⼯作的时候,经常涉及到维度扩展和压缩⼯作。
⽐如对⽂本进⾏embedding操作完成之后,若要进⾏卷积操作,就需要对embedded的向量扩展维度,将[batch_size,
embedding_dims]扩展成为[batch_size, embedding_dims, 1],利⽤tf.expand_dims(input, -1)就可实现,反过来⽤squeeze(input, -1)或者tf.squeeze(input)也可以把最第三维去掉。
拉丁美洲人tf.squeeze()
在第axis位置增加⼀个维度.
给定张量输⼊,此操作在输⼊形状的维度索引轴处插⼊1的尺⼨。尺⼨索引轴从零开始; 如果您指定轴的负数,则从最后向后计数。
如果要将批量维度添加到单个元素,则此操作⾮常有⽤。例如,如果您有⼀个单⼀的形状[height,width,channels],您可以使⽤expand_dims(image,0)使其成为1个图像,这将使形状[1,⾼度,宽度,通道]。
例⼦
# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
tf.squeeze()
胖人衣服tf.squeeze(input, axis=None, name=None, squeeze_dims=None)
直接上例⼦
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
我最尊敬的老师
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。如有错误或未考虑完全的地⽅,望不吝赐教。
>英语单词翻译