def class_balanced_cross_entropy_loss(output, label): """Define the class balanced cross entropy loss to train the network Args: output: Output of the network label: Ground truth label Returns: Tensor that evaluates the loss """ labels = tf.cast(tf.greater(label, 0.5), tf.float32) num_labels_pos = tf.reduce_sum(labels) num_labels_neg = tf.reduce_sum(1.0 - labels) num_total = num_labels_pos + num_labels_neg output_gt_zero = tf.cast(tf.greater_equal(output, 0), tf.float32) loss_val = tf.multiply(output, (labels - output_gt_zero)) - tf.log( 1 + tf.exp(output - 2 * tf.multiply(output, output_gt_zero))) loss_pos = tf.reduce_sum(-tf.multiply(labels, loss_val)) loss_neg = tf.reduce_sum(-tf.multiply(1.0 - labels, loss_val)) final_loss = num_labels_neg / num_total * loss_pos + num_labels_pos / num_total * loss_neg return final_loss
def call(self, x): """Execute this layer on input tensors. Parameters ---------- x: list of Tensor should be [atom_features(batch_size*max_n_atoms*n_embedding), distance_matrix(batch_size*max_n_atoms*max_n_atoms*n_distance), distance_matrix_mask(batch_size*max_n_atoms*max_n_atoms)] Returns ------- tf.Tensor new embeddings for atoms, same shape as x[0] """ self.build() atom_features = x[0] distance_matrix = x[1] distance_matrix_mask = x[2] outputs = tf.multiply( (tf.tensordot(distance_matrix, self.W_df, [[3], [0]]) + self.b_df), tf.expand_dims( tf.tensordot(atom_features, self.W_cf, [[2], [0]]) + self.b_cf, axis=1)) # for atom i in a molecule m, this step multiplies together distance info of atom pair(i,j) # and embeddings of atom j(both gone through a hidden layer) outputs = tf.tensordot(outputs, self.W_fc, [[3], [0]]) outputs = tf.multiply(outputs, tf.expand_dims(distance_matrix_mask, axis=3)) # masking the outputs tensor for pair(i,i) and all paddings outputs = self.activation(outputs) outputs = tf.reduce_sum(outputs, axis=2) + atom_features # for atom i, sum the influence from all other atom j in the molecule return outputs
def cross_entropy(u, label_u, alpha=0.5, normed=False): label_ip = tf.cast( tf.matmul(label_u, tf.transpose(label_u)), tf.float32) s = tf.clip_by_value(label_ip, 0.0, 1.0) # compute balance param # s_t \in {-1, 1} s_t = tf.multiply(tf.add(s, tf.constant(-0.5)), tf.constant(2.0)) sum_1 = tf.reduce_sum(s) sum_all = tf.reduce_sum(tf.abs(s_t)) balance_param = tf.add(tf.abs(tf.add(s, tf.constant(-1.0))), tf.multiply(tf.div(sum_all, sum_1), s)) if normed: # ip = tf.clip_by_value(tf.matmul(u, tf.transpose(u)), -1.5e1, 1.5e1) ip_1 = tf.matmul(u, tf.transpose(u)) def reduce_shaper(t): return tf.reshape(tf.reduce_sum(t, 1), [tf.shape(t)[0], 1]) mod_1 = tf.sqrt(tf.matmul(reduce_shaper(tf.square(u)), reduce_shaper(tf.square(u)), transpose_b=True)) ip = tf.div(ip_1, mod_1) else: ip = tf.clip_by_value(tf.matmul(u, tf.transpose(u)), -1.5e1, 1.5e1) ones = tf.ones([tf.shape(u)[0], tf.shape(u)[0]]) return tf.reduce_mean(tf.multiply(tf.log(ones + tf.exp(alpha * ip)) - s * alpha * ip, balance_param))
def attention_mechanism_parallel(self,c_full,m,q,i): """ parallel implemtation of gate function given a list of candidate sentence, a query, and previous memory. Input: c_full: candidate fact. shape:[batch_size,story_length,hidden_size] m: previous memory. shape:[batch_size,hidden_size] q: question. shape:[batch_size,hidden_size] Output: a scalar score (in batch). shape:[batch_size,story_length] """ q=tf.expand_dims(q,axis=1) #[batch_size,1,hidden_size] m=tf.expand_dims(m,axis=1) #[batch_size,1,hidden_size] # 1.define a large feature vector that captures a variety of similarities between input,memory and question vector: z(c,m,q) c_q_elementwise=tf.multiply(c_full,q) #[batch_size,story_length,hidden_size] c_m_elementwise=tf.multiply(c_full,m) #[batch_size,story_length,hidden_size] c_q_minus=tf.abs(tf.subtract(c_full,q)) #[batch_size,story_length,hidden_size] c_m_minus=tf.abs(tf.subtract(c_full,m)) #[batch_size,story_length,hidden_size] # c_transpose Wq c_w_q=self.x1Wx2_parallel(c_full,q,"c_w_q"+str(i)) #[batch_size,story_length,hidden_size] c_w_m=self.x1Wx2_parallel(c_full,m,"c_w_m"+str(i)) #[batch_size,story_length,hidden_size] # c_transposeWm q_tile=tf.tile(q,[1,self.story_length,1]) #[batch_size,story_length,hidden_size] m_tile=tf.tile(m,[1,self.story_length,1]) #[batch_size,story_length,hidden_size] z=tf.concat([c_full,m_tile,q_tile,c_q_elementwise,c_m_elementwise,c_q_minus,c_m_minus,c_w_q,c_w_m],2) #[batch_size,story_length,hidden_size*9] # 2. two layer feed foward g=tf.layers.dense(z,self.hidden_size*3,activation=tf.nn.tanh) #[batch_size,story_length,hidden_size*3] g=tf.layers.dense(g,1,activation=tf.nn.sigmoid) #[batch_size,story_length,1] g=tf.squeeze(g,axis=2) #[batch_size,story_length] return g
def batch_iou(bboxes, bbox): """Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'. Args: bboxes: A batch of boxes. 2-D with shape `[B, 4]`. bbox: A single box. 1-D with shape `[4]`. Returns: Batch of IOUs """ lr = tf.maximum( tf.minimum(bboxes[:, 3], bbox[3]) - tf.maximum(bboxes[:, 1], bbox[1]), 0 ) tb = tf.maximum( tf.minimum(bboxes[:, 2], bbox[2]) - tf.maximum(bboxes[:, 0], bbox[0]), 0 ) intersection = tf.multiply(tb, lr) union = tf.subtract( tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) + tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])), intersection ) iou = tf.div(intersection, union) return iou
def __init__(self, sess, batch_size=32, image_size=256, lam=0.8, checkpoint_dir=None): self.sess = sess self.batch_size = batch_size self.image_size = image_size self.image_shape = [image_size, image_size, 3] self.lam = lam self.checkpoint_dir = checkpoint_dir self.global_step = tf.Variable(0, trainable=False) self.images = tf.placeholder(tf.float32, [batch_size] + self.image_shape, name='images') self.images_summary = tf.summary.image("image", self.images) self.d_bns = [batch_norm(name='d_bn{}'.format(i)) for i in range(5)] self.local_d_bns = [batch_norm(name='d_local_bn{}'.format(i)) for i in range(4)] self.g_bns = [batch_norm(name='g_bn{}'.format(i, )) for i in range(15)] self.D, self.D_logits = self.discriminator(self.images, self.image_size) self.d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits( logits=self.D_logits, labels=tf.ones_like(self.D))) self.D_summary = tf.summary.histogram("d", self.D) self.d_loss_real_summary = tf.summary.scalar("d_loss_real", self.d_loss_real) self.masks = tf.placeholder(tf.float32, [batch_size] + self.image_shape, name='masks') self.MG = tf.multiply(self.images, self.masks) self.G = self.generator(self.MG) self.MG_summary = tf.summary.image("mg", self.MG) self.G_summary = tf.summary.image("g", self.G) self.D_fake, self.D_fake_logits = self.discriminator(self.G, self.image_size, reuse=True) self.d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits( logits=self.D_fake_logits, labels=tf.zeros_like(self.D_fake))) self.D_fake_summary = tf.summary.histogram("d_fake", self.D_fake) self.d_loss_fake_summary = tf.summary.scalar("d_loss_fake", self.d_loss_fake) self.d_loss = self.d_loss_real + self.d_loss_fake self.g_loss_d = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits( logits=self.D_fake_logits, labels=tf.ones_like(self.D_fake))) self.g_loss_l = tf.reduce_mean(tf.contrib.layers.flatten( tf.multiply(self.G - self.images, self.G - self.images))) self.g_loss = (1 - self.lam) * self.g_loss_d + self.lam * self.g_loss_l self.g_loss_d_summary = tf.summary.scalar("g_loss_d", self.g_loss_d) self.g_loss_l_summary = tf.summary.scalar("g_loss_l", self.g_loss_l) self.g_loss_summary = tf.summary.scalar("g_loss", self.g_loss) t_vars = tf.trainable_variables() self.d_vars = [var for var in t_vars if 'd_' in var.name] self.g_vars = [var for var in t_vars if 'g_' in var.name] self.saver = tf.train.Saver(max_to_keep=10) self.g_summary = tf.summary.merge([ self.G_summary, self.MG_summary, self.D_fake_summary, self.d_loss_fake_summary, self.g_loss_summary, self.g_loss_d_summary, self.g_loss_l_summary]) self.d_summary = tf.summary.merge([ self.images_summary, self.D_summary, self.d_loss_real_summary]) self.writer = tf.summary.FileWriter(os.path.join(self.checkpoint_dir, "logs"), self.sess.graph)
def log_loss(labels, predictions, epsilon=1e-7, scope=None, weights=None): """Calculate log losses. Same as tf.losses.log_loss except that this returns the individual losses instead of passing them into compute_weighted_loss and returning their weighted mean. This is useful for eval jobs that report the mean loss. By returning individual losses, that mean loss can be the same regardless of batch size. Args: labels: The ground truth output tensor, same dimensions as 'predictions'. predictions: The predicted outputs. epsilon: A small increment to add to avoid taking a log of zero. scope: The scope for the operations performed in computing the loss. weights: Weights to apply to labels. Returns: A `Tensor` representing the loss values. Raises: ValueError: If the shape of `predictions` doesn't match that of `labels`. """ with tf.name_scope(scope, "log_loss", (predictions, labels)): predictions = tf.to_float(predictions) labels = tf.to_float(labels) predictions.get_shape().assert_is_compatible_with(labels.get_shape()) losses = -tf.multiply(labels, tf.log(predictions + epsilon)) - tf.multiply( (1 - labels), tf.log(1 - predictions + epsilon)) if weights is not None: losses = tf.multiply(losses, weights) return losses
def create_variables_for_optimization(self): with tf.name_scope("optimization"): with tf.name_scope("masker"): self.mask = tf.sequence_mask(self.seq_len, self.num_step) self.mask = tf.reshape(tf.cast(self.mask, tf.float32), (-1,)) if self.loss_function == "cross_entropy": self.pl_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=self.logit, labels=self.actions_flatten) elif self.loss_function == "l2": self.one_hot_actions = tf.one_hot(self.actions_flatten, self.num_actions) self.pl_loss = tf.reduce_mean((self.probs - self.one_hot_actions) ** 2, axis=1) else: raise ValueError("loss function type is not defined") self.pl_loss = tf.multiply(self.pl_loss, self.mask) self.pl_loss = tf.reduce_mean(tf.multiply(self.pl_loss, self.returns_flatten)) self.entropy = tf.multiply(self.entropy, self.mask) self.entropy = tf.reduce_mean(self.entropy) self.loss = self.pl_loss - self.entropy_bonus * self.entropy self.trainable_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="policy_network") self.gradients = self.optimizer.compute_gradients(self.loss, var_list=self.trainable_variables) self.clipped_gradients = [(tf.clip_by_norm(grad, self.max_gradient), var) for grad, var in self.gradients] self.train_op = self.optimizer.apply_gradients(self.clipped_gradients, self.global_step) self.grad_norm = tf.global_norm([grad for grad, var in self.gradients]) self.var_norm = tf.global_norm(self.trainable_variables)
def task_specific_attention(self, inputs, output_size, initializer=layers.xavier_initializer(), activation_fn=tf.tanh, scope=None): """ Performs task-specific attention reduction, using learned attention context vector (constant within task of interest). Args: inputs: Tensor of shape [batch_size, units, input_size] `input_size` must be static (known) `units` axis will be attended over (reduced from output) `batch_size` will be preserved output_size: Size of output's inner (feature) dimension Returns: outputs: Tensor of shape [batch_size, output_dim]. """ assert len(inputs.get_shape()) == 3 and inputs.get_shape()[-1].value is not None with tf.variable_scope(scope or 'attention') as scope: # u_w, attention 向量 attention_context_vector = tf.get_variable(name='attention_context_vector', shape=[output_size], initializer=initializer, dtype=tf.float32) # 全连接层,把 h_i 转为 u_i , shape= [batch_size, units, input_size] -> [batch_size, units, output_size] input_projection = layers.fully_connected(inputs, output_size, activation_fn=activation_fn, scope=scope) # 输出 [batch_size, units] vector_attn = tf.reduce_sum(tf.multiply(input_projection, attention_context_vector), axis=2, keep_dims=True) attention_weights = tf.nn.softmax(vector_attn, dim=1) tf.summary.histogram('attention_weigths', attention_weights) weighted_projection = tf.multiply(inputs, attention_weights) outputs = tf.reduce_sum(weighted_projection, axis=1) return outputs # 输出 [batch_size, hidden_size*2]
def _forward(self, obs_prob_list): with tf.name_scope('init_scaling_factor'): self.scale = tf.Variable(tf.zeros([self.N], tf.float64)) #scale factors with tf.name_scope('forward_first_step'): # initialize with state starting priors init_prob = tf.multiply(self.T0, tf.squeeze(obs_prob_list[0])) # scaling factor at t=0 self.scale = tf.scatter_update(self.scale, 0, 1.0 / tf.reduce_sum(init_prob)) # scaled belief at t=0 self.forward = tf.scatter_update(self.forward, 0, self.scale[0] * init_prob) # propagate belief for step, obs_prob in enumerate(obs_prob_list[1:]): with tf.name_scope('time_step-%s' %step): # previous state probability prev_prob = tf.expand_dims(self.forward[step, :], 0) # transition prior prior_prob = tf.matmul(prev_prob, self.T) # forward belief propagation forward_score = tf.multiply(prior_prob, tf.squeeze(obs_prob)) forward_prob = tf.squeeze(forward_score) # scaling factor self.scale = tf.scatter_update(self.scale, step+1, 1.0 / tf.reduce_sum(forward_prob)) # Update forward matrix self.forward = tf.scatter_update(self.forward, step+1, self.scale[step+1] * forward_prob)
def pearsoncorrelation(ypred, y): muy_ypred = tf.reduce_mean(ypred) muy_y = tf.reduce_mean(y) numerator = tf.reduce_sum(tf.multiply(ypred - muy_ypred, y - muy_y)) denominator = tf.multiply(tf.sqrt(tf.reduce_sum(tf.square(ypred - muy_ypred))), tf.sqrt(tf.reduce_sum(tf.square(y - muy_y)))) + 1e-10 return numerator / denominator
def mask_similarity_matrix(similarity_matrix, mask_a, mask_b): """ Given the mask of the two sentences, apply the mask to the similarity matrix. Parameters ---------- similarity_matrix: Tensor Tensor of shape (batch_size, num_sentence_words, num_sentence_words). mask_a: Tensor Tensor of shape (batch_size, num_sentence_words). This mask should correspond to the first vector (v1) used to calculate the similarity matrix. mask_b: Tensor Tensor of shape (batch_size, num_sentence_words). This mask should correspond to the second vector (v2) used to calculate the similarity matrix. """ similarity_matrix = tf.multiply(similarity_matrix, tf.expand_dims(tf.cast(mask_a, "float"), 1)) similarity_matrix = tf.multiply(similarity_matrix, tf.expand_dims(tf.cast(mask_b, "float"), 2)) return similarity_matrix
def cell(self,s_t,h_all,w_all,i): """ parallel implementation of single time step for compute of input with memory :param s_t: [batch_size,hidden_size].vector representation of current input(is a sentence).notice:hidden_size=embedding_size :param w_all: [batch_size,block_size,hidden_size] :param h_all: [batch_size,block_size,hidden_size] :return: new hidden state: [batch_size,block_size,hidden_size] """ # 1.gate s_t_expand=tf.expand_dims(s_t, axis=1) #[batch_size,1,hidden_size] g=tf.nn.sigmoid(tf.multiply(s_t_expand,h_all)+tf.multiply(s_t_expand,w_all))#shape:[batch_size,block_size,hidden_size] # 2.candidate hidden state #below' shape:[batch_size*block_size,hidden_size] h_candidate_part1=tf.matmul(tf.reshape(h_all,shape=(-1,self.dimension)), self.U) + tf.matmul(tf.reshape(w_all,shape=(-1,self.dimension)), self.V)+self.h_bias print("======>h_candidate_part1:",h_candidate_part1) #(160, 100) h_candidate_part1=tf.reshape(h_candidate_part1,shape=(self.batch_size,self.block_size,self.dimension)) #[batch_size,block_size,hidden_size] h_candidate_part2=tf.expand_dims(tf.matmul(s_t,self.W)+self.h2_bias,axis=1) #shape:[batch_size,1,hidden_size] h_candidate=self.activation(h_candidate_part1+h_candidate_part2,scope="h_candidate"+str(i)) #shape:[batch_size,block_size,hidden_size] # 3.update hidden state h_all=h_all+tf.multiply(g,h_candidate) #shape:[batch_size,block_size,hidden_size] # 4.normalized hidden state h_all=tf.nn.l2_normalize(h_all,-1) #shape:[batch_size,block_size,hidden_size] return h_all #shape:[batch_size,block_size,hidden_size]
def energy(self, visible_state, hidden_state, scope='energy'): with tf.variable_scope(scope): visible_state = tf.stop_gradient(visible_state, name="visible_state") hidden_state = tf.stop_gradient(hidden_state, name="hidden_state") energy = -tf.reduce_mean(tf.reduce_sum(tf.multiply(tf.matmul(visible_state, self.W, name='visible_weights'), hidden_state, name='weights_hidden') , axis=1, name='energy_sum'), name="batch_energy_mean") if self.visible.use_bias: if self.visible.binary: energy = tf.add(energy, -tf.reduce_mean( tf.reduce_sum(tf.multiply(self.visible.bias, visible_state, name='visible_bias_energy'), axis=1))) else: v = visible_state - self.visible.bias energy = tf.add(energy, tf.reduce_mean(tf.reduce_sum(tf.multiply(v, v) / 2, axis=1))) if self.hidden.use_bias: if self.hidden.binary: energy = tf.add(energy, -tf.reduce_mean( tf.reduce_sum(tf.multiply(self.hidden.bias, hidden_state, name='hidden_bias_energy'), axis=1))) else: h = hidden_state - self.hidden.bias energy = tf.add(energy, tf.reduce_mean(tf.reduce_sum(tf.multiply(h, h) / 2, axis=1))) return energy
def compute_auc(tp, fn, tn, fp, name): """Computes the roc-auc or pr-auc based on confusion counts.""" rec = tf.div(tp + epsilon, tp + fn + epsilon) if curve == 'ROC': fp_rate = tf.div(fp, fp + tn + epsilon) x = fp_rate y = rec elif curve == 'R': # recall auc x = tf.linspace(1., 0., num_thresholds) y = rec else: # curve == 'PR'. prec = tf.div(tp + epsilon, tp + fp + epsilon) x = rec y = prec if summation_method == 'trapezoidal': return tf.reduce_sum( tf.multiply(x[:num_thresholds - 1] - x[1:], (y[:num_thresholds - 1] + y[1:]) / 2.), name=name) elif summation_method == 'minoring': return tf.reduce_sum( tf.multiply(x[:num_thresholds - 1] - x[1:], tf.minimum(y[:num_thresholds - 1], y[1:])), name=name) elif summation_method == 'majoring': return tf.reduce_sum( tf.multiply(x[:num_thresholds - 1] - x[1:], tf.maximum(y[:num_thresholds - 1], y[1:])), name=name) else: raise ValueError('Invalid summation_method: %s' % summation_method)
def __loss__(self): """ Calculate loss :return: """ # regularization ? self.d_loss_real = tf.reduce_mean(ops.binary_cross_entropy(preds=self.predict_d, targets=tf.ones_like(self.predict_d))) # tf.nn.sigmoid_cross_entropy_with_logits(logits=self.predict_d_logits, # labels=tf.ones_like(self.predict_d))) tf.summary.scalar('d_loss_real', self.d_loss_real, collections='D') self.d_loss_fake = tf.reduce_mean(ops.binary_cross_entropy(preds=self.predict_d_for_g, targets=tf.zeros_like(self.predict_d_for_g))) # tf.nn.sigmoid_cross_entropy_with_logits(logits=self.predict_d_logits_for_g, # labels=tf.zeros_like(self.predict_d_for_g))) tf.summary.scalar('d_loss_fake', self.d_loss_fake, collections='D') self.d_loss = self.d_loss_real + self.d_loss_fake tf.summary.scalar('d_loss', self.d_loss, collections='D') if len(self.regularization_values_d) > 0: reg_loss_d = self.reg_w * tf.reduce_sum(self.regularization_values_d) self.d_loss += reg_loss_d if self.FLAGS.dump_debug: tf.summary.scalar('d_loss_plus_reg', self.d_loss, collections='D') tf.summary.scalar('d_loss_reg_only', reg_loss_d, collections='D') # Generative loss g_loss = tf.reduce_mean(ops.binary_cross_entropy(preds=self.predict_d_for_g, targets=tf.ones_like(self.predict_d_for_g))) # tf.nn.sigmoid_cross_entropy_with_logits(logits=self.predict_d_logits_for_g, # labels=tf.ones_like(self.predict_d_for_g))) tf.summary.scalar('g_loss', g_loss, collections='G') # Context loss mask_not = tf.cast(tf.logical_not(tf.cast(self.labels['mask'], tf.bool)), tf.float32) real_diff = tf.contrib.layers.flatten(tf.multiply(self.predict_g['real'] - self.labels['real'], mask_not)) imag_diff = tf.contrib.layers.flatten(tf.multiply(self.predict_g['imag'] - self.labels['imag'], mask_not)) # real_diff = tf.multiply(tf.squeeze(self.predict_g['real']) - tf.squeeze(self.labels['real']), tf.squeeze(self.labels['mask'])) # imag_diff = tf.multiply(tf.squeeze(self.predict_g['imag']) - tf.squeeze(self.labels['imag']), tf.squeeze(self.labels['mask'])) self.context_loss = tf.reduce_mean(tf.square(real_diff) + tf.square(imag_diff), name='Context_loss_mean') tf.summary.scalar('g_loss_context_only', self.context_loss, collections='G') self.g_loss = self.adv_loss_w * g_loss + self.FLAGS.gen_loss_context * self.context_loss # self.g_loss = self.FLAGS.gen_loss_adversarial * g_loss + self.FLAGS.gen_loss_context * context_loss tf.summary.scalar('g_loss_plus_context', self.g_loss, collections='G') if len(self.regularization_values) > 0: reg_loss_g = self.reg_w * tf.reduce_sum(self.regularization_values) self.g_loss += reg_loss_g if self.FLAGS.dump_debug: tf.summary.scalar('g_loss_plus_context_plus_reg', self.g_loss, collections='G') tf.summary.scalar('g_loss_reg_only', reg_loss_g, collections='D') tf.summary.scalar('diff-loss', tf.abs(self.d_loss - self.g_loss), collections='G')
def thresholding(inputs): # find the mean for each example in the batch mean_output = tf.reduce_mean(inputs, axis=1) # scale each mean based on a factor threshold_scalar = tf.Variable(utils.threshold_scalar, tf.float32) scaled_mean = tf.scalar_mul(threshold_scalar, mean_output) scaled_mean = tf.reshape(scaled_mean, [utils.batch_size]) # setup matrix for min_thresh_for_max = tf.fill([utils.batch_size], 0.05) max_thresh_for_min = tf.fill([utils.batch_size], 0.15) #0.4 thresholds = tf.maximum(min_thresh_for_max, scaled_mean) thresholds = tf.minimum(max_thresh_for_min, thresholds) # zero values under the thresholds using bitmask thresholds = tf.reshape(thresholds, [128, 1, 1]) threshold_mask = tf.cast(tf.greater(inputs, thresholds), tf.float32) thresholded_input = tf.multiply(inputs, threshold_mask) # peak picking # select beats by x[i-1] < x[i] > x[i+1] (local maximum) x_minus_1 = tf.cast(tf.greater(thresholded_input, tf.manip.roll(thresholded_input, shift=-1, axis=1)), tf.float32) x_plus_1 = tf.cast(tf.greater(thresholded_input, tf.manip.roll(thresholded_input, shift=1, axis=1)), tf.float32) output = tf.multiply(x_minus_1, x_plus_1) return output
def attention_word_level(self, hidden_state): """ input1:self.hidden_state: hidden_state:list,len:sentence_length,element:[batch_size*num_sentences,hidden_size*2] input2:sentence level context vector:[batch_size*num_sentences,hidden_size*2] :return:representation.shape:[batch_size*num_sentences,hidden_size*2] """ hidden_state_ = tf.stack(hidden_state, axis=1) # shape:[batch_size*num_sentences,sequence_length,hidden_size*2] # 0) one layer of feed forward network hidden_state_2 = tf.reshape(hidden_state_, shape=[-1, self.hidden_size * 2]) # shape:[batch_size*num_sentences*sequence_length,hidden_size*2] # hidden_state_:[batch_size*num_sentences*sequence_length,hidden_size*2];W_w_attention_sentence:[,hidden_size*2,,hidden_size*2] hidden_representation = tf.nn.tanh(tf.matmul(hidden_state_2, self.W_w_attention_word) + self.W_b_attention_word) # shape:[batch_size*num_sentences*sequence_length,hidden_size*2] hidden_representation = tf.reshape(hidden_representation, shape=[-1, self.sequence_length, self.hidden_size * 2]) # shape:[batch_size*num_sentences,sequence_length,hidden_size*2] # attention process:1.get logits for each word in the sentence. 2.get possibility distribution for each word in the sentence. 3.get weighted sum for the sentence as sentence representation. # 1) get logits for each word in the sentence. hidden_state_context_similiarity = tf.multiply(hidden_representation, self.context_vecotor_word) # shape:[batch_size*num_sentences,sequence_length,hidden_size*2] attention_logits = tf.reduce_sum(hidden_state_context_similiarity, axis=2) # shape:[batch_size*num_sentences,sequence_length] # subtract max for numerical stability (softmax is shift invariant). tf.reduce_max:Computes the maximum of elements across dimensions of a tensor. attention_logits_max = tf.reduce_max(attention_logits, axis=1, keep_dims=True) # shape:[batch_size*num_sentences,1] # 2) get possibility distribution for each word in the sentence. p_attention = tf.nn.softmax( attention_logits - attention_logits_max) # shape:[batch_size*num_sentences,sequence_length] # 3) get weighted hidden state by attention vector p_attention_expanded = tf.expand_dims(p_attention, axis=2) # shape:[batch_size*num_sentences,sequence_length,1] # below sentence_representation'shape:[batch_size*num_sentences,sequence_length,hidden_size*2]<----p_attention_expanded:[batch_size*num_sentences,sequence_length,1];hidden_state_:[batch_size*num_sentences,sequence_length,hidden_size*2] sentence_representation = tf.multiply(p_attention_expanded, hidden_state_) # shape:[batch_size*num_sentences,sequence_length,hidden_size*2] sentence_representation = tf.reduce_sum(sentence_representation, axis=1) # shape:[batch_size*num_sentences,hidden_size*2] return sentence_representation # shape:[batch_size*num_sentences,hidden_size*2]
def get_lossfunc(z_pi, z_mu1, z_mu2, z_sigma1, z_sigma2, z_corr, z_pen_logits, x1_data, x2_data, pen_data): """Returns a loss fn based on eq #26 of http://arxiv.org/abs/1308.0850.""" # This represents the L_R only (i.e. does not include the KL loss term). result0 = tf_2d_normal(x1_data, x2_data, z_mu1, z_mu2, z_sigma1, z_sigma2, z_corr) epsilon = 1e-6 # result1 is the loss wrt pen offset (L_s in equation 9 of # https://arxiv.org/pdf/1704.03477.pdf) result1 = tf.multiply(result0, z_pi) result1 = tf.reduce_sum(result1, 1, keep_dims=True) result1 = -tf.log(result1 + epsilon) # avoid log(0) fs = 1.0 - pen_data[:, 2] # use training data for this fs = tf.reshape(fs, [-1, 1]) # Zero out loss terms beyond N_s, the last actual stroke result1 = tf.multiply(result1, fs) # result2: loss wrt pen state, (L_p in equation 9) result2 = tf.nn.softmax_cross_entropy_with_logits( labels=pen_data, logits=z_pen_logits) result2 = tf.reshape(result2, [-1, 1]) if not self.hps.is_training: # eval mode, mask eos columns result2 = tf.multiply(result2, fs) result = result1 + result2 return result
def __init__(self, params): self.params = params self.network_name = 'qnet' self.sess = tf.Session() self.x = tf.placeholder('float', [None, params['width'],params['height'], 6],name=self.network_name + '_x') self.q_t = tf.placeholder('float', [None], name=self.network_name + '_q_t') self.actions = tf.placeholder("float", [None, 4], name=self.network_name + '_actions') self.rewards = tf.placeholder("float", [None], name=self.network_name + '_rewards') self.terminals = tf.placeholder("float", [None], name=self.network_name + '_terminals') # Layer 1 (Convolutional) layer_name = 'conv1' ; size = 3 ; channels = 6 ; filters = 16 ; stride = 1 self.w1 = tf.Variable(tf.random_normal([size,size,channels,filters], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights') self.b1 = tf.Variable(tf.constant(0.1, shape=[filters]),name=self.network_name + '_'+layer_name+'_biases') self.c1 = tf.nn.conv2d(self.x, self.w1, strides=[1, stride, stride, 1], padding='SAME',name=self.network_name + '_'+layer_name+'_convs') self.o1 = tf.nn.relu(tf.add(self.c1,self.b1),name=self.network_name + '_'+layer_name+'_activations') # Layer 2 (Convolutional) layer_name = 'conv2' ; size = 3 ; channels = 16 ; filters = 32 ; stride = 1 self.w2 = tf.Variable(tf.random_normal([size,size,channels,filters], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights') self.b2 = tf.Variable(tf.constant(0.1, shape=[filters]),name=self.network_name + '_'+layer_name+'_biases') self.c2 = tf.nn.conv2d(self.o1, self.w2, strides=[1, stride, stride, 1], padding='SAME',name=self.network_name + '_'+layer_name+'_convs') self.o2 = tf.nn.relu(tf.add(self.c2,self.b2),name=self.network_name + '_'+layer_name+'_activations') o2_shape = self.o2.get_shape().as_list() # Layer 3 (Fully connected) layer_name = 'fc3' ; hiddens = 256 ; dim = o2_shape[1]*o2_shape[2]*o2_shape[3] self.o2_flat = tf.reshape(self.o2, [-1,dim],name=self.network_name + '_'+layer_name+'_input_flat') self.w3 = tf.Variable(tf.random_normal([dim,hiddens], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights') self.b3 = tf.Variable(tf.constant(0.1, shape=[hiddens]),name=self.network_name + '_'+layer_name+'_biases') self.ip3 = tf.add(tf.matmul(self.o2_flat,self.w3),self.b3,name=self.network_name + '_'+layer_name+'_ips') self.o3 = tf.nn.relu(self.ip3,name=self.network_name + '_'+layer_name+'_activations') # Layer 4 layer_name = 'fc4' ; hiddens = 4 ; dim = 256 self.w4 = tf.Variable(tf.random_normal([dim,hiddens], stddev=0.01),name=self.network_name + '_'+layer_name+'_weights') self.b4 = tf.Variable(tf.constant(0.1, shape=[hiddens]),name=self.network_name + '_'+layer_name+'_biases') self.y = tf.add(tf.matmul(self.o3,self.w4),self.b4,name=self.network_name + '_'+layer_name+'_outputs') #Q,Cost,Optimizer self.discount = tf.constant(self.params['discount']) self.yj = tf.add(self.rewards, tf.multiply(1.0-self.terminals, tf.multiply(self.discount, self.q_t))) self.Q_pred = tf.reduce_sum(tf.multiply(self.y,self.actions), reduction_indices=1) self.cost = tf.reduce_sum(tf.pow(tf.subtract(self.yj, self.Q_pred), 2)) if self.params['load_file'] is not None: self.global_step = tf.Variable(int(self.params['load_file'].split('_')[-1]),name='global_step', trainable=False) else: self.global_step = tf.Variable(0, name='global_step', trainable=False) # self.optim = tf.train.RMSPropOptimizer(self.params['lr'],self.params['rms_decay'],0.0,self.params['rms_eps']).minimize(self.cost,global_step=self.global_step) self.optim = tf.train.AdamOptimizer(self.params['lr']).minimize(self.cost, global_step=self.global_step) self.saver = tf.train.Saver(max_to_keep=0) self.sess.run(tf.global_variables_initializer()) if self.params['load_file'] is not None: print('Loading checkpoint...') self.saver.restore(self.sess,self.params['load_file'])
def attention_sentence_level(self, hidden_state_sentence): """ input1: hidden_state_sentence: a list,len:num_sentence,element:[None,hidden_size*4] input2: sentence level context vector:[self.hidden_size*2] :return:representation.shape:[None,hidden_size*4] """ hidden_state_ = tf.stack(hidden_state_sentence, axis=1) # shape:[None,num_sentence,hidden_size*4] # 0) one layer of feed forward hidden_state_2 = tf.reshape(hidden_state_, shape=[-1, self.hidden_size * 4]) # [None*num_sentence,hidden_size*4] hidden_representation = tf.nn.tanh(tf.matmul(hidden_state_2, self.W_w_attention_sentence) + self.W_b_attention_sentence) # shape:[None*num_sentence,hidden_size*2] hidden_representation = tf.reshape(hidden_representation, shape=[-1, self.num_sentences, self.hidden_size * 2]) # [None,num_sentence,hidden_size*2] # attention process:1.get logits for each sentence in the doc.2.get possibility distribution for each sentence in the doc.3.get weighted sum for the sentences as doc representation. # 1) get logits for each word in the sentence. hidden_state_context_similiarity = tf.multiply(hidden_representation, self.context_vecotor_sentence) # shape:[None,num_sentence,hidden_size*2] attention_logits = tf.reduce_sum(hidden_state_context_similiarity, axis=2) # shape:[None,num_sentence]. that is get logit for each num_sentence. # subtract max for numerical stability (softmax is shift invariant). tf.reduce_max:computes the maximum of elements across dimensions of a tensor. attention_logits_max = tf.reduce_max(attention_logits, axis=1, keep_dims=True) # shape:[None,1] # 2) get possibility distribution for each word in the sentence. p_attention = tf.nn.softmax(attention_logits - attention_logits_max) # shape:[None,num_sentence] # 3) get weighted hidden state by attention vector(sentence level) p_attention_expanded = tf.expand_dims(p_attention, axis=2) # shape:[None,num_sentence,1] sentence_representation = tf.multiply(p_attention_expanded, hidden_state_) # shape:[None,num_sentence,hidden_size*2]<---p_attention_expanded:[None,num_sentence,1];hidden_state_:[None,num_sentence,hidden_size*2] sentence_representation = tf.reduce_sum(sentence_representation, axis=1) # shape:[None,hidden_size*2] return sentence_representation # shape:[None,hidden_size*2]
def cosineface_losses(embedding, labels, out_num, w_init=None, s=30., m=0.4): ''' :param embedding: the input embedding vectors :param labels: the input labels, the shape should be eg: (batch_size, 1) :param s: scalar value, default is 30 :param out_num: output class num :param m: the margin value, default is 0.4 :return: the final cacualted output, this output is send into the tf.nn.softmax directly ''' with tf.variable_scope('cosineface_loss'): # inputs and weights norm embedding_norm = tf.norm(embedding, axis=1, keep_dims=True) embedding = tf.div(embedding, embedding_norm, name='norm_embedding') weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num), initializer=w_init, dtype=tf.float32) weights_norm = tf.norm(weights, axis=0, keep_dims=True) weights = tf.div(weights, weights_norm, name='norm_weights') # cos_theta - m cos_t = tf.matmul(embedding, weights, name='cos_t') cos_t_m = tf.subtract(cos_t, m, name='cos_t_m') mask = tf.one_hot(labels, depth=out_num, name='one_hot_mask') inv_mask = tf.subtract(1., mask, name='inverse_mask') output = tf.add(s * tf.multiply(cos_t, inv_mask), s * tf.multiply(cos_t_m, mask), name='cosineface_loss_output') return output
def fast_rcnn_bbox_loss(fast_rcnn_bbox_pred, bbox_targets, roi_inside_weights, roi_outside_weights): ''' Calculate the fast RCNN bounding box refinement loss. Measures how well the fast RCNN is able to refine localization. lam/N_reg * sum_i(p_i^* * L_reg(t_i,t_i^*)) lam: classification vs bbox loss balance parameter N_reg: Number of anchor locations (~2500) p_i^*: ground truth label for anchor (loss only for positive anchors) L_reg: smoothL1 loss t_i: Parameterized prediction of bounding box t_i^*: Parameterized ground truth of closest bounding box TODO: rpn_inside_weights likely deprecated; might consider obliterating ''' with tf.variable_scope('fast_rcnn_bbox_loss'): # How far off was the prediction? diff = tf.multiply(roi_inside_weights, fast_rcnn_bbox_pred - bbox_targets) diff_sL1 = smoothL1(diff, 1.0) # Only count loss for positive anchors roi_bbox_reg = tf.reduce_mean(tf.reduce_sum(tf.multiply(roi_outside_weights, diff_sL1), reduction_indices=[1])) # Constant for weighting bounding box loss with classification loss roi_bbox_reg = cfg.TRAIN.FRCNN_BBOX_LAMBDA * roi_bbox_reg return roi_bbox_reg
def __call__(self, inputs, state, scope=None): with _checked_scope(self, scope or "rwa_cell", reuse=self._reuse): h, n, d, a_max = state with vs.variable_scope("u"): u = _linear(inputs, self._num_units, True) with vs.variable_scope("g"): g = _linear([inputs, h], self._num_units, True) with vs.variable_scope("a"): a = _linear([inputs, h], self._num_units, False) # The bias term when factored out of the numerator and denominator cancels and is unnecessary z = tf.multiply(u, tanh(g)) a_newmax = tf.maximum(a_max, a) exp_diff = tf.exp(a_max - a_newmax) exp_scaled = tf.exp(a - a_newmax) n = tf.multiply(n, exp_diff) + tf.multiply(z, exp_scaled) # Numerically stable update of numerator d = tf.multiply(d, exp_diff) + exp_scaled # Numerically stable update of denominator h_new = self._activation(tf.div(n, d)) new_state = RWACellTuple(h_new, n, d, a_newmax) return h_new, new_state
def rpn_bbox_loss(rpn_bbox_pred, rpn_bbox_targets, rpn_inside_weights, rpn_outside_weights): ''' Calculate the Region Proposal Network bounding box loss. Measures how well the RPN is able to propose regions by the performance of its localization. lam/N_reg * sum_i(p_i^* * L_reg(t_i,t_i^*)) lam: classification vs bbox loss balance parameter N_reg: Number of anchor locations (~2500) p_i^*: ground truth label for anchor (loss only for positive anchors) L_reg: smoothL1 loss t_i: Parameterized prediction of bounding box t_i^*: Parameterized ground truth of closest bounding box ''' with tf.variable_scope('rpn_bbox_loss'): # Transposing rpn_bbox_targets = tf.transpose(rpn_bbox_targets, [0,2,3,1]) rpn_inside_weights = tf.transpose(rpn_inside_weights, [0,2,3,1]) rpn_outside_weights = tf.transpose(rpn_outside_weights, [0,2,3,1]) # How far off was the prediction? diff = tf.multiply(rpn_inside_weights, rpn_bbox_pred - rpn_bbox_targets) diff_sL1 = smoothL1(diff, 3.0) # Only count loss for positive anchors. Make sure it's a sum. rpn_bbox_reg = tf.reduce_sum(tf.multiply(rpn_outside_weights, diff_sL1)) # Constant for weighting bounding box loss with classification loss rpn_bbox_reg = cfg.TRAIN.RPN_BBOX_LAMBDA * rpn_bbox_reg return rpn_bbox_reg
def add_dyprune(weights): crate = config.crate[weights.name[:-2]] #hyperpara C rate prune_mask = tf.Variable(tf.ones_like(weights),name=weights.name[:-2]+'mask', trainable=False) #calculate mask mean = tf.divide(tf.reduce_sum(tf.multiply(tf.abs(weights),prune_mask)),tf.reduce_sum(prune_mask)) var = tf.multiply(weights,prune_mask) var = tf.square(var) mean_q = tf.square(mean)*tf.reduce_sum(prune_mask) var = tf.reduce_sum(var) - mean_q var = tf.divide(var,tf.reduce_sum(prune_mask)) var = tf.sqrt(var) t1_lower = (mean+var*crate)*0.25 #hyperpara a t1_upper = (mean+var*crate)*0.45 #hyperpara b indicator_lower1 = tf.greater_equal(tf.abs(weights), tf.ones_like(weights) * t1_lower) indicator_upper1 = tf.greater_equal(tf.abs(weights), tf.ones_like(weights) * t1_upper) indicator_matrix1 = tf.greater_equal(prune_mask, tf.zeros_like(weights)) indicator_matrix1 = tf.logical_and(indicator_matrix1,indicator_lower1) indicator_matrix1 = tf.logical_or(indicator_matrix1,indicator_upper1) indicator_matrix1 = tf.to_float(indicator_matrix1) update = prune_mask.assign(indicator_matrix1) prune_fc = tf.multiply(weights, prune_mask) return prune_fc
def psnr(prediction, ground_truth, maxp=None, name='psnr'): """`Peek Signal to Noise Ratio <https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio>`_. .. math:: PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE) Args: prediction: a :class:`tf.Tensor` representing the prediction signal. ground_truth: another :class:`tf.Tensor` with the same shape. maxp: maximum possible pixel value of the image (255 in in 8bit images) Returns: A scalar tensor representing the PSNR. """ maxp = float(maxp) def log10(x): with tf.name_scope("log10"): numerator = tf.log(x) denominator = tf.log(tf.constant(10, dtype=numerator.dtype)) return numerator / denominator mse = tf.reduce_mean(tf.square(prediction - ground_truth)) if maxp is None: psnr = tf.multiply(log10(mse), -10., name=name) else: psnr = tf.multiply(log10(mse), -10.) psnr = tf.add(tf.multiply(20., log10(maxp)), psnr, name=name) return psnr
def _combine_feature(net, feature, op=None): if op == "conv": options['stride']=[1,1] options['avg_pool']=[1,1] layers = int(args[1]) feature = self.layer_conv(feature, [layers], options) if op == "linear": feature = self.layer_linear(feature, [args[1]], options) feature = self.layer_reshape(feature, [args[2]], options) if op == 'gru': tanh = tf.tanh #tanh = self.ops.prelu() # tanh = self.ops.double_sided(default_activation=tanh) sigmoid = tf.sigmoid # sigmoid = self.ops.double_sided(default_activation=sigmoid) def _conv(_net,name, scale=1): _options = dict(options) _options['activation']=None _options['name']=self.ops.description+name return self.layer_conv(_net, [int(args[1])//scale], _options) z = sigmoid(_conv(net,'z',scale=2)) r = tf.sigmoid(_conv(net,'r',scale=2)) th = _conv(net,'net',scale=2) fh = _conv(feature,'feature',scale=2) h = tanh(th + fh * r) net = tf.multiply( (1-z), h) + tf.multiply(feature, z) if 'only' in options: return net if feature is not None: net = tf.concat([net, feature], axis=len(self.ops.shape(net))-1) return net
def streaming_f1(self, labels, predictions, n_classes, weights=None, type='macro'): labels_and_predictions_by_class = [(tf.equal(labels, c), tf.equal(predictions, c)) for c in range(0, n_classes)] tp_by_class_val, tp_by_class_update_op = zip(*[tf.metrics.true_positives(label, prediction, weights=weights) for label, prediction in labels_and_predictions_by_class]) fn_by_class_val, fn_by_class_update_op = zip(*[tf.metrics.false_negatives(label, prediction, weights=weights) for label, prediction in labels_and_predictions_by_class]) fp_by_class_val, fp_by_class_update_op = zip(*[tf.metrics.false_positives(label, prediction, weights=weights) for label, prediction in labels_and_predictions_by_class]) f1_update_op = tf.group(*chain(tp_by_class_update_op, fn_by_class_update_op, fp_by_class_update_op)) if type == 'macro': epsilon = [10e-6 for _ in range(n_classes)] f1_val = tf.multiply(2., tp_by_class_val) / (tf.reduce_sum([tf.multiply(2., tp_by_class_val), fp_by_class_val, fn_by_class_val, epsilon], axis=0)) f1_val = tf.reduce_mean(f1_val) else: epsilon = 10e-6 total_tp = tf.reduce_sum(tp_by_class_val) total_fn = tf.reduce_sum(fn_by_class_val) total_fp = tf.reduce_sum(fp_by_class_val) f1_val = tf.squeeze(tf.multiply(2., total_tp) / (tf.multiply(2., total_tp) + total_fp + total_fn + epsilon, )) return f1_val, f1_update_op
def sample_weights(self, weights): log_p = 0 log_q = 0 sampled_weights = [] for layer_i in range(len(self.network_architecture['decoder_net'])): if layer_i == 0: eps = tf.random_normal((self.n_z+1, self.network_architecture['decoder_net'][layer_i]), 0, 1, dtype=tf.float32) weights_ = tf.add(weights['l'+str(layer_i)+'mean'], tf.multiply(tf.sqrt(tf.exp(weights['l'+str(layer_i)+'logvar'])), eps)) n_decoder_weights = (self.n_z+1) * self.network_architecture['decoder_net'][layer_i] log_p += self.log_p_theta(weights_, n_decoder_weights) log_q += self.log_q_theta(weights_, weights['l'+str(layer_i)+'mean'], weights['l'+str(layer_i)+'logvar'], n_decoder_weights) else: eps = tf.random_normal((self.network_architecture['decoder_net'][layer_i-1]+1, self.network_architecture['decoder_net'][layer_i]), 0, 1, dtype=tf.float32) weights_ = tf.add(weights['l'+str(layer_i)+'mean'], tf.multiply(tf.sqrt(tf.exp(weights['l'+str(layer_i)+'logvar'])), eps)) n_decoder_weights = self.network_architecture['decoder_net'][layer_i-1]+1 * self.network_architecture['decoder_net'][layer_i] log_p += self.log_p_theta(weights_, n_decoder_weights) log_q += self.log_q_theta(weights_, weights['l'+str(layer_i)+'mean'], weights['l'+str(layer_i)+'logvar'], n_decoder_weights) sampled_weights.append(weights_) eps = tf.random_normal((self.network_architecture['decoder_net'][-1]+1, self.n_input), 0, 1, dtype=tf.float32) weights_ = tf.add(weights['out_mean_mean'], tf.multiply(tf.sqrt(tf.exp(weights['out_mean_logvar'])), eps)) sampled_weights.append(weights_) n_decoder_weights = self.network_architecture['decoder_net'][-1]+1 * self.n_input log_p += self.log_p_theta(weights_, n_decoder_weights) log_q += self.log_q_theta(weights_, weights['out_mean_mean'], weights['out_mean_logvar'], n_decoder_weights) # print log_p # print log_q # fasdf return sampled_weights, log_p, log_q
return a[:] return [val if idx < w else sum(a[(idx-w):idx])/w for idx , val in enumerate(a)] train_X = np.linspace(-1,1,100) #在a,b之间生成c个点 train_Y = 2*train_X+np.random.randn(*train_X.shape) * 0.3 # plt.plot(train_X,train_Y,'ro',label = 'Original data') # plt.legend() #显示图例 # plt.show() X = tf.placeholder("float") Y = tf.placeholder("float") #真实值 W = tf.Variable(tf.random_normal([1]),name="weight") b = tf.Variable(tf.zeros([1]),name="bias") z = tf.multiply(X,W) + b #预测值 tf.summary.histogram('z',z) #将预测值以直方图的形式显示 #反向优化 cost = tf.reduce_mean(tf.square(Y-z)) tf.summary.scalar('loss_function',cost) #将损失以标量的形式显示 learning_rate = 0.01 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #梯度下降算法 init = tf.global_variables_initializer() #初始化所有变量 #定义参数 training_epochs = 20 display_step = 2 saver = tf.train.Saver() saverdir= "log/"
def nnpda_cycle(Ns, Ni, Nr, Na, batch_size, num_steps, str_len, optimizer=RMSprop, activation=sigmoid): # cell = NnpdaCell words = tf.ones([Ni, num_steps], dtype=tf.dtypes.float32) # [10x200] st_desired = tf.Variable(tf.random.normal( [Ns, num_steps])) # Placeholder for the desired final state curr_state = tf.ones([Ns, 1]) delta, delta_ = get_delta(Ns) sym_stack = Stack() # Stack for storing the input symbols len_stack = Stack() # Stack for storing the lengths of input symbols for i in range(num_steps): # 200, length of input sequences print("time step", i) ############# STACK ACTION ############# # (Default) Pushing for the initial time step if i == 0: sym_stack.push(words[:, i]) # [, 10] len_stack.push(tf.norm(tensor=words[:, i], axis=-1)) # Pushing if At > 0 elif stack_axn > 0: sym_stack.push(words[:, i]) len_stack.push(stack_axn * tf.norm(tensor=words[:, i], axis=-1)) # Popping if At < 0 elif stack_axn < 0: len_popped = 0 # Popping a total of length |At| from the stack while len_popped != -stack_axn: # If len(top) > |At|, Updating the length if len_stack.peek() > -stack_axn: len_popped += -stack_axn len_stack.update(len_stack.peek() - stack_axn) # If len(top) < |At|, Popping the top else: len_popped += len_stack.peek() sym_stack.pop() len_stack.pop() # No action if At=0 else: continue ############# READING THE STACK ########## curr_read = tf.ones([Nr, 1]) len_read = 0 # Reading a total length '1' from the stack while len_read != 1: # print(len_stack.peek()) if len_stack.peek() < 1: curr_read = tf.math.add(curr_read, tf.multiply(sym_stack.peek(), len_stack.peek())) # print("current read b4 if", tf.shape(curr_read)) len_read += len_stack.peek() else: curr_read = tf.math.add(curr_read, tf.reshape(sym_stack.peek(), [Ni, 1])) # print(curr_read) # print("current read b4 else", tf.shape(curr_read)) len_read = 1 # https://stackoverflow.com/questions/9663562/what-is-the-difference-between-init-and-call cell = NnpdaCell(input_symbol=words[:, i], current_state=curr_state, current_stack=curr_read, state_activation=sigmoid, action_activation=tanh, state_weights=variables_dict["Ws"], state_bias=variables_dict["bs"], action_weights=variables_dict["Wa"], action_bias=variables_dict["ba"], delta=delta, delta_=delta_) # .rtrn() cell() next_state, stack_axn = cell.rtrn() print("stack action:", stack_axn) curr_state = next_state loss_per_example = tf.square( tf.norm(tensor=st_desired - curr_state)) + tf.square( len_stack.peek()) total_loss = tf.reduce_mean(input_tensor=loss_per_example) print("Loss per example", loss_per_example) print("Total loss", total_loss) return total_loss
# Set up the TensorFlow placeholders that get updated as we descend down the gradient tf_house_size = tf.placeholder("float", name="house_size") tf_price = tf.placeholder("float", name="price") # Define the variables holding the size_factor and price we set during training. # We initialize them to some random values based on the normal distribution. tf_size_factor = tf.Variable(np.random.randn(), name="size_factor") tf_price_offset = tf.Variable(np.random.randn(), name="price_offset") # 2. Define the operations for the predicting values - predicted price = (size_factor * house_size ) + price_offset # Notice, the use of the tensorflow add and multiply functions. These add the operations to the computation graph, # AND the tensorflow methods understand how to deal with Tensors. Therefore do not try to use numpy or other library # methods. tf_price_pred = tf.add(tf.multiply(tf_size_factor, tf_house_size), tf_price_offset) # 3. Define the Loss Function (how much error) - Mean squared error tf_cost = tf.reduce_sum(tf.pow(tf_price_pred-tf_price, 2))/(2*num_train_samples) # Optimizer learning rate. The size of the steps down the gradient learning_rate = 0.1 # 4. define a Gradient descent optimizer that will minimize the loss defined in the operation "cost". optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(tf_cost) # Initializing the variables init = tf.global_variables_initializer()
def main(_): ps_hosts = FLAGS.ps_hosts.split(',') worker_hosts = FLAGS.worker_hosts.split(',') job_name = FLAGS.job_name task_index = FLAGS.task_index learning_rate = FLAGS.learning_rate step_to_validate = FLAGS.step_to_validate is_sync = FLAGS.is_sync # create a cluster from the parameter server and worker hosts. cluster = tf.train.ClusterSpec({'ps': ps_hosts, 'worker': worker_hosts}) # create and start a server for the local task server = tf.train.Server(cluster, job_name=job_name, task_index=FLAGS.task_index) if job_name == 'ps': server.join() elif job_name == 'worker': # assign ops to the local worker by default device_setting = tf.train.replica_device_setter(worker_device='/job:worker/task:%d'%task_index, cluster=cluster) with tf.device(device_setting): input = tf.placeholder('float') label = tf.placeholder('float') weight = tf.get_variable('weight', [1], tf.float32, initializer=tf.random_normal_initializer()) bias = tf.get_variable('bias', [1], tf.float32, initializer=tf.random_normal_initializer()) out = tf.multiply(input, weight) + bias loss = tf.square(label - out) tf.summary.scalar('cost', loss) global_step = tf.Variable(0, name='global_step', trainable=False) optimizer = tf.train.GradientDescentOptimizer(learning_rate) grad_and_vars = optimizer.compute_gradients(loss) if is_sync == 1: rep_op = tf.train.SyncReplicasOptimizer(optimizer, replicas_to_aggregate=len(worker_hosts), replica_id=task_index, total_num_replicas=len(worker_hosts), use_locking=True) train_op = rep_op.apply_gradients(grad_and_vars, global_step=global_step) init_token_op = rep_op.get_init_tokens_op() chief_queue_runner = rep_op.get_chief_queue_runner() else: train_op = optimizer.apply_gradients(grad_and_vars, global_step=global_step) init_op = tf.initialize_all_variables() saver = tf.train.Saver() summary_op = tf.summary.merge_all() # create a supervisor which oversees the training process sv = tf.train.Supervisor(is_chief=(task_index == 0), logdir='cache/log/test', init_op=init_op, summary_op=summary_op, saver=saver, global_step=global_step, save_model_secs=600) if is_sync == 1: with sv.prepare_or_wait_for_session(server.target) as sess: if task_index == 0 and is_sync == 1: sv.start_queue_runners(sess, [chief_queue_runner]) sess.run(init_token_op) step = 0 while step < 10000: train_x = np.random.randn(1) train_y = 2* train_x + np.random.randn(1) * 0.33 + 10 _, loss_v, step = sess.run([train_op, loss, global_step], feed_dict={input: train_x, label: train_y}) if step % step_to_validate == 0: w, b = sess.run([weight, bias]) print("step: %d, weight:%f, bias:%f, loss:%f"%(step,weight,bias,loss)) else: # the supervisor takes care of session initialization, restoring from a checkpoint # and closing when done or an error occurs. with sv.managed_session(server.target) as sess: # look until the supervisor shuts down or 10000 steps have completed. step = 0 while not sv.should_stop() and step < 10000: # run a training step asynchronously # see tf.train.SyncReplicasOptimizer for additional details on how to perform synchronous training _, step = sess.run([train_op, global_step]) # ask for all services to stop sv.stop() else: print('No this job!')
def preprocess_for_train(image, height, width, bbox, fast_mode=True, scope=None, add_image_summaries=True, random_crop=True, use_grayscale=False): """Distort one image for training a network. Distorting images provides a useful technique for augmenting the data set during training in order to make the network invariant to aspects of the image that do not effect the label. Additionally it would create image_summaries to display the different transformations applied to the image. Args: image: 3-D Tensor of image. If dtype is tf.float32 then the range should be [0, 1], otherwise it would converted to tf.float32 assuming that the range is [0, MAX], where MAX is largest positive representable number for int(8/16/32) data type (see `tf.image.convert_image_dtype` for details). height: integer width: integer bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] where each coordinate is [0, 1) and the coordinates are arranged as [ymin, xmin, ymax, xmax]. fast_mode: Optional boolean, if True avoids slower transformations (i.e. bi-cubic resizing, random_hue or random_contrast). scope: Optional scope for name_scope. add_image_summaries: Enable image summaries. random_crop: Enable random cropping of images during preprocessing for training. use_grayscale: Whether to convert the image from RGB to grayscale. Returns: 3-D float Tensor of distorted image used for training with range [-1, 1]. """ with tf.name_scope(scope, 'distort_image', [image, height, width, bbox]): if bbox is None: bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) if image.dtype != tf.float32: image = tf.image.convert_image_dtype(image, dtype=tf.float32) # Each bounding box has shape [1, num_boxes, box coords] and # the coordinates are ordered [ymin, xmin, ymax, xmax]. image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0), bbox) if add_image_summaries: tf.summary.image('image_with_bounding_boxes', image_with_box) if not random_crop: distorted_image = image else: distorted_image, distorted_bbox = distorted_bounding_box_crop(image, bbox) # Restore the shape since the dynamic slice based upon the bbox_size loses # the third dimension. distorted_image.set_shape([None, None, 3]) image_with_distorted_box = tf.image.draw_bounding_boxes( tf.expand_dims(image, 0), distorted_bbox) if add_image_summaries: tf.summary.image('images_with_distorted_bounding_box', image_with_distorted_box) # This resizing operation may distort the images because the aspect # ratio is not respected. We select a resize method in a round robin # fashion based on the thread number. # Note that ResizeMethod contains 4 enumerated resizing methods. # We select only 1 case for fast_mode bilinear. num_resize_cases = 1 if fast_mode else 4 distorted_image = apply_with_random_selector( distorted_image, lambda x, method: tf.image.resize_images(x, [height, width], method), num_cases=num_resize_cases) if add_image_summaries: tf.summary.image(('cropped_' if random_crop else '') + 'resized_image', tf.expand_dims(distorted_image, 0)) # Randomly flip the image horizontally. distorted_image = tf.image.random_flip_left_right(distorted_image) # Randomly distort the colors. There are 1 or 4 ways to do it. num_distort_cases = 1 if fast_mode else 4 distorted_image = apply_with_random_selector( distorted_image, lambda x, ordering: distort_color(x, ordering, fast_mode), num_cases=num_distort_cases) if use_grayscale: distorted_image = tf.image.rgb_to_grayscale(distorted_image) if add_image_summaries: tf.summary.image('final_distorted_image', tf.expand_dims(distorted_image, 0)) distorted_image = tf.subtract(distorted_image, 0.5) distorted_image = tf.multiply(distorted_image, 2.0) return distorted_image
def log_regression(): learning_rate = 0.01 batch_size = 1024 iter_num = 30 display_step = 1 beta = 0.1 threshold = 0.5 seed = 5 np.random.seed(seed) tf.set_random_seed(seed) print("Loading training data...") train_data = pd.read_csv(currentDirectory + "train\\" + "CombinedTrainData.csv", header=None) print("Shuffling training data...") train_data = train_data.iloc[np.random.permutation(len(train_data))] train_X = train_data.iloc[:, 0:number_of_columns - 1] #selecting the feature columns train_X = np.array(train_X) train_Y = np.array(train_data.iloc[:, number_of_columns - 1:]) # selecting the label column train_Y = np.array(train_Y) # print ("Loading validation data...") # dev_data = pd.read_csv(currentDirectory + "dev\\" + "CombinedDevData.csv") # dev_X = dev_data.iloc[:,0:number_of_columns-1] #selecting the feature columns # #dev_X = min_max_normalized(np.array(dev_X)) # dev_Y = np.array(dev_data.iloc[:,number_of_columns-1:]) # selecting the label column print("Loading test data...") test_data = pd.read_csv(currentDirectory + "test\\" + "CombinedTestData.csv") test_X = test_data.iloc[:, 0:number_of_columns - 1] #selecting the feature columns test_X = np.array(test_X) #dev_X = min_max_normalized(np.array(dev_X)) test_Y = np.array(test_data.iloc[:, number_of_columns - 1:]) # selecting the label column test_Y = np.array(test_Y) #Defining the model framework w = tf.Variable(tf.zeros([44, 1]), name="weights") b = tf.Variable(tf.zeros([1]), name="bias") X = tf.placeholder(tf.float32, [None, 44], name='data') Y = tf.placeholder(tf.float32, [None, 1], name='target') pred = tf.sigmoid(tf.matmul(X, w) + b) # Minimize error using cross entropy (1st cost function) #cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=Y)) # (2nd cost function used) #cost = tf.multiply(-1.0, tf.reduce_mean(tf.add(tf.multiply(beta,tf.multiply(Y,tf.log(pred))),tf.multiply(tf.subtract(1.0, Y) , tf.log(tf.subtract(1.0, pred)))))) cost = tf.reduce_mean( tf.nn.weighted_cross_entropy_with_logits(logits=pred, targets=Y, pos_weight=beta)) #To calculate precision, which is the main metric rounded_pred = tf.cast(pred >= threshold, dtype=tf.float32) total_ones = tf.reduce_sum(rounded_pred) true_positive = tf.reduce_sum(tf.multiply(rounded_pred, Y)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(cost) #correct = tf.cast(tf.equal(tf.round(pred), Y), dtype=tf.float32) #accuracy = tf.metrics.precision(labels = Y, predictions = tf.round(pred)) writer = tf.summary.FileWriter('./graphs', tf.get_default_graph()) train_precison = [] test_precision = [] init = tf.global_variables_initializer() init_l = tf.local_variables_initializer() with tf.Session() as sess: sess.run(init) sess.run(init_l) for epoch in range(iter_num): avg_cost = 0.0 tp = 0 tp_fp = 0.000000000001 # to prevent division by 0 test_tp = 0 test_tp_fp = 0.000000000001 # to prevent division by 0 total_batch = math.ceil(train_X.shape[0] / batch_size) for i in range(total_batch - 1): batch_xs = train_X[i * batch_size:batch_size * i + batch_size, :] batch_ys = train_Y[i * batch_size:batch_size * i + batch_size, :] test_xs = test_X[i * batch_size:batch_size * i + batch_size, :] test_ys = test_Y[i * batch_size:batch_size * i + batch_size, :] # # convert into a matrix, and the shape of the placeholder to correspond # temp_train_acc = sess.run(accuracy, feed_dict={X: batch_xs, Y: batch_ys}) # temp_test_acc = sess.run(accuracy, feed_dict={X: dev_X, Y: dev_Y}) # # recode the result # loss_trace.append(temp_loss) # train_acc.append(temp_train_acc) # validation_acc.append(temp_test_acc) # output # Run optimization op (backprop) and cost op (to get loss value) #_, c, p, temp_train_acc = sess.run([optimizer, cost, pred, accuracy], feed_dict={X: batch_xs, Y: batch_ys}) _, c, p, ones, correct_ones = sess.run( [optimizer, cost, pred, total_ones, true_positive], feed_dict={ X: batch_xs, Y: batch_ys }) test_ones, test_correct_ones = sess.run( [total_ones, true_positive], feed_dict={ X: test_xs, Y: test_ys }) avg_cost += c tp += correct_ones tp_fp += ones test_tp += test_correct_ones test_tp_fp += test_ones #including the last batch batch_xs = train_X[batch_size * (total_batch - 1):, :] batch_ys = train_Y[batch_size * (total_batch - 1):, :] #_, c, p = sess.run([optimizer, cost, pred], feed_dict={X: batch_xs, Y: batch_ys}) #_, c, p, temp_train_acc = sess.run([optimizer, cost, pred, accuracy], feed_dict={X: batch_xs, Y: batch_ys}) _, c, p, ones, correct_ones = sess.run( [optimizer, cost, pred, total_ones, true_positive], feed_dict={ X: batch_xs, Y: batch_ys }) test_ones, test_correct_ones = sess.run( [total_ones, true_positive], feed_dict={ X: test_xs, Y: test_ys }) avg_cost += c tp += correct_ones tp_fp += ones test_tp += test_correct_ones test_tp_fp += test_ones #temp_train_acc = sess.run(accuracy, feed_dict={X: batch_xs, Y: batch_ys}) #train_acc.append(temp_train_acc[0]) #print ("Tensorflow precision: ", temp_train_acc[0]) print("Train Precision: ", float(tp) / float(tp_fp)) print("Test Precision: ", float(test_tp) / float(test_tp_fp)) # Display logs per epoch step if (epoch + 1) % display_step == 0: #print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)) #print('epoch: {:4d} cost = : {:.9f} train_precision: {:.9f} '.format(epoch + 1, avg_cost, temp_train_acc[0])) print( 'epoch: {:4d} cost = : {:.9f} train_precision: {:.9f} test_precision: {:.9f}' .format(epoch + 1, avg_cost, float(tp) / float(tp_fp), float(test_tp) / float(test_tp_fp))) print("Optimization Finished!") writer.close()
def __init__(self, n_actions, hidden=1024, learning_rate=0.00001, frame_height=84, frame_width=84, agent_history_length=4): """ Args: n_actions: Integer, number of possible actions hidden: Integer, Number of filters in the final convolutional layer. This is different from the DeepMind implementation learning_rate: Float, Learning rate for the Adam optimizer frame_height: Integer, Height of a frame of an Atari game frame_width: Integer, Width of a frame of an Atari game agent_history_length: Integer, Number of frames stacked together to create a state """ self.n_actions = n_actions self.hidden = hidden self.learning_rate = learning_rate self.frame_height = frame_height self.frame_width = frame_width self.agent_history_length = agent_history_length self.input = tf.placeholder( shape=[None, self.frame_height, self.frame_width, 1], dtype=tf.float32) # Normalizing the input self.inputscaled = self.input / 255 # Convolutional layers self.conv1 = tf.layers.conv2d( inputs=self.inputscaled, filters=32, kernel_size=[8, 8], strides=4, kernel_initializer=tf.variance_scaling_initializer(scale=2), padding="valid", activation=tf.nn.relu, use_bias=False, name='conv1') self.conv2 = tf.layers.conv2d( inputs=self.conv1, filters=64, kernel_size=[4, 4], strides=2, kernel_initializer=tf.variance_scaling_initializer(scale=2), padding="valid", activation=tf.nn.relu, use_bias=False, name='conv2') self.conv3 = tf.layers.conv2d( inputs=self.conv2, filters=64, kernel_size=[3, 3], strides=1, kernel_initializer=tf.variance_scaling_initializer(scale=2), padding="valid", activation=tf.nn.relu, use_bias=False, name='conv3') self.conv4 = tf.layers.conv2d( inputs=self.conv3, filters=1024, kernel_size=[7, 7], strides=1, kernel_initializer=tf.variance_scaling_initializer(scale=2), padding="valid", activation=tf.nn.relu, use_bias=False, name='conv4') # Splitting into value and advantage stream # value, num_of_splits, axis self.flat = tf.layers.flatten(self.conv4) # Format (timestep, batch, inputshape) self.lstm = tf.keras.layers.LSTM(units=hidden, input_shape=(1024, ), return_sequences=True)(tf.reshape( self.flat, [1, 1024, 1])) self.valuestream, self.advantagestream = tf.split(self.lstm, 2, -1) self.valuestream = tf.expand_dims(self.valuestream, -1) self.advantagestream = tf.expand_dims(self.advantagestream, -1) self.advantage = tf.layers.dense( inputs=self.advantagestream, units=self.n_actions, kernel_initializer=tf.variance_scaling_initializer(scale=2), name="advantage") self.value = tf.layers.dense( inputs=self.valuestream, units=1, kernel_initializer=tf.variance_scaling_initializer(scale=2), name='value') # Combining value and advantage into Q-values as described above self.q_values = self.value + tf.subtract( self.advantage, tf.reduce_mean(self.advantage, axis=1, keep_dims=True)) self.best_action = tf.argmax(self.q_values, 1) # The next lines perform the parameter update. This will be explained in detail later. # targetQ according to Bellman equation: # Q = r + gamma*max Q', calculated in the function learn() self.target_q = tf.placeholder(shape=[None], dtype=tf.float32) # Action that was performed self.action = tf.placeholder(shape=[None], dtype=tf.int32) # Q value of the action that was performed self.Q = tf.reduce_sum(tf.multiply( self.q_values, tf.one_hot(self.action, self.n_actions, dtype=tf.float32)), axis=1) self.TD_Error = self.target_q - tf.reduce_max(self.q_values, axis=1) # Parameter updates self.loss = tf.reduce_mean( tf.losses.huber_loss(labels=self.target_q, predictions=self.Q)) self.optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate) self.update = self.optimizer.minimize(self.loss)
def sample_sparse_grid_like(gt_flow, target_density=75, height=384, width=512): print("sample_sparse_grid_like") # Important: matches is already normalised to [0, 1] num_samples = tf.multiply( tf.multiply(tf.divide(target_density, 100.0), height), width) aspect_ratio = tf.divide(width, height) # Compute as in invalid_like for a random box to know the number of samples in horizontal and vertical num_samples_w = tf.cast(tf.round( tf.sqrt(tf.multiply(num_samples, aspect_ratio))), dtype=tf.int32) num_samples_h = tf.cast(tf.round( tf.divide(tf.cast(num_samples_w, dtype=tf.float32), aspect_ratio)), dtype=tf.int32) # Check crop dimensions are plausible, otherwise crop them to fit (this alters the density we were sampling at) num_samples_h = tf.cond(tf.greater(num_samples_h, tf.constant(height)), lambda: tf.constant(height, dtype=tf.int32), lambda: num_samples_h) num_samples_w = tf.cond(tf.greater(num_samples_w, tf.constant(width)), lambda: tf.constant(width, dtype=tf.int32), lambda: num_samples_w) delta_rows = tf.cast((height - 1 - 0) / num_samples_h, tf.float32) sample_points_h = tf.cast(tf.round( tf.range(start=0, limit=height, delta=delta_rows, dtype=tf.float32)), dtype=tf.int32) delta_cols = tf.cast((width - 1 - 0) / num_samples_w, tf.float32) sample_points_w = tf.cast(tf.round( tf.range(start=0, limit=width, delta=delta_cols, dtype=tf.float32)), dtype=tf.int32) # Create meshgrid of all combinations (i.e.: coordinates to sample at) rows, cols = tf.meshgrid(sample_points_h, sample_points_w, indexing='ij') rows_flatten = tf.reshape(rows, [-1]) cols_flatten = tf.reshape(cols, [-1]) # Compute absolute indices as row * width + cols indices = tf.add(tf.multiply(rows_flatten, width), cols_flatten) ones = tf.ones(tf.shape(indices), dtype=tf.float32) zeros = lambda: tf.zeros((height * width), dtype=tf.float32) matches = tf.Variable(initial_value=zeros, trainable=False) matches = tf.scatter_update(matches, indices, ones) # all 1D tensors # Randomly subtract a part with a random rectangle (superpixels in the future) corrupt_mask = tf.random_uniform([], maxval=2, dtype=tf.int32) matches = tf.cond( tf.greater(corrupt_mask, tf.constant(0)), lambda: corrupt_sparse_flow_once( matches, target_density, height, width), lambda: return_identity_one(matches)) sampling_mask = tf.reshape(matches, (height, width)) # sampling_mask of size (h, w) matches = tf.cast(tf.expand_dims(sampling_mask, -1), dtype=tf.float32) # convert to (h, w, 1) # Sample ground truth flow with given map # sampling_mask = sampling_mask[:, :, tf.newaxis] # sampling_mask_rep = tf.tile(sampling_mask, [1, 1, 2]) # sampling_mask_flatten = tf.reshape(sampling_mask_rep, [-1]) # sampling_mask_flatten_where = tf.where( # tf.equal(sampling_mask_flatten, tf.cast(1, dtype=sampling_mask_flatten.dtype))) # sampling_mask_flatten_where = tf.reshape(sampling_mask_flatten_where, [-1]) # # gt_flow_sampling_mask = tf.boolean_mask(gt_flow, sampling_mask_rep) # zeros = lambda: tf.zeros(tf.reduce_prod(gt_flow.shape), dtype=tf.float32) # sparse_flow = tf.Variable(initial_value=zeros, dtype=tf.float32, trainable=False) # sparse_flow = tf.scatter_update(sparse_flow, sampling_mask_flatten_where, gt_flow_sampling_mask) # sparse_flow = tf.reshape(sparse_flow, gt_flow.shape) sparse_flow = mask_to_sparse_flow(sampling_mask, gt_flow) return matches, sparse_flow
def model(X, w): return tf.add(tf.multiply(w[1], tf.pow(X, 1)), tf.multiply(w[0], tf.pow(X, 0)))
import tensorflow as tf #Fetch 同时多个操作 input1 = tf.constant(3.0) input2 = tf.constant(2.0) input3 = tf.constant(5.0) add = tf.add(input2, input3) mul = tf.multiply(input1, add) with tf.Session() as sess: result = sess.run([mul, add]) print(result) print(result) #Feed input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess: #feed数据以字典形式传入 print(sess.run(output, feed_dict={input1: [7.0], input2: [2.0]}))
# Now we will start creating our model by defining the placeholders X and Y, so that we can feed our training examples # X and Y into the optimizer during the training process. X = tf.compat.v1.placeholder("float") Y = tf.compat.v1.placeholder("float") # Now we will declare two trainable Tensorflow Variables for the Weights and Bias and initializing them randomly W = tf.Variable(np.random.randn(), name="W") b = tf.Variable(np.random.randn(), name="b") # Now we will define the hyperparameters of the model, the Learning Rate and the number of Epochs. learning_rate = 0.01 training_epochs = 1000 # Hypothesis y_pred = tf.add(tf.multiply(X, W), b) # Mean Squared Error Cost Function cost = tf.reduce_sum(tf.pow(y_pred - Y, 2)) / (2 * n) # Gradient Descent Optimizer optimizer = tf.compat.v1.train.GradientDescentOptimizer( learning_rate).minimize(cost) # Global Variables Initializer init = tf.compat.v1.global_variables_initializer() # Starting the Tensorflow Session with tf.compat.v1.Session() as sess: # Initializing the Variables
def learn(env, q_func, nn_size, n_hidden_units, exploration=LinearSchedule(1000000, 0.1), stopping_criterion=None, replay_buffer_size=1000000, batch_size=32, gamma=0.99, learning_starts=50000, learning_freq=4, frame_history_len=4, target_update_freq=10000, grad_norm_clipping=10, double_DQN=True, n_steps_ahead=3 ): """Run Deep Q-learning algorithm. You can specify your own convnet using q_func. All schedules are w.r.t. total number of steps taken in the environment. Parameters ---------- env: gym.Env gym environment to train on. q_func: function Model to use for computing the q function. It should accept the following named arguments: img_in: tf.Tensor tensorflow tensor representing the input image num_actions: int number of actions scope: str scope in which all the model related variables should be created reuse: bool whether previously created variables should be reused. optimizer_spec: OptimizerSpec Specifying the constructor and kwargs, as well as learning rate schedule for the optimizer session: tf.Session tensorflow session to use. exploration: rl_algs.deepq.utils.schedules.Schedule schedule for probability of chosing random action. stopping_criterion: (env, t) -> bool should return true when it's ok for the RL algorithm to stop. takes in env and the number of steps executed so far. replay_buffer_size: int How many memories to store in the replay buffer. batch_size: int How many transitions to sample each time experience is replayed. gamma: float Discount Factor learning_starts: int After how many environment steps to start replaying experiences learning_freq: int How many steps of environment to take between every experience replay frame_history_len: int frame_history_len: int How many past frames to include as input to the model. target_update_freq: int How many experience replay rounds (not steps!) to perform between each update to the target Q network grad_norm_clipping: float or None If not None gradients' norms are clipped to this value. """ exp_name = env.env_name logz.configure_output_dir('data/' + exp_name + time.strftime('%m-%d-%Y-%H:%M:%s')) ############### # BUILD MODEL # ############### input_shape = env.state_shape num_actions = env.num_actions # set up placeholders # placeholder for current observation (or state) obs_t_ph = tf.placeholder(tf.float32, [None] + list(input_shape)) # placeholder for current action act_t_ph = tf.placeholder(tf.int32, [None], name='act_t_ph') # placeholder for current reward rew_t_ph = tf.placeholder(tf.float32, [None]) # placeholder for next observation (or state) obs_tp1_ph = tf.placeholder(tf.float32, [None] + list(input_shape)) # placeholder for end of episode mask # this value is 1 if the next state corresponds to the end of an episode, # in which case there is no Q-value at the next state; at the end of an # episode, only the current state reward contributes to the target, not the # next state Q-value (i.e. target is just rew_t_ph, not rew_t_ph + gamma * q_tp1) done_mask_ph = tf.placeholder(tf.float32, [None]) transition_length_ph = tf.placeholder(tf.float32, [None]) q_func_net = q_func(obs_t_ph, num_actions, "q_func", False, n_layers=nn_size, size=n_hidden_units) q_func_net_argmax_target = q_func(obs_tp1_ph, num_actions, "q_func", True, n_layers=nn_size, size=n_hidden_units) target_q_func_net = q_func(obs_tp1_ph, num_actions, "target_q_func", False, n_layers=nn_size, size=n_hidden_units) if not double_DQN: target_y = rew_t_ph + tf.pow(gamma, transition_length_ph) *\ done_mask_ph * tf.reduce_max(target_q_func_net, axis=1) else: target_y = rew_t_ph + \ tf.pow(gamma, transition_length_ph) * done_mask_ph * \ tf.reduce_sum(target_q_func_net *\ tf.one_hot(tf.argmax(q_func_net_argmax_target, axis = 1), depth=num_actions),\ axis=1) actual_y = tf.reduce_sum(tf.multiply(q_func_net, tf.one_hot(act_t_ph, depth=num_actions)), axis=1) total_error = tf.nn.l2_loss(target_y - actual_y) q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_func') target_q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_q_func') training_error_summ_sy = tf.summary.scalar('training_total_error', total_error) # construct optimization op (with gradient clipping) learning_rate = tf.placeholder(tf.float32, (), name="learning_rate") optimizer = tf.train.AdamOptimizer(0.001) train_fn = optimizer.minimize(total_error) # update_target_fn will be called periodically to copy Q network to target Q network update_target_fn = [] for var, var_target in zip(sorted(q_func_vars, key=lambda v: v.name), sorted(target_q_func_vars, key=lambda v: v.name)): update_target_fn.append(var_target.assign(var)) update_target_fn = tf.group(*update_target_fn) # construct the replay buffer replay_buffer = replay_buffer_knapsack.ReplayBuffer(replay_buffer_size, input_shape[0]) # Model saver saver = tf.train.Saver() # Create session, initialize variables session = tf.InteractiveSession() log_files_name = 'DQN_' + str(env.env_name) + \ '-lf=' + str(learning_freq) + \ '-b=' + str(batch_size) + '-' + \ time.strftime('%m-%d-%Y-%H:%M:%S') writer = tf.summary.FileWriter('/tmp/' + log_files_name, session.graph) tf.global_variables_initializer().run() saver.save(session, '/tmp/saved_models/' + log_files_name) ############### # RUN ENV # ############### model_initialized = False num_param_updates = 0 mean_episode_reward = -float('nan') best_mean_episode_reward = -float('inf') observations = [env.reset()] LOG_EVERY_N_STEPS = 10000 episode_total_rewards = [] episode_total_optimal_rewards = [] episode_total_at_random_rewards = [] accuracies = [] done = False for t in itertools.count(): ### 1. Check stopping criterion if stopping_criterion is not None and stopping_criterion(env, t): break ### 2. Step the env and store the transition import random from numpy import array if done: observations = [env.reset()] if model_initialized: # Action taking 1 # epsilon = exploration.value(t) # q_values=session.run(q_func_net, feed_dict={obs_t_ph: last_obs[None]}) # action=np.argmax(q_values[0]) # r = random.random() # if r <= epsilon: # all_possible_action = list(range(num_actions)) # other_actions = [x for x in all_possible_action if x != action] # action = np.array(random.choice(other_actions)) # Action taking 2 q_values = session.run(q_func_net, feed_dict={obs_t_ph: observations[-1][None]}) if env.env_name == 'TSP': action = np.argmax(q_values[0] * (1-env.binary_vector_state()) -\ 1e5 * env.binary_vector_state()) else: action = np.argmax(q_values[0] * (1-env.xs) - 1e5 * env.xs) if random.random() <= exploration.value(t): all_possible_action = list(range(num_actions)) other_actions = [x for x in all_possible_action if x != action] action = np.array(random.choice(other_actions)) else: action = np.array(random.choice(list(range(num_actions)))) next_obs, reward, done = env.step(action) observations.append(next_obs) if len(observations) > n_steps_ahead: replay_buffer.store_transition(observations[-(n_steps_ahead + 1)], action, reward, observations[-1], done, n_steps_ahead) if done and 1 < len(observations) <= n_steps_ahead: replay_buffer.store_transition(observations[0], action, reward, observations[-1], done, len(observations) - 1) # at this point, the environment should have been advanced one step (and # reset if done was true), and last_obs should point to the new latest # observation ### 3. Perform experience replay and train the network. # note that this is only done if the replay buffer contains enough samples # for us to learn something useful -- until then, the model will not be # initialized and random actions should be taken if (t > learning_starts and t % learning_freq == 0 and replay_buffer.can_sample(batch_size)): obs_t_batch, act_batch, rew_batch, obs_tp1_batch, done_mask_batch, transition_length_batch\ = replay_buffer.sample(batch_size) if not(model_initialized): initialize_interdependent_variables(session, tf.global_variables(), { obs_t_ph: obs_t_batch, obs_tp1_ph: obs_tp1_batch, }) model_initialized=True training_error_summ, _ = session.run([training_error_summ_sy, train_fn], feed_dict={obs_t_ph: obs_t_batch, act_t_ph: act_batch, rew_t_ph: rew_batch, obs_tp1_ph: obs_tp1_batch, done_mask_ph:done_mask_batch, transition_length_ph: transition_length_batch}) if t % 100: writer.add_summary(training_error_summ, t) writer.flush() if num_param_updates%target_update_freq == 0: session.run(update_target_fn) num_param_updates += 1 ##### ### 4. Log progress # episode_rewards = get_wrapper_by_name(env, "Monitor").get_episode_rewards() if done: episode_total_rewards.append(env.accumulated_reward()) episode_total_optimal_rewards.append(env.optimal_solution()[0]) episode_total_at_random_rewards.append(env.at_random_solution()[0]) if env.env_name == 'Knapsack': accuracies.append(np.mean(1 - np.abs(env.xs - env.optimal_solution()[1]))) if len(episode_total_rewards) > 0: mean_episode_reward = np.mean(np.array(episode_total_rewards)[-1000:]) mean_optimal_episode_reward = np.mean(np.array(episode_total_optimal_rewards)[-1000:]) mean_at_random_episode_reward = np.mean(np.array(episode_total_at_random_rewards)[-1000:]) if env.env_name == 'Knapsack': mean_accuracy = np.mean(np.array(accuracies)[-1000:]) if len(episode_total_rewards) > 1000: best_mean_episode_reward = max(best_mean_episode_reward, mean_episode_reward) if t % LOG_EVERY_N_STEPS == 0 and model_initialized: # Save the model saver.save(session, '/tmp/saved_models/' + log_files_name, global_step=t) # Display and log episode stats logz.log_tabular("Timestep", t) logz.log_tabular("AtRandomAverageReturn", mean_at_random_episode_reward) logz.log_tabular("AverageReturn", mean_episode_reward) logz.log_tabular("OptimalAverageReturn", mean_optimal_episode_reward) if env.env_name == 'Knapsack': logz.log_tabular("AverageAccuracy", mean_accuracy) logz.log_tabular("MaxReturn", best_mean_episode_reward) logz.log_tabular("Episodes", len(episode_total_rewards)) logz.log_tabular("Exploration", exploration.value(t)) logz.dump_tabular() sys.stdout.flush()
def attention_layer(from_tensor, to_tensor, attention_mask=None, num_attention_heads=1, size_per_head=512, query_act=None, key_act=None, value_act=None, attention_probs_dropout_prob=0.0, initializer_range=0.02, do_return_2d_tensor=False, batch_size=None, from_seq_length=None, to_seq_length=None): """Performs multi-headed attention from `from_tensor` to `to_tensor`. This is an implementation of multi-headed attention based on "Attention is all you Need". If `from_tensor` and `to_tensor` are the same, then this is self-attention. Each timestep in `from_tensor` attends to the corresponding sequence in `to_tensor`, and returns a fixed-with vector. This function first projects `from_tensor` into a "query" tensor and `to_tensor` into "key" and "value" tensors. These are (effectively) a list of tensors of length `num_attention_heads`, where each tensor is of shape [batch_size, seq_length, size_per_head]. Then, the query and key tensors are dot-producted and scaled. These are softmaxed to obtain attention probabilities. The value tensors are then interpolated by these probabilities, then concatenated back to a single tensor and returned. In practice, the multi-headed attention are done with transposes and reshapes rather than actual separate tensors. Args: from_tensor: float Tensor of shape [batch_size, from_seq_length, from_width]. to_tensor: float Tensor of shape [batch_size, to_seq_length, to_width]. attention_mask: (optional) int32 Tensor of shape [batch_size, from_seq_length, to_seq_length]. The values should be 1 or 0. The attention scores will effectively be set to -infinity for any positions in the mask that are 0, and will be unchanged for positions that are 1. num_attention_heads: int. Number of attention heads. size_per_head: int. Size of each attention head. query_act: (optional) Activation function for the query transform. key_act: (optional) Activation function for the key transform. value_act: (optional) Activation function for the value transform. attention_probs_dropout_prob: (optional) float. Dropout probability of the attention probabilities. initializer_range: float. Range of the weight initializer. do_return_2d_tensor: bool. If True, the output will be of shape [batch_size * from_seq_length, num_attention_heads * size_per_head]. If False, the output will be of shape [batch_size, from_seq_length, num_attention_heads * size_per_head]. batch_size: (Optional) int. If the input is 2D, this might be the batch size of the 3D version of the `from_tensor` and `to_tensor`. from_seq_length: (Optional) If the input is 2D, this might be the seq length of the 3D version of the `from_tensor`. to_seq_length: (Optional) If the input is 2D, this might be the seq length of the 3D version of the `to_tensor`. Returns: float Tensor of shape [batch_size, from_seq_length, num_attention_heads * size_per_head]. (If `do_return_2d_tensor` is true, this will be of shape [batch_size * from_seq_length, num_attention_heads * size_per_head]). Raises: ValueError: Any of the arguments or tensor shapes are invalid. """ def transpose_for_scores(input_tensor, batch_size, num_attention_heads, seq_length, width): output_tensor = tf.reshape( input_tensor, [batch_size, seq_length, num_attention_heads, width]) output_tensor = tf.transpose(output_tensor, [0, 2, 1, 3]) return output_tensor from_shape = get_shape_list(from_tensor, expected_rank=[2, 3]) to_shape = get_shape_list(to_tensor, expected_rank=[2, 3]) if len(from_shape) != len(to_shape): raise ValueError( "The rank of `from_tensor` must match the rank of `to_tensor`.") if len(from_shape) == 3: batch_size = from_shape[0] from_seq_length = from_shape[1] to_seq_length = to_shape[1] elif len(from_shape) == 2: if (batch_size is None or from_seq_length is None or to_seq_length is None): raise ValueError( "When passing in rank 2 tensors to attention_layer, the values " "for `batch_size`, `from_seq_length`, and `to_seq_length` " "must all be specified.") # Scalar dimensions referenced here: # B = batch size (number of sequences) # F = `from_tensor` sequence length # T = `to_tensor` sequence length # N = `num_attention_heads` # H = `size_per_head` from_tensor_2d = reshape_to_matrix(from_tensor) to_tensor_2d = reshape_to_matrix(to_tensor) # `query_layer` = [B*F, N*H] query_layer = tf.layers.dense( from_tensor_2d, num_attention_heads * size_per_head, activation=query_act, name="query", kernel_initializer=create_initializer(initializer_range)) # `key_layer` = [B*T, N*H] key_layer = tf.layers.dense( to_tensor_2d, num_attention_heads * size_per_head, activation=key_act, name="key", kernel_initializer=create_initializer(initializer_range)) # `value_layer` = [B*T, N*H] value_layer = tf.layers.dense( to_tensor_2d, num_attention_heads * size_per_head, activation=value_act, name="value", kernel_initializer=create_initializer(initializer_range)) # `query_layer` = [B, N, F, H] query_layer = transpose_for_scores(query_layer, batch_size, num_attention_heads, from_seq_length, size_per_head) # `key_layer` = [B, N, T, H] key_layer = transpose_for_scores(key_layer, batch_size, num_attention_heads, to_seq_length, size_per_head) # Take the dot product between "query" and "key" to get the raw # attention scores. # `attention_scores` = [B, N, F, T] attention_scores = tf.matmul(query_layer, key_layer, transpose_b=True) attention_scores = tf.multiply(attention_scores, 1.0 / math.sqrt(float(size_per_head))) if attention_mask is not None: # `attention_mask` = [B, 1, F, T] attention_mask = tf.expand_dims(attention_mask, axis=[1]) # Since attention_mask is 1.0 for positions we want to attend and 0.0 for # masked positions, this operation will create a tensor which is 0.0 for # positions we want to attend and -10000.0 for masked positions. adder = (1.0 - tf.cast(attention_mask, tf.float32)) * -10000.0 # Since we are adding it to the raw scores before the softmax, this is # effectively the same as removing these entirely. attention_scores += adder # Normalize the attention scores to probabilities. # `attention_probs` = [B, N, F, T] attention_probs = tf.nn.softmax(attention_scores) # This is actually dropping out entire tokens to attend to, which might # seem a bit unusual, but is taken from the original Transformer paper. attention_probs = dropout(attention_probs, attention_probs_dropout_prob) # `value_layer` = [B, T, N, H] value_layer = tf.reshape( value_layer, [batch_size, to_seq_length, num_attention_heads, size_per_head]) # `value_layer` = [B, N, T, H] value_layer = tf.transpose(value_layer, [0, 2, 1, 3]) # `context_layer` = [B, N, F, H] context_layer = tf.matmul(attention_probs, value_layer) # `context_layer` = [B, F, N, H] context_layer = tf.transpose(context_layer, [0, 2, 1, 3]) if do_return_2d_tensor: # `context_layer` = [B*F, N*H] context_layer = tf.reshape( context_layer, [batch_size * from_seq_length, num_attention_heads * size_per_head]) else: # `context_layer` = [B, F, N*H] context_layer = tf.reshape( context_layer, [batch_size, from_seq_length, num_attention_heads * size_per_head]) return context_layer
def _build_graph(self, inputs): """This function should build the model which takes the input variables and define self.cost at the end""" # inputs contains a list of input variables defined above image, label = inputs # In tensorflow, inputs to convolution function are assumed to be # NHWC. Add a single channel here. image = tf.expand_dims(image, 3) image = image * 2 - 1 # center the pixels values at zero def activate(x): return tf.nn.relu(x) # The context manager `argscope` sets the default option for all the layers under # this context. Here we use 32 channel convolution with shape 3x3 with argscope(BatchNorm, decay=0.9, epsilon=1e-4), \ argscope(Conv2D, kernel_shape=3, nl=tf.identity, out_channel=8): logits = (LinearWrap(image).Conv2D( 'conv0').BatchNorm('bn0').apply(activate).MaxPooling( 'pool0', 2).Conv2D('conv1').BatchNorm('bn1').apply(activate).Conv2D( 'conv2').BatchNorm('bn2').apply(activate).MaxPooling( 'pool1', 2).Conv2D('conv3').BatchNorm('bn3').apply( activate).FullyConnected( 'fc0', 512, nl=tf.nn.relu).Dropout( 'dropout', 0.5).FullyConnected('fc1', out_dim=10, nl=tf.identity)()) prob = tf.nn.softmax(logits, name='prob') # a Bx10 with probabilities # a vector of length B with loss of each sample cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) cost = tf.reduce_mean( cost, name='cross_entropy_loss') # the average cross-entropy loss # compute the "incorrect vector", for the callback ClassificationError to use at validation time wrong = symbf.prediction_incorrect(logits, label, name='incorrect') accuracy = symbf.accuracy(logits, label, name='accuracy') # This will monitor training error (in a moving_average fashion): # 1. write the value to tensosrboard # 2. write the value to stat.json # 3. print the value after each epoch train_error = tf.reduce_mean(wrong, name='train_error') summary.add_moving_summary(train_error, accuracy) # Use a regex to find parameters to apply weight decay. # Here we apply a weight decay on all W (weight matrix) of all fc layers wd_cost = tf.multiply(1e-5, regularize_cost('fc.*/W', tf.nn.l2_loss), name='regularize_loss') self.cost = tf.add_n([wd_cost, cost], name='total_cost') summary.add_moving_summary(cost, wd_cost, self.cost) # monitor histogram of all weight (of conv and fc layers) in tensorboard summary.add_param_summary(('.*/W', ['histogram', 'rms']))
def test(batch_size, num_test, epoch_id, lstm_mod, html_type): #share placeholders keep_prob = tf.placeholder(tf.float32, name='keep_prob_placeholder') y = tf.placeholder(tf.float32, [ None, ], name='label_placeholder') #image placeholders x = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], name='image_placeholder') vr_type = tf.placeholder(tf.float32, [None, type_num], name='type_placeholder') #text placeholders title = tf.placeholder(tf.int32, (None, None)) title_len = tf.placeholder(tf.int32, (None)) snippet = tf.placeholder(tf.int32, (None, None)) snippet_len = tf.placeholder(tf.int32, (None)) session_title = tf.placeholder(tf.int32, (None, None)) sess_len_title = tf.placeholder(tf.int32, (None)) session_snippet = tf.placeholder(tf.int32, (None, None)) sess_len_snippet = tf.placeholder(tf.int32, (None)) sessions_weight_snippet = tf.placeholder( tf.float32, [None, sess_sen_len_snippet, feature_dim]) attention_title = tf.placeholder(tf.float32, [None, max_title_len_top, feature_dim]) attention_snippet = tf.placeholder( tf.float32, [None, max_snippet_len_top, feature_dim]) #html placeholders html_tag = tf.placeholder(tf.int32, [None, html_dim], name='tag_placeholder') html_class = tf.placeholder(tf.int32, [None, html_dim], name='class_placeholder') #with tf.name_scope('image'): image_placeholders = [x, vr_type, keep_prob] pred_image = image(image_placeholders) #with tf.name_scope('title'): title_placeholders = [ title, title_len, attention_title, session_title, sess_len_title ] pred_title = text(title_placeholders, 'title') #with tf.name_scope('snippet'): snippet_placeholders = [ snippet, snippet_len, attention_snippet, session_snippet, sess_len_snippet, sessions_weight_snippet ] pred_snippet = text(snippet_placeholders, 'snippet') #with tf.name_scope('html'): html_placeholders = [html_tag, html_class] pred_html = html(html_placeholders) pred_combine = tf.squeeze( tf.concat([pred_image, pred_title, pred_snippet, pred_html], 1)) balance_raw = tf.Variable(tf.ones([4]), name='balance', trainable=True) balance_sum = tf.reduce_sum(balance_raw) balance = tf.div(balance_raw, balance_sum) pred_final = tf.reduce_sum(tf.multiply(pred_combine, balance), 1) with tf.name_scope("loss"): sigmoid_cross_entropy = cross_entropy(labels=tf.squeeze(y), logits=pred_final) loss_cross_entropy = tf.reduce_mean(sigmoid_cross_entropy, name='loss_cross_entropy') loss_mse = tf.reduce_mean(tf.square(pred_final - tf.squeeze(y))) loss = loss_mse sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore( sess, model_base + 'checkpoint/JRE/model_JRE_epoch_' + epoch_id + '.ckpt') test_dataset = '201709' tvt_file = data_base + '201709/info_top_10_id_201709' images_test, rels_test, num_data_test = set_data_image( tvt_file, test_dataset) type_test = set_data_type(tvt_file) titles_test, snippets_test, rels_test, queries_test, num_data_test = set_data_text( 'text', tvt_file) sess_title_test, sessions_weight_title_test = set_data_sess( 'title', tvt_file, test_dataset) sess_snippet_test, sessions_weight_snippet_test = set_data_sess( 'snippet', tvt_file, test_dataset) DFS_tag_test, DFS_class_test, BFS_tag_test, BFS_class_test, rels_test, num_data_test = set_data_html( tvt_file, test_dataset) print('test data num:{}'.format(num_data_test)) if num_test == 'all': num_test = num_data_test else: num_test = int(num_test) dropout_rate_test = 1 print("{} Start testing...".format(datetime.now())) loss_total = 0. pred_all, pred_combine_all, label_all = [], [], [] iters = num_test / batch_size print('Start......') start = time.time() for iter in xrange(iters): ind = set_random_ind(num_data_test, batch_size, random=False, iter_=iter) pic_input, label_input = data_batch_image(images_test, rels_test, num_data_test, batch_size, ind) type_input = data_batch_type(type_test, batch_size, ind) title_input, title_len_input, label_input, attention_title_input = data_batch_text( titles_test, queries_test, window_weight, rels_test, num_data_test, batch_size, max_title_len_top, ind) snippet_input, snippet_len_input, label_input, attention_snippet_input = data_batch_text( snippets_test, queries_test, window_weight, rels_test, num_data_test, batch_size, max_snippet_len_top, ind) sess_title_input, sess_title_len_input, label_input, attention_sess_title_input = data_batch_text( sess_title_test, queries_test, window_weight, rels_test, num_data_test, batch_size, sess_sen_len_title, ind) sess_snippet_input, sess_snippet_len_input, label_input, attention_sess_snippet_input = data_batch_text( sess_snippet_test, queries_test, window_weight, rels_test, num_data_test, batch_size, sess_sen_len_snippet, ind) sessions_weight_snippet_input = sess_weight_batch( 'snippet', batch_size, sessions_weight_snippet_test, ind) if html_type == 'DFS': tag_input, label_input = data_batch_html(DFS_tag_test, rels_test, ind) class_input, label_input = data_batch_html(DFS_class_test, rels_test, ind) elif html_type == 'BFS': tag_input, label_input = data_batch_html(BFS_tag_test, rels_test, ind) class_input, label_input = data_batch_html(BFS_class_test, rels_test, ind) pred_final_, pred_combine_, loss_, loss_cross_entropy_, loss_mse_, balance_ = sess.run( [ pred_final, pred_combine, loss, loss_cross_entropy, loss_mse, balance ], feed_dict={ y: label_input, keep_prob: dropout_rate, x: pic_input, vr_type: type_input, title: title_input, title_len: title_len_input, session_title: sess_title_input, sess_len_title: sess_title_len_input, attention_title: attention_title_input, snippet: snippet_input, snippet_len: snippet_len_input, session_snippet: sess_snippet_input, sess_len_snippet: sess_snippet_len_input, sessions_weight_snippet: sessions_weight_snippet_input, attention_snippet: attention_snippet_input, html_tag: tag_input, html_class: class_input }) loss_total += loss_ * batch_size pred_all.append(pred_final_) pred_combine_all.append(pred_combine_) label_all.append(label_input) end = time.time() print('Total Time:{}'.format(end - start)) print('average loss: {}'.format(loss_total * 1.0 / iters / batch_size)) pred_all = np.squeeze(np.concatenate((np.array(pred_all)), axis=0)) label_all = np.squeeze(np.concatenate((np.array(label_all)), axis=0)) fusion_file = open( result_base + 'JRE_' + test_dataset + '_' + epoch_id + '.txt', 'w') for i in range(iters * batch_size): fusion_file.write(images_test[i].split('/')[-1] + '\t' + str(label_all[i]) + '\t' + str(pred_all[i]) + '\n')
def build_loss(output_prob, y, weight=None): y = tf.py_func(one_hot, [y], tf.float32) los = -tf.reduce_mean(tf.multiply(tf.log(tf.clip_by_value(output_prob, 1e-10, 1.0)), y)) return los
def __init__(self, params, hidden_weights=None): self.params = params self.network_shape = self.params['network_shape'] self.input_dim = self.network_shape[0] self.output_dim = self.network_shape[-1] self.batch_size = self.params['batch_size'] self.hidden_weights = hidden_weights # self.weights = self.initialize_weights() # self.mirror_weights = self.initialize_mirror_weights() # self.readout_weights = self.initialize_readout_weights() # self.hs = self.initialize_hs() # self.hidden_states = self.initialize_hidden_states() self.tensorboard_dir = self.params['tensorboard_dir'] self.activation_function = self.params['activation_function'] self.optimizer_ = self.params['optimizer'] # model self.input = tf.placeholder(tf.float32, [None, self.input_dim], name="input") self.output = tf.placeholder(tf.float32, [None, self.output_dim], name="output") self.activation_patterns = {} self.hidden_state_activation_patterns = {} self.activation = self.input self.hidden_states = {} self.hidden_states_update_ops = {} for i in range(1, len(self.network_shape) - 1): with tf.name_scope("layer{0}".format(i)): h = tf.Variable(tf.truncated_normal([self.network_shape[i]]), name="hidden_state", trainable=False) # h = tf.truncated_normal([self.batch_size, self.network_shape[i]]) self.hidden_states["hs_{0}".format(i)] = h Utils.variable_summaries( self.hidden_states["hs_{0}".format(i)], "hs_{0}".format(i)) if self.hidden_weights is not None: H_tune = tf.Variable(1.0, trainable=True, name="H_tune") Utils.variable_summaries(H_tune, "H_tune") else: H_tune = tf.Variable(1, trainable=False, name="H_tune") for i in range(len(self.network_shape) - 1): with tf.name_scope("layer{0}".format(i + 1)): if i < len(self.network_shape) - 2: with tf.name_scope("hidden"): # input weight and bias W = tf.Variable(tf.random_normal( [self.network_shape[i], self.network_shape[i + 1]], stddev=0.05), name="W") bW = tf.Variable(tf.random_normal( [self.network_shape[i + 1]], stddev=0.05), name="bW") Utils.variable_summaries(W, "W") Utils.variable_summaries(bW, "bW") H_name = "H_{0}".format(i + 1) if self.hidden_weights is not None and H_name in self.hidden_weights.keys( ): H = tf.Variable( self.hidden_weights[H_name].astype('float32'), dtype=tf.float32, trainable=False, name="H") else: H = tf.Variable(tf.random_normal([ self.network_shape[i + 1], self.network_shape[i + 1] ], stddev=0.05), trainable=False, name="H") input_for_hidden = tf.matmul(self.activation, W) + bW tiled_h = tf.reshape( tf.tile(self.hidden_states["hs_{0}".format(i + 1)], [self.batch_size]), [self.batch_size, -1]) hidden_update = tf.nn.tanh( tf.add( input_for_hidden, tf.matmul(tiled_h, tf.scalar_mul(H_tune, H)))) with tf.name_scope("mirror"): # mirror input and bias M = tf.Variable(tf.random_normal( [self.network_shape[i], self.network_shape[i + 1]], stddev=0.05), name="M") bM = tf.Variable(tf.random_normal( [self.network_shape[i + 1]], stddev=0.05), name="bM") Utils.variable_summaries(M, "M") Utils.variable_summaries(bM, "bM") input_for_mirror = tf.nn.tanh( tf.matmul(self.activation, M) + bM) with tf.name_scope("readout"): # readout weights and biases R = tf.Variable(tf.random_normal([ self.network_shape[i + 1], self.network_shape[i + 1] ], stddev=0.05), name="R") bR = tf.Variable(tf.random_normal( [self.network_shape[i + 1]], stddev=0.05), name="bR") Utils.variable_summaries(R, "R") Utils.variable_summaries(bR, "bR") readout = self.activation_function( tf.matmul(hidden_update, R) + bR) with tf.name_scope("activation"): self.activation = self.activation_function( tf.multiply(readout, input_for_mirror)) # self.hidden_state_activation_patterns['hidden_state_layer_{0}'.format(i + 1)] = self.hidden_states[self.hidden_states["hs_{0}".format(i+1)]] self.hidden_states_update_ops["hs_{0}".format( i + 1)] = self.hidden_states["hs_{0}".format( i + 1)].assign(hidden_update[0]) # self.hidden_states["hs_{0}".format(i+1)] = hidden_update else: W = tf.Variable(tf.random_normal( [self.network_shape[i], self.network_shape[i + 1]], stddev=0.05), name="W") bW = tf.Variable(tf.random_normal( [self.network_shape[i + 1]], stddev=0.05), name="bW") Utils.variable_summaries(W, "W") Utils.variable_summaries(bW, "bW") with tf.name_scope("activation"): self.activation = self.activation_function( tf.matmul(self.activation, W) + bW) act = self.activation if i > 0: self.activation_patterns['layer_{0}'.format(i)] = act # cost with tf.name_scope("cost"): self.cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( self.activation, self.output)) tf.summary.scalar('cost', self.cost) with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(self.activation, 1), tf.argmax(self.output, 1)) self.accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('accuracy', self.accuracy) self.optimizer = self.optimizer_.minimize(self.cost) self.sess = tf.Session() self.merged = tf.summary.merge_all() self.summ_writer = tf.summary.FileWriter(self.tensorboard_dir, self.sess.graph) init = tf.global_variables_initializer() self.sess.run(init)
def text(text_placeholders, text_type): if text_type == 'title': [text, text_len, attention_text, session, sess_len] = text_placeholders elif text_type == 'snippet': [text, text_len, attention_text, session, sess_len, sessions_weight] = text_placeholders voca = load_voca() voca_embed = tf.Variable(voca, trainable=True, dtype=tf.float32, name='voca_embed_' + text_type) text_embed = tf.nn.embedding_lookup(voca_embed, text) sess_embed = tf.nn.embedding_lookup(voca_embed, session) text_outputs, text_last_states = text_net(text_type, text_len, text_embed) if lstm_mode == 'mean': text_output = tf.multiply(text_outputs, attention_text) text_output = tf.reduce_mean(text_output, 1) elif lstm_mode == 'final': text_output = [0] * batch_size for i in range(batch_size): text_output[i] = text_outputs[i, text_len[i] - 1, :] text_output = tf.stack(text_output) text_feature_lstm = text_output sess_outputs, sess_last_states = text_net('sess_' + text_type, sess_len, sess_embed) if lstm_mode == 'mean': if text_type == 'title': sess_output = tf.reduce_mean(sess_outputs, 1) elif text_type == 'snippet': sess_output = tf.multiply(sess_outputs, sessions_weight) sess_output = tf.reduce_mean(sess_output, 1) elif lstm_mode == 'final': sess_output = [0] * batch_size for i in range(batch_size): sess_output[i] = sess_outputs[i, sess_len[i] - 1, :] sess_output = tf.stack(sess_output) sess_feature_lstm = sess_output print('text net seted') cls1_text = fc(text_feature_lstm, 1000, 1000, name='cls1_' + text_type) cls1_text_dropout = tf.nn.dropout(cls1_text, dropout_rate, name='cls1_dropout_' + text_type) text_feature = cls1_text_dropout cls1_sess = fc(sess_feature_lstm, 1000, 1000, name='cls1_sess_' + text_type) cls1_sess_dropout = tf.nn.dropout(cls1_sess, dropout_rate, name='cls1_dropout_sess_' + text_type) sess_feature = cls1_sess_dropout text_output_norm = tf.sqrt(tf.reduce_sum(tf.square(text_feature), 1)) sess_output_norm = tf.sqrt(tf.reduce_sum(tf.square(sess_feature), 1)) cosin = tf.divide( tf.reduce_sum(tf.multiply(text_feature, sess_feature), 1), tf.multiply(text_output_norm, sess_output_norm)) pred_text = tf.expand_dims(tf.nn.sigmoid(cosin), 1) return pred_text
def train_graph(game, player, display_on, inp, out, trained_steps): # define variables argmax = tf.placeholder("float", [None, ACTIONS]) ground_truth = tf.placeholder("float", [None]) global_step = tf.Variable(0, name='global_step') action = tf.reduce_sum(tf.multiply(out, argmax), reduction_indices = 1) cost = tf.reduce_mean(tf.square(action - ground_truth)) train_step = tf.train.AdamOptimizer(1e-6).minimize(cost) replay_memory = deque() # get, intial frame from 'Pong', process image, and stack frames frame = game.get_initial_frame(display_on) frame = cv2.cvtColor(cv2.resize(frame, (60, 60)), cv2.COLOR_BGR2GRAY) _, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY) inp_t = np.stack((frame, frame, frame, frame), axis = 2) # saver and session manager saver = tf.train.Saver(tf.global_variables(), max_to_keep=None) session = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True)) checkpoint_path = "./checkpoint_" + trained_steps checkpoint = tf.train.latest_checkpoint(checkpoint_path) if checkpoint != None: saver.restore(session, checkpoint) else: init = tf.global_variables_initializer() session.run(init) t = global_step.eval() c = 0 epsilon = INITIAL_EPSILON avg_max_q = 0 # main training loop while(1): out_t = out.eval(feed_dict = {inp : [inp_t]})[0] # output tensor argmax_t = np.zeros([ACTIONS]) # argmax tensor reward_t = 0 # reward tensor # choose action to take (random if epsilon) if(random.random() <= epsilon and not USE_MODEL): maxIndex = choice((0,1,2), 1, p=(0.9, 0.05, 0.05)) # make 0 the most choosen action for realistic randomness else: maxIndex = np.argmax(out_t) # set action to take argmax_t[maxIndex] = 1 # anneal epsilon according to cooling schedule if epsilon > FINAL_EPSILON: epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE_STEPS # get next frame (state) and reward from the resulting state if player == 1: reward_t, _, frame = game.get_next_frame(argmax_t, None, display_on) elif player == 2: _, reward_t, frame = game.get_next_frame(None, argmax_t, display_on) # process state frame = cv2.cvtColor(cv2.resize(frame, (60, 60)), cv2.COLOR_BGR2GRAY) _, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY) frame = np.reshape(frame, (60, 60, 1)) updated_inp_t = np.append(frame, inp_t[:, :, 0:3], axis = 2) # updated input tensor # add our input, argmax, reward, and updated input tensors to replay memory replay_memory.append((inp_t, argmax_t, reward_t, updated_inp_t)) # if we run out of replay memory, make room if len(replay_memory) > REPLAY_MEMORY_SIZE: replay_memory.popleft() # training update iteration if c > OBSERVE_STEPS and not USE_MODEL: # get values from our replay memory minibatch = random.sample(replay_memory, BATCH) inp_batch = [dim[0] for dim in minibatch] argmax_batch = [dim[1] for dim in minibatch] reward_batch = [dim[2] for dim in minibatch] updated_inp_t_batch = [dim[3] for dim in minibatch] ground_truth_batch = [] out_batch = out.eval(feed_dict = {inp : updated_inp_t_batch}) # add values to the batch for i in range(0, len(minibatch)): ground_truth_batch.append(reward_batch[i] + GAMMA * np.max(out_batch[i])) # train the model train_step.run(feed_dict = {ground_truth : ground_truth_batch, argmax : argmax_batch, inp : inp_batch}) # next frame inp_t = updated_inp_t t = t + 1 c = c + 1 # save model at set intervals if t % SAVE_STEP == 0 and not USE_MODEL: session.run(global_step.assign(t)) saver.save(session, './checkpoints/model.ckpt', global_step=t)
def train(batch_size, num_epochs, num_train, num_val, alpha_regularizer, lstm_mode, html_type): display_step = 10 filewriter_path = model_base + "tensorboard/" checkpoint_path = model_base + "checkpoint/" if os.path.exists(filewriter_path): shutil.rmtree(filewriter_path) os.makedirs(filewriter_path) if not os.path.isdir(checkpoint_path): os.makedirs(checkpoint_path) #share placeholders keep_prob = tf.placeholder(tf.float32, name='keep_prob_placeholder') y = tf.placeholder(tf.float32, [ None, ], name='label_placeholder') #image placeholders x = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], name='image_placeholder') vr_type = tf.placeholder(tf.float32, [None, type_num], name='type_placeholder') #text placeholders title = tf.placeholder(tf.int32, (None, None)) title_len = tf.placeholder(tf.int32, (None)) snippet = tf.placeholder(tf.int32, (None, None)) snippet_len = tf.placeholder(tf.int32, (None)) session_title = tf.placeholder(tf.int32, (None, None)) sess_len_title = tf.placeholder(tf.int32, (None)) session_snippet = tf.placeholder(tf.int32, (None, None)) sess_len_snippet = tf.placeholder(tf.int32, (None)) sessions_weight_snippet = tf.placeholder( tf.float32, [None, sess_sen_len_snippet, feature_dim]) attention_title = tf.placeholder(tf.float32, [None, max_title_len_top, feature_dim]) attention_snippet = tf.placeholder( tf.float32, [None, max_snippet_len_top, feature_dim]) #html placeholders html_tag = tf.placeholder(tf.int32, [None, html_dim], name='tag_placeholder') html_class = tf.placeholder(tf.int32, [None, html_dim], name='class_placeholder') #with tf.name_scope('image'): image_placeholders = [x, vr_type, keep_prob] pred_image = image(image_placeholders) #with tf.name_scope('title'): title_placeholders = [ title, title_len, attention_title, session_title, sess_len_title ] pred_title = text(title_placeholders, 'title') #with tf.name_scope('snippet'): snippet_placeholders = [ snippet, snippet_len, attention_snippet, session_snippet, sess_len_snippet, sessions_weight_snippet ] pred_snippet = text(snippet_placeholders, 'snippet') #with tf.name_scope('html'): html_placeholders = [html_tag, html_class] pred_html = html(html_placeholders) #fusion pred_combine = tf.squeeze( tf.concat([pred_image, pred_title, pred_snippet, pred_html], 1)) balance_raw = tf.Variable(tf.ones([4]), name='balance', trainable=True) #without XPN #pred_combine = tf.squeeze(tf.concat([pred_image, pred_title, pred_html], 1)) #balance_raw = tf.Variable(tf.ones([3]), name='balance', trainable=True) balance_sum = tf.reduce_sum(balance_raw) balance = tf.div(balance_raw, balance_sum) pred_final = tf.reduce_sum(tf.multiply(pred_combine, balance), 1) with tf.name_scope("loss"): regularizer = tf.contrib.layers.l2_regularizer(alpha_regularizer) loss_regularizer = tf.contrib.layers.apply_regularization( regularizer, tf.trainable_variables()) #sigmoid_cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels = tf.squeeze(y), logits = pred_final) sigmoid_cross_entropy = cross_entropy(labels=tf.squeeze(y), logits=pred_final) loss_cross_entropy = tf.reduce_mean(sigmoid_cross_entropy, name='loss_cross_entropy') loss_mse = tf.reduce_mean(tf.square(pred_final - tf.squeeze(y))) loss = loss_cross_entropy print('Get ready! We are going to print all the trainable vars.') var_list = [v for v in tf.trainable_variables()] for var in var_list: print(var.name) print('Ok, print done.') var_train_list = var_list with tf.name_scope("train"): gradients = tf.gradients(loss, var_train_list) #gradients, global_norm = tf.clip_by_global_norm(gradients, 1) gradients = list(zip(gradients, var_train_list)) #optimizer = tf.train.GradientDescentOptimizer(learning_rate) optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.apply_gradients(grads_and_vars=gradients) #train_op = optimizer.minimize(loss) for var in var_list: tf.summary.histogram(var.name, var) tf.summary.scalar('loss_regularizer_fusion', loss_regularizer) tf.summary.scalar('loss_cross_entropy_fusion', loss_cross_entropy) tf.summary.scalar('loss_mse_fusion', loss_mse) tf.summary.scalar('loss_fusion', loss) merged_summary = tf.summary.merge_all() writer = tf.summary.FileWriter(filewriter_path) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.InteractiveSession(config=config) sess.run(tf.global_variables_initializer()) writer.add_graph(sess.graph) images_vals, title_vars, snippet_vars, html_vars = [], [], [], [] for var in var_list: if var.name.find("title") != -1: print('title: ' + var.name) title_vars.append(var) elif var.name.find("snippet") != -1: print('snippet: ' + var.name) snippet_vars.append(var) elif var.name.find('html') != -1: print('html: ' + var.name) html_vars.append(var) elif var.name.find("balance") == -1: print('image: ' + var.name) images_vals.append(var) # saver_image = tf.train.Saver(images_vals) # saver_title = tf.train.Saver(title_vars) # saver_snippet = tf.train.Saver(snippet_vars) # saver_html = tf.train.Saver(html_vars) # saver_image.restore(sess, model_base+"checkpoint/VPN/model_image.ckpt") # print('image model successfully loaded!') # saver_title.restore(sess, model_base+"checkpoint/TSN/model_title.ckpt") # print('title model successfully loaded!') # saver_snippet.restore(sess, model_base+"checkpoint/SSN/model_snippet.ckpt") # print('snippet model successfully loaded!') # saver_html.restore(sess, model_base+'checkpoint/HSN/model_html.ckpt') # print('html model successfully loaded!') saver = tf.train.Saver(max_to_keep=20) train_dataset = val_dataset = '201709' train_path = data_base + '201709/info_top_10_id_201709' val_path = data_base + '201709/info_top_10_id_201709' images_train, rels_train, num_data_train = set_data_image( train_path, train_dataset) images_val, rels_val, num_data_val = set_data_image(val_path, val_dataset) type_train = set_data_type(train_path) type_val = set_data_type(val_path) titles_train, snippets_train, rels_train, queries_train, num_data_train = set_data_text( 'text', train_path) titles_val, snippets_val, rels_val, queries_val, num_data_val = set_data_text( 'text', val_path) sess_title_train, sessions_weight_title_train = set_data_sess( 'title', train_path, train_dataset) sess_snippet_train, sessions_weight_snippet_train = set_data_sess( 'snippet', train_path, train_dataset) sess_title_val, sessions_weight_title_val = set_data_sess( 'title', val_path, val_dataset) sess_snippet_val, sessions_weight_snippet_val = set_data_sess( 'snippet', val_path, val_dataset) DFS_tag_train, DFS_class_train, BFS_tag_train, BFS_class_train, rels_train, num_data_train = set_data_html( train_path, train_dataset) DFS_tag_val, DFS_class_val, BFS_tag_val, BFS_class_val, rels_val, num_data_val = set_data_html( val_path, val_dataset) print('train data num:{}'.format(num_data_train)) print('val data num:{}'.format(num_data_val)) print("{} Start training...".format(datetime.now())) print("{} Open Tensorboard at --logdir {}".format(datetime.now(), filewriter_path)) if num_train == 'all': num_train = num_data_train else: num_train = int(num_train) if num_val == 'all': num_val = num_data_val else: num_val = int(num_val) for epoch in range(num_epochs): print("{} Epoch number: {}".format(datetime.now(), epoch + 1)) step = 1 for iter in xrange(num_train / batch_size): ind = set_random_ind(num_data_train, batch_size, random=True) pic_input, label_input = data_batch_image(images_train, rels_train, num_data_train, batch_size, ind) type_input = data_batch_type(type_train, batch_size, ind) title_input, title_len_input, label_input, attention_title_input = data_batch_text( titles_train, queries_train, window_weight, rels_train, num_data_train, batch_size, max_title_len_top, ind) snippet_input, snippet_len_input, label_input, attention_snippet_input = data_batch_text( snippets_train, queries_train, window_weight, rels_train, num_data_train, batch_size, max_snippet_len_top, ind) sess_title_input, sess_title_len_input, label_input, attention_sess_title_input = data_batch_text( sess_title_train, queries_train, window_weight, rels_train, num_data_train, batch_size, sess_sen_len_title, ind) sess_snippet_input, sess_snippet_len_input, label_input, attention_sess_snippet_input = data_batch_text( sess_snippet_train, queries_train, window_weight, rels_train, num_data_train, batch_size, sess_sen_len_snippet, ind) sessions_weight_snippet_input = sess_weight_batch( 'snippet', batch_size, sessions_weight_snippet_train, ind) if html_type == 'DFS': tag_input, label_input = data_batch_html( DFS_tag_train, rels_train, ind) class_input, label_input = data_batch_html( DFS_class_train, rels_train, ind) elif html_type == 'BFS': tag_input, label_input = data_batch_html( BFS_tag_train, rels_train, ind) class_input, label_input = data_batch_html( BFS_class_train, rels_train, ind) train_op_, loss_, loss_cross_entropy_, loss_mse_, loss_regularizer_, merged_summary_, pred_final_, pred_combine_, balance_ = sess.run( [ train_op, loss, loss_cross_entropy, loss_mse, loss_regularizer, merged_summary, pred_final, pred_combine, balance ], feed_dict={ y: label_input, keep_prob: dropout_rate, x: pic_input, vr_type: type_input, title: title_input, title_len: title_len_input, session_title: sess_title_input, sess_len_title: sess_title_len_input, attention_title: attention_title_input, snippet: snippet_input, snippet_len: snippet_len_input, session_snippet: sess_snippet_input, sess_len_snippet: sess_snippet_len_input, sessions_weight_snippet: sessions_weight_snippet_input, attention_snippet: attention_snippet_input, html_tag: tag_input, html_class: class_input }) print( "the " + str(epoch + 1) + 'th epoch, ' + str(iter + 1) + 'th batch: loss:{} loss_cross_entropy:{} loss_mse:{} loss_regularizer:{}' .format(loss_, loss_cross_entropy_, loss_mse_, loss_regularizer_)) print(balance_) if step % display_step == 0: writer.add_summary(merged_summary_, epoch * num_train / batch_size + step) step += 1 dropout_rate_val = 1 print("{} Start validation...".format(datetime.now())) loss_total = 0. pred_all, label_all = [], [] iters = num_val / batch_size for iter in xrange(iters): ind = set_random_ind(num_data_val, batch_size, random=False, iter_=iter) pic_input, label_input = data_batch_image(images_val, rels_val, num_data_val, batch_size, ind) type_input = data_batch_type(type_val, batch_size, ind) title_input, title_len_input, label_input, attention_title_input = data_batch_text( titles_val, queries_val, window_weight, rels_val, num_data_val, batch_size, max_title_len_top, ind) snippet_input, snippet_len_input, label_input, attention_snippet_input = data_batch_text( snippets_val, queries_val, window_weight, rels_val, num_data_val, batch_size, max_snippet_len_top, ind) sess_title_input, sess_title_len_input, label_input, attention_sess_title_input = data_batch_text( sess_title_val, queries_val, window_weight, rels_val, num_data_val, batch_size, sess_sen_len_title, ind) sess_snippet_input, sess_snippet_len_input, label_input, attention_sess_snippet_input = data_batch_text( sess_snippet_val, queries_val, window_weight, rels_val, num_data_val, batch_size, sess_sen_len_snippet, ind) sessions_weight_snippet_input = sess_weight_batch( 'snippet', batch_size, sessions_weight_snippet_val, ind) if html_type == 'DFS': tag_input, label_input = data_batch_html( DFS_tag_val, rels_val, ind) class_input, label_input = data_batch_html( DFS_class_val, rels_val, ind) elif html_type == 'BFS': tag_input, label_input = data_batch_html( BFS_tag_val, rels_val, ind) class_input, label_input = data_batch_html( BFS_class_val, rels_val, ind) loss_, loss_cross_entropy_, loss_mse_, loss_regularizer_ = sess.run( [loss, loss_cross_entropy, loss_mse, loss_regularizer], feed_dict={ y: label_input, keep_prob: dropout_rate, x: pic_input, vr_type: type_input, title: title_input, title_len: title_len_input, session_title: sess_title_input, sess_len_title: sess_title_len_input, attention_title: attention_title_input, snippet: snippet_input, snippet_len: snippet_len_input, session_snippet: sess_snippet_input, sess_len_snippet: sess_snippet_len_input, sessions_weight_snippet: sessions_weight_snippet_input, attention_snippet: attention_snippet_input, html_tag: tag_input, html_class: class_input }) loss_total += loss_ * batch_size print( "the " + str(epoch + 1) + 'th epoch, ' + str(iter + 1) + 'th batch: loss:{} loss_cross_entropy:{} loss_mse:{} loss_regularizer:{}' .format(loss_, loss_cross_entropy_, loss_mse_, loss_regularizer_)) print('average loss: {}'.format(loss_total * 1.0 / iters / batch_size)) print("{} Saving checkpoint of model...".format(datetime.now())) checkpoint_name = os.path.join( checkpoint_path, 'model_JRE_epoch_' + str(epoch + 1) + '.ckpt') save_path = saver.save(sess, checkpoint_name) print("{} Model checkpoint saved at {}".format(datetime.now(), checkpoint_name))
import tensorflow as tf a = tf.placeholder(tf.int16) b = tf.placeholder(tf.int16) add = tf.add(a,b) mul = tf.multiply(a,b) matrixl = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrixl,matrix2) with tf.Session() as sess: result = sess.run(product) print(result)
def __init__(self, learning_rate=0.001, learning_rate_decay_step = 1000000, hidden_size=10, action_size = 4, history_size=4, name="Network"): with tf.variable_scope(name): # Set scope for copying purposes self.scope = name # Initializers conv_init = tf.contrib.layers.xavier_initializer_conv2d() init = tf.contrib.layers.xavier_initializer() # Store Variables self.inputs_ = tf.placeholder(tf.float32, [None, 88, 80, history_size], name='inputs') self.target_preds_ = tf.placeholder(tf.float32, [None,], name="expected_future_rewards") self.chosen_action_pred = tf.placeholder(tf.float32, [None,], name="chosen_action_pred") self.actions_ = tf.placeholder(tf.int32, shape=[None], name='actions') self.avg_max_Q_ = tf.placeholder(tf.float32, name="avg_max_Q") self.reward_ = tf.placeholder(tf.float32, name="reward") self.learning_rate_step = tf.placeholder('int64', None, name='learning_rate_step') # Normalizing the input self.inputs_scaled_ = self.inputs_/255.0 # Three Convolutional Layers self.conv1 = tf.layers.conv2d( inputs = self.inputs_scaled_, filters = 16, kernel_size = [8,8], strides = [4,4], padding = "VALID", kernel_initializer=conv_init, activation=tf.nn.relu) self.conv2 = tf.layers.conv2d( inputs = self.conv1, filters = 8, kernel_size = [4,4], strides = [2,2], padding = "VALID", kernel_initializer=conv_init, activation=tf.nn.relu) # self.conv3 = tf.layers.conv2d( # inputs = self.conv2, # filters = 128, # kernel_size = [4,4], # strides = [2,2], # padding = "VALID", # kernel_initializer=conv_init, # activation=tf.nn.relu) # Fully Connected Layers self.flatten = tf.contrib.layers.flatten(self.conv2) self.fc1 = tf.layers.dense(self.flatten, hidden_size, activation=tf.nn.relu, kernel_initializer=init) self.predictions = tf.layers.dense(self.fc1, action_size, activation=None, kernel_initializer=init) # Get Prediction for the chosen action (epsilon greedy) self.action_one_hot = tf.one_hot(self.actions_, action_size, 1.0, 0.0, name='action_one_hot') self.chosen_action_pred = tf.reduce_sum(tf.multiply(self.predictions, self.action_one_hot), axis=1) # Calculate Loss self.losses = tf.losses.huber_loss(self.target_preds_, self.chosen_action_pred) self.loss = tf.reduce_mean(self.losses) # Adjust Network self.learn = tf.train.MomentumOptimizer(learning_rate, momentum=0.95, use_nesterov=True).minimize(self.loss) # For Tensorboard with tf.name_scope("summaries"): tf.summary.scalar("loss", self.loss) tf.summary.scalar("avg_max_Q", self.avg_max_Q_) tf.summary.scalar("reward", self.reward_) self.summary_op = tf.summary.merge_all()
return data print("Use different loss func") data = data2Arr(DATA_FILE) print(len(data[0])) # life expectancy, birth rate = X, Y X, Y = tf.placeholder(tf.float32, name = 'X' ), tf.placeholder(tf.float32, name = 'Y' ) w, b = tf.Variable(0.0, name = 'w'), tf.Variable(0.0, name = 'b') # both scalars for simplicity Y_predicted = tf.add(tf.multiply(X,w), b, name = "Y_predicted") loss = tf.square(Y - Y_predicted, name='loss') optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss) start = time.time() # print(data[1]) with tf.Session() as sess: tf.initialize_all_variables().run() # tensorboard board = tf.summary.FileWriter('./graphs', sess.graph)
def add_loss_op(self, q, target_q): """ Sets the loss of a batch, self.loss is a scalar Args: q: (tf tensor) shape = (batch_size, num_actions) target_q: (tf tensor) shape = (batch_size, num_actions) """ # you may need this variable num_actions = self.env.action_space.n ############################################################## """ TODO: The loss for an example is defined as: Q_samp(s) = r if done = r + gamma * max_a' Q_target(s', a') loss = (Q_samp(s) - Q(s, a))^2 You need to compute the average of the loss over the minibatch and store the resulting scalar into self.loss HINT: - config variables are accessible through self.config - you can access placeholders like self.a (for actions) self.r (rewards) or self.done_mask for instance - target_q is the q-value evaluated at the s' states (the next states) - you may find the following functions useful - tf.cast - tf.reduce_max / reduce_sum - tf.one_hot - ... (be sure that you set self.loss) obs_batch: np.array Array of shape (batch_size, img_h, img_w, img_c * frame_history_len) and dtype np.uint8 act_batch: np.array Array of shape (batch_size,) and dtype np.int32 rew_batch: np.array Array of shape (batch_size,) and dtype np.float32 next_obs_batch: np.array Array of shape (batch_size, img_h, img_w, img_c * frame_history_len) and dtype np.uint8 done_mask: np.array Array of shape (batch_size,) and dtype np.float32 """ ############################################################## ##################### YOUR CODE HERE - 4-5 lines ############# # batch_size = self.config.batch_size # rows = tf.range(0, batch_size) # a_idx = tf.stack([rows, tf.cast(self.a, dtype=tf.int32)], axis=1) # ones = tf.ones([batch_size]) # loss_agg = (self.r + self.config.gamma * tf.reduce_max(target_q, axis=1) \ # - (ones - self.done_mask) * tf.gather_nd(q, a_idx)) ** 2 a_one_hot = tf.one_hot(self.a, depth=num_actions) loss_agg = self.r + \ (1. - self.done_mask) * self.config.gamma * tf.reduce_max(target_q, axis=1) - \ tf.reduce_sum(tf.multiply(q, a_one_hot), axis=1) loss = tf.reduce_mean(loss_agg**2) # self.loss = loss return loss
def run_model(self): # Run model # conv = tf.layers.conv2d(inputs=self.input_data, filters=32, kernel_size=(3,3), padding='same', activation=tf.nn.relu) # (64,64,128) # upsample = tf.image.resize_images(conv, size=(128,128), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) # (128,128,128) output = tf.layers.conv2d(inputs=self.input_data, filters=3, kernel_size=(3,3), padding='same', activation=tf.nn.relu, name='filters') self.output = tf.multiply(tf.nn.relu(tf.reshape(output, [par['batch_train_size'],par['n_output']])),1,name='output') # (128,128,16)
def call(self, x): temp = Lambda(lambda y: K.dot(y, self.kernel))(x[1]) temp = Lambda(lambda y: tf.expand_dims(y, 1))(temp) temp = Lambda(lambda y: tf.tile(y, multiples=[1, self.output_len, 1]))( temp) return Lambda(lambda y: tf.multiply(x[0], y))(temp)
def build_input(features, params): cat_columns = params['cat_columns'] val_columns = params['val_columns'] column_to_field = params['column_to_field'] #dnn_columns = params['dnn_columns'] dimension_config = params['dimension_config'] reg = params['reg'] embed_dim = params['embed_dim'] embedding_table = EmbeddingTable() embedding_dict = OrderedDict() with tf.variable_scope("fm", reuse=tf.AUTO_REUSE, values=[features]) as scope: with tf.device('/cpu:0'): for name, col in cat_columns.items(): field = column_to_field.get(name, name) cur_dimension = dimension_config[ field] if field in dimension_config else embed_dim embedding_table.add_linear_weights(vocab_name=name, vocab_size=col._num_buckets) embedding_table.add_embed_weights(vocab_name=field, vocab_size=col._num_buckets, embed_dim=cur_dimension, reg=reg) for name, col in val_columns.items(): field = column_to_field.get(name, name) cur_dimension = dimension_config[ field] if field in dimension_config else embed_dim embedding_table.add_linear_weights(vocab_name=name, vocab_size=1) embedding_table.add_embed_weights(vocab_name=field, vocab_size=1, embed_dim=cur_dimension, reg=reg) builder = _LazyBuilder(features) # linear part linear_outputs = [] for name, col in cat_columns.items(): # get sparse tensor of input feature from feature column sp_tensor = col._get_sparse_tensors(builder) sp_ids = sp_tensor.id_tensor linear_weights = embedding_table.get_linear_weights(name) # linear_weights: (vocab_size, 1) # sp_ids: (batch_size, max_tokens_per_example) # sp_values: (batch_size, max_tokens_per_example) linear_output = embedding_ops.safe_embedding_lookup_sparse( linear_weights, sp_ids, None, combiner='sum', name='{}_linear_output'.format(name)) linear_outputs.append(linear_output) for name, col in val_columns.items(): dense_tensor = col._get_dense_tensor(builder) linear_weights = embedding_table.get_linear_weights(name) linear_output = tf.multiply(dense_tensor, linear_weights) linear_outputs.append(linear_output) # linear_outputs: (batch_szie, nonzero_feature_num) linear_outputs = tf.concat(linear_outputs, axis=1) # poly part for name, col, in cat_columns.items(): # get sparse tensor of input feature from feature column field = column_to_field.get(name, name) sp_tensor = col._get_sparse_tensors(builder) sp_ids = sp_tensor.id_tensor embed_weights = embedding_table.get_embed_weights(field) # embeddings: (batch_size, embed_dim) # x_i * v_i embeddings = embedding_ops.safe_embedding_lookup_sparse( embed_weights, sp_ids, None, combiner='sum', name='{}_{}_embedding'.format(field, name)) embedding_dict[field] = embeddings for name, col in val_columns.items(): field = column_to_field.get(name, name) dense_tensor = col._get_dense_tensor(builder) embed_weights = embedding_table.get_embed_weights(field) embeddings = tf.multiply(dense_tensor, embed_weights) embedding_dict[field] = embeddings with tf.variable_scope("dnn_embed"): #embed_inputs = tf.concat(sum_then_square, axis=1) x = tf.concat(list(embedding_dict.values()), axis=1) N = len(embedding_dict) T = sum([ embedding.get_shape().as_list()[1] for embedding in embedding_dict.values() ]) print("wkfm N:", N, " T:", T) indices = [] for i, embeddings in enumerate(embedding_dict.values()): dim = embeddings.get_shape().as_list()[1] indices.extend([i] * dim) outputs = [] for field, embeddings in embedding_dict.items(): di = dimension_config[ field] if field in dimension_config else embed_dim U = tf.get_variable('{}_wkfm'.format(field), [T, di], initializer=tf.glorot_normal_initializer(), trainable=True) wkfm_weights = tf.get_variable('{}_wkfm_weights'.format(field), [N], initializer=tf.ones_initializer, trainable=True) weights = tf.gather(wkfm_weights, indices) y = tf.matmul(weights * x, U) outputs.append(y) y = tf.concat(outputs, axis=1) y = x * y new_inputs = tf.concat([linear_outputs, y], 1) return new_inputs, None
def optimize(self): # Calculate loss and optimize self.loss = tf.multiply(tf.losses.mean_squared_error(self.target_data, self.output), 1, name='l') self.train_op = tf.train.AdamOptimizer(par['learning_rate']).minimize(self.loss)
""" 变量-创建一个变量,初始化标量0 """ state = tf.Variable(0, name="counter") """ 创建一个常量张量 """ #input1 = tf.constant(3.0) input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) """ 创建一个矩阵乘法,把matrix1和matrix2作为输入 """ c = a * b """ 返回值product代表矩阵乘法的结果 """ product = tf.matmul(matrx1, matrx2) output = tf.multiply(input1, input2) """ 创建会话 """ with tf.Session() as sess: #指在第二个GPU上运行 #with tf.device("/gpu:1"): print(sess.run(c)) print(sess.run([product])) print(sess.run([output], feed_dict={input1: [7.], input2: [2.]})) #输出[array([14.], dtype=float32)] sess.close()
def prelu(self, inp, name): with tf.variable_scope(name): i = int(inp.get_shape()[-1]) alpha = self.make_var('alpha', shape=(i,)) output = tf.nn.relu(inp) + tf.multiply(alpha, -tf.nn.relu(-inp)) return output