How to iterate model parameter linearly?

I want to give every parameter a probability to change like this

    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() < 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() < 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() < faultProbability:
                                            model.state_dict()[layerName[i]][k0][k1][k2][k3] \
                                                = bitFlip(layerWeight[k0][k1][k2][k3], m)

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.

you can iterate in way mentioned above for any network.

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
for i in range(3): for j in range(3): #do change to parameter
And this is nonlinear in my opinion.
But the problem with that is that the code has to change according to torch.size().