コード例 #1
0
ファイル: Blob.py プロジェクト: vishalbelsare/neoml
    def __pow__(self, other):
        """Computes the power of self to other.
        """
        if self.size == 0:
            raise ValueError("The blob mustn't be empty.")

        if type(other) is Blob:
            if not neoml.Utils.check_can_broadcast(self, other):
                raise ValueError("The blobs have incompatible shapes.")
            return Blob(PythonWrapper.blob_pow(self._internal, other._internal))

        return Blob(PythonWrapper.blob_pow(self._internal, float(other)))
コード例 #2
0
    def __truediv__(self, other):
        """Elementwise divides two blobs or a blob and a scalar value.
        """
        if self.size == 0:
            raise ValueError("The blob mustn't be empty.")

        if type(other) is Blob:
            if self.shape != other.shape:
                raise ValueError("The blobs must have the same shape.")
            return Blob(PythonWrapper.blob_div(self._internal, other._internal))

        return Blob(PythonWrapper.blob_div(self._internal, float(other)))
コード例 #3
0
ファイル: Blob.py プロジェクト: vishalbelsare/neoml
    def __truediv__(self, other):
        """Elementwise divides two blobs or a blob and a scalar value.
        """
        if self.size == 0:
            raise ValueError("The blob mustn't be empty.")

        if type(other) is Blob:
            if not neoml.Utils.check_can_broadcast(self, other):
                raise ValueError("The blobs have incompatible shapes.")
            return Blob(PythonWrapper.blob_div(self._internal, other._internal))

        return Blob(PythonWrapper.blob_div(self._internal, float(other)))
コード例 #4
0
ファイル: Blob.py プロジェクト: vishalbelsare/neoml
    def __gt__(self, other):
        """Returns self > other elementwise.
        """
        if self.size == 0:
            raise ValueError("The blob mustn't be empty.")

        if type(other) is Blob:
            if self.shape != other.shape:
                raise ValueError("The blobs have incompatible shapes.")
            return Blob(PythonWrapper.blob_less(other._internal, self._internal))

        return Blob(PythonWrapper.blob_less(float(other), self._internal))
コード例 #5
0
    def __sub__(self, other):
        """Elementwise subtracts two blobs or a blob and a scalar value.
        """
        if self.size == 0:
            raise ValueError("The blob shouldn't be empty.")

        if type(other) is Blob:
            if self.shape != other.shape:
                raise ValueError("The blobs should have the same shape.")
            return Blob(PythonWrapper.blob_sub(self._internal, other._internal))

        return Blob(PythonWrapper.blob_sub(self._internal, float(other)))
コード例 #6
0
def store(blob, file_path):
    """Stores the blob in a file at the specified path.

    :param file_path: the full path to the file where the blob should be stored.
    :type file_path: str
    """
    if not type(blob) is Blob:
        raise ValueError('The `blob` must be neoml.Blob.')

    if blob.size == 0:
        raise ValueError("The `blob` can't be empty.")

    PythonWrapper.store_blob(blob._internal, str(file_path))
コード例 #7
0
ファイル: AutoDiff.py プロジェクト: IKhintsitskiy/neoml
def max(a, b):
    """Takes the elementwise maximum of two blobs or a blob and a scalar value.
    """
    if type(a) is Blob:
        if a.size == 0:
            raise ValueError("The blob shouldn't be empty.")
        return Blob(PythonWrapper.blob_max(a._internal, float(b)))
    elif type(b) is Blob:
        if b.size == 0:
            raise ValueError("The blob shouldn't be empty.")
        return Blob(PythonWrapper.blob_max(float(a), b._internal))

    raise ValueError('At least one of `a` and `b` should be neoml.Blob.')
コード例 #8
0
    def __init__(self, input_layers, head_count=1, hidden_size=1, dropout=0., feed_forward_size=1, activation='relu', name=None):

        if type(input_layers) is PythonWrapper.TransformerEncoder:
            super().__init__(input_layers)
            return

        if head_count < 1:
            raise ValueError('The `head_count` must be > 0.')

        if hidden_size < 1:
            raise ValueError('The `hidden_size` must be > 0.')
        
        if hidden_size % head_count != 0:
            raise ValueError('The `hidden_size` must be a multiple of `head_count`')

        if dropout >= 1.:
            raise ValueError('The `dropout` must be < 1.')

        if feed_forward_size < 1:
            raise ValueError('The `feed_forward_size` must be > 0.')

        if activation not in self.activations:
            raise ValueError('The `activation` has invalid value')

        activation = self.activations.index(activation)
 
        layers, outputs = check_input_layers(input_layers, (1, 2))

        internal = PythonWrapper.TransformerEncoder(str(name), layers, outputs,
            int(head_count), int(hidden_size), float(dropout), int(feed_forward_size), int(activation))
        super().__init__(internal)
