def calc_loss(logits: tf.Tensor, caps_out: tf.Tensor, x: tf.Tensor, y: tf.Tensor, decoded: tf.Tensor): with tf.variable_scope('calc_loss'): # margin loss 中调节上margin和下margind的权重 lambda_val = 0.5 # 上margin与下margin的参数值 m_plus = 0.95 m_minus = 0.05 max_l = tf.square(tf.maximum(0., m_plus-logits)) max_r = tf.square(tf.maximum(0., logits-m_minus)) margin_loss = tf.reduce_mean(tf.reduce_sum(y * max_l + lambda_val * (1. - y) * max_r, axis=-1)) orgin = tf.reshape(x, (x.shape[0], -1)) reconstruct_loss = 0.0005*tf.reduce_mean(tf.square(orgin-decoded)) total_loss = margin_loss+reconstruct_loss return total_loss
def _setup_actor_critic_loss(self, actor, critic, num_actions): actions_one_hot = tf.placeholder(tf.float32, [None, num_actions]) action_probability = tf.reduce_sum(actor * actions_one_hot, axis=1) log_prob = tf.log(tf.maximum(action_probability, self._log_noise)) advantage = self._R - tf.stop_gradient(critic) entropy = tf.reduce_sum(tf.log(tf.maximum(actor, self._log_noise)) * actor, axis=1) actor_loss = -(tf.reduce_sum((log_prob * advantage), axis=0) + tf.reduce_sum((-1 * self._entropy_beta * entropy), axis=0)) critic_loss = tf.reduce_sum(tf.square(self._R - critic), axis=0) loss = 0.5 * critic_loss + actor_loss return loss, actions_one_hot
def tf_iou(pred_xy: tf.Tensor, pred_wh: tf.Tensor, vaild_xy: tf.Tensor, vaild_wh: tf.Tensor) -> tf.Tensor: """ calc the iou form pred box with vaild box Parameters ---------- pred_xy : tf.Tensor pred box shape = [out h, out w, anchor num, 2] pred_wh : tf.Tensor pred box shape = [out h, out w, anchor num, 2] vaild_xy : tf.Tensor vaild box shape = [? , 2] vaild_wh : tf.Tensor vaild box shape = [? , 2] Returns ------- tf.Tensor iou value shape = [out h, out w, anchor num ,?] """ b1_xy = tf.expand_dims(pred_xy, -2) b1_wh = tf.expand_dims(pred_wh, -2) b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half b2_xy = tf.expand_dims(vaild_xy, 0) b2_wh = tf.expand_dims(vaild_wh, 0) b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = tf.maximum(b1_mins, b2_mins) intersect_maxes = tf.minimum(b1_maxes, b2_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou