零维张量和项有什么区别?

时间:2019-11-03 16:02:02

标签: c++ pytorch libtorch

这段代码占用大量内存:

int len = Input.size(0);
at::Tensor outputs = torch::zeros({ len });
for (int SliceStart = 0; SliceStart < len; SliceStart += SliceSize)
{
  std::vector<torch::jit::IValue> InputVec;
  InputVec.push_back(Input.slice(0, SliceStart, SliceEnd));
  output = module.forward(InputVec).toTensor();
  for (int i = SliceStart; i < SliceEnd; ++i)
    outputs[i] = output[i - SliceStart];
}

不可以(请注意上一次作业中的item()

int len = Input.size(0);
at::Tensor outputs = torch::zeros({ len });
for (int SliceStart = 0; SliceStart < len; SliceStart += SliceSize)
{
  std::vector<torch::jit::IValue> InputVec;
  InputVec.push_back(Input.slice(0, SliceStart, SliceEnd));
  output = module.forward(InputVec).toTensor();
  for (int i = SliceStart; i < SliceEnd; ++i)
    outputs[i] = output[i - SliceStart].item();
}

为什么会这样?

0 个答案:

没有答案