| 2007 | self._inference_helper(gpu_dev) |
| 2008 | |
| 2009 | def _retraining_helper(self, dev): |
| 2010 | # forward |
| 2011 | x = tensor.Tensor(shape=(2, 3, 3, 3), device=dev) |
| 2012 | x.gaussian(0.0, 1.0) |
| 2013 | |
| 2014 | class MyLayer(layer.Layer): |
| 2015 | |
| 2016 | def __init__(self): |
| 2017 | super(MyLayer, self).__init__() |
| 2018 | self.conv1 = layer.Conv2d(1, 2) |
| 2019 | self.conv2 = layer.Conv2d(1, 2) |
| 2020 | |
| 2021 | def forward(self, inputs): |
| 2022 | x = self.conv1(inputs) |
| 2023 | x = self.conv2(x) |
| 2024 | x = autograd.flatten(x) |
| 2025 | return x |
| 2026 | |
| 2027 | y = MyLayer()(x) |
| 2028 | y_t = tensor.Tensor(shape=(2, 1), device=dev) |
| 2029 | y_t.gaussian(0.0, 1.0) |
| 2030 | loss = autograd.MeanSquareError(y_t)(y)[0] |
| 2031 | # backward |
| 2032 | sgd = opt.SGD(lr=0.01) |
| 2033 | for p, gp in autograd.backward(loss): |
| 2034 | sgd.apply(p.name, p, gp) |
| 2035 | sgd.step() |
| 2036 | |
| 2037 | # frontend |
| 2038 | model = sonnx.to_onnx([x], [y]) |
| 2039 | # print('The model is:\n{}'.format(model)) |
| 2040 | |
| 2041 | # backend |
| 2042 | sg_ir = sonnx.prepare(model, device=dev) |
| 2043 | sg_ir.is_graph = True |
| 2044 | # forward |
| 2045 | y_o = sg_ir.run([x])[0] |
| 2046 | # backward |
| 2047 | loss = autograd.MeanSquareError(y_t)(y_o)[0] |
| 2048 | sgd = opt.SGD(lr=0.01) |
| 2049 | for p, gp in autograd.backward(loss): |
| 2050 | sgd.apply(p.name, p, gp) |
| 2051 | sgd.step() |
| 2052 | |
| 2053 | def test_retraining_cpu(self): |
| 2054 | self._retraining_helper(cpu_dev) |