gpt4 book ai didi

python - 如何将已训练网络的某些层的权重加载到新网络中作为返回?

转载 作者:行者123 更新时间:2023-12-01 09:17:00 25 4
gpt4 key购买 nike

我在文件夹 path/to/modelFile 中有以下网络的训练权重:

network={
"conv_1" : {"class": "conv", "filter_size": (400,), "activation":"abs" , "padding": "valid", "strides": 10, "n_out": 64 },
"pad_conv_1_time_dim" : {"class": "pad", "axes": "time", "padding": 20, "from": ["conv_1"]},
"conv_2" : {"class": "conv", "input_add_feature_dim": True, "filter_size": (40, 64), "activation":"abs", "padding": "valid","strides": 16, "n_out": 128, "from": ["pad_conv_1_time_dim"]},
"flatten_conv": {"class": "merge_dims", "axes": "except_time","n_out": 128, "from": ["conv_2"]},
"window_1": {"class": "window", "window_size": 17, "from": ["flatten_conv"]},
"flatten_window": {"class": "merge_dims", "axes":"except_time","from": ["window_1"]},
"lin_1" : { "class" : "linear", "activation": None, "n_out": 512,"from" : ["flatten_window"] },
"ff_2" : { "class" : "linear", "activation": "relu", "n_out": 2000, "from" : ["lin_1"] },
"output" : { "class" : "softmax", "loss" : "ce", "from" : ["ff_2"] }
}

我想将层“conv_1”和“conv_2”的训练权重加载到以下网络中:

network={
"conv_1" : {"class": "conv", "filter_size": (400,), "activation": "abs" , "padding": "valid", "strides": 10, "n_out": 64 },
"pad_conv_1_time_dim" : {"class": "pad", "axes": "time", "padding": 20, "from": ["conv_1"]},
"conv_2" : {"class": "conv", "input_add_feature_dim": True, "filter_size": (40, 64), "activation":"abs", "padding": "valid", "strides": 16, "n_out": 128, "from": ["pad_conv_1_time_dim"]},
"flatten_conv": {"class": "merge_dims", "axes": "except_time", "n_out": 128, "from": ["conv_2"]},
"lstm1_fw" : { "class": "rec", "unit": "lstmp", "n_out" : rnnLayerNodes, "direction": 1, "from" : ['flatten_conv'] },
"lstm1_bw" : { "class": "rec", "unit": "lstmp", "n_out" : rnnLayerNodes, "direction": -1, "from" : ['flatten_conv'] },
"lin_1" : { "class" : "linear", "activation": None, "n_out": 512, "from" : ["lstm1_fw", "lstm1_bw"] },
"ff_2" : { "class" : "linear", "activation": "relu", "n_out": 2000, "from" : ["lin_1"] },
"ff_3" : { "class" : "linear", "activation": "relu", "n_out": 2000,"from" : ["ff_2"] },
"ff_4" : { "class" : "linear", "activation": "relu", "n_out": 2000,"from" : ["ff_3"] },
"output" : { "class" : "softmax", "loss" : "ce", "from" : ["ff_4"] }
}

这怎么可能返回呢?

最佳答案

使用SubnetworkLayer是一种选择。看起来像:

trained_network_model_file = 'path/to/model_file'

trained_network = {
"conv_1" : {"class": "conv", "filter_size": (400,), "activation": "abs" , "padding": "valid", "strides": 10, "n_out": 64 },
"pad_conv_1_time_dim" : {"class": "pad", "axes": "time", "padding": 20, "from": ["conv_1"]},
"conv_2" : {"class": "conv", "input_add_feature_dim": True, "filter_size": (40, 64), "activation":"abs", "padding": "valid", "strides": 16, "n_out": 128, "from": ["pad_conv_1_time_dim"]},
"flatten_conv": {"class": "merge_dims", "axes": "except_time","n_out": 128, "from": ["conv_2"]}
}

network = {
"conv_layers" : { "class" : "subnetwork", "subnetwork": trained_network, "load_on_init": trained_network_model_file, "n_out": 128},
"lstm1_fw" : { "class": "rec", "unit": "lstmp", "n_out" : rnnLayerNodes, "direction": 1, "from" : ['conv_layers'] },
"lstm1_bw" : { "class": "rec", "unit": "lstmp", "n_out" : rnnLayerNodes, "direction": -1, "from" : ['conv_layers'] },
"lin_1" : { "class" : "linear", "activation": None, "n_out": 512, "from" : ["lstm1_fw", "lstm1_bw"] },
"ff_2" : { "class" : "linear", "activation": "relu", "n_out": 2000, "from" : ["lin_1"] },
"ff_3" : { "class" : "linear", "activation": "relu", "n_out": 2000, "from" : ["ff_2"] },
"ff_4" : { "class" : "linear", "activation": "relu", "n_out": 2000, "from" : ["ff_3"] },
"output" : { "class" : "softmax", "loss" : "ce", "from" : ["ff_4"] }
}

我认为对于您的情况,这将是我的首选。

否则,每个层都有 custom_param_importer 选项,您可能会使用它。

然后,对于许多层,您可以为参数定义初始值设定项,例如对于ConvLayer,您可以使用forward_weights_init。可以使用像 load_txt_file_initializer 这样的函数,或者应该添加类似的函数来直接从 TF 检查点文件加载。

关于python - 如何将已训练网络的某些层的权重加载到新网络中作为返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com