| 2058 | self._retraining_helper(gpu_dev) |
| 2059 | |
| 2060 | def _transfer_learning_helper(self, dev): |
| 2061 | # forward |
| 2062 | x = tensor.Tensor(shape=(2, 3, 3, 3), device=dev) |
| 2063 | x.gaussian(0.0, 1.0) |
| 2064 | |
| 2065 | class MyLayer(layer.Layer): |
| 2066 | |
| 2067 | def __init__(self): |
| 2068 | super(MyLayer, self).__init__() |
| 2069 | self.conv1 = layer.Conv2d(1, 2) |
| 2070 | |
| 2071 | def forward(self, inputs): |
| 2072 | x = self.conv1(inputs) |
| 2073 | x = autograd.flatten(x) |
| 2074 | return x |
| 2075 | |
| 2076 | y = MyLayer()(x) |
| 2077 | y_t = tensor.Tensor(shape=(2, 4), device=dev) |
| 2078 | y_t.gaussian(0.0, 1.0) |
| 2079 | loss = autograd.MeanSquareError(y_t)(y)[0] |
| 2080 | # backward |
| 2081 | sgd = opt.SGD(lr=0.01) |
| 2082 | for p, gp in autograd.backward(loss): |
| 2083 | sgd.apply(p.name, p, gp) |
| 2084 | sgd.step() |
| 2085 | |
| 2086 | # frontend |
| 2087 | model = sonnx.to_onnx([x], [y]) |
| 2088 | # print('The model is:\n{}'.format(model)) |
| 2089 | |
| 2090 | # backend |
| 2091 | sg_ir = sonnx.prepare(model, device=dev) |
| 2092 | sg_ir.is_graph = True |
| 2093 | # forward |
| 2094 | class MyLayer2(layer.Layer): |
| 2095 | |
| 2096 | def __init__(self, sg_ir): |
| 2097 | super(MyLayer2, self).__init__() |
| 2098 | self.sg_ir = sg_ir |
| 2099 | for node, operator in self.sg_ir.layers: |
| 2100 | self.__dict__[node.name] = operator |
| 2101 | self.conv2 = layer.Conv2d(1, 2) |
| 2102 | |
| 2103 | def forward(self, inputs): |
| 2104 | x = self.sg_ir.run(inputs, last_layers=-1)[0] |
| 2105 | x = self.conv2(inputs) |
| 2106 | x = autograd.flatten(x) |
| 2107 | return x |
| 2108 | |
| 2109 | y_o = MyLayer()(x) |
| 2110 | # backward |
| 2111 | y_ot = tensor.Tensor(shape=(2, 1), device=dev) |
| 2112 | y_ot.gaussian(0.0, 1.0) |
| 2113 | loss = autograd.MeanSquareError(y_ot)(y_o)[0] |
| 2114 | sgd = opt.SGD(lr=0.01) |
| 2115 | for p, gp in autograd.backward(loss): |
| 2116 | sgd.apply(p.name, p, gp) |
| 2117 | sgd.step() |