Python keras.backend.ndim() Examples

The following are code examples for showing how to use keras.backend.ndim(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: fancy-cnn   Author: textclf   File: timedistributed.py    MIT License 6 votes vote down vote up
def get_output(self, train=False):
        def format_shape(shape):
            if K._BACKEND == 'tensorflow':
                def trf(x):
                    try:
                        return int(x)
                    except TypeError:
                        return x

                return map(trf, shape)
            return shape

        X = self.get_input(train)

        in_shape = format_shape(K.shape(X))
        batch_flatten_len = K.prod(in_shape[:2])
        cast_in_shape = (batch_flatten_len, ) + tuple(in_shape[i] for i in range(2, K.ndim(X)))
        
        pre_outs = self.layer(K.reshape(X, cast_in_shape))
        
        out_shape = format_shape(K.shape(pre_outs))
        cast_out_shape = (in_shape[0], in_shape[1]) + tuple(out_shape[i] for i in range(1, K.ndim(pre_outs)))
        
        outputs = K.reshape(pre_outs, cast_out_shape)
        return outputs 
Example 2
Project: spektral   Author: danielegrattarola   File: pooling.py    MIT License 6 votes vote down vote up
def call(self, inputs):
        if self.data_mode == 'graph':
            X, I = inputs
            if K.ndim(I) == 2:
                I = I[:, 0]
        else:
            X = inputs
        inputs_linear = K.dot(X, self.lg_kernel) + self.lg_bias
        attn_map = K.dot(X, self.attn_kernel) + self.attn_bias
        attn_map = K.sigmoid(attn_map)
        masked_inputs = inputs_linear * attn_map
        if self.data_mode in {'single', 'batch'}:
            output = K.sum(masked_inputs, axis=-2, keepdims=self.data_mode=='single')
        else:
            output = tf.segment_sum(masked_inputs, I)

        return output 
Example 3
Project: spektral   Author: danielegrattarola   File: pooling.py    MIT License 6 votes vote down vote up
def call(self, inputs):
        if self.data_mode == 'graph':
            X, I = inputs
            if K.ndim(I) == 2:
                I = I[:, 0]
        else:
            X = inputs
        attn_coeff = K.dot(X, self.attn_kernel)
        attn_coeff = K.squeeze(attn_coeff, -1)
        attn_coeff = K.softmax(attn_coeff)
        if self.data_mode == 'single':
            output = K.dot(attn_coeff[None, ...], X)
        elif self.data_mode == 'batch':
            output = K.batch_dot(attn_coeff, X)
        else:
            output = attn_coeff[:, None] * X
            output = tf.segment_sum(output, I)

        return output 
Example 4
Project: spektral   Author: danielegrattarola   File: ops.py    MIT License 6 votes vote down vote up
def autodetect_mode(A, X):
    """
    Return a code identifying the mode of operation (single, mixed, batch),
    given A and X. See the modes variable for meaning of codes.
    :param A: Tensor.
    :param X: Tensor.
    :return: mode of operation.
    """
    if K.ndim(X) == 2:
        if K.ndim(A) == 2:
            return modes['S']
        elif K.ndim(A) == 3:
            return modes['iM']
        else:
            return modes['UNK']
    elif K.ndim(X) == 3:
        if K.ndim(A) == 2:
            return modes['M']
        elif K.ndim(A) == 3:
            return modes['B']
        else:
            return modes['UNK']
    else:
        return modes['UNK'] 
Example 5
Project: spektral   Author: danielegrattarola   File: ops.py    MIT License 6 votes vote down vote up
def matrix_power(x, k):
    """
    Computes the k-th power of a square matrix.
    :param x: a square matrix (Tensor or SparseTensor)
    :param k: exponent
    :return: matrix of same type and dtype as the input
    """
    if K.ndim(x) != 2:
        raise ValueError('x must have rank 2.')
    sparse = K.is_sparse(x)
    if sparse:
        x_dense = tf.sparse.to_dense(x)
    else:
        x_dense = x

    x_k = x_dense
    for _ in range(k - 1):
        x_k = K.dot(x_k, x_dense)

    if sparse:
        return tf.contrib.layers.dense_to_sparse(x_k)
    else:
        return x_k 
Example 6
Project: keras_extension   Author: k1414st   File: merge.py    MIT License 6 votes vote down vote up
def _merge_function(self, inputs):
        if len(inputs) != 2:
            raise ValueError('A `Dot` layer should be called '
                             'on exactly 2 inputs')
        x1 = inputs[0]
        x2 = inputs[1]
        if isinstance(self.axes, int):
            if self.axes < 0:
                axes = [self.axes % K.ndim(x1), self.axes % K.ndim(x2)]
            else:
                axes = [self.axes] * 2
        else:
            axes = []
            for i in range(len(self.axes)):
                if self.axes[i] < 0:
                    axes.append(self.axes[i] % K.ndim(inputs[i]))
                else:
                    axes.append(self.axes[i])
        if self.normalize:
            x1 = K.l2_normalize(x1, axis=axes[0])
            x2 = K.l2_normalize(x2, axis=axes[1])
        print(x1.shape, x2.shape)
        # output = K.batch_dot(x1, x2, axes)
        output = tf.sparse.matmul(x1, x2)
        return output 
Example 7
Project: zlyang-seq2seq-gec   Author: young-zonglin   File: layers.py    MIT License 6 votes vote down vote up
def call(self, inputs, **kwargs):
        """
        Mask placeholder to zero.
        :param inputs: token id seq or one-hot vec seq.
        :param kwargs:
        :return: mask seq.
        """
        input_ndim = K.ndim(inputs)
        if input_ndim == 2:
            token_id_seq = inputs
            mask = K.cast(K.greater(token_id_seq, 0), 'float32')
        elif input_ndim == 3:
            onehot_vec_seq = inputs
            # Note that the id of padding symbol is zero.
            middle_sum = K.reshape(K.sum(onehot_vec_seq[:, :, 1:], -1), (self.batch_size, -1))
            mask = K.cast(K.greater(middle_sum, 0), 'float32')
        else:
            raise ValueError('In class ' + self.__class__.__name__ +
                             ', input should be a 2D or 3D tensor.')
        return mask 
Example 8
Project: group-ksparse-temporal-cnns   Author: srph25   File: ops.py    MIT License 6 votes vote down vote up
def diff(a, n=1, axis=-1):
    if axis == -1:
        axis = K.ndim(a) - 1
    a_aug = K.tile(K.expand_dims(a, axis=1), [1, n] + [1] * (K.ndim(a) - 1))
    pattern = (axis,) + tuple(set(range(K.ndim(a))) - {axis})
    inv_pattern = tuple(range(1, axis + 1)) + (0,) + tuple(range(axis + 1, K.ndim(a)))
    def step(inputs, states):
        prev_output = states[0]
        t = states[1]
        t_int = K.cast(t[0], 'int32')
        prev_output_aug = K.permute_dimensions(prev_output, pattern)
        inputs_aug = K.permute_dimensions(inputs, pattern)
        output_aug = K.switch(K.all(t_int > 0),
                              K.concatenate([inputs_aug[:t_int], prev_output_aug[1:] - prev_output_aug[:-1]], axis=0),
                              K.concatenate([inputs_aug[:1], inputs_aug[1:] - inputs_aug[:-1]], axis=0))
        output = K.permute_dimensions(output_aug, inv_pattern)
        return output, [output, t + 1]
    d_aug = K.permute_dimensions(K.rnn(step, a_aug, [K.zeros_like(a), K.zeros((1,))])[0], pattern)
    d = K.permute_dimensions(d_aug[-(K.shape(a)[axis] - n):], inv_pattern)
    return d 
Example 9
Project: group-ksparse-temporal-cnns   Author: srph25   File: ops.py    MIT License 6 votes vote down vote up
def cumsum(a, n=1, axis=-1):
    if axis == -1:
        axis = K.ndim(a) - 1
    a_aug = K.tile(K.expand_dims(a, axis=1), [1, n] + [1] * (K.ndim(a) - 1))
    pattern = (axis,) + tuple(set(range(K.ndim(a))) - {axis})
    inv_pattern = tuple(range(1, axis + 1)) + (0,) + tuple(range(axis + 1, K.ndim(a)))
    def step(inputs, states):
        prev_output = states[0]
        t = states[1]
        t_int = K.cast(t[0], 'int32')
        prev_output_aug = K.permute_dimensions(prev_output, pattern)
        inputs_aug = K.permute_dimensions(inputs, pattern)
        output_aug = K.switch(K.all(t_int > 0), K.cumsum(prev_output_aug, axis=0), K.cumsum(inputs_aug, axis=0))
        output = K.permute_dimensions(output_aug, inv_pattern)
        return output, [output, t + 1]
    c_aug = K.permute_dimensions(K.rnn(step, a_aug, [K.zeros_like(a), K.zeros((1,))])[0], pattern)
    c = K.permute_dimensions(c_aug[-(K.shape(a)[axis] - n):], inv_pattern)
    return c 
Example 10
Project: group-ksparse-temporal-cnns   Author: srph25   File: ops.py    MIT License 6 votes vote down vote up
def ksparse(x, k, axis, alpha=1, absolute=False):
    if isinstance(axis, int):
        axis = (axis,)
    elif isinstance(axis, list):
        axis = tuple(axis)
    axis_complement = tuple(set(range(K.ndim(x))) - set(axis))
    shape_reduce = K.prod([K.shape(x)[j] for j in axis])
    _k = K.minimum(K.in_train_phase(k, alpha * k), shape_reduce)
    inputs_permute_dimensions = K.permute_dimensions(x, axis_complement + axis)
    inputs_permute_dimensions_reshape = K.reshape(inputs_permute_dimensions, (-1, shape_reduce))
    if absolute is True:
        inputs_permute_dimensions_reshape = K.abs(inputs_permute_dimensions_reshape)
    _, indices = tf.nn.top_k(inputs_permute_dimensions_reshape, _k)
    scatter_indices = K.concatenate([(K.arange(K.shape(inputs_permute_dimensions_reshape)[0])[:, None] * K.ones((1, _k), dtype='int32'))[:, :, None], indices[:, :, None]])
    scatter_updates = K.ones((K.shape(inputs_permute_dimensions_reshape)[0], _k))
    mask_permute_dimensions_reshape = K.cast(tf.scatter_nd(scatter_indices, scatter_updates, K.shape(inputs_permute_dimensions_reshape)), K.floatx())
    mask_permute_dimensions = K.reshape(mask_permute_dimensions_reshape, K.shape(inputs_permute_dimensions))
    mask = K.permute_dimensions(mask_permute_dimensions, tuple(np.argsort(axis_complement + axis)))
    return mask * x 
Example 11
Project: iMIMIC-RCVs   Author: medgift   File: rcv_utils.py    MIT License 6 votes vote down vote up
def build_loss(self):
        layer_output = self.layer.output

        # For all other layers it is 4
        is_dense = K.ndim(layer_output) == 2

        loss = 0.
        for idx in self.filter_indices:
            if is_dense:
                loss += -K.mean(layer_output[:, idx])
            else:
                # slicer is used to deal with `channels_first` or `channels_last` image data formats
                # without the ugly conditional statements.
                loss += -K.mean(layer_output[utils.slicer[:, idx, ...]])

        return loss
### regression 
Example 12
Project: iMIMIC-RCVs   Author: medgift   File: losses.py    MIT License 6 votes vote down vote up
def build_loss(self):
        layer_output = self.layer.output

        # For all other layers it is 4
        is_dense = K.ndim(layer_output) == 2

        loss = 0.
        for idx in self.filter_indices:
            if is_dense:
                loss += -K.mean(layer_output[:, idx])
            else:
                # slicer is used to deal with `channels_first` or `channels_last` image data formats
                # without the ugly conditional statements.
                loss += -K.mean(layer_output[utils.slicer[:, idx, ...]])

        return loss 
Example 13
Project: iMIMIC-RCVs   Author: medgift   File: __init__.py    MIT License 6 votes vote down vote up
def get_num_filters(layer):
    """Determines the number of filters within the given `layer`.

    Args:
        layer: The keras layer to use.

    Returns:
        Total number of filters within `layer`.
        For `keras.layers.Dense` layer, this is the total number of outputs.
    """
    # Handle layers with no channels.
    if K.ndim(layer.output) == 2:
        return K.int_shape(layer.output)[-1]

    channel_idx = 1 if K.image_data_format() == 'channels_first' else -1
    return K.int_shape(layer.output)[channel_idx] 
Example 14
Project: SPECT-CT-Seg-ResUNet-Keras   Author: junyuchen245   File: custom_losses.py    MIT License 6 votes vote down vote up
def exp_dice_loss(exp=1.0):
    """
    :param exp: exponent. 1.0 for no exponential effect, i.e. log Dice.
    """

    def inner(y_true, y_pred):
        """Computes the average exponential log Dice coefficients as the loss function.
        :param y_true: one-hot tensor multiplied by label weights, (batch size, number of pixels, number of labels).
        :param y_pred: softmax probabilities, same shape as y_true. Each probability serves as a partial volume.
        :return: average exponential log Dice coefficient.
        """

        dice = dice_coef(y_true, y_pred)
        #dice = generalized_dice(y_true, y_pred, exp)
        dice = K.clip(dice, K.epsilon(), 1 - K.epsilon())  # As log is used
        dice = K.pow(-K.log(dice), exp)
        if K.ndim(dice) == 2:
            dice = K.mean(dice, axis=-1)
        return dice

    return inner 
Example 15
Project: RPGOne   Author: RTHMaK   File: shareable_gru.py    Apache License 2.0 6 votes vote down vote up
def call(self, x, mask=None, **kwargs):
        input_shape = K.int_shape(x)
        res = super(ShareableGRU, self).call(x, mask, **kwargs)
        self.input_spec = InputSpec(shape=(self.input_spec.shape[0],
                                           None,
                                           self.input_spec.shape[2]))
        if K.ndim(x) == K.ndim(res) and K.backend() == 'tensorflow':
            # A recent change in Keras
            # (https://github.com/fchollet/keras/commit/a9b6bef0624c67d6df1618ca63d8e8141b0df4d0)
            # made it so that K.rnn with a tensorflow backend does not retain shape information for
            # the sequence length, even if it's present in the input.  We need to fix that here so
            # that our models have the right shape information.  A simple K.reshape is good enough
            # to fix this.
            # And, we need to hide this behind a check for K.backend() because K.int_shape(res)
            # doesn't work in Theano, because "res is not a Keras tensor"...
            result_shape = K.int_shape(res)
            if input_shape[1] is not None and result_shape[1] is None:
                shape = (input_shape[0] if input_shape[0] is not None else -1,
                         input_shape[1], result_shape[2])
                res = K.reshape(res, shape=shape)
        return res 
Example 16
Project: RPGOne   Author: RTHMaK   File: bag_of_words.py    Apache License 2.0 6 votes vote down vote up
def call(self, inputs, mask=None):
        # pylint: disable=redefined-variable-type
        if mask is None:
            return K.mean(inputs, axis=1)
        else:
            # Compute weights such that masked elements have zero weights and the remaining
            # weight is ditributed equally among the unmasked elements.
            # Mask (samples, num_words) has 0s for masked elements and 1s everywhere else.
            # Mask is of type int8. While theano would automatically make weighted_mask below
            # of type float32 even if mask remains int8, tensorflow would complain. Let's cast it
            # explicitly to remain compatible with tf.
            float_mask = K.cast(mask, 'float32')
            # Expanding dims of the denominator to make it the same shape as the numerator, epsilon added to avoid
            # division by zero.
            # (samples, num_words)
            weighted_mask = float_mask / (K.sum(float_mask, axis=1, keepdims=True) + K.epsilon())
            if K.ndim(weighted_mask) < K.ndim(inputs):
                weighted_mask = K.expand_dims(weighted_mask)
            return K.sum(inputs * weighted_mask, axis=1)  # (samples, word_dim) 
Example 17
Project: RPGOne   Author: RTHMaK   File: time_distributed.py    Apache License 2.0 6 votes vote down vote up
def reshape_inputs_and_masks(inputs, masks):
        reshaped_xs = []
        reshaped_masks = []
        for x_i, mask_i in zip(inputs, masks):
            input_shape = K.int_shape(x_i)
            reshaped_x = K.reshape(x_i, (-1,) + input_shape[2:])  # (batch_size * timesteps, ...)
            if mask_i is not None:
                mask_ndim = K.ndim(mask_i)
                input_ndim = K.ndim(x_i)
                if mask_ndim == input_ndim:
                    mask_shape = input_shape
                elif mask_ndim == input_ndim - 1:
                    mask_shape = input_shape[:-1]
                else:
                    raise Exception("Mask is of an unexpected shape. Mask's ndim: %s, input's ndim %s" %
                                    (mask_ndim, input_ndim))
                mask_i = K.reshape(mask_i, (-1,) + mask_shape[2:])  # (batch_size * timesteps, ...)
            reshaped_xs.append(reshaped_x)
            reshaped_masks.append(mask_i)
        if len(inputs) == 1:
            reshaped_xs = reshaped_xs[0]
            reshaped_masks = reshaped_masks[0]
        return reshaped_xs, reshaped_masks 
Example 18
Project: RPGOne   Author: RTHMaK   File: batch_dot.py    Apache License 2.0 6 votes vote down vote up
def call(self, inputs, mask=None):
        tensor_a, tensor_b = inputs
        if (K.ndim(tensor_a) > 3 or K.ndim(tensor_b) > 3) and K.backend() == 'theano':
            raise RuntimeError("K.batch_dot() in theano is broken for tensors with more than"
                               " three dimensions.  Use tensorflow instead.")

        a_dot_axis = K.ndim(tensor_a) - 1
        b_dot_axis = K.ndim(tensor_b) - 1
        if a_dot_axis > b_dot_axis:
            tensor_b = K.expand_dims(tensor_b, axis=-1)
        if a_dot_axis < b_dot_axis:
            tensor_a = K.expand_dims(tensor_a, axis=-1)
        a_dot_b = K.batch_dot(tensor_a, tensor_b, axes=(a_dot_axis, b_dot_axis))
        if a_dot_axis != b_dot_axis:
            a_dot_b = K.squeeze(a_dot_b, axis=a_dot_axis)
        return a_dot_b 
Example 19
Project: workspace_2017   Author: nwiizo   File: neural_doodle.py    MIT License 6 votes vote down vote up
def region_style_loss(style_image, target_image, style_mask, target_mask):
    '''Calculate style loss between style_image and target_image,
    for one common region specified by their (boolean) masks
    '''
    assert 3 == K.ndim(style_image) == K.ndim(target_image)
    assert 2 == K.ndim(style_mask) == K.ndim(target_mask)
    if K.image_dim_ordering() == 'th':
        masked_style = style_image * style_mask
        masked_target = target_image * target_mask
        nb_channels = K.shape(style_image)[0]
    else:
        masked_style = K.permute_dimensions(
            style_image, (2, 0, 1)) * style_mask
        masked_target = K.permute_dimensions(
            target_image, (2, 0, 1)) * target_mask
        nb_channels = K.shape(style_image)[-1]
    s = gram_matrix(masked_style) / K.mean(style_mask) / nb_channels
    c = gram_matrix(masked_target) / K.mean(target_mask) / nb_channels
    return K.mean(K.square(s - c)) 
Example 20
Project: workspace_2017   Author: nwiizo   File: neural_doodle.py    MIT License 6 votes vote down vote up
def style_loss(style_image, target_image, style_masks, target_masks):
    '''Calculate style loss between style_image and target_image,
    in all regions.
    '''
    assert 3 == K.ndim(style_image) == K.ndim(target_image)
    assert 3 == K.ndim(style_masks) == K.ndim(target_masks)
    loss = K.variable(0)
    for i in xrange(nb_labels):
        if K.image_dim_ordering() == 'th':
            style_mask = style_masks[i, :, :]
            target_mask = target_masks[i, :, :]
        else:
            style_mask = style_masks[:, :, i]
            target_mask = target_masks[:, :, i]
        loss += region_style_loss(style_image,
                                  target_image, style_mask, target_mask)
    return loss 
Example 21
Project: gandlf   Author: codekansas   File: core.py    MIT License 5 votes vote down vote up
def call(self, x, mask=None):
        sims = []
        for n, sim in zip(self.n, self.similarities):
            for _ in range(n):
                batch_size = K.shape(x)[0]
                idx = K.random_uniform((batch_size,), low=0, high=batch_size,
                                       dtype='int32')
                x_shuffled = K.gather(x, idx)
                pair_sim = sim(x, x_shuffled)
                for _ in range(K.ndim(x) - 1):
                    pair_sim = K.expand_dims(pair_sim, dim=1)
                sims.append(pair_sim)

        return K.concatenate(sims, axis=-1) 
Example 22
Project: FasterRCNN_KERAS   Author: akshaylamba   File: FixedBatchNormalization.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 23
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def gram_matrix(x):
    assert K.ndim(x) == 3
    if K.image_data_format() == 'channels_first':
        features = K.batch_flatten(x)
    else:
        features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
    gram = K.dot(features, K.transpose(features))
    return gram

# the "style loss" is designed to maintain
# the style of the reference image in the generated image.
# It is based on the gram matrices (which capture style) of
# feature maps from the style reference image
# and from the generated image 
Example 24
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def style_loss(style, combination):
    assert K.ndim(style) == 3
    assert K.ndim(combination) == 3
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return K.sum(K.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))

