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.