Beispiel #1
0
 def __init__(self, nfft, cfg):
     super().__init__()
     params_lookup = {'peak': ['w0', 'bw', 'g'], 'notch': ['w0', 'bw']}
     self.features = {f'{t}{i}': params_lookup[t] for i, t in enumerate(cfg['spectral_features'])}
     input_size = nfft // 2 + 1
     # add first stack
     frontend_sizes = cfg['frontend']['sizes']
     frontend_sizes.insert(0, input_size)
     frontend_act = cfg['frontend']['activation_type']
     frontend_do = cfg['frontend']['dropout_rate']
     self.frontend = self.dense_stack(frontend_sizes, frontend_act, frontend_do)
     # add a stack for each spectral feature to predict (peak or notch)
     specfeat_sizes = cfg['features_block']['sizes']
     specfeat_sizes.insert(0, frontend_sizes[-1])
     specfeat_act = cfg['features_block']['activation_type']
     specfeat_do = cfg['features_block']['dropout_rate']
     specfeat_dict = {k: self.dense_stack(specfeat_sizes, specfeat_act, specfeat_do) for k in self.features}
     self.spectral_features = ModuleDict(specfeat_dict)
     # add a stack for each parameter of each spectral features (w0, bw, g)
     specparams_sizes = cfg['parameters_block']['sizes']
     specparams_sizes.insert(0, specfeat_sizes[-1])
     specparams_sizes.append(1)
     specparams_act = cfg['parameters_block']['activation_type']
     specparams_do = cfg['parameters_block']['dropout_rate']
     specparams_dict = {f'{k}_{p}': self.dense_stack(specparams_sizes, specparams_act, specparams_do) for k in self.features for p in self.features[k]}
     self.spectral_parameters = ModuleDict(specparams_dict)
Beispiel #2
0
    def __init__(
        self,
        encoder_channels: List[int],
        scales: List[int],
        use_skips: bool = True,
    ):
        super().__init__()
        self.use_skips = use_skips
        self.scales = scales

        self.encoder_channels = encoder_channels
        self.decoder_channels = array([16, 32, 64, 128, 256])

        self.convs: ModuleDict[str, Module] = ModuleDict()
        self.scaled_convs: ModuleDict[str, Module] = ModuleDict()
        for i in range(4, -1, -1):
            # upconv_0
            input_channels = (self.encoder_channels[-1]
                              if i == 4 else self.decoder_channels[i + 1])
            self.convs[f"{i}0"] = ConvBlock(
                input_channels,
                self.decoder_channels[i],
            )
            # upconv_1
            input_channels = self.decoder_channels[i]
            if self.use_skips and i > 0:
                input_channels += self.encoder_channels[i - 1]
            self.convs[f"{i}1"] = ConvBlock(
                input_channels,
                self.decoder_channels[i],
            )
        for s in self.scales:
            self.scaled_convs[f"{s}"] = Conv3x3(self.decoder_channels[s], 1)
        self.sigmoid = Sigmoid()
Beispiel #3
0
    def __init__(
        self,
        n_stages,
        nf=128,
        nf_out=3,
        n_rnb=2,
        conv_layer=NormConv2d,
        spatial_size=256,
        final_act=True,
        dropout_prob=0.0,
    ):
        super().__init__()
        assert (2 ** (n_stages - 1)) == spatial_size
        self.final_act = final_act
        self.blocks = ModuleDict()
        self.ups = ModuleDict()
        self.n_stages = n_stages
        self.n_rnb = n_rnb
        for i_s in range(self.n_stages - 2, 0, -1):
            # for final stage, bisect number of filters
            if i_s == 1:
                # upsampling operations
                self.ups.update(
                    {
                        f"s{i_s+1}": Upsample(
                            in_channels=nf, out_channels=nf // 2, conv_layer=conv_layer,
                        )
                    }
                )
                nf = nf // 2
            else:
                # upsampling operations
                self.ups.update(
                    {
                        f"s{i_s+1}": Upsample(
                            in_channels=nf, out_channels=nf, conv_layer=conv_layer,
                        )
                    }
                )

            # resnet blocks
            for ir in range(self.n_rnb, 0, -1):
                stage = f"s{i_s}_{ir}"
                self.blocks.update(
                    {
                        stage: VUnetResnetBlock(
                            nf,
                            use_skip=True,
                            conv_layer=conv_layer,
                            dropout_prob=dropout_prob,
                        )
                    }
                )

        # final 1x1 convolution
        self.final_layer = conv_layer(nf, nf_out, kernel_size=1)

        # conditionally: set final activation
        if self.final_act:
            self.final_act = nn.Tanh()
Beispiel #4
0
    def __init__(
        self,
        n_stages,
        nf_in=3,
        nf_start=64,
        nf_max=128,
        n_rnb=2,
        conv_layer=NormConv2d,
    ):
        super().__init__()
        self.in_op = conv_layer(nf_in, nf_start, kernel_size=1)
        nf = nf_start
        self.blocks = ModuleDict()
        self.downs = ModuleDict()
        self.n_rnb = n_rnb
        self.n_stages = n_stages
        for i_s in range(self.n_stages):
            # prepare resnet blocks per stage
            if i_s > 0:
                self.downs.update(
                    {
                        f"s{i_s+1}": Downsample(
                            nf, min(2 * nf, nf_max), conv_layer=conv_layer
                        )
                    }
                )
                nf = min(2 * nf, nf_max)

            for ir in range(self.n_rnb):
                stage = f"s{i_s+1}_{ir+1}"
                self.blocks.update({stage: VUnetResnetBlock(nf, conv_layer=conv_layer)})
Beispiel #5
0
    def __init__(
        self,
        encoder_output_channels: int,
        scales: List[int],
    ):
        super().__init__()
        self.scales = scales
        self.decoder_channels = [16, 32, 64, 128, 256]

        self.convs: ModuleDict[str, Module] = ModuleDict()
        self.scaled_convs: ModuleDict[str, Module] = ModuleDict()
        for i in range(4, -1, -1):
            # upconv_0
            input_channels = (encoder_output_channels
                              if i == 4 else self.decoder_channels[i + 1])
            self.convs[f"{i}0"] = ConvBlock(
                input_channels,
                self.decoder_channels[i],
            )
            # upconv_1
            input_channels = self.decoder_channels[i]
            self.convs[f"{i}1"] = ConvBlock(
                input_channels,
                self.decoder_channels[i],
            )
        for s in self.scales:
            self.scaled_convs[f"{s}"] = Conv3x3(self.decoder_channels[s], 1)
        self.sigmoid = Sigmoid()
Beispiel #6
0
    def _build_cycle_layers(self, hidden_channels, num_layers, cycle_depth):
        """
        Build layers that convolve over the cyclic activations.
        """
        # Construct embedding layers for input
        self.cycle_embedders = ModuleDict()
        for k, key in zip(self.used_cycles, self._cycle_keys):
            self.cycle_embedders[key] = Embedding(4, hidden_channels)

        self.cycle_blocks = ModuleList()
        self.a2c_blocks = ModuleList()
        self.c2a_blocks = ModuleList()

        # Construct layers that convolve over cycles and move to/from atom activations
        for _ in range(num_layers):
            cb_k_dict = ModuleDict()  # Dict of intracycle blocks
            a2c_k_dict = ModuleDict()  # Dict of linears for conversions
            c2a_k_dict = ModuleDict()  # Dict of linears for conversions
            for k, key in zip(self.used_cycles, self._cycle_keys):
                cb_k_dict[key] = blocks.PathBlock(hidden_channels,
                                                  k,
                                                  num_resid_blocks=cycle_depth,
                                                  dropout=self.dropout)
                a2c_k_dict[key] = Linear(hidden_channels, hidden_channels)
                c2a_k_dict[key] = Linear(hidden_channels, hidden_channels)
            self.cycle_blocks.append(cb_k_dict)
            self.a2c_blocks.append(a2c_k_dict)
            self.c2a_blocks.append(c2a_k_dict)