# an auxiliary loss function
# designed to maintain the "content" of the
# base image in the generated image 
Example 25
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def total_variation_loss(x):
    assert K.ndim(x) == 4
    if K.image_data_format() == 'channels_first':
        a = K.square(
            x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1])
        b = K.square(
            x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:])
    else:
        a = K.square(
            x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :])
        b = K.square(
            x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :])
    return K.sum(K.pow(a + b, 1.25))

# combine these loss functions into a single scalar 
Example 26
Project: deepflying   Author: dslab-deepflying   File: nst.py    GNU General Public License v3.0 5 votes vote down vote up
def gram_matrix(x):
    assert K.ndim(x) == 3
    if K.image_data_format() == 'channels_first':
        features = K.batch_flatten(x)
    else:
        features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
    gram = K.dot(features, K.transpose(features))
    return gram 
Example 27
Project: deepflying   Author: dslab-deepflying   File: nst.py    GNU General Public License v3.0 5 votes vote down vote up
def style_loss(style, combination):
    assert K.ndim(style) == 3
    assert K.ndim(combination) == 3
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return K.sum(K.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2)) 
Example 28
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def gram_matrix(x):
    assert K.ndim(x) == 3
    if K.image_data_format() == 'channels_first':
        features = K.batch_flatten(x)
    else:
        features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
    gram = K.dot(features, K.transpose(features))
    return gram

