Beispiel #1
0
def rec_L(y_true, y_pred):
	s_flow = K.variable(np.array([1,0]))
	p = K.cast(K.equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	t = K.cast(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	f = K.cast(K.not_equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	tn = t*n
	fp = f*p
	return K.sum(tn) / (K.sum(tn) + K.sum(fp))
Beispiel #2
0
def rec_S(y_true, y_pred):
	s_flow = K.variable(np.array([1,0]))
	p = K.cast(K.equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	t = K.cast(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	f = K.cast(K.not_equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	tp = t*p
	fn = f*n
	return K.sum(tp) / (K.sum(tp) + K.sum(fn))
 def _get_triplet_mask(self, y_true: Tensor, pairwise_dist: Tensor) -> Tensor:
     # mask label(a) != label(p)
     mask1 = K.expand_dims(K.equal(K.expand_dims(y_true, 0), K.expand_dims(y_true, 1)), 2)
     mask1 = K.cast(mask1, K.dtype(pairwise_dist))
     # mask a == p
     mask2 = K.expand_dims(K.not_equal(pairwise_dist, 0.0), 2)
     mask2 = K.cast(mask2, K.dtype(pairwise_dist))
     # mask label(n) == label(a)
     mask3 = K.expand_dims(K.not_equal(K.expand_dims(y_true, 0), K.expand_dims(y_true, 1)), 1)
     mask3 = K.cast(mask3, K.dtype(pairwise_dist))
     return mask1 * mask2 * mask3
def custom_loss(y_true, y_pred):
  mask = K.cast(K.not_equal(y_true, 0), dtype='float32')
  diff = y_pred - y_true
  sqdiff = diff * diff * mask
  sse = K.sum(K.sum(sqdiff))
  n = K.sum(K.sum(mask))
  return sse / n
Beispiel #5
0
def mask_aware_max(x):
    mask = K.not_equal(K.sum(K.abs(x), axis=2, keepdims=True), 0)
    mask = K.cast(mask, 'float32')
    vecmin = K.min(x, axis=1, keepdims=True)

    xstar = x + (vecmin * (1 - mask))  # setting masked values to the min value

    return K.max(xstar, axis=1, keepdims=False)
Beispiel #6
0
def nonzero_rmse(y_true, y_pred):
    """RMSE that ignores zero values, assuming that there is at least
    one nonzero value per row
    """
    mask = K.cast(K.not_equal(y_true, 0), K.floatx())
    count = K.sum(mask, 1)
    se = K.sum(K.square(y_true - y_pred) * mask, 1)
    rmse = K.sqrt(se / count)
    return K.mean(rmse)
Beispiel #7
0
def nonzero_batch_mse(y_true, y_pred):
    """MSE that ignores zero values and averages over a batch, assuming
    that there is at least one nonzero value per batch.
    """
    mask = K.cast(K.not_equal(y_true, 0), K.floatx())
    count = K.sum(mask)
    se = K.sum(K.square(y_true - y_pred) * mask)
    mse = se / count
    return mse
Beispiel #8
0
def sparse_recon_loss_abs(y_true, y_pred):
    #this gets the numerator correct on the final denominator of the loss function
    #does this make a difference?
    mask_value = 0

    mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
    ones = K.ones(shape=K.shape(y_true))
    n = K.sum(mask * ones, axis=-1)
    return 1 / n * K.sum(K.abs(y_true * mask - y_pred * mask), axis=-1)
Beispiel #9
0
    def ignore_accuracy(y_true, y_pred):
        y_true_class = K.argmax(y_true, axis=-1)
        y_pred_class = K.argmax(y_pred, axis=-1)

        ignore_mask = K.cast(K.not_equal(y_pred_class, to_ignore), 'int32')
        matches = K.cast(K.equal(y_true_class, y_pred_class),
                         'int32') * ignore_mask
        accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1)
        return accuracy
Beispiel #10
0
 def __call__(self, Y, Yh):
     '''Weighted Mean Square Error (MSE)
     '''
     if not K.is_tensor(Yh): Yh = K.constant(Yh)
     Y = K.cast(Y, Yh.dtype)
     mask = K.not_equal(Y, self.Mask)  # OK! could use NaN <-> 0
     # return K.mean(K.square(Yh[mask]-Y[mask])) # BUT: No boolean indexing in Keras!
     mask = K.cast(mask, K.dtype(Y))  # OK!
     return K.sum(K.square(K.abs(Yh - Y) * mask)) / K.sum(mask)  # OK!
Beispiel #11
0
    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
Beispiel #12
0
def positive_hyperbolic_hammingoid(x, y):
    mask = K.cast(K.not_equal(K.greater(x, 0), K.greater(y, 0)), "float32")
    # Masked elementwise maximum
    err = K.maximum(mask * x, mask * y)
    # Make it smooth and asymptotically approach 1
    err = err / (err + 1)
    # Sum across rows, normalize by length
    length = int(x.get_shape()[1])
    return K.sum(err, axis=-1) / length
    def loss(y_true, y_pred):
        mask = K.cast_to_floatx(K.not_equal(y_true, mask_value))
        y_true_one_hot = K.one_hot(y_true, num_classes)

        loss = categorical_crossentropy(y_true_one_hot, y_pred, from_logits=from_logits, label_smoothing=label_smoothing)
        masked_loss = loss * mask
        reduced_loss = K.sum(masked_loss) / K.sum(mask)

        return reduced_loss
Beispiel #14
0
def positions_func(inputs, pad=0):
    """
    A layer filling i-th column of a 2D tensor with
    1+ln(1+i) when it contains a meaningful symbol
    and with 0 when it contains PAD
    """
    position_inputs = kb.cumsum(kb.ones_like(inputs, dtype="float32"), axis=1)
    position_inputs *= kb.cast(kb.not_equal(inputs, pad), "float32")
    return kb.log(1.0 + position_inputs)
 def sparse_categorical_accuracy(y_true, y_pred):
     y_true = K.max(y_true, axis=-1)
     y_pred = K.cast(K.argmax(y_pred, axis=-1), K.floatx())
     mask = K.not_equal(y_true, mask_id)
     masked_true = tf.boolean_mask(y_true, mask)
     masked_pred = tf.boolean_mask(y_pred, mask)
     accuracy = K.mean(K.cast(K.equal(masked_true, masked_pred),
                              K.floatx()))
     return accuracy
Beispiel #16
0
 def _get_anchor_positive_triplet_mask(self, y_true: Tensor,
                                       pairwise_dist: Tensor) -> Tensor:
     # mask label(a) != label(p)
     mask1 = K.equal(K.expand_dims(y_true, 0), K.expand_dims(y_true, 1))
     mask1 = K.cast(mask1, K.dtype(pairwise_dist))
     # mask a == p
     mask2 = K.not_equal(pairwise_dist, 0.0)
     mask2 = K.cast(mask2, K.dtype(pairwise_dist))
     return mask1 * mask2
 def objective(y_true_cat, y_pred_cat):
     y_true = K.argmax(y_true_cat, axis=1)
     y_pred = K.argmax(y_pred_cat, axis=1)
     correct = K.max(y_true_cat * y_pred_cat, axis=1)
     is_unrelated = K.not_equal(y_true, unrelated)
     is_related = (K.any([K.equal(y_true, r) for r in related_labels])
                   and K.any([K.equal(y_pred, r) for r in related_labels]))
     score = correct * (0.25 + is_unrelated * 0.5) + is_related * 0.25
     return 1.0 / (score + K.epsilon())
def mean_squared_error_label(y_true, y_pred, nonzero_count):
    index = K.not_equal(y_true, -1)
    index = K.cast(index, y_true.dtype)
    y_true_ = tf.multiply(y_true, index)
    y_pred_ = tf.multiply(y_pred, index)
    loss = tf.reduce_sum(K.square(y_pred_ - y_true_))
    nonzero_count = K.cast(nonzero_count, loss.dtype)
    loss = tf.divide(loss, nonzero_count)
    return loss
Beispiel #19
0
def masked_landmark(y_true_full, y_pred):
    y_true = y_true_full[:, 6:]
    mask = K.cast(K.not_equal(y_true[:, 0], -1), K.floatx())
    y_true = tf.boolean_mask(y_true, mask)
    y_pred = tf.boolean_mask(y_pred, mask)

    loss = K.mean(K.mean(K.square(y_pred - y_true), axis=-1))
    s = K.sum(mask)
    return K.switch(K.equal(s, 0), .0, loss)
Beispiel #20
0
def spancom_r(y_true, y_pred):

    y_true = K.argmax(y_true, axis=-1)
    y_pred = K.argmax(y_pred, axis=-1)

    mask = K.cast(K.not_equal(y_pred, 0), 'int64')
    matches = K.cast(K.equal(y_true, y_pred), 'int64') * mask
    accuracy = K.sum(matches) / K.maximum(K.sum(mask), 1)
    return accuracy
 def compute_mask(self, inputs, mask=None):
     if self.mode == self.MODE_EXPAND:
         if self.mask_zero:
             output_mask = K.not_equal(inputs, self.mask_zero)
         else:
             output_mask = None
     else:
         output_mask = mask
     return output_mask
Beispiel #22
0
def metrics_K(y_true, y_pred):
    """Calculates quality metrics from true and predicted Tensorflow values.

    Uses just the keras.backend (K) functions to handle Tensors without eager
    execution.
    Definitions from paper 'A Machine Learning Framework to Identify Detailed
    Routing Short Violations from a Placed Netlist' by Tabrizi et al. 2018

    Args:
        y_true (Tensor): Array of 'true' 0s and 1s
        y_pred (Tensor): Array of predicted 0s and 1s

    Returns:
        TN (Tensor):     Number of true negative samples
        FP (Tensor):     Number of false positive samples
        FN (Tensor):     Number of false negative samples
        TP (Tensor):     Number of true positive samples
        TPR (Tensor):    True Positive Rate ([0:1])
        SPC (Tensor):    Specificity ([0:1])
        FPR (Tensor):    False Positive Rate ([0:1])
        ACC (Tensor):    Accuracy ([0:1])
        MCC (Tensor):    Matthews Correlation Coefficient, ([-1:+1]), +1 is best
    """
    # Get predicted value by rounding the probability value to the nearest int.
    y_pred = K.round(y_pred)

    # assert(K.int_shape(y_true) == K.int_shape(y_pred))
    # Total number of elements in y_true or y_pred
    num_total = K.prod(K.shape(y_true))
    # Cast from int32 to floatx_wanted to ensure compatibility with other values
    num_total = K.cast(num_total, floatx_wanted)

    # Dot product gives sum of instances where y_true and y_pred are both 1.
    TP = K.dot(y_true, K.transpose(y_pred))
    FN = K.sum(y_true) - TP
    falses = K.not_equal(y_true, y_pred)
    # Convert from bools to 0. and 1.
    falses = K.cast(falses, floatx_wanted)
    FP = K.dot(y_pred, K.transpose(falses))
    TN = num_total - FP

    # Use K.epsilon to avoid division by zero.
    eps = K.epsilon()

    # Sensitivity or True Positive Rate (TPR)
    TPR = TP/(TP + FN + eps)
    # Specificity (SPC)
    SPC = TN/(TN + FP + eps)
    # False Alarm (FPR)
    FPR = FP/(TN + FP + eps)
    #  Accuracy (ACC)
    ACC = (TP + TN)/(TP + TN + FP + FN + eps)
    # Matthews Correlation Coefficient (MCC); Values from -1 to +1, +1 is best.
    MCC = (TP*TN - FP*FN)/K.sqrt((TP + FP + eps) * (TP + FN + eps) *
                                 (TN + FP + eps) * (TN + FN + eps))
    return TP, TN, FP, FN, TPR, SPC, FPR, ACC, MCC
Beispiel #23
0
    def call(self, inputs, mask=None):
        atoms, bonds, edges = inputs

        # Import dimensions
        num_samples = atoms._keras_shape[0]
        max_atoms = atoms._keras_shape[1]
        num_atom_features = atoms._keras_shape[-1]
        num_bond_features = bonds._keras_shape[-1]

        # Create a matrix that stores for each atom, the degree it is
        atom_degrees = K.sum(tf.keras.backend.cast(K.not_equal(edges, -1),
                                                   dtype='float32'),
                             axis=-1,
                             keepdims=True)

        # For each atom, look up the features of it's neighbour
        neighbour_atom_features = neighbour_lookup(atoms,
                                                   edges,
                                                   include_self=True)

        # Sum along degree axis to get summed neighbour features
        summed_atom_features = K.sum(neighbour_atom_features, axis=-2)

        # Sum the edge features for each atom
        summed_bond_features = K.sum(bonds, axis=-2)

        # Concatenate the summed atom and bond features
        summed_features = K.concatenate(
            [summed_atom_features, summed_bond_features], axis=-1)

        # For each degree we convolve with a different weight matrix
        new_features_by_degree = []
        for degree in range(self.max_degree):

            # Create mask for this degree
            atom_masks_this_degree = K.cast(K.equal(atom_degrees, degree),
                                            K.floatx())

            # Multiply with hidden merge layer
            #   (use time Distributed because we are dealing with 2D input/3D for batches)
            # Add keras shape to let keras now the dimensions
            summed_features._keras_shape = (None, max_atoms,
                                            num_atom_features +
                                            num_bond_features)
            new_unmasked_features = self.inner_3D_layers[degree](
                summed_features)

            # Do explicit masking because TimeDistributed does not support masking
            new_masked_features = new_unmasked_features * atom_masks_this_degree

            new_features_by_degree.append(new_masked_features)

        # Finally sum the features of all atoms
        new_features = keras.layers.add(new_features_by_degree)

        return new_features
Beispiel #24
0
def sparse_recon_loss_mse(y_true, y_pred):
    #this gets the numerator correct on the final denominator of the loss function

    mask_value = 0
    mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
    n = K.int_shape(y_true)[-1]
    ones = K.ones(shape=K.shape(y_true))
    denom = K.sum(mask * ones, axis=-1)

    return K.sum(K.square(y_true * mask - y_pred * mask), axis=-1) / denom
Beispiel #25
0
    def update_state(self, y_true, y_pred, sample_weight=None):

        y_true = K.argmax(y_true, axis=-1)
        y_pred = K.argmax(y_pred, axis=-1)
        y_true = K.flatten(y_true)

        true_neg = K.sum(
            K.cast((K.not_equal(y_true, y_pred)), dtype=tf.float32))

        self.cat_true_negatives.assign_add(true_neg)
Beispiel #26
0
 def compute_mask(self, inputs, input_mask=None):
     """
     Compute the mask for the hidden state tensor. For each node, [True] indicates un-masked, and [False] indicates
     masked. The returned value has only one element in the last dimension, since feature dimension is reduced to a
     boolean
     :param inputs:
     :param input_mask:
     :return: A boolean tensor of shape (num_samples, num_nodes, 1)
     """
     return K.any(K.not_equal(inputs[0], 0.0), axis=-1)
Beispiel #27
0
def masked_multi_weighted_bce(alpha, y_pred):
    y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
    y_true_ = K.cast(K.greater(alpha, 0.), K.floatx())
    mask = K.cast(K.not_equal(alpha, 0.), K.floatx())
    num_not_missing = K.sum(mask, axis=-1)
    alpha = K.abs(alpha)
    bce = -alpha * y_true_ * K.log(y_pred) - (1.0 - alpha) * (
        1.0 - y_true_) * K.log(1.0 - y_pred)
    masked_bce = bce * mask
    return K.sum(masked_bce, axis=-1) / num_not_missing
        def cropOutputs(x):
            # x[0] is decoded at the end
            # x[1] is inputs
            # both have the same shape

            # padding = 1 for actual data in inputs, 0 for 0
            padding = K.cast(K.not_equal(x[1], 0), dtype=K.floatx())
            # if you have zeros for non-padded data, they will lose their backpropagation

            return x[0] * padding
def _useful_symbols_mask_func(X, dtype):
    """
    возвращает маску, содержащую нули на месте нулей, идущих до содержательных символов,
    а также на месте PAD в конце
    """
    # X = kb.cast(kb.not_equal(X, 0), dtype="int32")
    # X_max = kb.max(X, axis=1)[:,None]
    # X, X_max = kb.cast(X, "int32"), kb.cast(X_max, "int32")
    # return kb.cast(kb.greater_equal(kb.cumsum(X, axis=1), X_max), dtype=dtype)
    return kb.cast(kb.not_equal(X, PAD), dtype=dtype)
Beispiel #30
0
 def call(self, text_input, mask=None):
     """ text_input should be a [None, max_words, max_chars] tensor.
     """
     bool_mask = K.not_equal(text_input[:, :, 0], 0.0)  # [None, max_words]
     float_mask = K.cast(bool_mask, K.floatx())  # [None, max_words]
     repeat_mask = K.repeat(
         float_mask, self.target_hidden)  # [None, target_hidden, max_words]
     permute_mask = K.permute_dimensions(
         repeat_mask, [0, 2, 1])  # [None, max_words, target_hidden]
     return permute_mask
Beispiel #31
0
	def lcompose(self, x):
		""" Compose a label representation given the word vectors in
		    the given word sequence (description). """
		if self.args['maskedavg']:
			mask = K.not_equal(K.sum(K.abs(x), axis=3, keepdims=False), 0)
			n = K.sum(K.cast(mask, 'float32'), axis=2, keepdims=True)
			x_mean = K.sum(x, axis=2, keepdims=False) / n
		else:
			x_mean = K.mean(x, axis=2)
		return x_mean
Beispiel #32
0
 def get_loss(args):
     y_pred, y_true = args
     y_true = K.cast(y_true, 'int32')
     loss = K.sparse_categorical_crossentropy(y_true,
                                              y_pred,
                                              from_logits=True)
     mask = K.cast(K.not_equal(y_true, 0), K.floatx())
     loss = K.sum(loss * mask, -1) / K.sum(mask, -1)
     loss = K.mean(loss)
     return loss
Beispiel #33
0
    def update_neurons(self):
        """Update neurons according to activation function."""

        # Update membrane potentials.
        new_mem = self.get_new_mem()

        # Generate spikes.
        output_spikes = self.linear_activation(new_mem)

        # Reset membrane potential after spikes.
        self.set_reset_mem(new_mem, output_spikes)

        # Store refractory period after spikes.
        new_refrac = k.tf.where(k.not_equal(output_spikes, 0),
                                k.ones_like(output_spikes) *
                                (self.time + self.tau_refrac),
                                self.refrac_until)
        c = new_refrac[:self.batch_size]
        cc = k.concatenate([c, c], 0)
        updates = [k.tf.assign(self.refrac_until, cc)]

        if self.spiketrain is not None:
            c = self.time * k.cast(k.not_equal(output_spikes, 0),
                                   k.floatx())[:self.batch_size]
            cc = k.concatenate([c, c], 0)
            updates += [k.tf.assign(self.spiketrain, cc)]

        with k.tf.control_dependencies(updates):
            masked_impulse = k.tf.where(k.greater(self.refrac_until, self.time),
                                        k.zeros_like(self.impulse),
                                        self.impulse)
            c = k.greater(masked_impulse, 0)[:self.batch_size]
            cc = k.cast(k.concatenate([c, c], 0), k.floatx())
            updates = [k.tf.assign(self.prospective_spikes, cc)]
            new_thresh = self._v_thresh * k.ones_like(self.v_thresh) + \
                self.missing_impulse
            updates += [k.tf.assign(self.v_thresh, new_thresh)]

            with k.tf.control_dependencies(updates):
                # Compute post-synaptic potential.
                psp = self.get_psp(output_spikes)

                return k.cast(psp, k.floatx())
Beispiel #34
0
def get_time_step_length_without_padding(x, time_step_dim=-2, padding=0):
    '''Gets time steps without padding (right) of a input tensor.

    # Parameters
    ----------
    x : a tensor whose dimensions >=3
    time_step_dim: the time step dimension of x
    padding : a scalar tensor that represents the padding
    # Returns
    ------
    a tensor represents the length of the input tensor after removing all right padding zeros
    '''
    ndim = K.ndim(x)
    time_step_dim = time_step_dim % ndim
    x = K.cast(K.not_equal(x, padding), 'int32')    # binary tensor
    axis = [i for i in range(ndim) if i != time_step_dim]
    s = K.sum(x, axis)
    s = K.cast(K.not_equal(s, 0), 'int32')
    return K.sum(s)
Beispiel #35
0
    def weighted_mean_squared_error(self, y_true, y_pred):
        mask = K.cast(K.not_equal(y_true, 0), 'float32')
        n = 1/K.sum(mask, 1)
        n = K.tile(n, (K.shape(n)[1], self.n_sessions))
        n = K.batch_flatten(mask) * n

        sq_errs = K.batch_flatten(K.square(y_pred - y_true))
        w_sq_errs = sq_errs * n
        sum_w_sq_errs = K.sum(w_sq_errs)
        return sum_w_sq_errs / K.sum(n)
Beispiel #36
0
def mask_aware_mean(x):
    # recreate the masks - all zero rows have been masked
    mask = K.not_equal(K.sum(K.abs(x), axis=2, keepdims=True), 0)

    # number of that rows are not all zeros
    n = K.sum(K.cast(mask, 'float32'), axis=1, keepdims=False)

    # compute mask-aware mean of x
    x_mean = K.sum(x, axis=1, keepdims=False) / n

    return x_mean
Beispiel #37
0
 def lookup(self, x, W, memory_length):
     # shape: (batch*memory_length, input_length)
     x = K.cast(K.reshape(x, (-1, self.input_length)), 'int32')
     mask = K.expand_dims(K.not_equal(x, 0.), dim=-1)
     # shape: (batch*memory_length, input_length, output_dim)
     X = K.gather(W, x)
     if self.bow_mode == "bow":
         # shape: (batch*memory_length, output_dim)
         X = K.sum(X + K.expand_dims(self.Te, 0), axis=1)
     # shape: (batch, memory_length, output_dim)
     X = K.reshape(X, (-1, memory_length, self.output_dim))
     return X, mask
 def _drop_path(self, inputs):
     count = len(inputs)
     drops = K.switch(
         self.is_global,
         self._gen_global_path(count),
         self._gen_local_drops(count, self.p)
     )
     ave = K.zeros(shape=self.average_shape)
     for i in range(0, count):
         ave += inputs[i] * drops[i]
     sum = K.sum(drops)
     # Check that the sum is not 0 (global droppath can make it
     # 0) to avoid divByZero
     ave = K.switch(
         K.not_equal(sum, 0.),
         ave/sum,
         ave)
     return ave
Beispiel #39
0
    def get_output(self, train=False):
        X = self.get_input(train)
        retain_p = 1. - self.dropout
        if train and self.dropout > 0:
            B = K.random_binomial((self.input_dim,), p=retain_p)
        else:
            B = K.ones((self.input_dim)) * retain_p
        # we zero-out rows of W at random
        Xs = K.cast(K.reshape(X, (-1, self.nb_words)), 'int32')

        # (samples*input_length, nb_words, dim)
        out = K.gather(self.W * K.expand_dims(B), Xs)
        out = K.reshape(out, (-1, self.input_length, self.nb_words,
                              self.output_dim))
        # (samples, input_length, nb_words, dim)
        out = out * K.expand_dims(K.not_equal(X, 0), dim=-1)
        if self.bow_mode == "bow":
            out = K.sum(out, axis=2)
        return out
Beispiel #40
0
    def lstmCh_word_pro(self):

        ''' input '''
        arg1_ch_input = Input(shape=(self.arg_maxlen * self.word_maxlen,), dtype='int32', name='arg1_ch')
        arg2_ch_input = Input(shape=(self.arg_maxlen * self.word_maxlen,), dtype='int32', name='arg2_ch')
        arg1_word_input = Input(shape=(self.arg_maxlen,), dtype='int32', name='arg1_word')
        arg2_word_input = Input(shape=(self.arg_maxlen,), dtype='int32', name='arg2_word')
        arg1_mask = K.not_equal(arg1_word_input, 0)
        arg2_mask = K.not_equal(arg2_word_input, 0)

        ''' embedding'''
        # chareters
        emb_ch = Embedding(input_dim=self.ch_dim, input_length=self.arg_maxlen * self.word_maxlen,
                           output_dim=self.ch_ndims)
        arg1_ch = emb_ch(arg1_ch_input)
        arg2_ch = emb_ch(arg2_ch_input)
        arg1_ch = Reshape((self.arg_maxlen, self.word_maxlen, self.ch_ndims))(arg1_ch)
        arg2_ch = Reshape((self.arg_maxlen, self.word_maxlen, self.ch_ndims))(arg2_ch)

        # words
        emb_word = Embedding(input_dim=self.word_dim, input_length=self.arg_maxlen, weights=[self.WE],output_dim=self.word_ndims,trainable=False)
        arg1_word = emb_word(arg1_word_input)
        arg2_word = emb_word(arg2_word_input)


        ''' convolution ch'''
        ch_conv1 = Convolution1D(nb_filter=self.ch_nb_filter,
                             filter_length=self.ch_filter_length[0],
                             border_mode='same',
                             activation=self.activation,
                             subsample_length=1)
        ch_conv2 = Convolution1D(nb_filter=self.ch_nb_filter,
                     filter_length=self.ch_filter_length[1],
                     border_mode='same',
                     activation=self.activation,
                     subsample_length=1)
        ch_conv3 = Convolution1D(nb_filter=self.ch_nb_filter,
             filter_length=self.ch_filter_length[2],
             border_mode='same',
             activation=self.activation,
             subsample_length=1)

        ''' pooling ch '''
        mpool = MaxPooling1D(pool_length=self.word_maxlen)
        tdcnn1 = TimeDistributed(ch_conv1)
        tdcnn2 = TimeDistributed(ch_conv2)
        tdcnn3 = TimeDistributed(ch_conv3)
        tdpol = TimeDistributed(mpool)

        '''arg1 cnn+pooling'''
        arg1_tconv1 = tdcnn1(arg1_ch)
        arg1_tpool1 = tdpol(arg1_tconv1)
        arg1_tpool_res1 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg1_tpool1)
        arg1_tconv2 = tdcnn2(arg1_ch)
        arg1_tpool2 = tdpol(arg1_tconv2)
        arg1_tpool_res2 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg1_tpool2)
        arg1_tconv3 = tdcnn3(arg1_ch)
        arg1_tpool3 = tdpol(arg1_tconv3)
        arg1_tpool_res3 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg1_tpool3)

        '''arg2 cnn+pooling'''
        arg2_tconv1 = tdcnn1(arg2_ch)
        arg2_tpool1 = tdpol(arg2_tconv1)
        arg2_tpool_res1 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg2_tpool1)
        arg2_tconv2 = tdcnn2(arg2_ch)
        arg2_tpool2 = tdpol(arg2_tconv2)
        arg2_tpool_res2 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg2_tpool2)
        arg2_tconv3 = tdcnn3(arg2_ch)
        arg2_tpool3 = tdpol(arg2_tconv3)
        arg2_tpool_res3 = Reshape((self.arg_maxlen, self.ch_nb_filter))(arg2_tpool3)

        '''lstm'''
        arg1_ch_cnn_merge = merge([arg1_tpool_res1,arg1_tpool_res2,arg1_tpool_res3], mode='concat', concat_axis=2)
        arg2_ch_cnn_merge = merge([arg2_tpool_res1,arg2_tpool_res2,arg2_tpool_res3], mode='concat', concat_axis=2)

        # arg1_lstmf = LSTM(self.lstm_size,return_sequences=True)(arg1_ch_cnn_merge, mask=arg1_mask)
        # arg1_lstmb = LSTM(self.lstm_size,go_backwards=True,return_sequences=True)(arg1_ch_cnn_merge, mask=arg1_mask)
        # arg2_lstmf = LSTM(self.lstm_size,return_sequences=True)(arg2_ch_cnn_merge, mask=arg2_mask)
        # arg2_lstmb = LSTM(self.lstm_size,go_backwards=True,return_sequences=True)(arg2_ch_cnn_merge, mask=arg2_mask)
        arg1_lstmf = LSTM(self.lstm_size,return_sequences=True)(arg1_ch_cnn_merge)
        arg1_lstmb = LSTM(self.lstm_size,go_backwards=True,return_sequences=True)(arg1_ch_cnn_merge)
        arg2_lstmf = LSTM(self.lstm_size,return_sequences=True)(arg2_ch_cnn_merge)
        arg2_lstmb = LSTM(self.lstm_size,go_backwards=True,return_sequences=True)(arg2_ch_cnn_merge)
        arg1_lstm = merge([arg1_lstmf,arg1_lstmb,arg1_word],mode='concat')
        arg2_lstm = merge([arg2_lstmf,arg2_lstmb,arg2_word],mode='concat')

        arg1_lstm = Dropout(self.dropout)(arg1_lstm)
        arg2_lstm = Dropout(self.dropout)(arg2_lstm)

        ''' ch+word-level cnn + pooling'''
        word_cnn1 = Convolution1D(nb_filter=self.word_nb_filter,
                                 filter_length=self.word_filter_length[0],
                                 border_mode='same',
                                 activation=self.activation,
                                 subsample_length=1)
        word_cnn2 = Convolution1D(nb_filter=self.word_nb_filter,
                                 filter_length=self.word_filter_length[1],
                                 border_mode='same',
                                 activation=self.activation,
                                 subsample_length=1)
        word_cnn3 = Convolution1D(nb_filter=self.word_nb_filter,
                                 filter_length=self.word_filter_length[2],
                                 border_mode='same',
                                 activation=self.activation,
                                 subsample_length=1)
        word_mpol = MaxPooling1D(pool_length=self.arg_maxlen)

        arg1_word_cnn1 = word_cnn1(arg1_lstm)
        arg1_word_cnn2 = word_cnn2(arg1_lstm)
        arg1_word_cnn3 = word_cnn3(arg1_lstm)
        arg2_word_cnn1 = word_cnn1(arg2_lstm)
        arg2_word_cnn2 = word_cnn2(arg2_lstm)
        arg2_word_cnn3 = word_cnn3(arg2_lstm)

        # merge all cnn arg:
        arg1_cnn_merge = merge([arg1_word_cnn1,arg1_word_cnn2,arg1_word_cnn3])
        arg2_cnn_merge = merge([arg2_word_cnn1,arg2_word_cnn2,arg2_word_cnn3])
        arg1_word_mp = word_mpol(arg1_cnn_merge)
        arg2_word_mp = word_mpol(arg2_cnn_merge)

        flatten = Flatten()
        arg1_word_mp = flatten(arg1_word_mp)
        arg2_word_mp = flatten(arg2_word_mp)

        merged_vector = merge([arg1_word_mp,arg2_word_mp], mode='concat', concat_axis=-1)

        for i in range(0,self.dense_num):
            merged_vector = Dense(self.dense_size,activation=self.activation)(merged_vector)
            print("add %d times dense..."%(i))

        merged_vector = Dropout(self.dropout)(merged_vector)

        predictions = Dense(11, activation='softmax', name="output")(merged_vector)

        model = Model(input=[arg1_ch_input, arg2_ch_input, arg1_word_input, arg2_word_input],
                      output=predictions)
        # model.summary()
        # plot(model, to_file='lstmcnn.png')

        ada = Adagrad(lr=self.lr, epsilon=1e-06)
        model.compile(optimizer=ada, loss='categorical_crossentropy', metrics=['accuracy'])
        return model
 def masked_avg_emb(src_seq_repr_seq, src_seq):
     mask = K.cast(K.expand_dims(K.not_equal(src_seq, 0), -1), 'float32')
     src_seq_repr_seq = src_seq_repr_seq * mask
     src_seq_repr_seq = K.mean(src_seq_repr_seq, axis=1, keepdims=True)
     return K.reshape(src_seq_repr_seq, [-1, d_model])
