【发布时间】:2021-06-09 15:43:38
【问题描述】:
我有以下代码:
import numpy as np
import torch
y = torch.ones((1000,10)) #This is the output of a neural network which does not matter here
theta_min = 0; theta_max = np.pi; K = 10; sigma = 10;
z = torch.zeros(y.shape)
for i in range(0,y.shape[0]):
theta = np.random.uniform(theta_min, theta_max)
vector = np.reshape(np.exp(-1j * np.arange(0,K) * np.pi * np.sin(theta)),(-1,1))
vector = torch.tensor(vector)
alpha = sigma * np.random.randn()
z[i,:] = alpha * vector @ vector.T @ y[i,:].T
如何避免循环以使代码更快?
【问题讨论】:
-
你能做一个可重现的例子吗?您可能只需要将向量复制到矩阵并让 alpha 绘制矩阵。
-
是的,我也将
y更改为矩阵。这不是向量,那是个错误
标签: python performance matrix pytorch