# the "style loss" is designed to maintain
# the style of the reference image in the generated image.
# It is based on the gram matrices (which capture style) of
# feature maps from the style reference image
# and from the generated image 
Example 29
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def style_loss(style, combination):
    assert K.ndim(style) == 3
    assert K.ndim(combination) == 3
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return K.sum(K.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))

# an auxiliary loss function
# designed to maintain the "content" of the
# base image in the generated image 
Example 30
Project: deepflying   Author: dslab-deepflying   File: neural_style_transfer.py    GNU General Public License v3.0 5 votes vote down vote up
def total_variation_loss(x):
    assert K.ndim(x) == 4
    if K.image_data_format() == 'channels_first':
        a = K.square(
            x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1])
        b = K.square(
            x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:])
    else:
        a = K.square(
            x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :])
        b = K.square(
            x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :])
    return K.sum(K.pow(a + b, 1.25))

# combine these loss functions into a single scalar 
Example 31
Project: ICASSP2019_TCN   Author: DSIP-UPatras   File: custom_layers.py    MIT License 5 votes vote down vote up
def call(self, inputs, mask=None):
        if mask is not None:
            if K.ndim(mask) == K.ndim(inputs) - 1:
                mask = K.expand_dims(mask)            

            inputs *= K.cast(mask, K.floatx())

        output = K.conv1d(
            inputs, self.kernel, 
            strides=self.strides[0],
            padding=self.padding,
            data_format=self.data_format,
            dilation_rate=self.dilation_rate[0]
        )

        if self.use_bias:
            m = K.not_equal(output, 0)
            output = K.bias_add(
                output,
                self.bias,
                data_format=self.data_format)
            output *= K.cast(m, K.floatx())
                
        # Apply activations on the image
        if self.activation is not None:
            output = self.activation(output)

        return output 
Example 32
Project: knowledgeflow   Author: 3rduncle   File: lcd.py    MIT License 5 votes vote down vote up
def letor_binary_crossentropy(y_true, y_pred):
    signed = 2 * y_pred * (y_true - 0.5)
    pos = signed[0::2]
    neg = signed[1::2]
    s = pos - neg
    es = K.exp(p)
    p = es / (1 + es)
    return K.mean(K.binary_crossentropy(p, y_true), axis=-1)

# 注意自定义的merge_mode接收到的tensor带有额外的batch_size,
# 即这一层接收到的tensor的ndim=(batch_size, row, col)
# 至于3d矩阵如何按照样本分别求内积就说来话长了。。看下面的用法
# http://deeplearning.net/software/theano/library/tensor/basic.html 
Example 33
Project: knowledgeflow   Author: 3rduncle   File: lcd.py    MIT License 5 votes vote down vote up
def buildDecomposition(self):
        q_embedding = self.tensors['q-embedding']
        a_embedding = self.tensors['a-embedding']
        q_match = self.tensors['q-match']
        a_match = self.tensors['a-match']
        # compute q+, q-, a+, a-
        # 注意为什么其他的层不需要加BATCH_SIZE,而这里却突然需要了呢?
        # 原因Lambda的坑,Lambda的ouput_shape不需要指定BATCH_SIZE,会
        # 自行推导:当Lambda的上层输出中含有BATCH_SIZE时,使用改值作
        # 为本层的BATCH_SIZE,如果没有时我就呵呵了,不知道是怎么推的。
        # 因此这层Merge给定BATCH_SIZE是填下层Lambda的坑
        q_channels = Merge(
            mode=lambda x: decomposite(*x),
            output_shape=(self.params['batch_size'], 2, self.q_length, self.wdim),
            name='q-channels'
        )([q_embedding, q_match])

        a_channels = Merge(
            mode=lambda x: decomposite(*x),
            output_shape=(self.params['batch_size'], 2, self.a_length, self.wdim),
            name='a-channels',
        )([a_embedding, a_match])
        print('q_channels', q_channels._keras_shape, K.ndim(q_channels))
        print('a_channels', a_channels._keras_shape, K.ndim(a_channels))
        self.tensors['q-channels'] = q_channels
        self.tensors['a-channels'] = a_channels 
Example 34
Project: knowledgeflow   Author: 3rduncle   File: lcd.py    MIT License 5 votes vote down vote up
def buildFeatures(self, type='shared'):
        assert self.checkTensor('q-channels')
        assert self.checkTensor('a-channels')
        srelu = lambda name: SReLU(name=name)
        features = []
        if type == 'shared':
            q_features = self.linkFeature('q-channels', 'shared-convolution', activation='tanh')
            a_features = self.linkFeature('a-channels', 'shared-convolution', activation='tanh')
        else:
            raise Error('Not Supported')
        print('q-features', q_features._keras_shape, K.ndim(q_features))
        print('a-features', a_features._keras_shape, K.ndim(a_features))
        self.tensors['q-features'] = q_features
        self.tensors['a-features'] = a_features 
Example 35
Project: spektral   Author: danielegrattarola   File: pooling.py    MIT License 5 votes vote down vote up
def call(self, inputs):
        if self.data_mode == 'graph':
            X = inputs[0]
            I = inputs[1]
            if K.ndim(I) == 2:
                I = I[:, 0]
        else:
            X = inputs

        if self.data_mode == 'graph':
            return self.pooling_op(X, I)
        else:
            return self.batch_pooling_op(X, axis=-2, keepdims=(self.data_mode == 'single')) 
Example 36
Project: spektral   Author: danielegrattarola   File: ops.py    MIT License 5 votes vote down vote up
def degree_matrix(A, return_sparse_batch=False):
    """
    Computes the degree matrix of A, deals with sparse A and batch mode
    automatically.
    :param A: Tensor or SparseTensor with rank k = {2, 3}.
    :param return_sparse_batch: if operating in batch mode, return a
    SparseTensor. Note that the sparse degree tensor returned by this function
    cannot be used for sparse matrix multiplication afterwards.
    :return: SparseTensor of rank k.
    """
    D = degrees(A)

    batch_mode = K.ndim(D) == 2
    N = tf.shape(D)[-1]
    batch_size = tf.shape(D)[0] if batch_mode else 1

    inner_index = tf.tile(tf.stack([tf.range(N)] * 2, axis=1), (batch_size, 1))
    if batch_mode:
        if return_sparse_batch:
            outer_index = repeat(
                tf.range(batch_size), tf.ones(batch_size) * tf.cast(N, tf.float32)
            )
            indices = tf.concat([outer_index[:, None], inner_index], 1)
            dense_shape = (batch_size, N, N)
        else:
            return tf.linalg.diag(D)
    else:
        indices = inner_index
        dense_shape = (N, N)

    indices = tf.cast(indices, tf.int64)
    values = tf.reshape(D, (-1, ))
    return tf.SparseTensor(indices, values, dense_shape)


################################################################################
# Scipy to tf.sparse conversion
################################################################################ 
Example 37
Project: image-processing-service   Author: aetrapp   File: FixedBatchNormalization.py    GNU General Public License v3.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 38
Project: zlyang-seq2seq-gec   Author: young-zonglin   File: basic_model.py    MIT License 5 votes vote down vote up
def _masked_loss(self, target, preds):
        y_mask = GetPadMask(self.batch_size)(target)
        cross_entropy = K.categorical_crossentropy(target, preds)
        assert K.ndim(cross_entropy) == 2
        loss = K.sum(cross_entropy * y_mask, axis=1, keepdims=True) / K.sum(y_mask, axis=1, keepdims=True)
        return K.reshape(loss, [self.batch_size, -1])

    # Get done => masked acc 
Example 39
Project: zlyang-seq2seq-gec   Author: young-zonglin   File: basic_model.py    MIT License 5 votes vote down vote up
def _masked_categorical_accuracy(self, target, preds):
        y_mask = GetPadMask(self.batch_size)(target)
        raw_tag = K.cast(K.equal(K.argmax(target, axis=-1),
                                 K.argmax(preds, axis=-1)),
                         K.floatx())
        assert K.ndim(raw_tag) == 2
        return raw_tag * y_mask

    # TODO 优化算法
    # 动态学习率 => done,在回调中更改学习率
    # Get done => Masked loss function. 
Example 40
Project: group-ksparse-temporal-cnns   Author: srph25   File: ops.py    MIT License 5 votes vote down vote up
def group_norms(inputs, groups, axis, norm=2, epsilon=None):
    if axis == -1:
        axis = K.ndim(inputs) - 1
    if epsilon is None:
        epsilon = K.epsilon()
    inputs_group = tf.split(inputs, num_or_size_splits=groups, axis=axis)
    inputs_group = K.concatenate([K.expand_dims(t, axis=axis) for t in inputs_group], axis=axis)
    return n_p(inputs_group, p=norm, axis=(axis + 1), epsilon=epsilon) 
Example 41
Project: group-ksparse-temporal-cnns   Author: srph25   File: ops.py    MIT License 5 votes vote down vote up
def group_ksparse(x, groups, k, axis_group, axis_sparse, norm=2, alpha=1, epsilon=None):
    if isinstance(axis_group, int):
        axis_group = (axis_group,)
    elif isinstance(axis_group, list):
        axis_group = tuple(axis_group)
    if isinstance(axis_sparse, int):
        axis_sparse = (axis_sparse,)
    elif isinstance(axis_sparse, list):
        axis_sparse = tuple(axis_sparse)
    assert(1 - bool(set(axis_group) & set(axis_sparse)))
    if epsilon is None:
        epsilon = K.epsilon()
    axis_complement = tuple(set(range(K.ndim(x))) - set(axis_group) - set(axis_sparse))
    shape_reduce_group = K.prod([K.shape(x)[j] for j in axis_group])
    shape_reduce_sparse = K.prod([K.shape(x)[j] for j in axis_sparse])
    _k = K.minimum(K.in_train_phase(k, alpha * k), shape_reduce_sparse)
    inputs_permute_dimensions = K.permute_dimensions(x, axis_complement + axis_sparse + axis_group)
    inputs_permute_dimensions_reshape = K.reshape(inputs_permute_dimensions, (-1, shape_reduce_sparse, shape_reduce_group))
    norm_group_permute_dimensions_reshape = group_norms(inputs=inputs_permute_dimensions_reshape, groups=groups, axis=-1, norm=norm, epsilon=epsilon)
    norm_group_permute_dimensions_reshape = K.permute_dimensions(norm_group_permute_dimensions_reshape, (0, 2, 1))
    norm_group_permute_dimensions_reshape = K.reshape(norm_group_permute_dimensions_reshape, (-1, shape_reduce_sparse))
    _, indices = tf.nn.top_k(norm_group_permute_dimensions_reshape, _k)
    scatter_indices = K.concatenate([(K.arange(K.shape(norm_group_permute_dimensions_reshape)[0])[:, None] * K.ones((1, _k), dtype='int32'))[:, :, None], indices[:, :, None]])
    scatter_updates = K.ones((K.shape(norm_group_permute_dimensions_reshape)[0], _k))
    mask_group_permute_dimensions_reshape = K.cast(tf.scatter_nd(scatter_indices, scatter_updates, K.shape(norm_group_permute_dimensions_reshape)), K.floatx())
    mask_group_permute_dimensions_reshape = K.reshape(mask_group_permute_dimensions_reshape, (-1, groups, shape_reduce_sparse))
    mask_group_permute_dimensions_reshape = K.permute_dimensions(mask_group_permute_dimensions_reshape, (0, 2, 1))
    mask_permute_dimensions_reshape = (mask_group_permute_dimensions_reshape[:, :, :, None] * K.ones((1, 1, 1, floor_div(shape_reduce_group, groups))))
    mask_permute_dimensions = K.reshape(mask_permute_dimensions_reshape, K.shape(inputs_permute_dimensions))
    mask = K.permute_dimensions(mask_permute_dimensions, tuple(np.argsort(axis_complement + axis_sparse + axis_group)))
    return mask * x 
Example 42
Project: iMIMIC-RCVs   Author: medgift   File: regularizers.py    MIT License 5 votes vote down vote up
def build_loss(self):
        r"""Implements the N-dim version of function
        $$TV^{\beta}(x) = \sum_{whc} \left ( \left ( x(h, w+1, c) - x(h, w, c) \right )^{2} +
        \left ( x(h+1, w, c) - x(h, w, c) \right )^{2} \right )^{\frac{\beta}{2}}$$
        to return total variation for all images in the batch.
        """
        image_dims = K.ndim(self.img) - 2

        # Constructing slice [1:] + [:-1] * (image_dims - 1) and [:-1] * (image_dims)
        start_slice = [slice(1, None, None)] + [slice(None, -1, None) for _ in range(image_dims - 1)]
        end_slice = [slice(None, -1, None) for _ in range(image_dims)]
        samples_channels_slice = [slice(None, None, None), slice(None, None, None)]

        # Compute pixel diffs by rolling slices to the right per image dim.
        tv = None
        for i in range(image_dims):
            ss = tuple(samples_channels_slice + start_slice)
            es = tuple(samples_channels_slice + end_slice)
            diff_square = K.square(self.img[utils.slicer[ss]] - self.img[utils.slicer[es]])
            tv = diff_square if tv is None else tv + diff_square

            # Roll over to next image dim
            start_slice = np.roll(start_slice, 1).tolist()
            end_slice = np.roll(end_slice, 1).tolist()

        tv = K.sum(K.pow(tv, self.beta / 2.))
        return normalize(self.img, tv) 
Example 43
Project: table_detection_with_faster_rcnn   Author: Dimetrix   File: FixedBatchNormalization.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 44
Project: yoctol-keras-layer-zoo   Author: Yoctol   File: maskConv.py    GNU General Public License v3.0 5 votes vote down vote up
def compute_mask(self, inputs, mask=None):
        dimension = K.ndim(inputs)
        mask_tensor = K.any(K.not_equal(inputs, self.mask_value), axis=-1)
        mask_base = K.any(mask_tensor, axis=1, keepdims=True)
        for axis in range(2, dimension - 1):
            mask_axis = K.any(mask_tensor, axis=axis, keepdims=True)
            mask_base = tf.logical_and(mask_base, mask_axis)
        return mask_base 
Example 45
Project: yoctol-keras-layer-zoo   Author: Yoctol   File: mask_ConvNet.py    GNU General Public License v3.0 5 votes vote down vote up
def compute_mask(self, inputs, mask):
        channel_axis = K.ndim(inputs) - 1
        mask_tensor = K.cast(mask, K.floatx())
        mask_tensor = K.expand_dims(mask_tensor)

        mask_output = self._compute_mask_output(mask_tensor)
        mask_output = K.sum(mask_output, axis=channel_axis)
        next_mask_tensor = K.not_equal(mask_output, 0.0)
        return next_mask_tensor 