Beispiel #42
0
def tn(y_true, y_pred):
	s_flow = K.variable(np.array([1,0]))
	n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	t = K.cast(K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	tr_n = t*n
	return K.sum(tr_n)
 def compute_mask(self, x, mask=None):
     if not self.mask_zero:
         return None
     else:
         return K.not_equal(x, 1)
 def compute_mask(self, inputs, mask=None):
     return K.any(K.not_equal(inputs, 0.), axis=[2, 3, 4])
Beispiel #45
0
def fn(y_true, y_pred):
	s_flow = K.variable(np.array([1,0]))
	n = K.cast(K.not_equal(K.argmax(s_flow, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	f = K.cast(K.not_equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)), K.floatx())
	fl_n = f*n
	return K.sum(fl_n)
 def _get_anchor_negative_triplet_mask(self, y_true: Tensor, pairwise_dist: Tensor) -> Tensor:
     # mask label(n) == label(a)
     mask = K.not_equal(K.expand_dims(y_true, 0), K.expand_dims(y_true, 1))
     mask = K.cast(mask, K.dtype(pairwise_dist))
     return mask
Beispiel #47
0
 def get_output_mask(self, train=None):
     X = self.get_input(train)
     if not self.mask_zero:
         return None
     else:
         return K.not_equal(X, 0)