我正在尝试在 Pytorch 中矢量化以下 for 循环。我很高兴只对内部 for 循环进行矢量化,但完成整个批次也很棒。
# B: the batch size
# N: the number of training examples
# dim: the dimension of each feature vector
# K: the number of discrete labels. each vector has a single label
# delta: margin for hinge loss
batch_data = torch.tensor(...) # Tensor of shape [B x N x d]
batch_labels = torch.tensor(...) # Tensor of shape [B x N x 1], each element is one of K labels (ints)
batch_losses = …Run Code Online (Sandbox Code Playgroud)