Example 46
Project: yoctol-keras-layer-zoo   Author: Yoctol   File: mask_ConvNet.py    GNU General Public License v3.0 5 votes vote down vote up
def call(self, inputs, mask=None):
        outputs = self.layer.call(inputs)
        channel_axis = K.ndim(inputs) - 1
        mask_tensor = K.cast(mask, K.floatx())
        mask_tensor = K.expand_dims(mask_tensor)

        mask_output = self._compute_mask_output(mask_tensor)
        mask_output = K.repeat_elements(
            mask_output,
            self.layer.filters,
            channel_axis
        )
        return outputs * mask_output 
Example 47
Project: yoctol-keras-layer-zoo   Author: Yoctol   File: mask_pooling.py    GNU General Public License v3.0 5 votes vote down vote up
def compute_mask(self, inputs, mask=None):
        channel_axis = K.ndim(inputs) - 1
        mask_tensor = K.cast(mask, K.floatx())
        mask_tensor = K.expand_dims(mask_tensor)
        mask_output = self.layer._pooling_function(
            mask_tensor,
            self.layer.pool_size,
            self.layer.strides,
            self.layer.padding,
            self.layer.data_format,
        )
        mask_output = K.sum(mask_output, axis=channel_axis)
        next_mask_tensor = K.not_equal(mask_output, 0.0)
        return next_mask_tensor 
Example 48
Project: keras-frcnn   Author: kbardool   File: FixedBatchNormalization.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 49
Project: CarCrash_forecasting_and_detection   Author: ankitshah009   File: fixed_batch_normalization.py    GNU General Public License v3.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 50
Project: diktya   Author: BioroboticsLab   File: core.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):
        index = []
        for i in range(K.ndim(x)):
            if i == self.axis:
                index.append(slice(self.start, self.stop, self.step))
            else:
                index.append(slice(None, None, 1))
        return x[tuple(index)] 
Example 51
Project: diktya   Author: BioroboticsLab   File: core.py    Apache License 2.0 5 votes vote down vote up
def call(self, xs, mask=None):
        def build_index(start, stop):
            index = []
            for i in range(K.ndim(arr)):
                if i == self.axis:
                    index.append(slice(start, stop, 1))
                else:
                    index.append(slice(None, None, 1))
            return index
        idx, arr = xs
        idx = idx.flatten()[0]
        front_index = build_index(0, idx)
        back_index = build_index(idx, None)
        return [arr[tuple(front_index)], arr[tuple(back_index)]] 
Example 52
Project: diktya   Author: BioroboticsLab   File: core.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):
        condition, then_expr, else_expr = x
        pattern = (0, 1) + ('x',) * (K.ndim(then_expr) - 2)
        return K.switch(condition.dimshuffle(*pattern), then_expr, else_expr) 
Example 53
Project: diktya   Author: BioroboticsLab   File: core.py    Apache License 2.0 5 votes vote down vote up
def _get_mean_and_std(self, x):
        reduction_axes = list(range(K.ndim(x)))
        del reduction_axes[self.axis]
        mean = K.mean(x, axis=reduction_axes)
        std = K.sqrt(K.var(x, axis=reduction_axes) + K.epsilon())
        return mean, std 
Example 54
Project: diktya   Author: BioroboticsLab   File: core.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):
        if self.normalize:
            mean, std = self._get_mean_and_std(x)
            broadcast_shape = [1] * K.ndim(x)
            broadcast_shape[self.axis] = K.shape(x)[self.axis]
            broadcast_mean = K.reshape(mean, broadcast_shape)
            broadcast_std = K.reshape(std, broadcast_shape)
            return K.in_train_phase((x - broadcast_mean) /
                                    (broadcast_std + K.epsilon()), x)
        else:
            return x * 1. 
Example 55
Project: neural-style-keras   Author: robertomest   File: training.py    MIT License 5 votes vote down vote up
def gram_matrix(x, norm_by_channels=False):
    '''
    Returns the Gram matrix of the tensor x.
    '''
    if K.ndim(x) == 3:
        features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
        shape = K.shape(x)
        C, H, W = shape[0], shape[1], shape[2]
        gram = K.dot(features, K.transpose(features))
    elif K.ndim(x) == 4:
        # Swap from (H, W, C) to (B, C, H, W)
        x = K.permute_dimensions(x, (0, 3, 1, 2))
        shape = K.shape(x)
        B, C, H, W = shape[0], shape[1], shape[2], shape[3]
        # Reshape as a batch of 2D matrices with vectorized channels
        features = K.reshape(x, K.stack([B, C, H*W]))
        # This is a batch of Gram matrices (B, C, C).
        gram = K.batch_dot(features, features, axes=2)
    else:
        raise ValueError('The input tensor should be either a 3d (H, W, C) or 4d (B, H, W, C) tensor.')
    # Normalize the Gram matrix
    if norm_by_channels:
        denominator = C * H * W # Normalization from Johnson
    else:
        denominator = H * W # Normalization from Google
    gram = gram /  K.cast(denominator, x.dtype)

    return gram 
Example 56
Project: neural-style-keras   Author: robertomest   File: training.py    MIT License 5 votes vote down vote up
def tv_loss(x):
    '''
    Total variation loss is used to keep the image locally coherent
    '''
    assert K.ndim(x) == 4
    a = K.square(x[:, :-1, :-1, :] - x[:, 1:, :-1, :])
    b = K.square(x[:, :-1, :-1, :] - x[:, :-1, 1:, :])
    return K.sum(a + b, axis=(1, 2, 3)) 
Example 57
Project: neural-event-model   Author: pdasigi   File: keras_extensions.py    Apache License 2.0 5 votes vote down vote up
def compute_mask(self, inputs, mask=None):
        if mask is None:
            return None
        else:
            if K.ndim(mask) == 2:
                # This needs special treatment. It means that the input ndim is 3, and output ndim is 2, thus
                # requiring the mask's ndim to be 1.
                return K.any(mask, axis=-1)
            else:
                return K.batch_flatten(mask) 
Example 58
Project: X   Author: EderSantana   File: models.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def values(self, observation):
        if observation.ndim == 1:
            vals = self.table[tuple(observation)]
        else:
            obs_tuple = tuple(map(tuple, observation))  # convert to tuple of tuples
            vals = map(self.table.__getitem__, obs_tuple)  # get values from dict as list of arrays
        vals = np.asarray(vals)  # convert list of arrays to matrix (2-d array)
        return vals 
Example 59
Project: RPGOne   Author: RTHMaK   File: text_trainer.py    Apache License 2.0 5 votes vote down vote up
def _get_embedded_input(self,
                            input_layer: Layer,
                            embedding_name: str="embedding",
                            vocab_name: str='words'):
        """
        This function does most of the work for self._embed_input.

        Additionally, we allow for multiple vocabularies, e.g., if you want to embed both
        characters and words with separate embedding matrices.
        """
        embedding_dim = self.embedding_dim[vocab_name]
        if embedding_name not in self.embedding_layers:
            self.embedding_layers[embedding_name] = self._get_new_embedding(embedding_name,
                                                                            embedding_dim,
                                                                            vocab_name)

        embedding_layer, projection_layer = self.embedding_layers[embedding_name]
        embedded_input = embedding_layer(input_layer)
        if projection_layer is not None:
            for _ in range(2, K.ndim(input_layer)):  # 2 here to account for batch_size.
                projection_layer = TimeDistributed(projection_layer, name="timedist_" + projection_layer.name)
            embedded_input = projection_layer(embedded_input)
        if self.embedding_dropout > 0.0:
            embedded_input = Dropout(self.embedding_dropout)(embedded_input)

        return embedded_input 
Example 60
Project: RPGOne   Author: RTHMaK   File: backend.py    Apache License 2.0 5 votes vote down vote up
def last_dim_flatten(input_tensor):
    '''
    Takes a tensor and returns a matrix while preserving only the last dimension from the input.
    '''
    input_ndim = K.ndim(input_tensor)
    shuffle_pattern = (input_ndim - 1,) + tuple(range(input_ndim - 1))
    dim_shuffled_input = K.permute_dimensions(input_tensor, shuffle_pattern)
    return K.transpose(K.batch_flatten(dim_shuffled_input)) 
Example 61
Project: RPGOne   Author: RTHMaK   File: vector_matrix_split.py    Apache License 2.0 5 votes vote down vote up
def _split_tensor(tensor, split_axis: int):
        modes = K.ndim(tensor)
        if split_axis < 0:
            split_axis = modes + split_axis
        vector_slice = []
        matrix_slice = []
        for mode in range(modes):
            if mode == split_axis:
                vector_slice.append(0)
                matrix_slice.append(slice(1, None, None))
            else:
                vector_slice.append(slice(None, None, None))
                matrix_slice.append(slice(None, None, None))
        return [tensor[vector_slice], tensor[matrix_slice]] 
Example 62
Project: RPGOne   Author: RTHMaK   File: bag_of_words.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, **kwargs):
        self.input_spec = [InputSpec(ndim=3)]

        # For consistency of handling sentence encoders, we will often get passed this parameter.
        # We don't use it, but Layer will complain if it's there, so we get rid of it here.
        kwargs.pop('units', None)
        super(BOWEncoder, self).__init__(**kwargs) 
Example 63
Project: RPGOne   Author: RTHMaK   File: l1_normalize.py    Apache License 2.0 5 votes vote down vote up
def call(self, inputs, mask=None):
        if K.ndim(inputs) == 3:
            inputs = K.squeeze(inputs, axis=2)
        if K.ndim(inputs) != 2:
            raise ValueError("L1Normalize layer only supports inputs of shape "
                             "(batch_size, x) or (batch_size, x, 1)")
        return l1_normalize(inputs, mask) 
