python - PyTorch: How to get around the RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables -


with pytorch i'm having problem doing operation 2 variables:

sub_patch  : [torch.floattensor of size 9x9x32]  pred_patch : [torch.floattensor of size 5x5x32] 

sub_patch variable made torch.zeros pred_patch variable of index each of 25 nodes nested for-loop, , multiply corresponding unique filter (sub_filt_patch) of size [5,5,32]. result added respective place in sub_patch.

this piece of code:

for in range(filter_sz):     j in range(filter_sz):          # index correct filter filter tensor         sub_filt_col = (patch_col + j) * filter_sz         sub_filt_row = (patch_row + i) * filter_sz          sub_filt_patch = sub_filt[sub_filt_row:(sub_filt_row + filter_sz), sub_filt_col:(sub_filt_col+filter_sz), :]          # multiply filter , pred_patch , sum onto sub patch         sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] += (sub_filt_patch * pred_patch[i,j]).sum(dim=3) 

the error bottom line of piece of code here is

runtimeerror: in-place operations can used on variables don't share storage other variables, detected there 2 objects sharing 

i why happens, since sub_patch variable, , pred_patch variable too, how can around error? appreciated!

thank you!

i've found problem in

        sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] += (sub_filt_patch * pred_patch[i,j]).sum(dim=3) 

when separating line this:

sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] = sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] + (sub_filt_patch * pred_patch[i,j]).sum(dim=3) 

then worked!

the difference between += b , = + b in first case, b added in inplace (so content of changed contain a+b). in second case, brand new tensor created contains a+b , assign new tensor name a. able compute gradients, need keep original value of a, , prevent inplace operation being done because otherwise, won't able compute gradients.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -