这段代码占用大量内存:
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();
}
为什么会这样?