Esempio n. 1
0
    def topology_factory(key, seq_list) -> Topology:
        fourth_sequence_excluded = np.array([[0.8, 0.1, 0.1, 0.0],
                                             [0.1, 0.8, 0.1, 0.0],
                                             [0.1, 0.1, 0.8, 0.0],
                                             [1.0, 0.0, 0.0, 0.0]])
        cycle_except_third = np.array([[0.0, 1.0, 0.0, 0.0],
                                       [0.0, 0.0, 0.0, 1.0],
                                       [0.0, 0.0, 0.0, 1.0],
                                       [1.0, 0.0, 0.0, 0.0]])
        sequence_table = {
            "s0":
            SequenceGenerator([
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [5, 4, 3, 5, 4, 3, 5, 4, 3],
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [5, 4, 3, 5, 4, 3, 5, 4, 3],
            ], diagonal_transition_matrix(4, 0.8)),
            "s1":
            SequenceGenerator([
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [2, 1, 3, 2, 1, 3, 2, 1, 3],
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [5, 4, 3, 5, 4, 3, 5, 4, 3],
            ], diagonal_transition_matrix(4, 0.8)),
            "s2":
            SequenceGenerator(
                [[1, 2, 3, 1, 2, 3, 1, 2, 3], [2, 1, 3, 2, 1, 3, 2, 1, 3],
                 [1, 2, 3, 1, 2, 3, 1, 2, 3], [1]], fourth_sequence_excluded),
            "test":
            SequenceGenerator([
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [2, 1, 3, 2, 1, 3, 2, 1, 3],
                [1, 2, 3, 1, 2, 3, 1, 2, 3],
                [5, 4, 3, 5, 4, 3, 5, 4, 3],
            ], cycle_except_third)
        }

        seqs = [sequence_table[seq] for seq in seq_list]

        if key == "gl_nn":
            model = GlNnTopology(seqs, args.n_predictors)
        elif key == "gl_nn_fg":
            model = GlFakeGateNnTopology()
        else:
            raise NotImplementedError

        model.assign_ids()

        return model
Esempio n. 2
0
    def __init__(self):
        super().__init__(device='cpu')
        generator = SequenceGenerator([[1, 2, 3], [4, 5, 6]],
                                      np.array([0.5, 0.5]))
        seq_node = SequenceNode(generator)

        self.add_node(seq_node)
Esempio n. 3
0
def test_multiple():
    seq_1 = [1, 2, 3]
    seq_2 = [9, 8]
    transitions = np.matrix([[0., 1.], [1., 0.]])
    combined_seq = seq_1 + seq_2
    iterator = SequenceGenerator.from_multiple([seq_1, seq_2], transitions)
    for expected, actual in zip(combined_seq, iterator):
        assert expected == actual
Esempio n. 4
0
def test_seq_node():
    seq = [1, 2, 3, 4, 5, 10, 2, 2]
    iterator = SequenceGenerator.from_list(seq)
    node = SequenceNode(seq=iterator)
    node._prepare_unit(AllocatingCreator(device='cpu'))
    for element in seq * 2:
        node.step()
        assert element == node.outputs.output.tensor.item()
Esempio n. 5
0
def define_sequence_generators() -> List[SequenceGenerator]:
    a = SequenceGenerator(
        [
            [1, 2, 3, 1, 2, 3, 1, 2, 3],
            [5, 4, 3, 5, 4, 3, 5, 4, 3],
            [1, 2, 3, 1, 2, 3, 1, 2, 3],
            [5, 4, 3, 5, 4, 3, 5, 4, 3],
        ]
        , diagonal_transition_matrix(4, 0.8))

    b = SequenceGenerator(
        [
            [1, 2, 3, 1, 2, 3, 1, 2, 3],
            [1, 2, 3, 1, 2, 3, 1, 2, 3],
        ]
        , diagonal_transition_matrix(2, 0.8))

    return [a, b]
