- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想通过交错索引获得更大的张量 4 个子张量。
这是我的问题:
四个输入,形状为 [batch x 2m x 2n x 1];
输出形状为[batch x 4m x 4n x 1];
index_y_0_1 = [0,1,4,5,8,9...],index_y_2_3 = [2,3,6,7,10,11...]
index_x_0_1 = [0,1,4,5,8,9...],index_x_2_3 = [2,3,6,7,10,11...]
输出[index_y_0_1,index_x_0_1] = 输入0
输出[index_y_0_1,index_x_2_3] = 输入1
输出[index_y_2_3,index_x_0_1] = 输入2
输出[index_y_2_3,index_x_2_3] = 输入3
这是我对 python 代码的问题:
import numpy as np
UpperLeft = np.ones((3,2,4,1))
UpperRight = np.ones((3,2,4,1))*2
BottonLeft = np.ones((3,2,4,1))*3
BottonRight = np.ones((3,2,4,1))*4
output = np.zeros((UpperLeft.shape[0], UpperLeft.shape[1]*2, UpperLeft.shape[2]*2, 1))
assert(output.shape[1]%4 == 0)
assert(output.shape[2]%4 == 0)
# UpperLeft Assignment
start_y = 0
start_x = 0
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = UpperLeft[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = UpperLeft[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = UpperLeft[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = UpperLeft[:,1::2, 1::2, :]
# UpperRight Assignment
start_y = 0
start_x = 2
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = UpperRight[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = UpperRight[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = UpperRight[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = UpperRight[:,1::2, 1::2, :]
# BottonLeft Assignment
start_y = 2
start_x = 0
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = BottonLeft[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = BottonLeft[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = BottonLeft[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = BottonLeft[:,1::2, 1::2, :]
# BottonRight Assignment
start_y = 2
start_x = 2
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = BottonRight[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = BottonRight[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = BottonRight[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = BottonRight[:,1::2, 1::2, :]
show_out = output[0,:,:,0]
我怎样才能在 tensorflow 上做到这一点?谢谢!
最佳答案
您可以拥有一个像这样在一个轴上交错张量的函数。
import tensorflow as tf
# Interlaves tensors across one axis
def interleave(tensors, size=1, axis=-1):
# Reshape tensors
tensors_res = []
for tensor in tensors:
s = tf.shape(tensor)
new_s = tf.concat([s[:axis], [s[axis] // size, size], s[axis:][1:]], axis=0)
tensors_res.append(tf.reshape(tensor, new_s))
# Concatenate across new dimension
if isinstance(axis, (tf.Tensor, tf.Variable)):
selected_axis = tf.cond(tf.less(axis, 0), lambda: axis - 1, lambda: axis)
concat_axis = tf.cond(tf.less(axis, 0), lambda: axis, lambda: axis + 1)
else:
selected_axis = (axis - 1) if axis < 0 else axis
concat_axis = axis if axis < 0 else (axis + 1)
tensors_concat = tf.concat(tensors_res, axis=concat_axis)
# Reshape concatenation
concat_s = tf.shape(tensors_concat)
res_s = tf.concat([concat_s[:selected_axis], [-1], concat_s[concat_axis:][1:]], axis=0)
return tf.reshape(tensors_concat, res_s)
然后您可以使用它首先跨一个维度进行交错,然后跨另一个维度进行交错。
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
# Input data
UpperLeft = tf.ones((3, 2, 4, 1))
UpperRight = tf.ones((3, 2, 4, 1)) * 2
BottomLeft = tf.ones((3, 2, 4, 1)) * 3
BottomRight = tf.ones((3, 2, 4, 1)) * 4
# Interleave across axis 2
Upper = interleave([UpperLeft, UpperRight], size=2, axis=2)
Bottom = interleave([BottomLeft, BottomRight], size=2, axis=2)
# Interleave across axis 1
Result = interleave([Upper, Bottom], size=2, axis=1)
# Show result
print(sess.run(Result)[0, :, :, 0])
# [[1. 1. 2. 2. 1. 1. 2. 2.]
# [1. 1. 2. 2. 1. 1. 2. 2.]
# [3. 3. 4. 4. 3. 3. 4. 4.]
# [3. 3. 4. 4. 3. 3. 4. 4.]]
关于python - 如何将 4 个子张量交错分配给一个更大的张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572447/
我想矢量化以下代码: def style_noise(self, y, style): n = torch.randn(y.shape) for i in range(n.shape[
对于给定的二维张量,我想检索值为 1 的所有索引。我希望能够简单地使用 torch.nonzero(a == 1).squeeze(),它将返回张量([1, 3, 2])。但是,torch.nonze
如果 x 是 dtype torch.float 的 torch.Tensor 那么操作 x.item() 和 float(x)完全一样? 最佳答案 操作x.item() 和float(x) 是不一样
我正在尝试提取 n 点 3D 坐标和 b 批处理中的特定行。本质上,我的张量 T1 的形状为 b*n*3。我有另一个形状为 b * n 的 bool 张量 T2,指示需要获取 n 的哪些行。本质上我的
以下代码掩码很好 mask = targets >= 0 targets = targets[mask] 但是,当我尝试使用两个条件进行屏蔽时,它会给出 RuntimeError: Boolean v
我正在定义一个简单的 conv2d 函数来计算输入和内核(均为 2D 张量)之间的互相关,如下所示: import torch def conv2D(X, K): h = K.shape[0]
作为训练 CNN 的一部分,我正在使用数组 inputs包含 对象。我想轮换一个人一些随机度数的对象 x ,如下所示: def rotate(inputs, x): # Rotate inpu
我有一个索引列表和一个具有形状的张量: shape = [batch_size, d_0, d_1, ..., d_k] idx = [i_0, i_1, ..., i_k] 有没有办法用索引 i_0
假设我有张量 t = torch.tensor([1,2,3,4,5]) 我想使用相同大小的索引张量来拆分它,该张量告诉我每个元素应该进行哪个拆分。 indices = torch.tensor([0
我尝试从生成器构建一个张量,如下所示: >>> torch.tensor(i**2 for i in range(10)) Traceback (most recent call last): F
假设我有一个一维 PyTorch 张量 end_index长度为L。 我想构造一个 2D PyTorch 张量 T有 L 行,其中 T[i,j] = 2什么时候j < end_index[i]和 T[
我在 pytorch 中有一个张量 x 比方说形状 (5,3,2,6) 和另一个形状 (5,3,2,1) 的张量 idx,其中包含第一个张量中每个元素的索引。我想用第二个张量的索引对第一个张量进行切片
我有以下火炬张量: tensor([[-0.2, 0.3], [-0.5, 0.1], [-0.4, 0.2]]) 以及以下 numpy 数组:(如有必要,我可以将其转换为其他内
tf.data.Dataset的构造函数接受一个参数 variant_tensor ,这只是 documented as : A DT_VARIANT tensor that represents t
我有: inp = torch.randn(4, 1040, 161) 我还有另一个名为 indices 的张量值: tensor([[124, 583, 158, 529], [1
我有一个张量 inps ,其大小为 [64, 161, 1]我有一些新数据d大小为 [64, 161] .如何添加 d至inps这样新的大小是[64, 161, 2] ? 最佳答案 使用 .unsqu
我有张量 t = torch.tensor([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) 和一个查询张量 q = torch.te
给定一个 3d 张量,说:batch x sentence length x embedding dim a = torch.rand((10, 1000, 96)) 以及每个句子的实际长度数组(或张
我想使用 [int, -1] 符号 reshape 张量(例如,压平图像)。但我事先并不知道第一个维度。一个用例是在大批量上进行训练,然后在较小的批量上进行评估。 为什么会出现以下错误:获取包含“_M
我有两个 torch 张量。一个形状为 [64, 4, 300],一个形状为 [64, 300]。我如何连接这两个张量以获得形状为 [64, 5, 300] 的合成张量。我知道用于此的 tensor.
我是一名优秀的程序员,十分优秀!