コード例 #9
0
ファイル: Blob.py プロジェクト: reisei/neoml
def matrix(math_engine, matrix_height, matrix_width, dtype="float32"):
    """Creates a two-dimensional blob, that is, a matrix.
    
    Parameters
    ---------
    math_engine : object
        The math engine that works with this blob.
    matrix_height : int, > 0
        The matrix height.
    matrix_width : int, > 0
        The matrix width.
    dtype : {"float32", "int32"}, default="float32"
        The type of data in the blob.
    """
    if dtype != "float32" and dtype != "int32":
        raise ValueError('The `dtype` must be one of {`float32`, `int32`}.')
    if matrix_height < 1:
        raise ValueError('The `matrix_height` must be > 0.')
    if matrix_width < 1:
        raise ValueError('The `matrix_width` must be > 0.')

    shape = numpy.array((matrix_height, matrix_width, 1, 1, 1, 1, 1),
                        dtype=numpy.int32)

    return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))
コード例 #10
0
    def __init__(self,
                 input_layer,
                 hidden_size=1,
                 dropout_rate=0.,
                 reverse_sequence=False,
                 activation='relu',
                 name=None):

        if type(input_layer) is PythonWrapper.IndRnn:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, 1)

        if hidden_size <= 0:
            raise ValueError('The `hidden_size` must be > 0.')
        if dropout_rate >= 1.:
            raise ValueError('The `dropout_rate` must be < 1.')
        if activation != 'sigmoid' and activation != 'relu':
            raise ValueError(
                'The `activation` must be one of {`sigmoid`, `relu`}')

        internal = PythonWrapper.IndRnn(str(name), layers, outputs,
                                        int(hidden_size), float(dropout_rate),
                                        bool(reverse_sequence),
                                        str(activation))
        super().__init__(internal)
コード例 #11
0
ファイル: Transform.py プロジェクト: vishalbelsare/neoml
    def __init__(self, input_layer, transforms, name=None):

        if type(input_layer) is PythonWrapper.Transform:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, 1)

        if len(transforms) != 7:
            raise ValueError('The `transforms` array must have 7 elements.')

        operations = numpy.ones(7, numpy.int32)
        parameters = numpy.ones(7, numpy.int32)
        for i in range(len(transforms)):
            if len(transforms[i]) != 2:
                raise ValueError(
                    'The `transforms` array must contain pairs (operation, value).'
                )

            operations[i] = self.rules.index(transforms[i][0])
            if transforms[i][1] < 0:
                raise ValueError('All values in `transforms` must be >= 0.')
            parameters[i] = transforms[i][1]

        internal = PythonWrapper.Transform(str(name), layers[0],
                                           int(outputs[0]), operations,
                                           parameters)
        super().__init__(internal)
コード例 #12
0
    def __init__(self, dnn, name=None):
        if type(dnn) is PythonWrapper.Source:
            super().__init__(dnn)
            return

        internal = PythonWrapper.Source(dnn, str(name))
        super().__init__(internal)
コード例 #13
0
ファイル: Lstm.py プロジェクト: valinurovdenis/neoml
    def __init__(self,
                 input_layer,
                 hidden_size=1,
                 dropout_rate=0.0,
                 activation="sigmoid",
                 reverse_seq=False,
                 name=None):

        if type(input_layer) is PythonWrapper.Lstm:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, (1, 3))

        if hidden_size <= 0:
            raise ValueError('The `hidden_size` must be > 0.')

        if dropout_rate < 0 or dropout_rate >= 1:
            raise ValueError('The `dropout_rate` must be in [0, 1).')

        recurrent_activation_index = self.activations.index(activation)

        internal = PythonWrapper.Lstm(str(name), layers, outputs,
                                      int(hidden_size), float(dropout_rate),
                                      recurrent_activation_index,
                                      bool(reverse_seq))
        super().__init__(internal)