Beispiel #7
0
    def __init__(self, multitaskdict):

        """
        Args:
            multitaskdict (dict): dictionary of items used for setting up the networks.
        Returns:
            None
        """

        super(AuTopologyReadOut, self).__init__()

        trainable = multitaskdict["trainable_prior"]
        Fr = multitaskdict["Fr"]
        Lh = multitaskdict["Lh"]
        # bond_terms = multitaskdict.get("bond_terms", ["morse"])
        # angle_terms =  multitaskdict.get("angle_terms", ['harmonic'])  # harmonic and/or cubic and/or quartic
        # dihedral_terms = multitaskdict.get("dihedral_terms", ['OPLS'])  # OPLS and/or multiharmonic
        # improper_terms = multitaskdict.get("improper_terms", ['harmonic'])  # harmonic
        # pair_terms = multitaskdict.get("pair_terms", ['LJ'])  # coulomb and/or LJ and/or induced_dipole
        autopology_keys = multitaskdict["output_keys"]

        default_terms_dict = {
            "bond_terms": ["morse"],
            "angle_terms": ["harmonic"],
            "dihedral_terms": ["OPLS"],
            "improper_terms": ["harmonic"],
            "pair_terms": ["LJ", "coulombs"]
        }

        # self.terms = {
        #     'bond': bond_terms,
        #     'angle': angle_terms,
        #     'dihedral': dihedral_terms,
        #     'improper': improper_terms,
        #     'pair': pair_terms
        # }
        self.terms = {}

        # remove terms that is not included 
        for top in ['bond', 'angle', 'dihedral', 'improper', 'pair']:
            if top + '_terms' in multitaskdict.keys():
                self.terms[top] = multitaskdict.get(top + '_terms', default_terms_dict[top + '_terms'])


        topologynet = {key: {} for key in autopology_keys}
        for key in autopology_keys:
            for top in self.terms.keys():
                if top + '_terms' in multitaskdict:
                    topologynet[key][top] = TopologyNet[top](Fr, Lh, self.terms[top], trainable=trainable)


        # module dictionary of the form {"energy_0": {"bond": BondNet0, "angle": AngletNet0},
        # "energy_1": {"bond": BondNet1, "angle": AngletNet1} }
        self.auto_modules = ModuleDict({key: ModuleDict({top: topologynet[key][top] for top in
            self.terms.keys()}) for key in autopology_keys})

        # energy offset for each state
        self.offset = ModuleDict({key: ParameterPredictor(Fr, Lh, 1)
            for key in autopology_keys})
 def __init__(self):
     super().__init__()
     self.metrics_list = ModuleList([DummyMetric() for _ in range(2)])
     self.metrics_dict = ModuleDict({"a": DummyMetric(), "b": DummyMetric()})
     self.metrics_collection_dict = MetricCollection({"a": DummyMetric(), "b": DummyMetric()})
     self.metrics_collection_dict_nested = ModuleDict(
         {"a": ModuleList([ModuleDict({"b": DummyMetric()}), DummyMetric()])}
     )
Beispiel #9
0
    def __init__(
        self,
        n_stages,
        nf,
        device,
        n_rnb=2,
        n_auto_groups=4,
        conv_layer=NormConv2d,
        dropout_prob=0.0,
    ):
        super().__init__()
        self.device = device
        self.blocks = ModuleDict()
        self.channel_norm = ModuleDict()
        self.conv1x1 = conv_layer(nf, nf, 1)
        self.up = Upsample(in_channels=nf, out_channels=nf, conv_layer=conv_layer)
        self.depth_to_space = DepthToSpace(block_size=2)
        self.space_to_depth = SpaceToDepth(block_size=2)
        self.n_stages = n_stages
        self.n_rnb = n_rnb
        # number of autoregressively modeled groups
        self.n_auto_groups = n_auto_groups
        for i_s in range(self.n_stages, self.n_stages - 2, -1):
            self.channel_norm.update({f"s{i_s}": conv_layer(2 * nf, nf, 1)})
            for ir in range(self.n_rnb):
                self.blocks.update(
                    {
                        f"s{i_s}_{ir+1}": VUnetResnetBlock(
                            nf,
                            use_skip=True,
                            conv_layer=conv_layer,
                            dropout_prob=dropout_prob,
                        )
                    }
                )

        self.auto_blocks = ModuleList()
        # model the autoregressively groups rnb
        for i_a in range(4):
            if i_a < 1:
                self.auto_blocks.append(
                    VUnetResnetBlock(
                        nf, conv_layer=conv_layer, dropout_prob=dropout_prob
                    )
                )
                self.param_converter = conv_layer(4 * nf, nf, kernel_size=1)
            else:
                self.auto_blocks.append(
                    VUnetResnetBlock(
                        nf,
                        use_skip=True,
                        conv_layer=conv_layer,
                        dropout_prob=dropout_prob,
                    )
                )
