python中嵌套字典结构对保持同一结构的字典列表的排列

2024-04-25 18:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下JSON/字典结构。你知道吗

{
    "layers": [{
            "type": "fc",
            "name": "fc1",
            "nodes": [100, 200],
            "activation": "relu",
            "dropout": false
        }, {
            "type": "fc",
            "name": "fc2",
            "nodes": 50,
            "activation": ["relu", "tanh"],
            "dropout": false
        }
    ],
    "hyper_parameters": {
        "dropout": [0.25, 0.30],
        "lr": [0.01, 0.002, 0.5],
        "lr_decay": 1,
        "epochs": 100,
        "epoch_no_imprv": 10,
        "batch_size": 100
    }
}

我想生成上述结构的所有排列,只要有一个列表,就保持相同的结构。 所以上面的结构应该生成24个这样的结构的列表。 显示下面的4个结构示例。你知道吗

[
    {
        "layers": [{
                "type": "fc",
                "name": "fc1",
                "nodes": 100,
                "activation": "relu",
                "dropout": false
            }, {
                "type": "fc",
                "name": "fc2",
                "nodes": 50,
                "activation": "relu",
                "dropout": false
            }
        ],
        "hyper_parameters": {
            "dropout": 0.25,
            "lr": 0.01,
            "lr_decay": 1,
            "epochs": 100,
            "epoch_no_imprv": 10,
            "batch_size": 100
        }
    },
    {
        "layers": [{
                "type": "fc",
                "name": "fc1",
                "nodes": 200,
                "activation": "relu",
                "dropout": false
            }, {
                "type": "fc",
                "name": "fc2",
                "nodes": 50,
                "activation": "relu",
                "dropout": false
            }
        ],
        "hyper_parameters": {
            "dropout": 0.25,
            "lr": 0.01,
            "lr_decay": 1,
            "epochs": 100,
            "epoch_no_imprv": 10,
            "batch_size": 100
        }
    },
    {
        "layers": [{
                "type": "fc",
                "name": "fc1",
                "nodes": 100,
                "activation": "relu",
                "dropout": false
            }, {
                "type": "fc",
                "name": "fc2",
                "nodes": 50,
                "activation": "tanh",
                "dropout": false
            }
        ],
        "hyper_parameters": {
            "dropout": 0.25,
            "lr": 0.01,
            "lr_decay": 1,
            "epochs": 100,
            "epoch_no_imprv": 10,
            "batch_size": 100
        }
    },
    {
        "layers": [{
                "type": "fc",
                "name": "fc1",
                "nodes": 200,
                "activation": "relu",
                "dropout": false
            }, {
                "type": "fc",
                "name": "fc2",
                "nodes": 50,
                "activation": "tanh",
                "dropout": false
            }
        ],
        "hyper_parameters": {
            "dropout": 0.25,
            "lr": 0.01,
            "lr_decay": 1,
            "epochs": 100,
            "epoch_no_imprv": 10,
            "batch_size": 100
        }
    }
]

在这里,下面的键可以有一个列表,但我更喜欢它是否可以变得足够通用

节点、激活、退出、lr。

“层”列表可以有任意数量的层。你知道吗

任何其他简化的结构,以实现同样的事情也将工作。你知道吗


Tags: namefalselayerstype结构activationhyperdropout