コード例 #14
0
    def __init__(self, input_layers, dimensions=None, name=None):

        if dimensions is None:
            dimensions = [(1, 1)]

        if not type(dimensions) is list:
            raise ValueError(
                '`dimensions` must be a list of elements like (VectorCount, VectorSize).'
            )

        if any(not type(d) is tuple for d in dimensions):
            raise ValueError(
                '`dimensions` must be a list of elements like (VectorCount, VectorSize).'
            )

        if any(len(d) != 2 for d in dimensions):
            raise ValueError(
                '`dimensions` must be a list of elements like (VectorCount, VectorSize).'
            )

        if any(d[0] < 0 or d[1] < 1 for d in dimensions):
            raise ValueError(
                '`dimensions` must be a list of elements like (VectorCount, VectorSize).'
            )

        if type(input_layers) is PythonWrapper.MultichannelLookup:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, 0)

        internal = PythonWrapper.MultichannelLookup(str(name), layers, outputs,
                                                    dimensions)
        super().__init__(internal)
コード例 #15
0
    def __init__(self,
                 input_layers,
                 score,
                 hidden_size,
                 output_object_size,
                 output_seq_len,
                 name=None):

        if type(input_layers) is PythonWrapper.AttentionDecoder:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, 2)

        score_index = self.scores.index(score)

        if output_object_size <= 0:
            raise ValueError('`output_object_size` must be > 0.')

        if output_seq_len <= 0:
            raise ValueError('`output_seq_len` must be > 0.')

        if hidden_size <= 0:
            raise ValueError('`hidden_size` must be > 0.')

        internal = PythonWrapper.AttentionDecoder(str(name), layers[0],
                                                  int(outputs[0]), layers[1],
                                                  int(outputs[1]), score_index,
                                                  int(output_object_size),
                                                  int(output_seq_len),
                                                  int(hidden_size))
        super().__init__(internal)
コード例 #16
0
    def __init__(self,
                 input_layer,
                 head_count,
                 hidden_size,
                 output_size,
                 dropout_rate,
                 name=None):

        if type(input_layer) is PythonWrapper.MultiheadAttention:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, (3, 4))

        if head_count < 1:
            raise ValueError('The `head_count` must be > 0.')

        if hidden_size < 1:
            raise ValueError('The `hidden_size` must be > 0.')

        if output_size < 1:
            raise ValueError('The `output_size` must be > 0.')

        internal = PythonWrapper.MultiheadAttention(str(name), layers, outputs,
                                                    int(head_count),
                                                    int(hidden_size),
                                                    int(output_size),
                                                    dropout_rate)
        super().__init__(internal)
コード例 #17
0
def matrix(math_engine, matrix_height, matrix_width, dtype="float32"):
    """Creates a two-dimensional blob, that is, a matrix.

    :param neoml.MathEngine.MathEngine math_engine: the math engine that works with this blob.

    :param matrix_height: the matrix height.
    :type matrix_height: int, > 0

    :param matrix_width: the matrix width.
    :type matrix_width: int, > 0

    :param dtype: the type of data in the blob.
    :type dtype: str, {"float32", "int32"}, default="float32"
    """
    if dtype != "float32" and dtype != "int32":
        raise ValueError('The `dtype` must be one of {`float32`, `int32`}.')
    if matrix_height < 1:
        raise ValueError('The `matrix_height` must be > 0.')
    if matrix_width < 1:
        raise ValueError('The `matrix_width` must be > 0.')

    shape = np.array((matrix_height, matrix_width, 1, 1, 1, 1, 1),
                     dtype=np.int32)

    return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))
コード例 #18
0
def list_blob(math_engine, batch_len, batch_width, list_size, channels, dtype="float32"):
    """Creates a blob with one-dimensional **Height** * **Width** * **Depth** elements.

    :param neoml.MathEngine.MathEngine math_engine: the math engine that works with this blob.

    :param batch_len: the **BatchLength** dimension of the new blob.
    :type batch_len: int, > 0

    :param batch_width: the **BatchWidth** dimension of the new blob.
    :type batch_width: int, > 0

    :param list_size: the **ListSize** dimension of the new blob.
    :type list_size: int, > 0

    :param channels: the **Channels** dimension of the new blob.
    :type channels: int, > 0

    :param dtype: the type of data in the blob.
    :type dtype: str, {"float32", "int32"}, default="float32"
    """
    if dtype != "float32" and dtype != "int32":
        raise ValueError('The `dtype` must be one of {`float32`, `int32`}.')
    if batch_len < 1:
        raise ValueError('The `batch_len` must be > 0.')
    if batch_width < 1:
        raise ValueError('The `batch_width` must be > 0.')
    if list_size < 1:
        raise ValueError('The `list_size` must be > 0.')
    if channels < 1:
        raise ValueError('The `channels` must be > 0.')

    shape = numpy.array((batch_len, batch_width, list_size, 1, 1, 1, channels), dtype=numpy.int32, copy=False)

    return Blob(PythonWrapper.tensor(math_engine._internal, shape, dtype))
