dimanche 28 avril 2019

Random Index from a Tensor (Sampling with Replacement from a Tensor)

I'm trying to manipulate individual weights of different neural nets to see how their performance degrades. As part of these experiments, I'm required to sample randomly from their weight tensors, which I've come to understand as sampling with replacement (in the statistical sense). However, since it's high-dimensional, I've been stumped by how to do this in a fair manner. Here are the approaches and research I've put into considering this problem:

  • This was previously implemented by selecting a random layer and then selecting a random weight in that layer (ignore the implementation of picking a random weight). Since layers are different sizes, we discovered that weights were being sampled unevenly.

  • I considered what would happen if we sampled according to the numpy.shape of the tensor; however, I realize now that this encounters the same problem as above.

    Consider what happens to a rank 2 tensor like this:

    [[*, *, *],
     [*, *, *, *]]
    
    

    Selecting a row randomly and then a value from that row results in an unfair selection. This method could work if you're able to assert that this scenario never occurs, but it's far from a general solution.

    Note that this possible duplicate actually implements it in this fashion.

  • I found people suggesting flattening the tensor and use numpy.random.choice to select randomly from a 1D array. That's a simple solution, except I have no idea how to invert the flattened tensor back into its original shape. Further, flattening millions of weights would be a somewhat slow implementation.

  • I found someone discussing tf.random.multinomial here, but I don't understand enough of it to know whether it's applicable or not.

  • I ran into this paper about resevoir sampling, but again, it went over my head.

  • I found another paper which specifically discusses tensors and sampling techniques, but it went even further over my head.


Any help understanding how to do this? I'm working in Python with Keras, but I'll take an algorithm in any form that it exists. Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire