.detach().cpu().numpy()的作用

讨论 多洛莉丝
Lv2 初级炼丹师
发布在 NumPy   2337   0
讨论 多洛莉丝   2337   0
    out = model(inputs)
    ls.append(out.detach().cpu().numpy())

    out是device:CUDA得到的CUDA tensor。关于detach()的官方文档如下:

    Returns a new Tensor, detached from the current graph.
    The result will never require gradient.

    返回一个new Tensor,只不过不再有梯度。

    如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor

    所以得写成.cpu().numpy()

    版权声明:作者保留权利,不代表意本站立场。如需转载请联系本站以及作者。

    参与讨论

    回复《 .detach().cpu().numpy()的作用

    EditorJs 编辑器

    沙发,很寂寞~
    反馈
    to-top--btn