Exemple #1
0
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
Exemple #2
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "EmbeddingBag",
         [
             "num_embeddings",
             "embedding_dim",
             "max_norm",
             "norm_type",
             "scale_grad_by_freq",
             "mode",
             "sparse",
             "_weight",
         ],
     )
     self.params = self.template_fn(
         num_embeddings=IntParam(name="num_embeddings", default=1),
         embedding_dim=IntParam(name="embedding_dim", default=1),
         max_norm=Param(name="max_norm", default=None),
         norm_type=IntParam(name="norm_type", default=2.0),
         scale_grad_by_freq=BinaryParam(name="scale_grad_by_freq",
                                        default=False,
                                        true_prob=0.5),
         mode=ChoiceParam(name="mode",
                          choices=("mean", ),
                          cprobs=(1, ),
                          default="mean"),
         sparse=BinaryParam(name="sparse", default=False, true_prob=0.5),
         _weight=Param(name="_weight", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #3
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "GRU",
         [
             "input_size",
             "hidden_size",
             "num_layers",
             "bias",
             "batch_first",
             "dropout",
             "bidirectional",
         ],
     )
     self.params = self.template_fn(
         input_size=IntParam(name="input_size", default=1),
         hidden_size=Param(name="hidden_size", default=None),
         num_layers=IntParam(name="num_layers", default=1),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
         batch_first=BinaryParam(name="batch_first",
                                 default=False,
                                 true_prob=0.5),
         dropout=IntParam(name="dropout", default=0.0),
         bidirectional=BinaryParam(name="bidirectional",
                                   default=False,
                                   true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #4
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("Flatten", ["start_dim", "end_dim"])
     self.params = self.template_fn(
         start_dim=IntParam(name="start_dim", default=1),
         end_dim=IntParam(name="end_dim", default=-1),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #5
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("BasicBlock", ["inplanes", "planes"])
     self.params = self.template_fn(
         inplanes=IntParam(name="inplanes", default=1),
         planes=IntParam(name="planes", default=1),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #6
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("Linear",
                                   ["in_features", "out_features", "bias"])
     self.params = self.template_fn(
         in_features=IntParam(name="in_features", default=1),
         out_features=IntParam(name="out_features", default=1),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #7
0
 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="")
Exemple #8
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "Unfold", ["kernel_size", "dilation", "padding", "stride"])
     self.params = self.template_fn(
         kernel_size=Param(name="kernel_size", default=None),
         dilation=IntParam(name="dilation", default=1),
         padding=IntParam(name="padding", default=0),
         stride=IntParam(name="stride", default=1),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #9
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "ConvTranspose3d",
         [
             "in_channels",
             "out_channels",
             "kernel_size",
             "stride",
             "padding",
             "output_padding",
             "groups",
             "bias",
             "dilation",
             "padding_mode",
         ],
     )
     self.params = self.template_fn(
         in_channels=IntParam(name="in_channels", default=1),
         out_channels=IntParam(name="out_channels", default=1),
         kernel_size=TupleParam(
             name="kernel_size",
             size=3,
             limits=((1, 1, 1), (1, 1, 1)),
             default=(1, 1, 1),
         ),
         stride=TupleParam(
             name="stride", size=3, limits=((1, 1, 1), (1, 1, 1)), default=(1, 1, 1)
         ),
         padding=TupleParam(
             name="padding", size=3, limits=((0, 0, 0), (0, 0, 0)), default=(0, 0, 0)
         ),
         output_padding=TupleParam(
             name="output_padding",
             size=3,
             limits=((0, 0, 0), (0, 0, 0)),
             default=(0, 0, 0),
         ),
         groups=IntParam(name="groups", default=1),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
         dilation=TupleParam(
             name="dilation",
             size=3,
             limits=((1, 1, 1), (1, 1, 1)),
             default=(1, 1, 1),
         ),
         padding_mode=ChoiceParam(
             name="padding_mode", choices=("zeros",), cprobs=(1,), default="zeros"
         ),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #10
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "Hardtanh",
         ["min_val", "max_val", "inplace", "min_value", "max_value"])
     self.params = self.template_fn(
         min_val=IntParam(name="min_val", default=-1.0),
         max_val=IntParam(name="max_val", default=1.0),
         inplace=BinaryParam(name="inplace", default=False, true_prob=0.5),
         min_value=Param(name="min_value", default=None),
         max_value=Param(name="max_value", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #11
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "AdaptiveLogSoftmaxWithLoss",
         ["in_features", "n_classes", "cutoffs", "div_value", "head_bias"],
     )
     self.params = self.template_fn(
         in_features=IntParam(name="in_features", default=1),
         n_classes=Param(name="n_classes", default=None),
         cutoffs=Param(name="cutoffs", default=None),
         div_value=IntParam(name="div_value", default=4.0),
         head_bias=BinaryParam(name="head_bias", default=False, true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #12
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "MultiheadAttention",
         [
             "embed_dim",
             "num_heads",
             "dropout",
             "bias",
             "add_bias_kv",
             "add_zero_attn",
             "kdim",
             "vdim",
         ],
     )
     self.params = self.template_fn(
         embed_dim=Param(name="embed_dim", default=None),
         num_heads=Param(name="num_heads", default=None),
         dropout=IntParam(name="dropout", default=0.0),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
         add_bias_kv=BinaryParam(name="add_bias_kv",
                                 default=False,
                                 true_prob=0.5),
         add_zero_attn=BinaryParam(name="add_zero_attn",
                                   default=False,
                                   true_prob=0.5),
         kdim=Param(name="kdim", default=None),
         vdim=Param(name="vdim", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #13
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("PixelShuffle", ["upscale_factor"])
     self.params = self.template_fn(
         upscale_factor=IntParam(name="upscale_factor", default=1)
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #14
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("PReLU", ["num_parameters", "init"])
     self.params = self.template_fn(
         num_parameters=IntParam(name="num_parameters", default=1),
         init=FloatParam(name="init", default=0.25),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #15
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("Softplus", ["beta", "threshold"])
     self.params = self.template_fn(
         beta=FloatParam(name="beta", default=1),
         threshold=IntParam(name="threshold", default=20),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #16
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("CosineSimilarity", ["dim", "eps"])
     self.params = self.template_fn(
         dim=IntParam(name="dim", default=1),
         eps=FloatParam(name="eps", default=1e-08),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #17
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("PairwiseDistance", ["p", "eps", "keepdim"])
     self.params = self.template_fn(
         p=IntParam(name="p", default=2.0),
         eps=FloatParam(name="eps", default=1e-06),
         keepdim=BinaryParam(name="keepdim", default=False, true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #18
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("UpsamplingBilinear2d",
                                   ["size", "scale_factor"])
     self.params = self.template_fn(
         size=Param(name="size", default=None),
         scale_factor=IntParam(name="scale_factor", default=1),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #19
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "MultiMarginLoss",
         ["p", "margin", "weight", "size_average", "reduce", "reduction"],
     )
     self.params = self.template_fn(
         p=IntParam(name="p", default=1),
         margin=IntParam(name="margin", default=1.0),
         weight=Param(name="weight", default=None),
         size_average=Param(name="size_average", default=None),
         reduce=Param(name="reduce", default=None),
         reduction=ChoiceParam(name="reduction",
                               choices=("mean", ),
                               cprobs=(1, ),
                               default="mean"),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #20
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("GRUCell",
                                   ["input_size", "hidden_size", "bias"])
     self.params = self.template_fn(
         input_size=IntParam(name="input_size", default=1),
         hidden_size=Param(name="hidden_size", default=None),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #21
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "RNNCellBase", ["input_size", "hidden_size", "bias", "num_chunks"])
     self.params = self.template_fn(
         input_size=IntParam(name="input_size", default=1),
         hidden_size=Param(name="hidden_size", default=None),
         bias=Param(name="bias", default=None),
         num_chunks=Param(name="num_chunks", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #22
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("CrossMapLRN2d",
                                   ["size", "alpha", "beta", "k"])
     self.params = self.template_fn(
         size=Param(name="size", default=None),
         alpha=FloatParam(name="alpha", default=0.0001),
         beta=FloatParam(name="beta", default=0.75),
         k=IntParam(name="k", default=1),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #23
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("Transformer", [
         'd_model', 'nhead', 'num_encoder_layers', 'num_decoder_layers',
         'dim_feedforward', 'dropout', 'activation', 'custom_encoder',
         'custom_decoder'
     ])
     self.params = self.template_fn(
         d_model=IntParam(name="d_model", default=512),
         nhead=IntParam(name="nhead", default=8),
         num_encoder_layers=IntParam(name="num_encoder_layers", default=6),
         num_decoder_layers=IntParam(name="num_decoder_layers", default=6),
         dim_feedforward=IntParam(name="dim_feedforward", default=2048),
         dropout=FloatParam(name="dropout", default=0.1),
         activation=ChoiceParam(name="activation",
                                choices=("relu", ),
                                cprobs=(1, ),
                                default="relu"),
         custom_encoder=Param(name="custom_encoder", default=None),
         custom_decoder=Param(name="custom_decoder", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #24
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "TripletMarginLoss",
         [
             "margin", "p", "eps", "swap", "size_average", "reduce",
             "reduction"
         ],
     )
     self.params = self.template_fn(
         margin=IntParam(name="margin", default=1.0),
         p=IntParam(name="p", default=2.0),
         eps=FloatParam(name="eps", default=1e-06),
         swap=BinaryParam(name="swap", default=False, true_prob=0.5),
         size_average=Param(name="size_average", default=None),
         reduce=Param(name="reduce", default=None),
         reduction=ChoiceParam(name="reduction",
                               choices=("mean", ),
                               cprobs=(1, ),
                               default="mean"),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #25
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "Upsample", ["size", "scale_factor", "mode", "align_corners"])
     self.params = self.template_fn(
         size=Param(name="size", default=None),
         scale_factor=IntParam(name="scale_factor", default=1),
         mode=ChoiceParam(name="mode",
                          choices=("nearest", ),
                          cprobs=(1, ),
                          default="nearest"),
         align_corners=Param(name="align_corners", default=None),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #26
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "RNNCell", ["input_size", "hidden_size", "bias", "nonlinearity"])
     self.params = self.template_fn(
         input_size=IntParam(name="input_size", default=1),
         hidden_size=Param(name="hidden_size", default=None),
         bias=BinaryParam(name="bias", default=True, true_prob=0.5),
         nonlinearity=ChoiceParam(name="nonlinearity",
                                  choices=("tanh", ),
                                  cprobs=(1, ),
                                  default="tanh"),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #27
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple("CTCLoss",
                                   ["blank", "reduction", "zero_infinity"])
     self.params = self.template_fn(
         blank=IntParam(name="blank", default=0),
         reduction=ChoiceParam(name="reduction",
                               choices=("mean", ),
                               cprobs=(1, ),
                               default="mean"),
         zero_infinity=BinaryParam(name="zero_infinity",
                                   default=False,
                                   true_prob=0.5),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #28
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "CosineEmbeddingLoss",
         ["margin", "size_average", "reduce", "reduction"])
     self.params = self.template_fn(
         margin=IntParam(name="margin", default=0.0),
         size_average=Param(name="size_average", default=None),
         reduce=Param(name="reduce", default=None),
         reduction=ChoiceParam(name="reduction",
                               choices=("mean", ),
                               cprobs=(1, ),
                               default="mean"),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #29
0
 def __init__(self, **kwargs):
     _Factory.__init__(self)
     self.template_fn = namedtuple(
         "NLLLoss",
         ["weight", "size_average", "ignore_index", "reduce", "reduction"])
     self.params = self.template_fn(
         weight=Param(name="weight", default=None),
         size_average=Param(name="size_average", default=None),
         ignore_index=IntParam(name="ignore_index", default=-100),
         reduce=Param(name="reduce", default=None),
         reduction=ChoiceParam(name="reduction",
                               choices=("mean", ),
                               cprobs=(1, ),
                               default="mean"),
     )
     for k, v in kwargs.items():
         getattr(self.params, k).val = v
Exemple #30
0
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