Esempio n. 6
0
    def _init_node(self, creator: TensorCreator):

        device = self._device

        if self._seq_params is not None:
            # each _init_node, also the new SequenceGenerator should be created (reset the current position etc)
            # self._seq = SequenceGenerator.from_params(self._seq_params, random=self._random)
            transition_probs = self._seq_params.transition_probs if self._seq_params.transition_probs is not None \
                else SequenceGenerator.default_transition_probs(self._seq_params.seqs)
            self._seq = SequenceGenerator(self._seq_params.seqs,
                                          transition_probs,
                                          random=self._random)
            self._update_class_filter_by_sequences()
        else:
            self._seq = None

        self._sanitize_class_filter()

        class_filter = self._params.class_filter

        dataset_result = self._dataset.get_filtered(
            class_filter
        ) if class_filter is not None else self._dataset.get_all()
        self._data = dataset_result.data.type(FLOAT_TYPE_CPU) / 255.0
        self._labels = dataset_result.labels

        self._data_seq = self._get_data_sequence_iterator()

        # init the output tensors
        self.label_tensor = creator.zeros(1, device=device, dtype=torch.int64)
        self.output_sequence_id = creator.zeros(1,
                                                device=device,
                                                dtype=torch.int64)
        self.output_data = creator.zeros((28, 28),
                                         device=device,
                                         dtype=self._float_dtype)
        if self._params.one_hot_labels:
            self.output_label = creator.zeros(10,
                                              device=device,
                                              dtype=self._float_dtype)
        else:
            self.output_label = creator.zeros(1,
                                              device=device,
                                              dtype=torch.int64)
Esempio n. 7
0
    def __init__(self,
                 creator: TensorCreator,
                 params: DatasetAlphabetParams,
                 random: Optional[RandomState] = None):
        super().__init__(creator.device)
        self._validate_params(params)

        random = random or np.random.RandomState()

        # Generate all symbols
        generator = AlphabetGenerator(params.padding_right)
        all_symbols = generator.create_symbols(params.symbols)
        self.all_symbols = creator.zeros_like(all_symbols)
        self.all_symbols.copy_(all_symbols.to(creator.device))

        # Create output tensors
        shape = list(self.all_symbols.shape)
        self.output_data = creator.zeros(shape[1:], device=creator.device)
        self.output_label = creator.zeros(1,
                                          dtype=torch.int64,
                                          device=creator.device)
        self.output_sequence_id = creator.zeros(1,
                                                dtype=torch.int64,
                                                device=creator.device)
        self.output_sequence_id_one_hot = creator.zeros(
            (1, len(params.sequence_probs.seqs)),
            dtype=self._float_dtype,
            device=creator.device)

        if params.mode == DatasetAlphabetMode.SEQUENCE_PROBS:
            seqs = [
                self.convert_string_to_positions(params.symbols, seq)
                for seq in params.sequence_probs.seqs
            ]
            transition_probs = params.sequence_probs.transition_probs or SequenceGenerator.default_transition_probs(
                seqs)
            self.seq = SequenceGenerator(seqs, transition_probs, random=random)
            self._current = next(self.seq)

        self._n_symbols = shape[0]
Esempio n. 8
0
def test_range_seq():
    seq = [1, 2, 3]
    iterator = SequenceGenerator.from_list(range(1, 4))
    for element in seq * 2:
        assert element == next(iterator)
Esempio n. 9
0
def test_seq():
    seq = [1, 2, 3, 4, 5, 10, 2, 2]
    iterator = SequenceGenerator.from_list(seq)
    for element in seq * 2:
        assert element == next(iterator)
