samedi 25 avril 2020

How to add different random values to n elements of a numpy array?

I am trying to add random values to a specific amount of values in a numpy array to mutate weights of my neural network. For example, 2 of the values in this array

[ [0 1 2]
  [3 4 5]
  [6 7 8] ]

are supposed to be mutated (i. e. a random value between -1 and 1 is added to them). The result may look something like this then:

[ [0   0.7 2]
  [3   4   5]
  [6.9 7   8]]

I would prefer a solution without looping, as my real problem is a little bigger than a 3x3 matrix and looping usually is inefficient. I have thought about creating a list containing n numbers, then random coordinates for these numbers, then using some tf.scatter_nd function equivalent for numpy and then adding this new matrix to the original one. I assume there must be a far more elegant way to solve this problem, I'd appreciate your help!




Aucun commentaire:

Enregistrer un commentaire