Unexpected results after normalization torch==0.4

I want to normalize the input data by subtracting the 6th value. However, the results are unexpected.

import numpy as np
import torch

a = np.array([[[  -29.8316,   506.7845,  5400.1382],
         [ -155.6416,    73.0718,  5448.8071],
         [  -52.9619,  -309.7045,  5251.0830],
         [ -300.4998,  -332.3928,  5156.6812],
         [ -258.2405,    99.6090,  5244.6816],
         [ -209.4844,   548.8338,  5290.7637],
         [ -176.7308,  -321.0486,  5203.8818],
         [ -109.1576,  -529.7281,  5123.8906],
         [ -140.1912,  -780.1214,  5074.6050],
         [ -118.9348,  -970.2283,  5058.5991],
         [   13.8925,  -279.8529,  5421.0684],
         [   35.7916,  -470.1449,  5257.7383],
         [  -19.7603,  -716.9181,  5140.2725],
         [ -259.0900,  -690.1336,  5050.5923],
         [ -370.6709,  -448.5993,  5134.1772],
         [ -462.2866,  -290.8295,  5307.6274],
         [ -153.1819,  -886.9761,  5130.1655]],

        [[  -29.6097,   507.0160,  5400.0415],
         [ -156.1013,    73.2300,  5446.2266],
         [  -52.8395,  -309.0523,  5247.8511],
         [ -299.8065,  -331.7188,  5151.9609],
         [ -258.0193,    99.6480,  5243.2402],
         [ -209.7421,   548.7822,  5290.6899],
         [ -176.3229,  -320.3856,  5199.9062],
         [ -108.0304,  -530.1070,  5123.3218],
         [ -140.6482,  -779.8441,  5071.7817],
         [ -118.2912,  -970.1854,  5056.8403],
         [   10.7354,  -281.1750,  5414.3501],
         [   40.3466,  -470.5802,  5251.2056],
         [  -19.6162,  -718.2531,  5137.8745],
         [ -258.3210,  -688.7090,  5046.1133],
         [ -368.4557,  -443.3939,  5120.0488],
         [ -460.3479,  -281.6299,  5289.6309],
         [ -153.4338,  -886.6000,  5127.5791]],

        [[  -29.7913,   507.1184,  5400.0073],
         [ -156.4464,    73.1930,  5444.3994],
         [  -52.3020,  -308.4679,  5245.2900],
         [ -299.1556,  -330.9422,  5149.0630],
         [ -258.1010,   100.1828,  5241.8062],
         [ -210.1292,   549.2088,  5290.5708],
         [ -175.7287,  -319.7050,  5197.1768],
         [ -107.6006,  -531.0508,  5125.0386],
         [ -142.8517,  -779.9221,  5071.1001],
         [ -118.7543,  -970.0274,  5056.5713],
         [   25.9413,  -271.5096,  5396.5942],
         [   45.9570,  -469.2388,  5242.0957],
         [  -21.8357,  -718.7902,  5137.6470],
         [ -258.9276,  -687.5615,  5042.6875],
         [ -361.8696,  -434.6036,  5099.1748],
         [ -458.3741,  -250.1555,  5240.7217],
         [ -154.4211,  -886.6248,  5127.2637]]])


a = torch.from_numpy(a)
a -= a[:,7:8,:]
print(a)

output

tensor([[[   79.3260,  1036.5126,   276.2476],
         [  -46.4840,   602.7999,   324.9165],
         [   56.1957,   220.0236,   127.1924],
         [ -191.3422,   197.3353,    32.7906],
         [ -149.0829,   629.3371,   120.7910],
         [ -100.3268,  1078.5619,   166.8731],
         [  -67.5732,   208.6795,    79.9912],
         [    0.0000,     0.0000,     0.0000],
         [ -140.1912,  -780.1214,  5074.6050], #<---here
         [ -118.9348,  -970.2283,  5058.5991],
         [   13.8925,  -279.8529,  5421.0684],
         [   35.7916,  -470.1449,  5257.7383],
         [  -19.7603,  -716.9181,  5140.2725],
         [ -259.0900,  -690.1336,  5050.5923],
         [ -370.6709,  -448.5993,  5134.1772],
         [ -462.2866,  -290.8295,  5307.6274],
         [ -153.1819,  -886.9761,  5130.1655]],

        [[   78.4207,  1037.1230,   276.7197],
         [  -48.0709,   603.3370,   322.9048],
         [   55.1909,   221.0547,   124.5293],
         [ -191.7761,   198.3882,    28.6391],
         [ -149.9889,   629.7550,   119.9184],
         [ -101.7117,  1078.8892,   167.3681],
         [  -68.2925,   209.7214,    76.5844],
         [    0.0000,     0.0000,     0.0000],
         [ -140.6482,  -779.8441,  5071.7817],
         [ -118.2912,  -970.1854,  5056.8403],
         [   10.7354,  -281.1750,  5414.3501],
         [   40.3466,  -470.5802,  5251.2056],
         [  -19.6162,  -718.2531,  5137.8745],
         [ -258.3210,  -688.7090,  5046.1133],
         [ -368.4557,  -443.3939,  5120.0488],
         [ -460.3479,  -281.6299,  5289.6309],
         [ -153.4338,  -886.6000,  5127.5791]],

        [[   77.8093,  1038.1692,   274.9687],
         [  -48.8458,   604.2438,   319.3608],
         [   55.2986,   222.5829,   120.2514],
         [ -191.5550,   200.1086,    24.0244],
         [ -150.5004,   631.2336,   116.7676],
         [ -102.5286,  1080.2596,   165.5322],
         [  -68.1281,   211.3458,    72.1382],
         [    0.0000,     0.0000,     0.0000],
         [ -142.8517,  -779.9221,  5071.1001],
         [ -118.7543,  -970.0274,  5056.5713],
         [   25.9413,  -271.5096,  5396.5942],
         [   45.9570,  -469.2388,  5242.0957],
         [  -21.8357,  -718.7902,  5137.6470],
         [ -258.9276,  -687.5615,  5042.6875],
         [ -361.8696,  -434.6036,  5099.1748],
         [ -458.3741,  -250.1555,  5240.7217],
         [ -154.4211,  -886.6248,  5127.2637]]], dtype=torch.float64)

Oh, this looks like a bug using the inplace subtraction.
If you use this code, it seems b has the right result:

a = torch.from_numpy(a)
b = a - a[:, 7:8, :]
a -= a[:,7:8,:]
print(a)
print(b)

I was able to reproduce this issue in 1.0.0a0+dfad8b6 and 1.0.0.dev20181007.
Thanks for catching it! Could you create an issue in Gihhub?

1 Like

Okay, sorry for the late reply.