Beispiel #10
0
    def _init_attention(self):
        '''
            Initialises the attention map vector/matrix

            Takes attention_type-Span, Sentence, Span-param, Sentence-param
            as a parameter to decide the size of the attention matrix
        '''

        self.att_map_repr = ModuleDict({})
        self.att_map_W = ModuleDict({})
        self.att_map_V = ModuleDict({})
        self.att_map_context = ModuleDict({})
        for prot in self.attention_type.keys():
            # Token representation
            if self.attention_type[prot]['repr'] == "span":
                repr_dim = 2 * self.reduced_embedding_dim
                self.att_map_repr[prot] = Linear(self.reduced_embedding_dim,
                                                 1,
                                                 bias=False)
                self.att_map_W[prot] = Linear(self.reduced_embedding_dim,
                                              self.reduced_embedding_dim)
                self.att_map_V[prot] = Linear(self.reduced_embedding_dim,
                                              1,
                                              bias=False)
            elif self.attention_type[prot]['repr'] == "param":
                repr_dim = 2 * self.reduced_embedding_dim
                self.att_map_repr[prot] = Linear(self.reduced_embedding_dim,
                                                 self.reduced_embedding_dim,
                                                 bias=False)
                self.att_map_W[prot] = Linear(2 * self.reduced_embedding_dim,
                                              self.reduced_embedding_dim)
                self.att_map_V[prot] = Linear(self.reduced_embedding_dim,
                                              1,
                                              bias=False)
            else:
                repr_dim = self.reduced_embedding_dim

            # Context representation
            # There is no attention for argument davidsonian
            if self.attention_type[prot]['context'] == 'param':
                self.att_map_context[prot] = Linear(repr_dim,
                                                    self.reduced_embedding_dim,
                                                    bias=False)
            elif self.attention_type[prot][
                    'context'] == 'david' and prot == 'arg':
                self.att_map_context[prot] = Linear(repr_dim,
                                                    self.reduced_embedding_dim,
                                                    bias=False)
Beispiel #11
0
    def __init__(
        self,
        name: Optional[str] = None,
        tasks: Optional[Union[EmmentalTask, List[EmmentalTask]]] = None,
    ) -> None:
        """Initialize EmmentalModel."""
        super().__init__()
        self.name = name if name is not None else type(self).__name__

        # Initiate the model attributes
        self.module_pool: ModuleDict = ModuleDict()
        self.task_names: Set[str] = set()
        self.task_flows: Dict[str, Any] = dict()  # TODO: make it concrete
        self.loss_funcs: Dict[str, Callable] = dict()
        self.output_funcs: Dict[str, Callable] = dict()
        self.scorers: Dict[str, Scorer] = dict()
        self.action_outputs: Dict[
            str, Optional[List[Union[Tuple[str, str], Tuple[str, int]]]]
        ] = dict()
        self.module_device: Dict[str, Union[int, str, torch.device]] = {}
        self.task_weights: Dict[str, float] = dict()
        self.require_prob_for_evals: Dict[str, bool] = dict()
        self.require_pred_for_evals: Dict[str, bool] = dict()

        # Build network with given tasks
        if tasks is not None:
            self.add_tasks(tasks)

        if Meta.config["meta_config"]["verbose"]:
            logger.info(
                f"Created emmental model {self.name} that contains "
                f"task {self.task_names}."
            )
Beispiel #12
0
    def _init_regression(self):
        '''
            Define the linear maps
        '''

        # Output regression parameters
        self.linmaps = ModuleDict(
            {prot: ModuleList([])
             for prot in self.all_attributes.keys()})

        for prot in self.all_attributes.keys():
            last_size = self.reduced_embedding_dim
            # Handle varying size of dimension depending on representation
            if self.attention_type[prot]['repr'] == "root":
                if self.attention_type[prot]['context'] != "none":
                    last_size *= 2
            else:
                if self.attention_type[prot]['context'] == "none":
                    last_size *= 2
                else:
                    last_size *= 3
            # self.layer_norm[prot] = torch.nn.LayerNorm(last_size)
            last_size += self.hand_feat_dim
            for out_size in self.layers:
                linmap = Linear(last_size, out_size)
                self.linmaps[prot].append(linmap)
                last_size = out_size
            final_linmap = Linear(last_size, self.output_size)
            self.linmaps[prot].append(final_linmap)

        # Dropout layer
        self.dropout = Dropout()
