Exemplo n.º 1
0
    def create_message_formation(self, operations):
        """
        Parameters
        ----------
        operations:
            Array of operation dictionaries
        """

        result = []
        counter = 0

        for op in operations:
            op_type = op.get('type')
            if op_type == 'neural_network':
                result.append(
                    FeedForwardOperation(op,
                                         model_role='message_creation_' +
                                         str(counter)))

            elif op_type == 'direct_assignment':
                result.append(None)

            elif op_type == 'product':
                result.append(ProductOperation(op))
            counter += 1
        return result
Exemplo n.º 2
0
    def create_aggregations(self, attrs):
        """
        Parameters
        ----------
        attrs:    dict
            Dictionary with the required attributes for the aggregation (defining the set of operations)
        """
        aggregations = []
        single_embedding = None
        multiple_embedding = None
        for attr in attrs:
            attr_type = attr.get('type')
            if attr_type == 'interleave':
                aggregations.append(InterleaveAggr(attr))
                multiple_embedding = True
            elif attr_type == 'neural_network':
                aggregations.append(
                    FeedForwardOperation(attr, model_role='aggregation'))
                single_embedding = True
            elif attr_type == 'concat':
                aggregations.append(ConcatAggr(attr))
                multiple_embedding = True
            elif attr_type == 'sum':
                aggregations.append(SumAggr(attr))
                single_embedding = True
            elif attr_type == 'mean':
                aggregations.append(MeanAggr(attr))
                single_embedding = True
            elif attr_type == 'min':
                aggregations.append(MinAggr(attr))
                single_embedding = True
            elif attr_type == 'max':
                aggregations.append(MaxAggr(attr))
                single_embedding = True
            elif attr_type == 'std':
                aggregations.append(StdAggr(attr))
                single_embedding = True
            elif attr_type == 'attention':
                aggregations.append(AttentionAggr(attr))
                single_embedding = True
            elif attr_type == 'edge_attention':
                aggregations.append(EdgeAttentionAggr(attr))
                single_embedding = True
            elif attr_type == 'convolution':
                aggregations.append(ConvAggr(attr))
                single_embedding = True
            else:  # this is for the ordered aggregation
                multiple_embedding = True

        if single_embedding and multiple_embedding:
            raise CombinedAggregationError(
                message=
                "It is not possible to combine aggregations that return a sequence "
                "of tensors and aggregations that return a single embedding.")

        elif single_embedding:
            return aggregations, 0

        else:
            return aggregations, 1
Exemplo n.º 3
0
    def __get_readout_op(self, output_operations):
        """
        Parameters
        ----------
        output_operations:    dict
           List of dictionaries with the definition of the operations forming one readout model
        """

        result = []
        for op in output_operations:
            op_type = op.get('type')
            if op_type == 'pooling':
                result.append(PoolingOperation(op))

            elif op_type == 'product':
                result.append(ProductOperation(op))

            elif op_type == 'neural_network':
                result.append(FeedForwardOperation(self.__add_readout_architecture(op), model_role='readout'))

            elif op_type == 'extend_adjacencies':
                result.append(ExtendAdjacencies(op))

            elif op_type == 'concat':
                result.append(Concat(op))

        return result
Exemplo n.º 4
0
    def __init__(self, op):
        """
        Parameters
        ----------
        op:    dict
            Dictionary with the user's definition of this operation
        """

        super(EdgeAttentionAggr, self).__init__(op)
        del op['type']
        self.aggr_model = FeedForwardOperation(op, model_role='edge_attention')
Exemplo n.º 5
0
    def create_update(self, u):
        """
        Parameters
        ----------
        u:    dict
            Dictionary with the required attributes for the update
        """

        type_update = u.get('type')
        if type_update == 'direct_assignment':
            return None

        # it is using a neural network
        else:
            first_layer_type = u['architecture'][0]['type_layer']
            if first_layer_type == 'GRU' or first_layer_type == 'LSTM':
                return RNNOperation(u)
            else:
                return FeedForwardOperation(u, model_role='update')
Exemplo n.º 6
0
    def compute_hs_operations(self, operations):
        """
        Parameters
        ----------
        operations:    dict
            Dictionary specifying an operation to be performed as part of the pipeline to compute the
            hidden states of a given entity layer_type nodes.
        """

        hs_operations = []
        all_inputs = set()
        all_outputs = set()
        for op in operations:
            # keep track of all the necessary features (remove the initial $ to indicate that it is in the dataset)
            for input_item in op.get('input'):
                feature_name = input_item.split('$')[-1]
                all_inputs.add(feature_name)

            # keep track of the outputs
            if 'output_name' in op:
                all_outputs.add(op.get('output_name'))

            # create the new operation
            type_update = op.get('type')
            if type_update == 'neural_network':
                hs_operations.append(
                    FeedForwardOperation(op, model_role='hs_creation'))

            elif type_update == 'build_state':
                hs_operations.append(
                    BuildState(op, self.name, self.state_dimension))

        # remove all the references in the inputs that refer to a previous output
        final_inputs = list(all_inputs.difference(all_outputs))

        return hs_operations, list(final_inputs)