Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • L LRP tutorial
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Merge requests 2
    • Merge requests 2
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Metrics
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Jobs
  • Commits
Collapse sidebar
  • Gregoire Montavon
  • LRP tutorial
  • Merge requests
  • !2

PyTorch: Migrate deprecated Tensor .data to .detach()

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open rodrigobdz requested to merge rodrigobdz/lrp-tutorial:migrate-deprecated-tensor-data-to-detach into main Nov 27, 2021
  • Overview 0
  • Commits 1
  • Pipelines 0
  • Changes 2

The attribute .data was deprecated in PyTorch version v0.4.0 (see quoted changelog below), .detach() should be used instead.

.data was the primary way to get the underlying Tensor from a Variable. [...] However, .data can be unsafe in some cases. Any changes on x.data wouldn't be tracked by autograd, and the computed gradients would be incorrect if x is needed in a backward pass.

A safer alternative is to use x.detach(), which also returns a Tensor that shares data with requires_grad=False, but will have its in-place changes reported by autograd if x is needed in backward.

Additionally, .data is not officially documented, whereas .detach() is, and can break in future releases.

Edited Nov 28, 2021 by rodrigobdz
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: migrate-deprecated-tensor-data-to-detach