Esempio n. 10
0
    def __init__(self, sequence_generators: List[Any] = None, gamma=0.8):
        super().__init__('cpu')

        # environment
        if sequence_generators is None:
            sequence_generator_0 = SequenceGenerator(
                [
                    [1, 2, 3, 1, 2, 3, 1, 2, 3],
                    [5, 4, 3, 5, 4, 3, 5, 4, 3],
                    [1, 2, 3, 1, 2, 3, 1, 2, 3],
                    [5, 4, 3, 5, 4, 3, 5, 4, 3],
                ]
                , diagonal_transition_matrix(4, 0.8))
            sequence_generator_1 = SequenceGenerator(
                [
                    [1, 2, 3, 1, 2, 3, 1, 2, 3],
                    [5, 4, 3, 5, 4, 3, 5, 4, 3],
                    [1, 2, 3, 1, 2, 3, 1, 2, 3],
                    [5, 4, 3, 5, 4, 3, 5, 4, 3],
                ]
                , diagonal_transition_matrix(4, 0.8))
            sequence_generators = [sequence_generator_0, sequence_generator_1]

        self.sequences_len = len(sequence_generators)

        # switch
        switch_node = SwitchableSequencesNodeGroup(sequence_generators)
        self.switch_node = switch_node
        self.add_node(switch_node)

        # gate
        self.add_node(self.get_gate())

        # predictors
        self.add_node(self.get_predictors())

        # discount buffer
        def discount(inputs: List[torch.Tensor], outputs: List[torch.Tensor], memory: List[torch.Tensor]):
            cum_val = (1. - gamma) * inputs[0].squeeze() + gamma * memory[0]
            memory[0].copy_(cum_val)
            outputs[0].copy_(cum_val)
        discount_node = LambdaNode(discount, 1, [(self.get_num_predictors(),)],
                                   memory_shapes=[(self.get_num_predictors(),)],
                                   name="Discount")
        self.add_node(discount_node)

        # argmin
        def argmin(inputs: List[torch.Tensor], outputs: List[torch.Tensor]):
            outputs[0].copy_(id_to_one_hot(inputs[0].argmin(), self.get_num_predictors()))
        argmin_lambda_node = LambdaNode(argmin, 1, [(self.get_num_predictors(),)], name="Argmin")
        self.add_node(argmin_lambda_node)

        # dot
        def dot_product(inputs: List[torch.Tensor], outputs: List[torch.Tensor]):
            outputs[0].copy_(inputs[0].squeeze().dot(inputs[1].squeeze()))
        dot_node = LambdaNode(dot_product, 2, [(1,)], name="predictors error dot gate activations")
        self.dot_node = dot_node
        self.add_node(dot_node)

        # connections
        Connector.connect(switch_node.outputs.output, self.get_predictors().inputs.data)

        Connector.connect(switch_node.outputs.output, self.get_gate().inputs.data_predicted)
        Connector.connect(switch_node.outputs.sequence_num, self.get_gate().inputs.data)

        Connector.connect(self.get_predictors().outputs.predictors_activations_error, discount_node.inputs[0])
        Connector.connect(discount_node.outputs[0], argmin_lambda_node.inputs[0])
        Connector.connect(argmin_lambda_node.outputs[0], self.get_gate().inputs.predictor_activations_target)

        Connector.connect(self.get_gate().outputs.gate_activations,
                          self.get_predictors().inputs.temporal_class_distribution,
                          is_backward=True)

        Connector.connect(self.get_predictors().outputs.predictors_activations_error, dot_node.inputs[0])
        Connector.connect(self.get_gate().outputs.gate_activations, dot_node.inputs[1])
Esempio n. 11
0
                                        [0.1, 0.1, 0.0, 0.8]])
    only_third_sequence = np.array([[0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 1.0, 0.0],
                                    [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 1.0,
                                                           0.0]])
    fourth_sequence_excluded = np.array([[0.8, 0.1, 0.1, 0.0],
                                         [0.1, 0.8, 0.1, 0.0],
                                         [0.1, 0.1, 0.8, 0.0],
                                         [1.0, 0.0, 0.0, 0.0]])
    only_fourth_sequence = np.array([[0.0, 0.0, 0.0,
                                      1.0], [0.0, 0.0, 0.0, 1.0],
                                     [0.0, 0.0, 0.0, 1.0],
                                     [0.0, 0.0, 0.0, 1.0]])

    all_seq = SequenceGenerator([
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [2, 1, 3, 2, 1, 3, 2, 1, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [5, 4, 3, 5, 4, 3, 5, 4, 3],
    ], all_sequences)
    no_fourth = SequenceGenerator(
        [[1, 2, 3, 1, 2, 3, 1, 2, 3], [2, 1, 3, 2, 1, 3, 2, 1, 3],
         [1, 2, 3, 1, 2, 3, 1, 2, 3], [1]], fourth_sequence_excluded)
    only_fourth = SequenceGenerator([
        [1],
        [2],
        [3],
        [5, 4, 3, 5, 4, 3, 5, 4, 3],
    ], only_fourth_sequence)
    no_third = SequenceGenerator([
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [2, 1, 3, 2, 1, 3, 2, 1, 3],
        [1],