Example 64
Project: RPGOne   Author: RTHMaK   File: multiply.py    Apache License 2.0 5 votes vote down vote up
def expand_dims_if_necessary(tensor_1, tensor_2):
        tensor_1_ndim = K.ndim(tensor_1)
        tensor_2_ndim = K.ndim(tensor_2)
        if tensor_1_ndim == tensor_2_ndim:
            return tensor_1, tensor_2
        elif tensor_1_ndim == tensor_2_ndim - 1:
            return K.expand_dims(tensor_1), tensor_2
        elif tensor_2_ndim == tensor_1_ndim - 1:
            return tensor_1, K.expand_dims(tensor_2)
        else:
            raise RuntimeError("Can't multiply two tensors with ndims "
                               "{} and {}".format(tensor_1_ndim, tensor_2_ndim)) 
Example 65
Project: keras_frcnn_comment   Author: q5390498   File: FixedBatchNormalization.py    Apache License 2.0 5 votes vote down vote up
def call(self, x, mask=None):

        assert self.built, 'Layer must be built before being called'
        input_shape = K.int_shape(x)

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
            x_normed = K.batch_normalization(
                x, self.running_mean, self.running_std,
                self.beta, self.gamma,
                epsilon=self.epsilon)
        else:
            # need broadcasting
            broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
            broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
            broadcast_beta = K.reshape(self.beta, broadcast_shape)
            broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
            x_normed = K.batch_normalization(
                x, broadcast_running_mean, broadcast_running_std,
                broadcast_beta, broadcast_gamma,
                epsilon=self.epsilon)

        return x_normed 
Example 66
Project: workspace_2017   Author: nwiizo   File: neural_style_transfer.py    MIT License 5 votes vote down vote up
def gram_matrix(x):
    assert K.ndim(x) == 3
    if K.image_dim_ordering() == 'th':
        features = K.batch_flatten(x)
    else:
        features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
    gram = K.dot(features, K.transpose(features))
    return gram

# the "style loss" is designed to maintain
# the style of the reference image in the generated image.
# It is based on the gram matrices (which capture style) of
# feature maps from the style reference image
# and from the generated image 
Example 67
Project: workspace_2017   Author: nwiizo   File: neural_style_transfer.py    MIT License 5 votes vote down vote up
def style_loss(style, combination):
    assert K.ndim(style) == 3
    assert K.ndim(combination) == 3
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2))

# an auxiliary loss function
# designed to maintain the "content" of the
# base image in the generated image 
Example 68
Project: workspace_2017   Author: nwiizo   File: neural_style_transfer.py    MIT License 5 votes vote down vote up
def total_variation_loss(x):
    assert K.ndim(x) == 4
    if K.image_dim_ordering() == 'th':
        a = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, 1:, :img_ncols - 1])
        b = K.square(x[:, :, :img_nrows - 1, :img_ncols - 1] - x[:, :, :img_nrows - 1, 1:])
    else:
        a = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, 1:, :img_ncols - 1, :])
        b = K.square(x[:, :img_nrows - 1, :img_ncols - 1, :] - x[:, :img_nrows - 1, 1:, :])
    return K.sum(K.pow(a + b, 1.25))

# combine these loss functions into a single scalar 
Example 69
Project: workspace_2017   Author: nwiizo   File: deep_dream.py    MIT License 5 votes vote down vote up
def continuity_loss(x):
    assert K.ndim(x) == 4
    if K.image_dim_ordering() == 'th':
        a = K.square(x[:, :, :img_width - 1, :img_height - 1] -
                     x[:, :, 1:, :img_height - 1])
        b = K.square(x[:, :, :img_width - 1, :img_height - 1] -
                     x[:, :, :img_width - 1, 1:])
    else:
        a = K.square(x[:, :img_width - 1, :img_height - 1, :] -
                     x[:, 1:, :img_height - 1, :])
        b = K.square(x[:, :img_width - 1, :img_height - 1, :] -
                     x[:, :img_width - 1, 1:, :])
    return K.sum(K.pow(a + b, 1.25))

# define the loss 
Example 70
Project: workspace_2017   Author: nwiizo   File: neural_doodle.py    MIT License 5 votes vote down vote up
def kmeans(xs, k):
    assert xs.ndim == 2
    try:
        from sklearn.cluster import k_means
        _, labels, _ = k_means(xs.astype("float64"), k)
    except ImportError:
        from scipy.cluster.vq import kmeans2
        _, labels = kmeans2(xs, k, missing='raise')
    return labels 
Example 71
Project: knowledgeflow   Author: 3rduncle   File: lcd.py    MIT License 4 votes vote down vote up
def buildComposition(self, shared=True):
        q_input = self.tensors['q_input']
        a_input = self.tensors['a_input']
        if shared:
            q_embedding = self.layers['shared-embedding'](q_input)
            a_embedding = self.layers['shared-embedding'](a_input)
        else:
            q_embedding = self.layers['q-embedding'](q_input)
            a_embedding = self.layers['a-embedding'](a_input)

        print('Embedding ndim q %d a %d' % (K.ndim(q_embedding), K.ndim(a_embedding)))
        print('Embedding shape ', q_embedding._keras_shape, a_embedding._keras_shape)

        # compute Semantic Matching
        cross = Merge(
        #   [q_embedding, a_embedding],
            mode=semantic_matrix,
            output_shape=(self.q_length, self.a_length),
            name='semantic'
        )
        semantic = cross([q_embedding, a_embedding])
        print('Semantic ndim %d' % K.ndim(semantic))
        print('Semantic shape ', semantic._keras_shape)
        print('Semantic shape ', cross.get_output_shape_at(0))

        # compute cross 
        q_match = merge(
            [a_embedding, semantic],
            mode=lambda x: match_matrix(*x,axis=0, w=self.params['window']),
            output_shape=(self.q_length, self.wdim),
            name='q_match'
        )
        print('q_match ', q_match._keras_shape, K.ndim(q_match))

        a_match = merge(
            [q_embedding, semantic],
            mode=lambda x: match_matrix(*x,axis=1, w=self.params['window']),
            output_shape=(self.a_length, self.wdim),
            name='a_match'
        )
        print('Match ndim q %d a %d' % (K.ndim(q_match), K.ndim(a_match)))
        print('Match shape ', q_match._keras_shape, a_match._keras_shape)
        self.tensors['q-embedding'] = q_embedding
        self.tensors['a-embedding'] = a_embedding
        self.tensors['q-match'] = q_match
        self.tensors['a-match'] = a_match 
Example 72
Project: knowledgeflow   Author: 3rduncle   File: lcd.py    MIT License 4 votes vote down vote up
def buildDecomposition(self):
        q_embedding = self.tensors['q-embedding']
        a_embedding = self.tensors['a-embedding']
        q_match = self.tensors['q-match']
        a_match = self.tensors['a-match']
        # compute q+, q-, a+, a-
        # 注意为什么其他的层不需要加BATCH_SIZE,而这里却突然需要了呢?
        # 原因Lambda的坑,Lambda的ouput_shape不需要指定BATCH_SIZE,会
        # 自行推导:当Lambda的上层输出中含有BATCH_SIZE时,使用改值作
        # 为本层的BATCH_SIZE,如果没有时我就呵呵了,不知道是怎么推的。
        # 因此这层Merge给定BATCH_SIZE是填下层Lambda的坑
        q_pos = Merge(
            mode=lambda x: parallel(*x),
            output_shape=(self.params['batch_size'], self.q_length, self.wdim),
            name='q+'
        )([q_embedding, q_match])

        # 注意这里不能直接用1 - q_pos获取,否则会丢掉_keras_shape属性
        # 注意这里的output_shape是不需要给batch_size的和Merge不同
        q_neg = Merge(
            mode=lambda x: x[0] - x[1],
            output_shape=(self.params['batch_size'], self.q_length, self.wdim),
            name='q-'
        )([q_embedding, q_pos])
        print('q_pos', q_pos._keras_shape, K.ndim(q_pos))
        print('q_neg', q_neg._keras_shape, K.ndim(q_neg))

        a_pos = Merge(
            mode=lambda x: parallel(*x),
            output_shape=(self.params['batch_size'], self.a_length, self.wdim),
            name='a+',
        )([a_embedding, a_match])
        a_neg = Merge(
            mode=lambda x: x[0] - x[1],
            output_shape=(self.params['batch_size'], self.a_length, self.wdim),
            name='a-'
        )([a_embedding, a_pos])
        print('a_pos', a_pos._keras_shape, K.ndim(a_pos))
        print('a_neg', a_neg._keras_shape, K.ndim(a_neg))
        self.tensors['q+'] = q_pos
        self.tensors['q-'] = q_neg
        self.tensors['a+'] = a_pos
        self.tensors['a-'] = a_neg 
