- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
其中e_i
代表欧氏空间的第i
标准基。有没有一种有效的方法来计算这个?我正在使用以下 for 循环和 Kruskal-tensor ktensor
来使用 tensor toolbox 计算它由桑迪亚国家实验室管理:
x=ktensor({c,c,c});
I=eye(d);
for i=1:d
x=x+2*c(i)*ktensor({I(:,i),I(:,i),I(:,i)}
end
for i=1:d
for j=1:d
x=x- c(i)*c(j)*(ktensor({I(:,i),I(:,i),I(:,j)})+ktensor({I(:,i),I(:,j),I(:,i)})+ktensor({I(:,i),I(:,j),I(:,j)}))
end
end
最佳答案
这是一种可能性。
c
的值沿张量的“对角线”放置。bsxfun
似乎是合适的。bsxfun
,但由于结果有点稀疏,如果矩阵的大小很大,您可能会受益于“手动”填充它。代码如下:
dim = 10;
c = [1:dim]';
e = eye(dim);
x = zeros([dim, dim, dim]);
% initialize with second term
x(1:dim*(dim+1)+1:end) = 2 * c;
% add first term
x = x + bsxfun(@times, bsxfun(@times, c, shiftdim(c, -1)), shiftdim(c, -2));
% add third term
x = x - sum(sum(bsxfun(@times, shiftdim(c*c',-3), ...
bsxfun(@times, bsxfun(@times, permute(e, [1, 3, 4, 2, 5]), permute(e, [3, 1, 4, 2, 5])), permute(e, [3, 4, 1, 5, 2])) +...
bsxfun(@times, bsxfun(@times, permute(e, [1, 3, 4, 2, 5]), permute(e, [3, 1, 4, 5, 2])), permute(e, [3, 4, 1, 2, 5])) +...
bsxfun(@times, bsxfun(@times, permute(e, [1, 3, 4, 5, 2]), permute(e, [3, 1, 4, 2, 5])), permute(e, [3, 4, 1, 2, 5]))), 5), 4);
编辑
第三项的更有效(尤其是内存方面)计算:
ec = bsxfun(@times, e, c);
x = x - ...
bsxfun(@times, ec, shiftdim(c, -2)) -...
bsxfun(@times, c', reshape(ec, [dim, 1, dim])) -....
bsxfun(@times, c, reshape(ec, [1, dim, dim]));
关于matlab - 计算张量的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44829751/
我想矢量化以下代码: 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.
我是一名优秀的程序员,十分优秀!