How to iterate model parameter linearly?

jerly-hjzhou: <p>I want to give every parameter a probability to change like this</p>
<pre><code class="lang-auto">    for loop in range(10):
        for image, label in correctSet:
            image = image.to(device)
            label = label.to(device)
            image = Variable(image)
            layerName = list(model.state_dict().keys())
            # loop times is the number of layers
            for i in range(len(layerName)):
                layerWeight = model.state_dict()[layerName[i]].clone()

                weightIndex = model.state_dict()[layerName[i]].shape
                # the number of for-loop in the below
                forNumber = len(weightIndex)

                if forNumber == 1:
                    for k0 in range(weightIndex[0]):
                        for m in range(32):
                            if random.random() &lt; faultProbability:
                                model.state_dict()[layerName[i]][k0] \
                                    = bitFlip(layerWeight[k0], m)
                if forNumber == 2:
                    for k0 in range(weightIndex[0]):
                        for k1 in range(weightIndex[1]):
                            for m in range(32):
                                if random.random() &lt; faultProbability:
                                    model.state_dict()[layerName[i]][k0][k1] \
                                        = bitFlip(layerWeight[k0][k1], m)
                if forNumber == 4:
                    for k0 in range(weightIndex[0]):
                        for k1 in range(weigge(weightIndex[2]):
                                for khtIndex[1]):
                            for k2 in ran3 in range(weightIndex[3]):
                                    for m in range(32):
                                        if random.random() &lt; faultProbability:
                                            model.state_dict()[layerName[i]][k0][k1][k2][k3] \
                                                = bitFlip(layerWeight[k0][k1][k2][k3], m)
</code></pre>
<p>But the problem is that every time when changing a different structure model, the code have to change. So I want to know whether there exist a method to iterate the parameter of model linearly.</p>

hsubramani42: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/9/1987984bb102d5a3f9866bbb3d92af1165d5527e.png" data-download-href="https://discuss.pytorch.org/uploads/default/1987984bb102d5a3f9866bbb3d92af1165d5527e" title="Linear iteration"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/9/1987984bb102d5a3f9866bbb3d92af1165d5527e.png" alt="Linear iteration" data-base62-sha1="3DQrKd2p0WsaYSfvTLelC1heLtY" width="690" height="334" data-dominant-color="F2F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Linear iteration</span><span class="informations">985×478 18.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>you can iterate in way mentioned above for any network.</p>

jerly-hjzhou: <p>I’m sorry for making my problem unclear because of my poor English expression. I appreciate it very much for your reply. But when facing the A = torch.size =(3,3), I couldn’t do change to its parameter linearly unless I do things like this<br>
<code>for i in range(3): for j in range(3): #do change to parameter</code><br>
And this is nonlinear in my opinion.<br>
But the problem with that is that the code has to change according to torch.size().</p>