如何在返回的新网络中加载经过训练的网络中某层的权重?

时间:2018-07-03 15:03:48

标签: python tensorflow returnn

我在文件夹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"] }
}

这怎么可能返回?

1 个答案:

答案 0 :(得分:1)

使用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检查点文件加载。