How do I convert a np.add.at statement into tensorflow?
np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D))
Edit
s
Here's an example of what np.add.at
does:
In [324]: a=np.ones((10,))
In [325]: x=np.array([1,2,3,1,4,5])
In [326]: b=np.array([1,1,1,1,1,1])
In [327]: np.add.at(a,x,b)
In [328]: a
Out[328]: array([ 1., 3., 2., 2., 2., 2., 1., 1., 1., 1.])
If instead I use +=
In [331]: a1=np.ones((10,))
In [332]: a1[x]+=b
In [333]: a1
Out[333]: array([ 1., 2., 2., 2., 2., 2., 1., 1., 1., 1.])
note that a1[1]
is 2, not 3.
If instead I use an iterative solution
In [334]: a2=np.ones((10,))
In [335]: for i,j in zip(x,b):
...: a2[i]+=j
...:
In [336]: a2
Out[336]: array([ 1., 3., 2., 2., 2., 2., 1., 1., 1., 1.])
it matches.
If x
does not have duplicates then +=
works just fine. But with the duplicates, the add.at
is required to match the iterative solution.