def loss_CCC(seq1, seq2): """ FUNCTION NAME: loss_CCC This function implements the Concordance Correlation Coefficient (CCC) to be used as a loss function to train models. INPUT: ------ -> seq1: tensor with the true output: (num_batches, seq_len, 1) -> seq2: tensor with the predicted output: (num_batches, seq_len, 1) OUTPUT: ------- <- cccLoss: (1 - CCC) computed to be used as a CCC loss """ seq1 = K.squeeze(seq1, axis=-1) seq2 = K.squeeze(seq2, axis=-1) seq1_mean = K.mean(seq1, axis=-1, keepdims=True) seq2_mean = K.mean(seq2, axis=-1, keepdims=True) cov = (seq1-seq1_mean)*(seq2-seq2_mean) seq1_var = K.mean(K.square(seq1-seq1_mean), axis=-1, keepdims=True) seq2_var = K.mean(K.square(seq2-seq2_mean), axis=-1, keepdims=True) CCC = K.constant(2.) * cov / (seq1_var + seq2_var + K.square(seq1_mean - seq2_mean) + K.epsilon()) CCC_loss = K.constant(1.) - CCC return CCC_loss
def merge_pred(mean_pred, max_pred, partV): mean_pred = K.squeeze(mean_pred, -1) max_pred = K.squeeze(max_pred, -1) output = tf.map_fn(lambda x: each_merge(x[0], x[1], partV), (mean_pred, max_pred), dtype=tf.float32) return tf.reshape(output, [-1, 1])
def fpn_classifier_graph(rois, feature_maps, image_shape, pool_size, num_classes): """Builds the computation graph of the feature pyramid network classifier and regressor heads. rois: [batch, num_rois, (y1, x1, y2, x2)] Proposal boxes in normalized coordinates. feature_maps: List of feature maps from diffent layers of the pyramid, [P2, P3, P4, P5]. Each has a different resolution. image_shape: [height, width, depth] pool_size: The width of the square feature map generated from ROI Pooling. num_classes: number of classes, which determines the depth of the results Returns: logits: [N, NUM_CLASSES] classifier logits (before softmax) probs: [N, NUM_CLASSES] classifier probabilities bbox_deltas: [N, (dy, dx, log(dh), log(dw))] Deltas to apply to proposal boxes """ # ROI Pooling # Shape: [batch, num_boxes, pool_height, pool_width, channels] x = PyramidROIAlign([pool_size, pool_size], image_shape, name="roi_align_classifier")([rois] + feature_maps) print x.get_shape() # Two 1024 FC layers (implemented with Conv2D for consistency) x = TimeDistributed(Conv2D(1024, (pool_size, pool_size), padding="valid"), name="mrcnn_class_conv1")(x) x = TimeDistributed(BatchNorm(axis=3), name='mrcnn_class_bn1')(x) x = Activation('relu')(x) x = TimeDistributed(Conv2D(1024, (1, 1)), name="mrcnn_class_conv2")(x) x = TimeDistributed(BatchNorm(axis=3), name='mrcnn_class_bn2')(x) x = Activation('relu')(x) shared = Lambda(lambda x: K.squeeze(K.squeeze(x, 4), 3), name="pool_squeeze")(x) print shared.get_shape() # Classifier head mrcnn_class_logits = TimeDistributed(Dense(num_classes), name='mrcnn_class_logits')(shared) mrcnn_probs = TimeDistributed(Activation("softmax"), name="mrcnn_class")(mrcnn_class_logits) print mrcnn_class_logits.get_shape() print mrcnn_probs.get_shape() # BBox head # [batch, boxes, num_classes * (dy, dx, log(dh), log(dw))] x = TimeDistributed(Dense(num_classes * 4, activation='linear'), name='mrcnn_bbox_fc')(shared) # Reshape to [batch, boxes, num_classes, (dy, dx, log(dh), log(dw))] s = tf.shape(x) mrcnn_bbox = Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox
def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox): """Return the RPN bounding box loss graph. config: the model config object. target_bbox: [batch, max positive anchors, (dy, dx, log(dh), log(dw))]. Uses 0 padding to fill in unsed bbox deltas. rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive, -1=negative, 0=neutral anchor. rpn_bbox: [batch, anchors, (dy, dx, log(dh), log(dw))] """ # Positive anchors contribute to the loss, but negative and # neutral anchors (match value of 0 or -1) don't. rpn_match = K.squeeze(rpn_match, -1) indices = tf.where(K.equal(rpn_match, 1)) # Pick bbox deltas that contribute to the loss rpn_bbox = tf.gather_nd(rpn_bbox, indices) # Trim target bounding box deltas to the same length as rpn_bbox. batch_counts = K.sum(K.cast(K.equal(rpn_match, 1), tf.int32), axis=1) target_bbox = batch_pack_graph(target_bbox, batch_counts, config.IMAGES_PER_GPU) loss = smooth_l1_loss(target_bbox, rpn_bbox) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) return loss
def _each(board1, board2): # 按x排序, 没起作用 board1 = tf.map_fn(lambda x: _sort(x), board1, dtype=tf.float32) board2 = tf.map_fn(lambda x: _sort(x), board2, dtype=tf.float32) # # 查看输入x+v值 # if EmbeddingsLayer.debug: # logging.getLogger().info("--board1\n %s" % (board1)) # logging.getLogger().info("--board2\n %s" % (board2)) board1 = tf.slice(board1, [0, 0, 1], [-1, -1, 1]) board2 = tf.slice(board2, [0, 0, 1], [-1, -1, 1]) board1 = K.squeeze(board1, -1) board2 = K.squeeze(board2, -1) # shape (17, 5) output = tf.map_fn(lambda x: _compare(x[0], x[1]), (board1, board2), dtype=tf.float32) return output
def energy_step(inputs, states): """ Step function for computing energy for a single decoder state """ # input: (batch_size, latent_dim) assert_msg = "States must be a list. However states {} is of type {}".format( states, type(states)) assert isinstance(states, list) or isinstance(states, tuple), assert_msg """ Computing sj.Ua """ # (batch_size, 1, d3) U_a_dot_s = K.expand_dims(K.dot(inputs, self.U_a), 1) if verbose: print('Ua.h>', K.int_shape(U_a_dot_s)) """ tanh(h.Wa + s.Ua) """ # (batch_size, h1*h2*...*hn, d3) = (batch_size, h1*h2*...*hn, d3) + (batch_size, 1, d3) Wh_plus_Us = K.tanh(W_hi + U_a_dot_s) # (batch_size, d3, h1*h2*...*hn) Wh_plus_Us = K.permute_dimensions(Wh_plus_Us, (0, 2, 1)) if verbose: print('Wh+Us>', K.int_shape(Wh_plus_Us)) """ softmax(va.tanh(S.Wa + hj.Ua)) """ # (1, batch_size, h1*h2*...*hn) = (1, d3) . (batch_size, d3, h1*h2*...*hn) Wh_plus_Us_dot_Va = K.dot(self.V_a, Wh_plus_Us) # (batch_size, h1*h2*...*hn) e_i = K.squeeze(Wh_plus_Us_dot_Va, 0) e_i = K.softmax(e_i) if verbose: print('ei>', K.int_shape(e_i)) # (batch_size, h1*h2*...*hn) return e_i, states
def call(self, inputs, training=None): class_labels = K.squeeze(inputs[1], axis=1) inputs = inputs[0] input_shape = K.int_shape(inputs) reduction_axes = list(range(0, len(input_shape))) if self.axis is not None: del reduction_axes[self.axis] del reduction_axes[0] normed = inputs broadcast_shape = [1] * len(input_shape) broadcast_shape[0] = K.shape(inputs)[0] if self.axis is not None: broadcast_shape[self.axis] = input_shape[self.axis] if self.scale: broadcast_gamma = K.reshape(K.gather(self.gamma, class_labels), broadcast_shape) normed = normed * broadcast_gamma if self.center: broadcast_beta = K.reshape(K.gather(self.beta, class_labels), broadcast_shape) normed = normed + broadcast_beta return normed
def call(self, inputs, **kwargs): inputs_shape = K.shape(inputs) mask = K.cast(K.squeeze(K.any(K.not_equal(inputs, 0.), axis=(-2, -1), keepdims=True), axis=-1), dtype=inputs.dtype) inputs_to_lstm = K.reshape(inputs, (-1, inputs.shape[-2], inputs.shape[-1])) inputs_embed = super(InferenceSpeakerEmbedding, self).call(inputs_to_lstm) inputs_embed = K.reshape( inputs_embed, (inputs_shape[0], inputs_shape[1], inputs_embed.shape[-1])) inputs_embed = inputs_embed * mask n = K.sum(mask, axis=1) inputs_embed = K.sum(inputs_embed, axis=1) / n return inputs_embed
def call(self, inputs): def apply_separate_filter_for_each_batch(inputs): kernel = inputs[1] x = K.expand_dims(inputs[0], axis=0) outputs = K.conv2d( x, kernel, strides=self.strides, padding=self.padding, data_format=self.data_format, dilation_rate=self.dilation_rate) if self.bias is not None: bias = inputs[2] outputs = K.bias_add(outputs, bias, data_format=self.data_format) return K.squeeze(outputs, axis=0) x = inputs[0] classes = K.squeeze(inputs[1], axis=1) if self.bias is not None: outputs = K.map_fn(apply_separate_filter_for_each_batch, [x, K.gather(self.kernel, classes), K.gather(self.bias, classes)], dtype='float32') else: outputs = K.map_fn(apply_separate_filter_for_each_batch, [x, K.gather(self.kernel, classes)], dtype='float32') if self.activation is not None: return self.activation(outputs) return outputs
def call(self, inputs): if self.data_format is None: data_format = self.data_format if self.data_format not in {'channels_first', 'channels_last'}: raise ValueError('Unknown data_format ' + str(data_format)) strides = (1,) + self.strides + (1,) x = inputs[0] cls = K.squeeze(inputs[1], axis=-1) #Kernel preprocess kernel = K.gather(self.kernel, cls) #(bs, w, h, c) kernel = tf.transpose(kernel, [1, 2, 3, 0]) #(w, h, c, bs) kernel = K.reshape(kernel, (self.kernel_size[0], self.kernel_size[1], -1)) #(w, h, c * bs) kernel = K.expand_dims(kernel, axis=-1) #(w, h, c * bs, 1) if self.data_format == 'channles_first': x = tf.transpose(x, [0, 2, 3, 1]) bs, w, h, c = K.int_shape(x) #(bs, w, h, c) x = tf.transpose(x, [1, 2, 3, 0]) #(w, h, c, bs) x = K.reshape(x, (w, h, -1)) #(w, h, c * bs) x = K.expand_dims(x, axis=0) #(1, w, h, c * bs) padding = _preprocess_padding(self.padding) outputs = tf.nn.depthwise_conv2d(x, kernel, strides=strides, padding=padding, rate=self.dilation_rate) #(1, w, h, c * bs) _, w, h, _ = K.int_shape(outputs) outputs = K.reshape(outputs, [w, h, self.filters, -1]) #(w, h, c, bs) outputs = tf.transpose(outputs, [3, 0, 1, 2]) #(bs, w, h, c) if self.bias is not None: #(num_cls, out) bias = tf.gather(self.bias, cls) #(bs, bias) bias = tf.expand_dims(bias, axis=1) bias = tf.expand_dims(bias, axis=1) #(bs, bias, 1, 1) outputs += bias if self.data_format == 'channles_first': outputs = tf.transpose(outputs, [0, 3, 1, 2]) if self.activation is not None: return self.activation(outputs) return outputs
def call(self, x, mask=None): result = self.elmo( K.squeeze(K.cast(x, tf.string), axis=1), as_dict=True, signature='default', )['default'] return result
def energy_step(inputs, states): """ Step function for computing energy for a single decoder state inputs: (batchsize * 1 * de_in_dim) states: (batchsize * 1 * de_latent_dim) """ """ Some parameters required for shaping tensors""" en_seq_len, en_hidden = encoder_out_seq.shape[ 1], encoder_out_seq.shape[2] de_hidden = inputs.shape[-1] """ Computing S.Wa where S=[s0, s1, ..., si]""" # <= batch size * en_seq_len * latent_dim W_a_dot_s = K.dot(encoder_out_seq, self.W_a) """ Computing hj.Ua """ U_a_dot_h = K.expand_dims(K.dot(inputs, self.U_a), 1) # <= batch_size, 1, latent_dim """ tanh(S.Wa + hj.Ua) """ # <= batch_size*en_seq_len, latent_dim Ws_plus_Uh = K.tanh(W_a_dot_s + U_a_dot_h) """ softmax(va.tanh(S.Wa + hj.Ua)) """ # <= batch_size, en_seq_len e_i = K.squeeze(K.dot(Ws_plus_Uh, self.V_a), axis=-1) # <= batch_size, en_seq_len e_i = K.softmax(e_i) return e_i, [e_i]
def soft_min_reg(cv, axis=None, min_disp=None, max_disp=None, labels=None): if axis == 1: cv = Lambda(lambda x: K.squeeze(x, axis=-1))(cv) disp_map = K.reshape( K.arange(min_disp, max_disp - 0.000001, (max_disp - min_disp) / labels, dtype="float32"), (1, 1, labels, 1)) if axis == 1: output = K.conv2d(cv, disp_map, strides=(1, 1), padding='valid', data_format="channels_first") x = K.expand_dims(K.squeeze(output, axis=1), axis=-1) else: x = K.conv2d(cv, disp_map, strides=(1, 1), padding='valid') return x
def add_dcnn_layer(self, x_input, filter_size, kernel_size, stride, padding='valid', activation='relu', name=None): x_2d = Lambda(lambda x: K.expand_dims(x, axis=2))(x_input) x_2d_t = Conv2DTranspose(filters=filter_size, kernel_size=kernel_size, strides=stride, padding=padding, activation=activation)(x_2d) x_s = Lambda(lambda w: K.squeeze(w, axis=2), name=name)(x_2d_t) return x_s
def body(i, max_len, output): i1 = tf.cast(mi[i], dtype=tf.int32) i2 = tf.cast(n[i], dtype=tf.int32) #logging.getLogger().info("--i1,i2,max_len %s %s %s" % (i1,i2,max_len) ) tmp = tf.slice(p, [i1, i2], [1, 1]) tmp = K.squeeze(tmp, 0) output = tf.concat([output, tmp], 0) return i + 1, max_len, output
def accuracy(y_true, y_pred): # reshape in case it's in shape (num_samples, 1) instead of (num_samples,) if K.ndim(y_true) == K.ndim(y_pred): y_true = K.squeeze(y_true, -1) # convert dense predictions to labels y_pred_labels = K.argmax(y_pred, axis=-1) y_pred_labels = K.cast(y_pred_labels, K.floatx()) return K.cast(K.equal(y_true, y_pred_labels), K.floatx())
def decode(example): feature = tf.io.parse_single_example(example, feature_discription) data = tf.image.decode_jpeg(feature['data']) data = tf.image.convert_image_dtype(data, tf.float32) label = feature['label'] label = kbe.one_hot(label, num_classes=10) label = kbe.squeeze(label, axis=0) return data, label
def call(self, inputs, mask=None): ''' :param inputs: a list of tensor of length not larger than 2, or a memory tensor of size BxTXD1. If a list, the first entry is memory, and the second one is query tensor of size BxD2 if any :param mask: the masking entry will be directly discarded :return: a tensor of size BxD1, weighted summing along the sequence dimension ''' if isinstance(inputs, list) and len(inputs) == 2: memory, query = inputs if self.method is None: return memory[:, -1, :] elif self.method == 'cba': hidden = K.dot(memory, self.Wh) + K.expand_dims(K.dot(query, self.Wq), 1) hidden = K.tanh(hidden) s = K.squeeze(K.dot(hidden, self.v), -1) elif self.method == 'ga': s = K.sum(K.expand_dims(K.dot(query, self.Wq), 1) * memory, axis=-1) else: s = K.squeeze(K.dot(memory, self.v), -1) if mask is not None: mask = mask[0] else: if isinstance(inputs, list): if len(inputs) != 1: raise ValueError('inputs length should not be larger than 2') memory = inputs[0] else: memory = inputs if self.method is None: return memory[:, -1, :] elif self.method == 'cba': hidden = K.dot(memory, self.Wh) hidden = K.tanh(hidden) s = K.squeeze(K.dot(hidden, self.v), -1) elif self.method == 'ga': raise ValueError('general attention needs the second input') else: s = K.squeeze(K.dot(memory, self.v), -1) s = K.softmax(s) if mask is not None: s *= K.cast(mask, dtype='float32') sum_by_time = K.sum(s, axis=-1, keepdims=True) s = s / (sum_by_time + K.epsilon()) return K.sum(memory * K.expand_dims(s), axis=1)
def loss(y_true, y_pred): y_true = K.squeeze(y_true, axis=-1) # Squeeze y_pred; i.e remove last layer which is of dim 1 y_pred_squeezed = K.squeeze(y_pred, axis=-1) """ Reconstructing x_org """ # reversed_wnorm = Lambda(lambda x: ) # reversed_wnorm = dict(map(reversed, wnorm.items())) # x_org = Lambda(lambda x: [tf.reshape(tf.where(tf.equal(wnorm, word)), [-1])[0] for sent in x for word in sent])(y_true) # x_org = # x_org = [reversed_wnorm.get(word) for sent in y_true for word in sent] x_org = raw_input print(raw_input.shape) x_temp = Lambda(lambda x: tf.cast(tf.reshape(x, [-1, ]), dtype=tf.int32))(x_org) K.print_tensor(K.shape(y_pred_squeezed), message='y_pred_squeezed are ') print(f'Inside decoder....After reshape of x_norm is {y_pred_squeezed.shape}') # Calc prob logits print(type(y_pred_squeezed)) print(type(wnorm)) print(f'wnorm shape is {wnorm.shape}') prob_logits = K.batch_dot(y_pred_squeezed, wnorm, axes=[2, 1]) prob = Lambda(lambda x: tf.nn.log_softmax(x * 100, axis=-1, name='prob_lambda'))(prob_logits) print(f'Prob shape is {prob.shape}') prob = Lambda(lambda x: tf.reshape(x, [-1, n_words]))(prob) # prob = K.reshape(prob, [-1, wnorm.shape[0]]) print(f'Prob reshaped is {prob.shape}') """ Get prob of all the words """ idx = Lambda(lambda x: tf.range(K.shape(x)[0], K.shape(x)[1]))(y_pred_squeezed) all_idx = K.transpose(K.stack([idx, x_temp])) all_prob = Lambda(lambda prob_idx_list: tf.gather_nd(prob_idx_list[0], prob_idx_list[1]))([prob, all_idx]) K.print_tensor(K.shape(all_prob), message='all_prob shape is: ') recons_loss = Lambda(lambda x: -tf.reduce_mean(x))(all_prob) # K.print_tensor(loss, message='Loss is: ') # weighted_recons_loss = loss_weight * recons_loss return recons_loss
def model(embedding_size, n_a): X = Input(shape=(T,)) E = Embedding(input_dim=input_dim, output_dim=embedding_size, input_length=T, mask_zero=True, trainable=False)(X) a1 = Bidirectional(LSTM(units=n_a, return_sequences = True))(E) # functional API needs specifying inputs, just like any functions. a2 = Dense(16, activation = "tanh")(a1) yhat = Dense(1, activation = "sigmoid")(a2) yhat = K.squeeze(yhat, axis=-1) model = Model(inputs = X, outputs = yhat) return model
def down_scaling_loop(x1, iterations, i, conv_channels, window_len, final_shape, max_iterations, b_norm): # Define parameters for model kernel_width = min([window_len, 3]) pooling_width = min([window_len, 2]) x_shrink = final_shape[0] - (2**max_iterations) * round( final_shape[0] / (2**max_iterations)) + 1 if i == 0: x1 = layers.Conv2D(1, kernel_size=(x_shrink, 1), strides=(1, 1))(x1) conv_kernel = (kernel_width, 1) x2 = custom_layers.ReflectionPadding2D(padding=(0, 2))(x1) x2 = layers.Conv2D(conv_channels[i], kernel_size=conv_kernel, dilation_rate=(2, 1))(x2) x2 = norm_activate(x2, 'relu', b_norm) x2 = custom_layers.ReflectionPadding2D(padding=(0, 2))(x2) x3 = layers.Conv2D(conv_channels[i], kernel_size=conv_kernel, dilation_rate=(2, 1))(x2) x3 = norm_activate(x3, 'relu', b_norm) if iterations > 0: # Downscale x_down = layers.Conv2D(conv_channels[i], kernel_size=(2, 1), strides=(2, 1), padding='same')(x3) x4 = layers.Conv2D(final_shape[-1], kernel_size=(1, 4))(x1) x4 = norm_activate(x4, 'relu', b_norm) x_down = down_scaling_loop(x_down, iterations - 1, i + 1, conv_channels, window_len, final_shape, max_iterations, b_norm) x_up = layers.Conv2DTranspose(conv_channels[-1], kernel_size=conv_kernel, strides=(pooling_width, 1), padding='same')(x_down) x4 = tf.add(x4, x_up) else: x4 = layers.Conv2D(conv_channels[i], kernel_size=(1, 4))(x3) x4 = norm_activate(x4, 'relu', b_norm) if i == 0: # Recover original shape x4 = layers.Conv2DTranspose(final_shape[0], kernel_size=(x_shrink, 1))(x4) x4 = k_b.squeeze(x4, axis=2) return x4
def _concat(p, mi, n, max_len): output = tf.constant([], dtype=tf.float32) for i in range(max_len): i1 = tf.cast(mi[i], dtype=tf.int32) i2 = tf.cast(n[i], dtype=tf.int32) tmp = tf.slice(p, [i1, i2], [1, 1]) tmp = K.squeeze(tmp, 0) output = np.concatenate([output, tmp]) output = tf.cast(output, dtype=tf.float32) return output
def top_k_accuracy(y_true, y_pred, mask_value=-1, k=5): """ Top-K Accuracy with Masking """ mask = K.squeeze(K.not_equal(y_true, mask_value), axis=-1) acc = sparse_top_k_categorical_accuracy(tf.boolean_mask(y_true, mask), tf.boolean_mask(y_pred, mask), k=k) # return 0 if result is nan acc_filtered = tf.cond(tf.is_nan(acc), lambda: tf.constant(0, tf.float32), lambda: acc) return acc_filtered
def call(self, inputs, **kwargs): pair1, pair2 = inputs pair1_shape, pair2_shape = K.shape(pair1), K.shape(pair2) pair1_mask = K.cast(K.squeeze(K.any(K.not_equal(pair1, 0.), axis=(-2, -1), keepdims=True), axis=-1), dtype=pair1.dtype) pair2_mask = K.cast(K.squeeze(K.any(K.not_equal(pair2, 0.), axis=(-2, -1), keepdims=True), axis=-1), dtype=pair2.dtype) pair1_to_lstm = K.reshape(pair1, (-1, pair1.shape[-2], pair1.shape[-1])) pair2_to_lstm = K.reshape(pair2, (-1, pair2.shape[-2], pair2.shape[-1])) batch = K.concatenate([pair1_to_lstm, pair2_to_lstm], axis=0) embedded = super(TestSpeakerEmbedding, self).call(batch) pair1_embed = embedded[:K.shape(pair1_to_lstm)[0]] pair2_embed = embedded[K.shape(pair1_to_lstm)[0]:] pair1_embed = K.reshape(pair1_embed, (pair1_shape[0], pair1_shape[1], -1)) pair2_embed = K.reshape(pair2_embed, (pair2_shape[0], pair2_shape[1], -1)) pair1_embed = pair1_embed * pair1_mask pair2_embed = pair2_embed * pair2_mask pair1_n = K.sum(pair1_mask, axis=1) pair2_n = K.sum(pair2_mask, axis=1) pair1_embed = K.sum(pair1_embed, axis=1) / pair1_n pair2_embed = K.sum(pair2_embed, axis=1) / pair2_n return pair1_embed, pair2_embed
def call(self, inputs): classes = K.squeeze(inputs[1], axis=1) kernel = K.gather(self.kernel, classes) #(bs, in, out) x = K.expand_dims(inputs[0], axis=1) #(bs, 1, in) output = tf.matmul(x, kernel) #(bs, 1, out) output = K.squeeze(output, axis=1) #(bs, out) if self.bias is not None: b = K.gather(self.bias, classes) output += b if self.activation is not None: return self.activation(output) return output
def fn(elements): _embedded_results_ = elements[0] _categories_size_input_ = K.cast(K.squeeze(elements[1], axis=0), tf.int32) # 具体原因未知:bug: From merging shape 0 with other shapes. for 'model_2_2/map/while/strided_slice/stack_1' (op: 'Pack') with input shapes: [1], []. if len(_categories_size_input_.shape) == 1: _categories_size_input_ = K.cast(K.squeeze(_categories_size_input_, axis=0), tf.int32) # print('_embedded_results_1',_embedded_results_) # print('_categories_size_input_', _categories_size_input_) # def slice2D(x, index): # return x[150-index:, :] # embedded_results_ = Lambda(slice2D, arguments={'index':_categories_size_input_})(_embedded_results_) # 切片 2D _embedded_results_ = _embedded_results_[MAX_SEQUENCE_LENGTH - _categories_size_input_:, :] # 切片 2D # print('_embedded_results_2',_embedded_results_) _embedded_results_ = Lambda(lambda x: K.sum(x, axis=0))(_embedded_results_) # print('_embedded_results_3', _embedded_results_) return _embedded_results_
def _compare(row1, row2): ### 比较行与行 # shape (5),(5) # 比较方式: 类型+个数 _sum = tf.constant(0., dtype=tf.float32) _cnt = tf.constant(0., dtype=tf.float32) # 类型--两两相乘/总次数 r = 0 while (r < board_size): c = 0 vr = tf.slice(row1, [r], [1]) vr = K.squeeze(vr, -1) while (c < board_size): vc = tf.slice(row2, [c], [1]) vc = K.squeeze(vc, -1) calc = K.abs(vr + vc) / (K.abs(vr - vc) + K.epsilon()) calc = K.clip(calc, 1, 9) #裁剪 calc = calc - 0.5 + K.abs(vr + vc) * 0.001 _sum = tf.cond( tf.logical_or(tf.not_equal(vc, 0), tf.not_equal(vr, 0)), lambda: _sum + calc, lambda: _sum) _cnt = tf.cond( tf.logical_or(tf.not_equal(vc, 0), tf.not_equal(vr, 0)), lambda: _cnt + 1, lambda: _cnt) c = c + 1 r = r + 1 cnt3 = _compare_cnt(row1, row2) cnt3 = tf.cond(tf.equal(cnt3, 0), lambda: cnt3 + 1, lambda: cnt3) output = tf.cond(tf.equal(_cnt, 0), lambda: _sum, lambda: tf.squeeze(_sum * cnt3 / _cnt)) ### 查看行与行比较结果 # if EmbeddingsLayer.debug: # logging.getLogger().info("\n--row1 %s" % (row1)) # logging.getLogger().info("--row2 %s" % (row2)) # logging.getLogger().info("--sum %s" % (_sum)) # logging.getLogger().info("--cnt %s" % (_cnt)) # logging.getLogger().info("--compare %s" % (output)) ### ### 直接输出概率: ### 手动测试0.7比较好 output = output * 0.7 return output
def accuracy(y_true, y_pred, mask_value=-1): """ Accuracy with Masking """ # mask = K.not_equal(y_true, mask_value) mask = K.squeeze(K.not_equal(y_true, mask_value), axis=-1) acc = sparse_categorical_accuracy(tf.boolean_mask(y_true, mask), tf.boolean_mask(y_pred, mask)) # return 0 if result is empty res_size = tf.shape(acc)[0] acc_filtered = tf.cond(tf.equal(res_size, 0), lambda: tf.constant(0, tf.float32), lambda: acc) return acc_filtered
def call(self, x, mask=None): x = K.expand_dims(x, axis=2) x = tf.nn.separable_conv2d(x, self.depthwise_w, self.pointwise_w, strides=(1, 1, 1, 1), padding="SAME") x += self.bias x = K.relu(x) outputs = K.squeeze(x, axis=2) return outputs
def max_singular_val(w, u, fully_differentiable=False, ip=1): if not fully_differentiable: w_ = K.stop_gradient(w) else: w_ = w u = K.expand_dims(u, axis=-1) u_bar = u for _ in range(ip): v_bar = tf.matmul(w_, u_bar, transpose_a=True) v_bar = K.l2_normalize(v_bar, axis=(-1, -2)) u_bar_raw = tf.matmul(w_, v_bar) u_bar = K.l2_normalize(u_bar_raw, axis=(-1, -2)) sigma = tf.matmul(u_bar, tf.matmul(w, v_bar), transpose_a=True) sigma = K.squeeze(sigma, axis=-1) sigma = K.squeeze(sigma, axis=-1) u_bar = K.squeeze(u_bar, axis=-1) return sigma, u_bar