Beispiel #13
0
    def __init__(
        self, decomposition: Dict[str, List[int]], batch_shape: torch.Size
    ) -> None:
        super().__init__(batch_shape=batch_shape)
        self.decomposition = decomposition

        num_param = len(next(iter(decomposition.values())))
        for active_parameters in decomposition.values():
            # check number of parameters are same in each decomp
            if len(active_parameters) != num_param:
                raise ValueError(
                    "num of parameters needs to be same across all contexts"
                )

        self._indexers = {
            context: torch.tensor(active_params)
            for context, active_params in self.decomposition.items()
        }

        self.base_kernel = MaternKernel(
            nu=2.5,
            ard_num_dims=num_param,
            batch_shape=batch_shape,
            lengthscale_prior=GammaPrior(3.0, 6.0),
        )

        self.kernel_dict = {}  # scaled kernel for each parameter space partition
        for context in list(decomposition.keys()):
            self.kernel_dict[context] = ScaleKernel(
                base_kernel=self.base_kernel, outputscale_prior=GammaPrior(2.0, 15.0)
            )
        self.kernel_dict = ModuleDict(self.kernel_dict)
Beispiel #14
0
 def __init__(self,
              n_atom_basis,
              n_filters,
              n_gaussians,
              cutoff,
              trainable_gauss,
              ):
     super(SchNetConv, self).__init__()
     self.moduledict = ModuleDict({
         'message_edge_filter': Sequential(
             GaussianSmearing(
                 start=0.0,
                 stop=cutoff,
                 n_gaussians=n_gaussians,
                 trainable=trainable_gauss
             ),
             Dense(in_features=n_gaussians, out_features=n_gaussians),
             shifted_softplus(),
             Dense(in_features=n_gaussians, out_features=n_filters)
         ),
         'message_node_filter': Dense(in_features=n_atom_basis, out_features=n_filters),
         'update_function': Sequential(
             Dense(in_features=n_filters, out_features=n_atom_basis),
             shifted_softplus(),
             Dense(in_features=n_atom_basis, out_features=n_atom_basis)
         )
     })
Beispiel #15
0
    def __init__(self,
                 vocabs: Dict[str, Vocabulary],
                 config: Config,
                 pre_load_model: bool = True):
        super().__init__(config=config)

        self.embeddings = ModuleDict()
        self.embeddings[const.TARGET] = TokenEmbeddings(
            num_embeddings=len(vocabs[const.TARGET]),
            pad_idx=vocabs[const.TARGET].pad_id,
            config=config.embeddings.target,
            vectors=vocabs[const.TARGET].vectors,
        )
        self.embeddings[const.SOURCE] = TokenEmbeddings(
            num_embeddings=len(vocabs[const.SOURCE]),
            pad_idx=vocabs[const.SOURCE].pad_id,
            config=config.embeddings.source,
            vectors=vocabs[const.SOURCE].vectors,
        )

        total_size = sum(emb.size() for emb in self.embeddings.values())
        self._sizes = {
            const.TARGET: total_size * self.config.window_size,
            const.SOURCE: total_size * self.config.window_size,
        }
Beispiel #16
0
 def __init__(self,
              model_dim,
              ff_dim,
              nheads,
              nlayers,
              dropout=0.0,
              mem_att_dropout=0.0,
              mem_dim=None,
              len_prop=True,
              rating_prop=True,
              rouge_prop=True,
              pov_prop=True):
     super(Plugin, self).__init__()
     assert len_prop or rating_prop or rouge_prop or pov_prop
     self.model_dim = model_dim
     self.tr_stack = TransformerStack(model_dim=model_dim,
                                      mem_dim=mem_dim,
                                      num_layers=nlayers,
                                      dropout=dropout,
                                      mem_att_dropout=mem_att_dropout,
                                      ff_dim=ff_dim,
                                      nheads=nheads)
     self.fns = ModuleDict()
     if len_prop:
         self.fns[ModelF.LEN_PROP] = Linear(model_dim, 1)
     if rating_prop:
         self.fns[ModelF.RATING_PROP] = Linear(model_dim, 1)
     if rouge_prop:
         self.fns[ModelF.ROUGE_PROP] = Sequential()
         self.fns[ModelF.ROUGE_PROP].add_module('lin', Linear(model_dim, 3))
         self.fns[ModelF.ROUGE_PROP].add_module('sigmoid', Sigmoid())
     if pov_prop:
         self.fns[ModelF.POV_PROP] = Sequential()
         self.fns[ModelF.POV_PROP].add_module("lin", Linear(model_dim, 4))
         self.fns[ModelF.POV_PROP].add_module('softmax', Softmax(dim=-1))