コード例 #19
0
ファイル: Crf.py プロジェクト: vishalbelsare/neoml
    def __init__(self,
                 input_layer,
                 class_count,
                 padding=0,
                 dropout_rate=0.0,
                 name=None):

        if type(input_layer) is PythonWrapper.Crf:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, (1, 2))

        if class_count < 1:
            raise ValueError('The `class_count` must be > 0.')

        if padding < 0:
            raise ValueError('The `padding` must be >= 0.')

        if float(dropout_rate) < 0 or float(dropout_rate) >= 1:
            raise ValueError('The `dropout_rate` must be in [0, 1).')

        internal = PythonWrapper.Crf(str(name), layers, outputs, class_count,
                                     padding, dropout_rate)
        super().__init__(internal)
コード例 #20
0
    def __init__(self,
                 input_layer,
                 hidden_size=1,
                 identity_scale=1.,
                 input_weight_std=1e-3,
                 reverse_seq=False,
                 name=None):

        if type(input_layer) is PythonWrapper.Irnn:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, 1)

        if hidden_size <= 0:
            raise ValueError('The `hidden_size` must be > 0.')

        if identity_scale < 0:
            raise ValueError('The `identity_scale` must be > 0.')

        if input_weight_std < 0:
            raise ValueError('The `input_weight_std` must be > 0.')

        internal = PythonWrapper.Irnn(str(name), layers, outputs,
                                      int(hidden_size), float(identity_scale),
                                      float(input_weight_std),
                                      bool(reverse_seq))
        super().__init__(internal)
コード例 #21
0
    def __init__(self, math_engine, learning_rate=0.01, l1=0, l2=0, max_gradient_norm=-1.0, moment_decay_rate=0.9):
        if isinstance(math_engine, PythonWrapper.SimpleGradient):
            super().__init__(math_engine)
            return

        internal = PythonWrapper.SimpleGradient(math_engine._internal, float(learning_rate), float(l1), float(l2),
                                                float(max_gradient_norm), float(moment_decay_rate))
        super().__init__(internal)
コード例 #22
0
    def __init__(self, input_layer, name=None):
        if type(input_layer) is PythonWrapper.BertConv:
            super().__init__(input_layer)
            return

        layers, outputs = check_input_layers(input_layer, 2)

        internal = PythonWrapper.BertConv(str(name), layers, outputs)
        super().__init__(internal)
コード例 #23
0
def cross_validation_score(classifier, X, Y, weight=None, score="accuracy", parts=5, stratified=False):
    """Performs cross-validation of the given classifier on a set of data.
    The input sample is divided into the specified number of parts, then each of them in turn serves as the testing set while all the others are taken for the training set.
    Can calculate either accuracy or F-measure.

    :param classifier: the classifier to be tested.
    :type classifier: object

    :param X: the input vectors, put into a matrix. The values will be 
        converted to ``dtype=np.float32``. If a sparse matrix is
        passed in, it will be converted to a sparse ``csr_matrix``.
    :type X: {array-like, sparse matrix} of shape (n_samples, n_features)
        
    :param Y: correct function values (``float``) for the training set vectors.
    :type Y: array-like of shape (n_samples,)

    :param weight: sample weights. If None, then all vectors are equally weighted.
    :type weight: array-like of shape (n_samples,), default=None

    :param score: the metric that should be calculated.
    :type score: str, {'accuracy', 'f1'}, default='accuracy'
  
    :param parts: the number of parts into which the input sample should be divided.
    :type parts: int, default=5

    :param stratified: specifies if the input set should be divided so that 
        the ratio of classes in each part is (almost) the same as in the input data.
    :type stratified: bool, default=False

    :return: the calculated metrics.
    :rtype: *array-like of shape (parts,)*
    """

    x = convert_data( X )
    y = numpy.array( Y, dtype=numpy.int32, copy=False )

    if x.shape[0] != y.size:
        raise ValueError('The `X` and `Y` inputs must be the same length.')

    if weight is None:
        weight = numpy.ones(y.size, numpy.float32)
    else:
        weight = numpy.array( weight, dtype=numpy.float32, copy=False )

    if numpy.any(y < 0):
        raise ValueError('All `Y` elements must be >= 0.')

    if numpy.any(weight < 0):
        raise ValueError('All `weight` elements must be >= 0.')

    if score != "accuracy" and score != "f1":
        raise ValueError('The `score` must be one of: `accuracy`, `f1`.')

    if parts <= 0 or parts >= y.size / 2:
        raise ValueError('`parts` must be in (0, vectorCount).')

    return PythonWrapper._cross_validation_score(classifier, *get_data(x), int(x.shape[1]), y, weight, score, parts, bool(stratified))