Example 73
Project: spektral   Author: danielegrattarola   File: pooling.py    MIT License 4 votes vote down vote up
def call(self, inputs):
        if len(inputs) == 3:
            X, A, I = inputs
            self.data_mode = 'graph'
        else:
            X, A = inputs
            I = tf.zeros(tf.shape(X)[:1], dtype=tf.int32)
            self.data_mode = 'single'
        if K.ndim(I) == 2:
            I = I[:, 0]

        A_is_sparse = K.is_sparse(A)

        # Get mask
        y = K.dot(X, K.l2_normalize(self.kernel))
        N = K.shape(X)[-2]
        indices = ops.segment_top_k(y[:, 0], I, self.ratio, self.top_k_var)
        mask = tf.scatter_nd(tf.expand_dims(indices, 1), tf.ones_like(indices), (N,))

        # Multiply X and y to make layer differentiable
        features = X * self.gating_op(y)

        axis = 0 if len(K.int_shape(A)) == 2 else 1  # Cannot use negative axis in tf.boolean_mask
        # Reduce X
        X_pooled = tf.boolean_mask(features, mask, axis=axis)

        # Compute A^2
        if A_is_sparse:
            A_dense = tf.sparse.to_dense(A)
        else:
            A_dense = A
        A_squared = K.dot(A, A_dense)

        # Reduce A
        A_pooled = tf.boolean_mask(A_squared, mask, axis=axis)
        A_pooled = tf.boolean_mask(A_pooled, mask, axis=axis + 1)
        if A_is_sparse:
            A_pooled = tf.contrib.layers.dense_to_sparse(A_pooled)

        output = [X_pooled, A_pooled]

        # Reduce I
        if self.data_mode == 'graph':
            I_pooled = tf.boolean_mask(I[:, None], mask)[:, 0]
            output.append(I_pooled)

        if self.return_mask:
            output.append(mask)

        return output 
Example 74
Project: spektral   Author: danielegrattarola   File: pooling.py    MIT License 4 votes vote down vote up
def call(self, inputs):
        if len(inputs) == 3:
            X, A, I = inputs
            self.data_mode = 'graph'
        else:
            X, A = inputs
            I = tf.zeros(tf.shape(X)[:1], dtype=tf.int32)
            self.data_mode = 'single'
        if K.ndim(I) == 2:
            I = I[:, 0]

        A_is_sparse = K.is_sparse(A)

        # Get mask
        y = K.dot(X, self.kernel)
        y = filter_dot(A, y)
        N = K.shape(X)[-2]
        indices = ops.segment_top_k(y[:, 0], I, self.ratio, self.top_k_var)
        mask = tf.scatter_nd(tf.expand_dims(indices, 1), tf.ones_like(indices), (N,))

        # Multiply X and y to make layer differentiable
        features = X * self.gating_op(y)

        axis = 0 if len(K.int_shape(A)) == 2 else 1  # Cannot use negative axis in tf.boolean_mask
        # Reduce X
        X_pooled = tf.boolean_mask(features, mask, axis=axis)

        # Compute A^2
        if A_is_sparse:
            A_dense = tf.sparse.to_dense(A)
        else:
            A_dense = A
        A_squared = K.dot(A, A_dense)

        # Reduce A
        A_pooled = tf.boolean_mask(A_squared, mask, axis=axis)
        A_pooled = tf.boolean_mask(A_pooled, mask, axis=axis + 1)
        if A_is_sparse:
            A_pooled = tf.contrib.layers.dense_to_sparse(A_pooled)

        output = [X_pooled, A_pooled]

        # Reduce I
        if self.data_mode == 'graph':
            I_pooled = tf.boolean_mask(I[:, None], mask)[:, 0]
            output.append(I_pooled)

        if self.return_mask:
            output.append(mask)

        return output 
Example 75
Project: keras_extension   Author: k1414st   File: layer.py    MIT License 4 votes vote down vote up
def _batch_dot(x, y, axes=None):
    """
    K.batch_dot is only limited in the case of (x.ndim<=3 and y.ndim<=3).
    this wrapped function is not limited to that case.
    (using "tf.einsum" or "K.batch_dot with reshape".)
    """
    if K.ndim(x) <= 3 and K.ndim(y) <= 3:
        return K.batch_dot(x, y, axes=axes)
    else:
        if (axes[0] == 0) or (axes[1] == 0):
            raise ValueError('each axis must not be 0 (N_batch place).')
        if backend() == 'tensorflow':
            if K.ndim(x) + K.ndim(y) >= 27:
                raise ValueError('x.ndim + y.ndim must be less than 27')
            import tensorflow as tf

            # make einsum string
            # ex). NabcXefg, NhijkXlm -> Nabcefghijklm (N: pos_batch, X: pos_axes)
            str_x = ''.join([chr(97+i) for i in range(K.ndim(x))])
            str_y = ''.join([chr(97+len(str_x)+i) for i in range(K.ndim(y))])
            str_y = str_y.replace(str_y[0], str_x[0])
            str_y = str_y.replace(str_y[axes[1]], str_x[axes[0]])
            str_out = str_x.replace(str_x[axes[0]], '') + \
                str_y.replace(str_y[axes[1]], '')[1:]
            str_einsum = '%s,%s->%s' % (str_x, str_y, str_out)

            return tf.einsum(str_einsum, x, y)
        else:
            # set shape, targat-idim, target-shape
            sx, sy = x.shape, y.shape
            ax0, ax1 = axes[0], axes[1]
            s0, s1 = sx[ax0], sy[ax1]

            # reshape: (B, a1, a2, ... axis, ... an) -> (B, prod(~axis), axis)
            dx_rm = [i for i in range(len(sx)) if i != ax0]
            dy_rm = [i for i in range(len(sy)) if i != ax1]
            sx_rm = [sx[i] for i in dx_rm]
            sy_rm = [sy[i] for i in dy_rm]
            x = K.permute_dimensions(x, dx_rm + [ax0])
            y = K.permute_dimensions(y, dy_rm + [ax1])
            x = K.reshape(x, [-1, np.prod(sx_rm[1:]), s0])
            y = K.reshape(y, [-1, np.prod(sy_rm[1:]), s1])

            # reshape: (B, prod(sx_rm), prod(sy_rm)) -> (B, sx_rm, sy_rm)
            out = K.batch_dot(x, y, axes=(K.ndim(x)-1, K.ndim(y)-1))
            return K.reshape(out, [-1] + sx_rm[1:] + sy_rm[1:]) 
Example 76
Project: keras_extension   Author: k1414st   File: layer.py    MIT License 4 votes vote down vote up
def _batch_dot(x, y, axes=None):
    """
    K.batch_dot is only limited in the case of (x.ndim<=3 and y.ndim<=3).
    this wrapped function is not limited to that case.
    (using "tf.einsum" or "K.batch_dot with reshape".)
    """
    if K.ndim(x) <= 3 and K.ndim(y) <= 3:
        return K.batch_dot(x, y, axes=axes)
    else:
        if (axes[0] == 0) or (axes[1] == 0):
            raise ValueError('each axis must not be 0 (N_batch place).')
        if backend() == 'tensorflow':
            if K.ndim(x) + K.ndim(y) >= 27:
                raise ValueError('x.ndim + y.ndim must be less than 27')
            import tensorflow as tf

            # make einsum string
            # ex). NabcXefg, NhijkXlm -> Nabcefghijklm (N: pos_batch, X: pos_axes)
            str_x = ''.join([chr(97+i) for i in range(K.ndim(x))])
            str_y = ''.join([chr(97+len(str_x)+i) for i in range(K.ndim(y))])
            str_y = str_y.replace(str_y[0], str_x[0])
            str_y = str_y.replace(str_y[axes[1]], str_x[axes[0]])
            str_out = str_x.replace(str_x[axes[0]], '') + \
                str_y.replace(str_y[axes[1]], '')[1:]
            str_einsum = '%s,%s->%s' % (str_x, str_y, str_out)

            return tf.einsum(str_einsum, x, y)
        else:
            # set shape, targat-idim, target-shape
            sx, sy = x.shape, y.shape
            ax0, ax1 = axes[0], axes[1]
            s0, s1 = sx[ax0], sy[ax1]

            # reshape: (B, a1, a2, ... axis, ... an) -> (B, prod(~axis), axis)
            dx_rm = [i for i in range(len(sx)) if i != ax0]
            dy_rm = [i for i in range(len(sy)) if i != ax1]
            sx_rm = [sx[i] for i in dx_rm]
            sy_rm = [sy[i] for i in dy_rm]
            x = K.permute_dimensions(x, dx_rm + [ax0])
            y = K.permute_dimensions(y, dy_rm + [ax1])
            x = K.reshape(x, [-1, np.prod(sx_rm[1:]), s0])
            y = K.reshape(y, [-1, np.prod(sy_rm[1:]), s1])

            # reshape: (B, prod(sx_rm), prod(sy_rm)) -> (B, sx_rm, sy_rm)
            out = K.batch_dot(x, y, axes=(K.ndim(x)-1, K.ndim(y)-1))
            return K.reshape(out, [-1] + sx_rm[1:] + sy_rm[1:]) 