Beispiel #17
0
    def __init__(
        self,
        name: Optional[str] = None,
        tasks: Optional[Union[EmmentalTask, List[EmmentalTask]]] = None,
    ) -> None:
        super().__init__()
        self.name = name if name is not None else type(self).__name__

        # Initiate the model attributes
        self.module_pool: ModuleDict = ModuleDict()
        self.task_names: Set[str] = set()
        self.task_flows: Dict[str, Any] = dict()  # TODO: make it concrete
        self.loss_funcs: Dict[str, Callable] = dict()
        self.output_funcs: Dict[str, Callable] = dict()
        self.scorers: Dict[str, Scorer] = dict()
        self.weights: Dict[str, float] = dict()

        # Build network with given tasks
        if tasks is not None:
            self._build_network(tasks)

        if Meta.config["meta_config"]["verbose"]:
            logger.info(f"Created emmental model {self.name} that contains "
                        f"task {self.task_names}.")

        # Move model to specified device
        self._move_to_device()
    def __init__(
            self,
            module_dict: ModuleDict or dict,
            reference_values: torch.Tensor = None,
            dimensions: ModuleDimensions = None
    ):
        r"""
        Initialization

        Args:
            module_dict (): Only support the following form of ``modules``:
                ModuleDict[str:ShapleyModule]: this contains already
                initialized torch
                    modules, each of which will be wrapped in a
                    :class:`~{\sc ShapNet}.ShapleyModule`.
            reference_values (): the reference values for each of the variables
                Defaults to zeros
        """
        # input validation: correct the type of the module_dict argument
        module_dict = module_dict if not isinstance(module_dict, ModuleDict) \
            else ModuleDict(module_dict)
        module_dim = list(module_dict.values())[0].dimensions
        # input validation: correct the input for dimensions
        if dimensions is None:
            dimensions = ModuleDimensions(
                features=self._get_input_features(module_dict),
                in_channel=module_dim.in_channel,
                out_channel=module_dim.out_channel
            )

        super(ShallowShapleyNetwork, self).__init__(
            dimensions=dimensions,
            reference_values=reference_values,
        )
        if isinstance(module_dict, ModuleDict):
            self.module_dict = module_dict
        else:
            self.module_dict = ModuleDict(module_dict)
        # align the reference values of the entire model with those of the 
        # Shapley modules
        self.align_reference_values()

        assert len(self.module_dict) != 0
        assert isinstance(self.module_dict, ModuleDict)
        assert isinstance(list(module_dict.values())[0], ShapleyModule)
Beispiel #19
0
    def _init_outputs(self, params, input_dim):
        """Internal method to initialize the output modules of the model."""
        outputs = ModuleDict({outp_id: self._init_output(params[outp_id], input_dim) for outp_id in params})

        # Save output_vocabs to model directory if model is being saved
        if self.saving:
            self._save_output_vocabs(outputs)

        return outputs
Beispiel #20
0
 def __init__(self, model_dict, mode='sum'):
     """Summary
     
     Args:
         model_dict (TYPE): Description
         mode (str, optional): Description
     """
     super().__init__()
     self.models = ModuleDict(model_dict)
Beispiel #21
0
    def __init__(self,
                 hparams: "SentenceEmbeddings.Options",
                 statistics,
                 plugins=None):

        super().__init__()
        self.hparams = hparams
        self.mode = hparams.mode
        self.plugins = ModuleDict(plugins) if plugins is not None else {}

        # embedding
        input_dims = {}
        if hparams.dim_word != 0:
            self.word_embeddings = Embedding(len(statistics.words),
                                             hparams.dim_word,
                                             padding_idx=0)
            self.word_dropout = FeatureDropout2(hparams.word_dropout)
            input_dims["word"] = hparams.dim_word

        if hparams.dim_postag != 0:
            self.pos_embeddings = Embedding(len(statistics.postags),
                                            hparams.dim_postag,
                                            padding_idx=0)
            self.pos_dropout = FeatureDropout2(hparams.postag_dropout)
            input_dims["postag"] = hparams.dim_postag

        if hparams.dim_char > 0:
            self.bilm = None
            self.character_lookup = Embedding(len(statistics.characters),
                                              hparams.dim_char_input)
            self.char_embeded = CharacterEmbedding.get(
                hparams.character_embedding,
                dim_char_input=hparams.dim_char_input,
                input_size=hparams.dim_char)
            if not hparams.replace_unk_with_chars:
                input_dims["char"] = hparams.dim_char
            else:
                assert hparams.dim_word == hparams.dim_char
        else:
            self.character_lookup = None

        for name, plugin in self.plugins.items():
            input_dims[name] = plugin.output_dim

        if hparams.mode == "concat":
            self.output_dim = sum(input_dims.values())
        else:
            assert hparams.mode == "add"
            uniq_input_dims = list(set(input_dims.values()))
            if len(uniq_input_dims) != 1:
                raise ValueError(f"Different input dims: {input_dims}")
            print(input_dims)
            self.output_dim = uniq_input_dims[0]

        self.input_layer_norm = LayerNorm(self.output_dim, eps=1e-6) \
            if hparams.input_layer_norm else None
