1 """Script to generate baseline values from PyTorch initialization algorithms""" 7 #include <torch/types.h> 11 namespace expected_parameters { 14 FOOTER =
"} // namespace expected_parameters" 16 PARAMETERS =
"inline std::vector<std::vector<torch::Tensor>> {}() {{" 26 def emit(initializer_parameter_map):
28 print(
"// @{} from {}".format(
'generated', __file__))
30 for initializer_name, weights
in initializer_parameter_map.items():
31 print(PARAMETERS.format(initializer_name))
33 for sample
in weights:
35 for parameter
in sample:
36 parameter_values =
"{{{}}}".format(
", ".join(map(str, parameter)))
37 print(
" torch::tensor({}),".format(parameter_values))
47 layer1 = torch.nn.Linear(7, 15)
48 INITIALIZERS[initializer](layer1.weight)
50 layer2 = torch.nn.Linear(15, 15)
51 INITIALIZERS[initializer](layer2.weight)
53 layer3 = torch.nn.Linear(15, 2)
54 INITIALIZERS[initializer](layer3.weight)
56 weight1 = layer1.weight.data.numpy()
57 weight2 = layer2.weight.data.numpy()
58 weight3 = layer3.weight.data.numpy()
60 return [weight1, weight2, weight3]
64 initializer_parameter_map = {}
65 for initializer
in INITIALIZERS.keys():
66 sys.stderr.write(
'Evaluating {} ...\n'.format(initializer))
67 initializer_parameter_map[initializer] = run(initializer)
69 emit(initializer_parameter_map)
71 if __name__ ==
"__main__":