Setting .data of Tensor doesn't change it

The below statements doesn’t change the values in opt_weights[tr == kth].data

opt_weights[tr == kth].data = dist.sample_n(opt_weights[tr == kth].shape[0])
opt_weights[tr == kth].data = dist.sample_n(opt_weights[tr == kth].shape[0]).data
opt_weights[tr == kth].data.copy_(dist.sample_n(opt_weights[tr == kth].shape[0]))

I don’t get an error in above cases but as I said they’re not helpful in changing values.

When I try to change it without .data part, I get the following.
*** RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.

What is the right way of overwriting certain values in a tensor with samples of a distribution?

I have managed to do it via index_copy_ , is this the right solution?, (tr == kth).nonzero().flatten(), dist.sample_n(opt_weights[tr == kth].shape[0]))


In general, you should never use .data otherwise you might get unexpected behaviors like that.

The other error comes from the fact that you modify a leaf Tensor inplace. You can add a opt_weights = opt_weights.clone() before this code to make sure you don’t modify the original Tensor.

1 Like