Beispiel #22
0
    def __init__(self, in_channels, out_channels, node_types, edge_types):
        super(RGCNConv, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels

        # `ModuleDict` does not allow tuples :(
        self.rel_lins = ModuleDict({
            f'{key[0]}_{key[1]}_{key[2]}': Linear(in_channels, out_channels,
                                                  bias=False)
            for key in edge_types
        })

        self.root_lins = ModuleDict({
            key: Linear(in_channels, out_channels, bias=True)
            for key in node_types
        })

        self.reset_parameters()
 def create_metric_computers(self) -> ModuleDict:
     """
     Gets a set of objects that compute all the metrics for the type of model that is being trained,
     across all prediction targets (sequence positions when using a sequence model).
     :return: A dictionary mapping from names of prediction targets to a list of metric computers.
     """
     # The metric computers should be stored in an object that derives from torch.Module,
     # so that they are picked up when moving the whole LightningModule to GPU.
     # https://github.com/PyTorchLightning/pytorch-lightning/issues/4713
     return ModuleDict({p: self._get_metrics_computers() for p in self.target_names})
Beispiel #24
0
    def __init__(self, num_channels: int, path_depth: int, path_lengths: Sequence[int],
                 dropout: float=0.0, bottleneck_blocks: bool=False, cyclic: bool=False):
        """Creates a new PathConvolutionLayer with the given parameters.

        Parameters
        ----------
        num_channels : int
            Number of channels to use.
        path_depth : int
            Depth of path transformation.
        dropout : float
            Use dropout between layers.
        bottleneck_blocks : bool
            Whether to use bottleneck residual blocks.
        cyclic : bool
            If True, indicates that the path is cyclic and should use cyclic padding and symmetric convolution.
        """
        super().__init__()

        self.path_lengths = tuple(path_lengths)
        self.path_keys = [str(k) for k in path_lengths]

        self.path_blocks = ModuleDict()
        self.atom_to_path_linear = ModuleDict()
        self.path_to_atom_linear = ModuleDict()

        if cyclic:
            padding_mode = 'circular'
            use_symmetric_convs = True
        else:
            padding_mode = 'zeros'
            use_symmetric_convs = False

        # Construct layers that convolve over paths and move to/from atom activations
        for k, key in zip(self.path_lengths, self.path_keys):
            self.path_blocks[key] = blocks.PathBlock(
                num_channels, k, num_resid_blocks=path_depth,
                padding_mode=padding_mode, use_symmetric_convs=use_symmetric_convs,
                dropout=dropout, bottleneck_blocks=bottleneck_blocks)
            self.atom_to_path_linear[key] = Linear(num_channels, num_channels)
            self.path_to_atom_linear[key] = Linear(num_channels, num_channels)
Beispiel #25
0
 def make_linear_soup(self):
     soup = ModuleDict()
     for block_idx in range(self.n_blocks):
         block_channels = max(1, 32*block_idx)
         soup[str(block_idx)] = ModuleDict()
         for i in range(self.block_size):
             if i > 0:
                 prev_layers = list(range(max(-1, i-self.random_input_factor[block_idx][i]), i))
             else:
                 prev_layers = [-1]
             random_layer = random.sample(layer_stock, 1)[0]
             layer = get_soup_layer(random_layer, block_channels, block_channels)
             layer.num_receptors = self.num_receptors[block_idx][i]
             soup[str(block_idx)][f'{i}_{prev_layers}'] = layer
             if layer.num_receptors > 1:
                 soup[str(block_idx)][f'{i}_{prev_layers}_connector'] = \
                     ConcatThenOneByOne(block_channels, layer.num_receptors)
     block_channels += 32
     if self.paths > 1:
         soup['final_connector'] = ConcatThenOneByOne(block_channels, self.paths)
     return soup
Beispiel #26
0
    def __init__(self, model_dict, mode='sum'):
        super().__init__()
        implemented_mode = ['sum', 'mean']

        if mode not in implemented_mode:
            raise NotImplementedError(
                '{} mode is not implemented for Stack'.format(mode))

        # to implement a check for readout keys

        self.models = ModuleDict(model_dict)
        self.mode = mode
Beispiel #27
0
    def __init__(
        self,
        vocab: Vocabulary,
        source_embedder: TextFieldEmbedder,
        embedding_dropout: float,
        encoder: Seq2VecEncoder,
        max_decoding_steps: int,
        beam_size: int = 10,
        target_names: List[str] = None,
        target_namespace: str = "tokens",
        target_embedding_dim: int = None,
        regularizer: Optional[RegularizerApplicator] = None,
    ) -> None:
        super().__init__(vocab, regularizer)
        target_names = target_names or ["xintent", "xreact", "oreact"]

        # Note: The original tweaks the embeddings for "personx" to be the mean
        # across the embeddings for "he", "she", "him" and "her". Similarly for
        # "personx's" and so forth. We could consider that here as a well.
        self._source_embedder = source_embedder
        self._embedding_dropout = nn.Dropout(embedding_dropout)
        self._encoder = encoder
        self._max_decoding_steps = max_decoding_steps
        self._target_namespace = target_namespace

        # We need the start symbol to provide as the input at the first timestep of decoding, and
        # end symbol as a way to indicate the end of the decoded sequence.
        self._start_index = self.vocab.get_token_index(START_SYMBOL,
                                                       self._target_namespace)
        self._end_index = self.vocab.get_token_index(END_SYMBOL,
                                                     self._target_namespace)
        # Warning: The different decoders share a vocabulary! This may be
        # counterintuitive, but consider the case of xreact and oreact. A
        # reaction of "happy" could easily apply to both the subject of the
        # event and others. This could become less appropriate as more decoders
        # are added.
        num_classes = self.vocab.get_vocab_size(self._target_namespace)
        # Decoder output dim needs to be the same as the encoder output dim since we initialize the
        # hidden state of the decoder with that of the final hidden states of the encoder.
        self._decoder_output_dim = self._encoder.get_output_dim()
        target_embedding_dim = target_embedding_dim or self._source_embedder.get_output_dim(
        )

        self._states = ModuleDict()
        for name in target_names:
            self._states[name] = StateDecoder(num_classes,
                                              target_embedding_dim,
                                              self._decoder_output_dim)

        self._beam_search = BeamSearch(self._end_index,
                                       beam_size=beam_size,
                                       max_steps=max_decoding_steps)
Beispiel #28
0
def construct_module_dict(moduledict):
    """construct moduledict from a dictionary of layers
    
    Args:
        moduledict (dict): Description
    
    Returns:
        ModuleDict: Description
    """
    models = ModuleDict()
    for key in moduledict:
        models[key] = construct_sequential(moduledict[key])
    return models
Beispiel #29
0
    def param_dict(self) -> ModuleDict:
        p = ModuleDict()
        for process_name, process in self.processes.items():
            p[f"process:{process_name}"] = process.param_dict()

        p['measure_cov'] = self.measure_covariance.param_dict()

        p['init_state'] = ParameterDict([('mean', self.init_mean_params)])
        p['init_state'].update(self.init_covariance.param_dict().items())

        p['process_cov'] = self.process_covariance.param_dict()

        return p
Beispiel #30
0
 def _init_submodule(self, module: Module, target: str) -> Module:
     if isinstance(module, ModuleList) or isinstance(module, Sequential):
         return ModuleList([
             self._init_submodule(submodule, f'{target}.{i}')
             for i, submodule in enumerate(module)
         ])
     elif isinstance(module, ModuleDict):
         return ModuleDict({
             key: self._init_submodule(submodule, f'{target}.{key}')
             for key, submodule in module.items()
         })
     else:
         return self.init_submodule(module, target)