コード例 #24
0
ファイル: Qrnn.py プロジェクト: vishalbelsare/neoml
    def __init__(self,
                 input_layers,
                 pooling_type='f',
                 hidden_size=1,
                 window_size=1,
                 stride=1,
                 paddings=(0, 0),
                 activation="tanh",
                 dropout=0.0,
                 mode="direct",
                 name=None):

        if type(input_layers) is PythonWrapper.Qrnn:
            super().__init__(input_layers)
            return

        pooling_type_index = self.pooling_types.index(pooling_type)

        if hidden_size < 1:
            raise ValueError('The `hidden_size` must be > 0.')

        if window_size < 1:
            raise ValueError('The `window_size` must be > 0.')

        if stride < 1:
            raise ValueError('The `stride` must be > 0.')

        if len(paddings) != 2:
            raise ValueError(
                'The `paddings` must have two values (padding_front, padding_back).'
            )

        padding_front = paddings[0]
        if padding_front < 0:
            raise ValueError('The `padding_front` must be >= 0.')

        padding_back = paddings[1]
        if padding_back < 0:
            raise ValueError('The `padding_back` must be >= 0.')

        activation_index = self.activations.index(activation)

        if dropout < 0 or dropout >= 1:
            raise ValueError('The `dropout` must be in [0, 1).')

        mode_index = self.recurrent_modes.index(mode)

        layers, outputs = check_input_layers(input_layers, (1, 2))

        internal = PythonWrapper.Qrnn(str(name),
                                      layers, int(pooling_type_index),
                                      int(hidden_size), int(window_size),
                                      int(stride), int(padding_front),
                                      int(padding_back), activation_index,
                                      float(dropout), mode_index, outputs)
        super().__init__(internal)
コード例 #25
0
ファイル: MathEngine.py プロジェクト: neoml-lib/neoml
    def __init__(self, thread_count=None):
        if thread_count is None:
            thread_count = 0

        if isinstance(thread_count, PythonWrapper.MathEngine):
            super().__init__(thread_count)
            return

        internal = PythonWrapper.MathEngine('cpu', int(thread_count), 0)
        super().__init__(internal)
コード例 #26
0
ファイル: Crf.py プロジェクト: vishalbelsare/neoml
    def __init__(self, input_layers, name=None):

        if type(input_layers) is PythonWrapper.BestSequence:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, 2)

        internal = PythonWrapper.BestSequence(str(name), layers, outputs)
        super().__init__(internal)
コード例 #27
0
    def __init__(self, input_layers, name=None):

        if type(input_layers) is PythonWrapper.GlobalMeanPooling:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, 1)

        internal = PythonWrapper.GlobalMeanPooling(str(name), layers[0], outputs[0])
        super().__init__(internal)
コード例 #28
0
ファイル: Loss.py プロジェクト: valinurovdenis/neoml
    def __init__(self, input_layers, loss_weight=1.0, name=None):

        if type(input_layers) is PythonWrapper.MultiSquaredHingeLoss:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, (2, 3))

        internal = PythonWrapper.MultiSquaredHingeLoss(str(name), layers, outputs, float(loss_weight))
        super().__init__(internal)
コード例 #29
0
ファイル: Loss.py プロジェクト: valinurovdenis/neoml
    def __init__(self, input_layers, class_count, rate, loss_weight=1.0, name=None):

        if type(input_layers) is PythonWrapper.CenterLoss:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, (2, 3))

        internal = PythonWrapper.CenterLoss(str(name), layers, outputs, int(class_count), float(rate), float(loss_weight))
        super().__init__(internal)
コード例 #30
0
ファイル: Loss.py プロジェクト: valinurovdenis/neoml
    def __init__(self, input_layers, positive_weight=1.0, loss_weight=1.0, name=None):

        if type(input_layers) is PythonWrapper.BinaryCrossEntropyLoss:
            super().__init__(input_layers)
            return

        layers, outputs = check_input_layers(input_layers, (2, 3))

        internal = PythonWrapper.BinaryCrossEntropyLoss(str(name), layers, outputs, float(positive_weight), float(loss_weight))
        super().__init__(internal)