Example 77
Project: keras_extension   Author: k1414st   File: merge.py    MIT License 4 votes vote down vote up
def call(self, inputs):
        if not isinstance(inputs, list):
            raise ValueError('A merge layer should be called '
                             'on a list of inputs.')
        if self._reshape_required:
            reshaped_inputs = []
            input_ndims = list(map(K.ndim, inputs))
            if None not in input_ndims:
                # If ranks of all inputs are available,
                # we simply expand each of them at axis=1
                # until all of them have the same rank.
                max_ndim = max(input_ndims)
                for x in inputs:
                    x_ndim = K.ndim(x)
                    for _ in range(max_ndim - x_ndim):
                        x = K.expand_dims(x, 1)
                    reshaped_inputs.append(x)
                return self._merge_function(reshaped_inputs)
            else:
                # Transpose all inputs so that batch size is the last dimension.
                # (batch_size, dim1, dim2, ... ) -> (dim1, dim2, ... , batch_size)
                transposed = False
                for x in inputs:
                    x_ndim = K.ndim(x)
                    if x_ndim is None:
                        x_shape = K.shape(x)
                        batch_size = x_shape[0]
                        new_shape = K.concatenate(
                            [x_shape[1:], K.expand_dims(batch_size)])
                        x_transposed = K.reshape(x, K.stack(
                            [batch_size, K.prod(x_shape[1:])]))
                        x_transposed = K.permute_dimensions(x_transposed, (1, 0))
                        x_transposed = K.reshape(x_transposed, new_shape)
                        reshaped_inputs.append(x_transposed)
                        transposed = True
                    elif x_ndim > 1:
                        dims = list(range(1, x_ndim)) + [0]
                        reshaped_inputs.append(K.permute_dimensions(x, dims))
                        transposed = True
                    else:
                        # We don't transpose inputs if they are 1D vectors or scalars.
                        reshaped_inputs.append(x)
                y = self._merge_function(reshaped_inputs)
                y_ndim = K.ndim(y)
                if transposed:
                    # If inputs have been transposed, we have to transpose the output too.
                    if y_ndim is None:
                        y_shape = K.shape(y)
                        y_ndim = K.shape(y_shape)[0]
                        batch_size = y_shape[y_ndim - 1]
                        new_shape = K.concatenate(
                            [K.expand_dims(batch_size), y_shape[:y_ndim - 1]])
                        y = K.reshape(y, (-1, batch_size))
                        y = K.permute_dimensions(y, (1, 0))
                        y = K.reshape(y, new_shape)
                    elif y_ndim > 1:
                        dims = [y_ndim - 1] + list(range(y_ndim - 1))
                        y = K.permute_dimensions(y, dims)
                return y
        else:
            return self._merge_function(inputs) 
Example 78
Project: keras_extension   Author: k1414st   File: merge_org.py    MIT License 4 votes vote down vote up
def build(self, input_shape):
        # Used purely for shape validation.
        if not isinstance(input_shape, list) or len(input_shape) != 2:
            raise ValueError('A `Dot` layer should be called '
                             'on a list of 2 inputs.')
        shape1 = input_shape[0]
        shape2 = input_shape[1]
        if shape1 is None or shape2 is None:
            return
        if isinstance(self.axes, int):
            if self.axes < 0:
                axes = [self.axes % len(shape1), self.axes % len(shape2)]
            else:
                axes = [self.axes] * 2
        else:
            axes = self.axes
        if shape1[axes[0]] != shape2[axes[1]]:
            raise ValueError(
                'Dimension incompatibility '
                '%s != %s. ' % (shape1[axes[0]], shape2[axes[1]]) +
                'Layer shapes: %s, %s' % (shape1, shape2))

    # def _merge_function(self, inputs):
    #     if len(inputs) != 2:
    #         raise ValueError('A `Dot` layer should be called '
    #                          'on exactly 2 inputs')
    #     x1 = inputs[0]
    #     x2 = inputs[1]
    #     if isinstance(self.axes, int):
    #         if self.axes < 0:
    #             axes = [self.axes % K.ndim(x1), self.axes % K.ndim(x2)]
    #         else:
    #             axes = [self.axes] * 2
    #     else:
    #         axes = []
    #         for i in range(len(self.axes)):
    #             if self.axes[i] < 0:
    #                 axes.append(self.axes[i] % K.ndim(inputs[i]))
    #             else:
    #                 axes.append(self.axes[i])
    #     if self.normalize:
    #         x1 = K.l2_normalize(x1, axis=axes[0])
    #         x2 = K.l2_normalize(x2, axis=axes[1])
    #     output = K.batch_dot(x1, x2, axes)
    #     return output 
Example 79
Project: RPGOne   Author: RTHMaK   File: word_and_character_tokenizer.py    Apache License 2.0 4 votes vote down vote up
def embed_input(self,
                    input_layer: Layer,
                    text_trainer,
                    embedding_name: str="embedding"):
        """
        A combined word-and-characters representation requires some fancy footwork to do the
        embedding properly.

        This method assumes the input shape is (..., sentence_length, word_length + 1), where the
        first integer for each word in the tensor is the word index, and the remaining word_length
        entries is the character sequence.  We'll first split this into two tensors, one of shape
        (..., sentence_length), and one of shape (..., sentence_length, word_length), where the
        first is the word sequence, and the second is the character sequence for each word.  We'll
        pass the word sequence through an embedding layer, as normal, and pass the character
        sequence through a _separate_ embedding layer, then an encoder, to get a word vector out.
        We'll then concatenate the two word vectors, returning a tensor of shape
        (..., sentence_length, embedding_dim * 2).
        """
        # pylint: disable=protected-access
        # This is happening before any masking is done, so we don't need to worry about the
        # mask_split_axis argument to VectorMatrixSplit.
        words, characters = VectorMatrixSplit(split_axis=-1)(input_layer)
        word_embedding = text_trainer._get_embedded_input(words,
                                                          embedding_name='word_' + embedding_name,
                                                          vocab_name='words')
        character_embedding = text_trainer._get_embedded_input(characters,
                                                               embedding_name='character_' + embedding_name,
                                                               vocab_name='characters')

        # A note about masking here: we care about the character masks when encoding a character
        # sequence, so we need the mask to be passed to the character encoder correctly.  However,
        # we _don't_ care here about whether the whole word will be masked, as the word_embedding
        # will carry that information.  Because of the way `Concatenate` handles masks, if you've
        # done something crazy where you have a word index but your character indices are all zero,
        # you will get a 0 in the mask for that word at the end of this.  But assuming you're using
        # this correctly, you should only get a 0 in the character-level mask in the same places
        # that you have 0s in the word-level mask, so `Concatenate` will do the right thing.
        word_encoder = EncoderWrapper(
                text_trainer._get_encoder(name="word", fallback_behavior="use default params"))
        # We might need to TimeDistribute this again, if our input has ndim higher than 3.
        for _ in range(3, K.ndim(characters)):
            word_encoder = EncoderWrapper(word_encoder, name="timedist_" + word_encoder.name)
        word_encoding = word_encoder(character_embedding)

        # If you're embedding multiple inputs in your model, we need the final concatenation here
        # to have a unique name each time.  In order to get a unique name, we use the name of the
        # input layer.  Except sometimes Keras adds funny things to the ends of the input layer, so
        # we'll strip those off.
        input_name = input_layer.name
        if '/' in input_name:
            input_name = input_name.rsplit('/', 1)[1]
        if ':' in input_name:
            input_name = input_name.split(':')[0]
        if input_name.split('_')[-1].isdigit():
            input_name = '_'.join(input_name.split('_')[:-1])
        name = 'combined_word_embedding_for_' + input_name
        final_embedded_input = Concatenate(name=name)([word_embedding, word_encoding])
        return final_embedded_input 
Example 80
Project: RPGOne   Author: RTHMaK   File: masked_operations.py    Apache License 2.0 4 votes vote down vote up
def masked_batch_dot(tensor_a, tensor_b, mask_a, mask_b):
    '''
    The simplest case where this function is applicable is the following:

    tensor_a: (batch_size, a_length, embed_dim)
    tensor_b: (batch_size, b_length, embed_dim)
    mask_a: None or (batch_size, a_length)
    mask_b: None or (batch_size, b_length)

    Returns:
    a_dot_b: (batch_size, a_length, b_length), with zeros for masked elements.

    This function will also work for larger tensors, as long as `abs(K.ndim(tensor_a) -
    K.ndim(tensor_b)) < 1` (this is due to the limitations of `K.batch_dot`).  We always assume the
    dimension to perform the dot is the last one, and that the masks have one fewer dimension than
    the tensors.
    '''
    if K.ndim(tensor_a) < K.ndim(tensor_b):
        # To simplify the logic below, we'll make sure that tensor_a is always the bigger one.
        tensor_a, tensor_b = tensor_b, tensor_a
        mask_a, mask_b = mask_b, mask_a

    if K.ndim(tensor_a) > 3 and K.backend() == 'theano':
        raise RuntimeError("K.batch_dot() in theano is broken for tensors with more than"
                           " three dimensions.  Use tensorflow instead.")
    a_dot_axis = K.ndim(tensor_a) - 1
    b_dot_axis = K.ndim(tensor_b) - 1
    if b_dot_axis < a_dot_axis:
        tensor_b = K.expand_dims(tensor_b, axis=-1)

    # (batch_size, a_length, b_length)
    a_dot_b = K.batch_dot(tensor_a, tensor_b, axes=(a_dot_axis, b_dot_axis))
    if b_dot_axis < a_dot_axis:
        a_dot_b = K.squeeze(a_dot_b, axis=-1)

    if mask_a is None and mask_b is None:
        return a_dot_b
    elif mask_a is None:
        # (batch_size, a_length)
        mask_a = K.sum(K.ones_like(tensor_a), axis=-1)
    elif mask_b is None:
        # (batch_size, b_length)
        sum_axis = -1
        if b_dot_axis < a_dot_axis:
            sum_axis -= 1
        mask_b = K.sum(K.ones_like(tensor_b), axis=sum_axis)
    # Casting masks to float since we TF would complain if we multiplied bools.
    float_mask_a = K.cast(mask_a, 'float32')
    float_mask_b = K.cast(mask_b, 'float32')

    if b_dot_axis < a_dot_axis:
        float_mask_b = K.expand_dims(float_mask_b, axis=-1)
    else:
        float_mask_a = K.expand_dims(float_mask_a, axis=-1)
        float_mask_b = K.expand_dims(float_mask_b, axis=-2)
    # (batch_size, a_length, b_length)
    a2b_mask = float_mask_a * float_mask_b

    result = switch(a2b_mask, a_dot_b, K.zeros_like(a_dot_b))
    return result