class ConvThenLinear(_Net): def __init__(self, start_shape, stop_shape, depth=2, conv_part=Conv1dAC): _Net.__init__(self, start_shape, stop_shape, depth) self.conv_part = conv_part(start_shape=start_shape, stop_shape=stop_shape, depth=depth, bias_prob=0.3) self.lin_part = LinearAC(start_shape=None, stop_shape=(10, ), depth=depth, bias_prob=0.3) self.change_num = IntParam(name="") def change_when(self, x): self.change_num.unrandomize(val=x) def change_random(self): self.change_num.randomize(limits=(1, self.depth - 1)) def generate(self): self.change_random() t = self.change_num.value self.lin_part.depth = self.depth - t cp = self.conv_part.generate()[:t] self.lin_part.start_shape = cp[-1].out_shape lp = self.lin_part.generate() return cp + lp
class ResNetStyle(_Net): def __init__(self, start_shape, stop_shape, depth=3): _Net.__init__(self, start_shape, stop_shape, depth) self.layers.append(Conv2dAC(start_shape, [10, 1, 1], depth)) self.layers.append(Conv2dThenLinear(start_shape, [10, 1, 1], depth)) self.change_num = IntParam("", default=1) def generate(self): z1 = Conv2dAC.ac.val Conv2dAC.ac.val = Conv2dAC.ac.choices[2] z2 = LinearAC.ac.val LinearAC.ac.val = LinearAC.ac.choices[2] self.change_num.randomize(limits=(1, self.depth - 2 * self.depth // 3)) t = self.change_num.value cp = self.layers[0].generate(resnet_cover=20)[:t] skip_gen = BasicBlock() self.change_num.randomize( limits=(1, max([1, self.depth - (t + self.depth // 3)])) ) t2 = self.change_num.value sp = [] for i in range(t2): sp.append(skip_gen(_in_shape=cp[-1].out_shape)) self.change_num.randomize(limits=(2, max([2, self.depth - (t + t2)]))) t3 = self.change_num.value self.layers[1].start_shape = sp[-1].out_shape self.layers[1].conv_part.start_shape = sp[-1].out_shape self.layers[1].depth = t3 lp = self.layers[1].generate() Conv2dAC.ac.val = z1 LinearAC.ac.val = z2 return cp + sp + lp