pythonnumpytensorflownumpy-ufunc

Equivalent for np.add.at in tensorflow


How do I convert a np.add.at statement into tensorflow?

np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D))

Edit

self.dW.shape is (V, D), self.D.shape is (N, D) and self.x.size is N


Solution

  • For np.add.at, you probably want to look at tf.SparseTensor, which represents a tensor by a list of values and a list of indices (which is more suitable for sparse data, hence the name).

    So for your example:

    np.add.at(dW, self.x.ravel(), dout.reshape(-1, self.D))
    

    that would be (assuming dW, x and dout are tensors):

    tf.sparse_add(dW, tf.SparseTensor(x, tf.reshape(dout, [-1])))
    

    This is assuming x is of shape [n, nDims] (i.e. x is a 'list' of n indices, each of dimension nDims), and dout has shape [n].