def build_sampler(self, input_var): assert self.built, 'The encoder and the decoder have not been built yet!' temp = self._decoder(input_var) self.sampler_mean = self._dec_mean(temp) self.sampler_log_std_sq = tf.clip_by_value( self._dec_log_std_sq(temp), -self.sigma_clip, self.sigma_clip ) sampler_std = tf.exp( tf.mul( 0.5, self.sampler_log_std_sq ) ) self.sampler = tf.add( tf.mul( tf.random_normal( [self.n_samples, self.input_dims] ), sampler_std ), self.sampler_mean ) return self.sampler
def cell_locate(size, bbox, S): """ locate the center of ground truth in which grid cell """ x = tf.cast(tf.slice(bbox, [0,0], [-1,1]), tf.float32) y = tf.cast(tf.slice(bbox, [0,1], [-1,1]), tf.float32) w = tf.cast(tf.slice(bbox, [0,2], [-1,1]), tf.float32) h = tf.cast(tf.slice(bbox, [0,3], [-1,1]), tf.float32) height, width = size cell_w = width / S cell_h = height / S center_y = tf.add(y, tf.mul(h, 0.5)) center_x = tf.add(x, tf.mul(w, 0.5)) cell_coord_x = tf.cast(tf.div(center_x, cell_w), tf.int32) cell_coord_y = tf.cast(tf.div(center_y, cell_h), tf.int32) cell_num = tf.add(tf.mul(cell_coord_y, S), cell_coord_x) return cell_num
def compute_IOU(bboxA, bboxB): """Compute the Intersection Over Union. Args: bboxA: [N X 4 tensor] format = [left, top, right, bottom] bboxB: [N X 4 tensor] Return: IOU: [N X 1 tensor] """ x1A, y1A, x2A, y2A = tf.split(1, 4, bboxA) x1B, y1B, x2B, y2B = tf.split(1, 4, bboxB) # compute intersection x1_max = tf.maximum(x1A, x1B) y1_max = tf.maximum(y1A, y1B) x2_min = tf.minimum(x2A, x2B) y2_min = tf.minimum(y2A, y2B) # overlap_flag = tf.logical_and( tf.less(x1_max, x2_min), tf.less(y1_max, y2_min)) overlap_flag = tf.to_float(tf.less(x1_max, x2_min)) * \ tf.to_float(tf.less(y1_max, y2_min)) overlap_area = tf.mul(overlap_flag, tf.mul( x2_min - x1_max, y2_min - y1_max)) # compute union areaA = tf.mul(x2A - x1A, y2A - y1A) areaB = tf.mul(x2B - x1B, y2B - y1B) union_area = areaA + areaB - overlap_area return tf.div(overlap_area, union_area)
def __init__(self, action1_bounds, action2_bounds, session): self.graph = session.graph with self.graph.as_default(): self.sess = session self.action_bounds = [[action1_bounds[1], action2_bounds[1]], [action1_bounds[0], action2_bounds[0]]] self.action_size = len(self.action_bounds[0]) self.action_input = tf.placeholder(tf.float32, [None, self.action_size]) self.p_max = tf.constant(self.action_bounds[0], dtype=tf.float32) self.p_min = tf.constant(self.action_bounds[1], dtype=tf.float32) self.p_range = tf.constant([x - y for x, y in zip(self.action_bounds[0], self.action_bounds[1])], dtype=tf.float32) self.p_diff_max = tf.div(-self.action_input + self.p_max, self.p_range) self.p_diff_min = tf.div(self.action_input - self.p_min, self.p_range) self.zeros_act_grad_filter = tf.zeros([self.action_size]) self.act_grad = tf.placeholder(tf.float32, [None, self.action_size]) self.grad_inverter = tf.select(tf.greater(self.act_grad, self.zeros_act_grad_filter), tf.mul(self.act_grad, self.p_diff_max), tf.mul(self.act_grad, self.p_diff_min))
def cross_entropy(output, target): """Returns the cost function of Cross-entropy of two distributions, implement softmax internally. Parameters ---------- output : Tensorflow variable A distribution with shape: [None, n_feature]. target : Tensorflow variable A distribution with shape: [None, n_feature]. Examples -------- >>> ce = tf.cost.cross_entropy(y_logits, y_target_logits) Notes ----- About cross-entropy: `wiki <https://en.wikipedia.org/wiki/Cross_entropy>`_.\n The code is borrowed from: `here <https://en.wikipedia.org/wiki/Cross_entropy>`_. """ with tf.name_scope("cross_entropy_loss"): net_output_tf = output target_tf = target cross_entropy = tf.add(tf.mul(tf.log(net_output_tf, name=None),target_tf), tf.mul(tf.log(1 - net_output_tf), (1 - target_tf))) return -1 * tf.reduce_mean(tf.reduce_sum(cross_entropy, 1), name='cross_entropy_mean')
def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or type(self).__name__): initializer = tf.random_uniform_initializer(-0.1, 0.1) def get_variable(name, shape): return tf.get_variable(name, shape, initializer=initializer, dtype=inputs.dtype) c_prev, y_prev = tf.split(1, 2, state) W_z = get_variable("W_z", [self.input_size, self._num_blocks]) W_f = get_variable("W_f", [self.input_size, self._num_blocks]) W_o = get_variable("W_o", [self.input_size, self._num_blocks]) R_z = get_variable("R_z", [self._num_blocks, self._num_blocks]) R_f = get_variable("R_f", [self._num_blocks, self._num_blocks]) R_o = get_variable("R_o", [self._num_blocks, self._num_blocks]) b_z = get_variable("b_z", [1, self._num_blocks]) b_f = get_variable("b_f", [1, self._num_blocks]) b_o = get_variable("b_o", [1, self._num_blocks]) p_f = get_variable("p_f", [self._num_blocks]) p_o = get_variable("p_o", [self._num_blocks]) g = h = tf.tanh z = g(tf.matmul(inputs, W_z) + tf.matmul(y_prev, R_z) + b_z) i = 1 f = tf.sigmoid(tf.matmul(inputs, W_f) + tf.matmul(y_prev, R_f) + tf.mul(c_prev, p_f) + b_f) c = tf.mul(i, z) + tf.mul(f, c_prev) o = tf.sigmoid(tf.matmul(inputs, W_o) + tf.matmul(y_prev, R_o) + tf.mul(c, p_o) + b_o) y = tf.mul(h(c), o) return y, tf.concat(1, [c, y])
def entropy(self, n, p): # Note that given n and p where p is a probability vector of # length k, the entropy requires a sum over all # possible configurations of a k-vector which sums to n. It's # expensive. # http://stackoverflow.com/questions/36435754/generating-a-numpy-array-with-all-combinations-of-numbers-that-sum-to-less-than sess = tf.Session() n = sess.run(tf.cast(tf.squeeze(n), dtype=tf.int32)) sess.close() p = tf.cast(tf.squeeze(p), dtype=tf.float32) if isinstance(n, np.int32): k = get_dims(p)[0] max_range = np.zeros(k, dtype=np.int32) + n x = np.array([i for i in product(*(range(i+1) for i in max_range)) if sum(i)==n]) logpmf = self.logpmf(x, n, p) return tf.reduce_sum(tf.mul(tf.exp(logpmf), logpmf)) else: out = [] for j in range(n.shape[0]): k = get_dims(p)[0] max_range = np.zeros(k, dtype=np.int32) + n[j] x = np.array([i for i in product(*(range(i+1) for i in max_range)) if sum(i)==n[j]]) logpmf = self.logpmf(x, n[j], p[j, :]) out += [tf.reduce_sum(tf.mul(tf.exp(logpmf), logpmf))] return tf.pack(out)
def drawGraph(self, n_row, n_latent, n_col): with tf.name_scope('matDecomp'): self._p = tf.placeholder(tf.float32, shape=[None, n_col]) self._c = tf.placeholder(tf.float32, shape=[None, n_col]) self._lambda = tf.placeholder(tf.float32) self._index = tf.placeholder(tf.float32, shape=[None, n_row]) self._A = tf.Variable(tf.truncated_normal([n_row, n_latent])) self._B = tf.Variable(tf.truncated_normal([n_latent, n_col])) self._h = tf.matmul(tf.matmul(self._index, self._A), self._B) weighted_loss = tf.reduce_mean(tf.mul(self._c, tf.squared_difference(self._p, self._h))) self._weighted_loss = weighted_loss l2_A = tf.reduce_sum(tf.square(self._A)) l2_B = tf.reduce_sum(tf.square(self._B)) n_w = tf.constant(n_row * n_latent + n_latent * n_col, tf.float32) l2 = tf.truediv(tf.add(l2_A, l2_B), n_w) reg_term = tf.mul(self._lambda, l2) self._loss = tf.add(weighted_loss, reg_term) self._mask = tf.placeholder(tf.float32, shape=[n_row, n_col]) one = tf.constant(1, tf.float32) pred = tf.cast(tf.greater_equal(tf.matmul(self._A, self._B), one), tf.float32) cor = tf.mul(tf.cast(tf.equal(pred, self._p), tf.float32), self._c) self._vali_err = tf.reduce_sum(tf.mul(cor, self._mask)) self._saver = tf.train.Saver([v for v in tf.all_variables() if v.name.find('matDecomp') != -1]) tf.scalar_summary('training_weighted_loss_l2', self._loss) tf.scalar_summary('validation_weighted_loss', self._weighted_loss) merged = tf.merge_all_summaries()
def get_model(name): name = functools.partial('{}-{}'.format, name) self_pos = tf.placeholder(Config.dtype, Config.data_shape, name='self_pos') self_ability = tf.placeholder(Config.dtype, Config.data_shape, name='self_ability') enemy_pos = tf.placeholder(Config.dtype, Config.data_shape, name='enemy_pos') input_label = tf.placeholder(Config.dtype, Config.label_shape, name='input_label') x = tf.concat(3, [self_pos, self_ability, enemy_pos], name=name('input_concat')) y = input_label nl = tf.nn.tanh def conv_pip(name, x): name = functools.partial('{}_{}'.format, name) x = conv2d(name('0'), x, Config.data_shape[3]*2, kernel=3, stride=1, nl=nl) x = conv2d(name('1'), x, Config.data_shape[3], kernel=3, stride=1, nl=nl) return x pred = conv_pip(name('conv0'), x) for layer in range(5): pred_branch = tf.concat(3, [pred,x], name=name('concate%d'%layer)) pred += conv_pip(name('conv%d'%(layer+1)), pred_branch) x = tf.tanh(pred, name=name('control_tanh')) z = tf.mul(tf.exp(x), self_ability) z_sum = tf.reduce_sum(z, reduction_indices=[1,2,3], name=name('partition_function')) # partition function # another formula of y*logy loss = -tf.reduce_sum(tf.mul(x, y), reduction_indices=[1,2,3]) + tf.log(z_sum) z_sum = tf.reshape(z_sum, [-1, 1, 1, 1]) pred = tf.div(z, z_sum, name=name('predict')) return Model([self_pos, self_ability, enemy_pos], input_label, loss, pred, debug=z)
def w(input_data, cu, kappas_t_1, config): batch_size = config.batch_size mixture_size = config.mixture_size vocab_length = config.vocab_length # split along dim of mixture size * 3 hat_alphas_t, hat_betas_t, hat_kappas_t = tf.split(1, 3, input_data) alphas_t = tf.exp(hat_alphas_t) betas_t = tf.exp(hat_betas_t) kappas_t = tf.add(kappas_t_1, tf.exp(hat_kappas_t)) speech_length = tf.shape(cu)[1] u = tf.linspace(1.0, tf.cast(speech_length,tf.float32) , speech_length) u = tf.expand_dims(u, 0) u = tf.expand_dims(u, 0) u = tf.tile(u, [batch_size, mixture_size, 1]) alphas_t_expanded = tf.tile(tf.expand_dims(alphas_t, -1), [1, 1, speech_length]) betas_t_expanded = tf.tile(tf.expand_dims(betas_t, -1), [1, 1, speech_length]) kappas_t_expanded = tf.tile(tf.expand_dims(kappas_t, -1), [1, 1, speech_length]) calc = tf.square(tf.sub(kappas_t_expanded, u)) calc = tf.mul(calc, tf.neg(betas_t_expanded)) calc = tf.exp(calc) calc = tf.mul(calc, alphas_t_expanded) phi_t = tf.expand_dims(tf.reduce_sum(calc, 1), 1) output = tf.squeeze(tf.batch_matmul(phi_t, cu), [1]) return output, kappas_t, phi_t
def blend_images(data_folder1, data_folder2, out_folder, alpha=.5): filename_queue = tf.placeholder(dtype=tf.string) label = tf.placeholder(dtype=tf.int32) tensor_image = tf.read_file(filename_queue) image = tf.image.decode_jpeg(tensor_image, channels=3) multiplier = tf.div(tf.constant(224, tf.float32), tf.cast(tf.maximum(tf.shape(image)[0], tf.shape(image)[1]), tf.float32)) x = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[0], tf.float32), multiplier)), tf.int32) y = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[1], tf.float32), multiplier)), tf.int32) image = tf.image.resize_images(image, [x, y]) image = tf.image.rot90(image, k=label) image = tf.image.resize_image_with_crop_or_pad(image, 224, 224) sess = tf.Session() sess.run(tf.local_variables_initializer()) for root, folders, files in os.walk(data_folder1): for each in files: if each.find('.jpg') >= 0: img1 = Image.open(os.path.join(root, each)) img2_path = os.path.join(root.replace(data_folder1, data_folder2), each.split("-")[-1]) rotation = int(each.split("-")[1]) img2 = sess.run(image, feed_dict={filename_queue: img2_path, label: rotation}) imsave(os.path.join(os.getcwd(), "temp", "temp.jpg"), img2) img2 = Image.open(os.path.join(os.getcwd(), "temp", "temp.jpg")) out_image = Image.blend(img1, img2, alpha) outfile = os.path.join(root.replace(data_folder1, out_folder), each) if not os.path.exists(os.path.split(outfile)[0]): os.makedirs(os.path.split(outfile)[0]) out_image.save(outfile) else: print(each) sess.close()
def fztloss( f, pVecs, nVecs ): """ Tensorized cost function from Fast Zero-Shot Learning paper Args: f: The output from the network, a tensor of shape (# images, word embedding size) pVecs: The vector embeddings of the ground truth tags, a tensor of shape (# images, # positive tags, word embedding size) nVecs: The vector embeddings of negatively sampled tags, a tensor of shape (# images, # negative samples, word embedding size) Returns: Scalar tensor representing the batch cost """ posmul = tf.mul(pVecs, f) negmul = tf.mul(nVecs, f) tfpos = tf.reduce_sum(posmul, reduction_indices=2) tfneg = tf.reduce_sum(negmul, reduction_indices=2) tfpos = tf.transpose(tfpos, [1,0]) tfneg = tf.transpose(tfneg, [1,0]) negexpan = tf.tile( tf.expand_dims(tfneg, -1), [1, 1, tf.shape(tfpos)[1]] ) posexpan = tf.tile( tf.transpose(tf.expand_dims(tfpos, -1), [0,2,1]), [1, tf.shape(tfneg)[1], 1]) differences = tf.sub(negexpan, posexpan) return tf.reduce_sum(tf.reduce_sum(tf.log(1 + tf.exp(differences)), reduction_indices=[1,2]))
def U_t_variance(timestep_outputs_matrix, total_timesteps, gamma = 5): with tf.op_scope(timestep_outputs_matrix + total_timesteps + gamma, "U_t_variance"): G_i_matrix = G_i_piecewise_variance(timestep_outputs_matrix, total_timesteps) tf.mul(timestep_outputs_matrix, ) tf.reduce_prod(timestep_outputs_matrix_with_g)
def logpmf(self, x, n, p): x = tf.cast(tf.squeeze(x), dtype=tf.float32) n = tf.cast(tf.squeeze(n), dtype=tf.float32) p = tf.cast(tf.squeeze(p), dtype=tf.float32) return log_gamma(n + 1.0) - log_gamma(x + 1.0) - \ log_gamma(n - x + 1.0) + \ tf.mul(x, tf.log(p)) + tf.mul(n - x, tf.log(1.0-p))
def _build_loss(self): with tf.variable_scope("loss"): # Compute y_j = r_j * discount*best_qvalue self.tf_discount = tf.constant(self.discount) self.qtarget = tf.add(self.pl_rewards, tf.mul(1.0-self.pl_terminals, tf.mul(self.tf_discount, self.pl_qtargets))) # Select Q-values for given actions self.actions_one_hot = tf.one_hot(self.pl_actions, self.num_actions, 1.0, 0.0) self.qvalue_pred = tf.reduce_sum(tf.mul(self.qvalues, self.actions_one_hot), reduction_indices=1) # Difference between target and predicted Q-network output self.delta = tf.sub(self.qtarget, self.qvalue_pred) if self.clip_delta > 0: # Perform clipping of the error term, default clipping is to (-1, +1) range self.quadratic_part = tf.minimum(tf.abs(self.delta), tf.constant(self.clip_delta)) self.linear_part = tf.sub(tf.abs(self.delta), self.quadratic_part) self.delta_square = tf.mul(tf.constant(0.5), tf.square(self.quadratic_part)) + (self.clip_delta*self.linear_part) #self.delta_clipped = tf.clip_by_value(self.delta, -1.0*self.clip_delta, self.clip_delta) #self.delta_square = tf.square(self.delta_clipped) else: # No error clipping self.delta_square = tf.square(self.delta) # Actual loss if self.batch_accumulator == "sum": self.loss = tf.reduce_sum(self.delta_square) else: self.loss = tf.reduce_mean(self.delta_square) # Running average of the loss for TensorBoard self.loss_moving_avg = tf.train.ExponentialMovingAverage(decay=0.999) self.loss_moving_avg_op = self.loss_moving_avg.apply([self.loss])
def compute_loss_reg(self, sim_reg_mat, offset_label): sim_score_mat, p_reg_mat, l_reg_mat = tf.split(2, 3, sim_reg_mat) sim_score_mat = tf.reshape(sim_score_mat, [self.batch_size, self.batch_size]) l_reg_mat = tf.reshape(l_reg_mat, [self.batch_size, self.batch_size]) p_reg_mat = tf.reshape(p_reg_mat, [self.batch_size, self.batch_size]) # unit matrix with -2 I_2 = tf.diag(tf.constant(-2.0, shape=[self.batch_size])) all1 = tf.constant(1.0, shape=[self.batch_size, self.batch_size]) # | -1 1 1... | # mask_mat = | 1 -1 -1... | # | 1 1 -1 ... | mask_mat = tf.add(I_2, all1) # loss cls, not considering iou I = tf.diag(tf.constant(1.0, shape=[self.batch_size])) I_half = tf.diag(tf.constant(0.5, shape=[self.batch_size])) batch_para_mat = tf.constant(self.alpha, shape=[self.batch_size, self.batch_size]) para_mat = tf.add(I,batch_para_mat) loss_mat = tf.log(tf.add(all1, tf.exp(tf.mul(mask_mat, sim_score_mat)))) loss_mat = tf.mul(loss_mat, para_mat) loss_align = tf.reduce_mean(loss_mat) # regression loss l_reg_diag = tf.matmul(tf.mul(l_reg_mat, I), tf.constant(1.0, shape=[self.batch_size, 1])) p_reg_diag = tf.matmul(tf.mul(p_reg_mat, I), tf.constant(1.0, shape=[self.batch_size, 1])) offset_pred = tf.concat(1, (p_reg_diag, l_reg_diag)) loss_reg = tf.reduce_mean(tf.abs(tf.sub(offset_pred, offset_label))) loss=tf.add(tf.mul(self.lambda_regression, loss_reg), loss_align) return loss, offset_pred, loss_reg
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.mul(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.mul(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 getActivationTF(z): try: import tensorflow as tf return tf.add(tf.mul(z>0,z), tf.mul(z<0,0.01*z)) except ImportError: raise ImportError( 'TensorFlow is not installed on your computer. Please use other technology for building your network or install tensorflow.')
def test(model_path='models/model-900', video_feat_path=video_feat_path): meta_data, train_data, val_data, test_data = get_video_data_jukin(video_data_path_train, video_data_path_val, video_data_path_test) test_data = val_data ixtoword = pd.Series(np.load('./data'+str(gpu_id)+'/ixtoword.npy').tolist()) model = Video_Caption_Generator( dim_image=dim_image, n_words=len(ixtoword), dim_hidden=dim_hidden, batch_size=batch_size, n_lstm_steps=n_frame_step, drop_out_rate = 0, bias_init_vector=None) video_tf, video_mask_tf, caption_tf, pos_mask_tf, lstm1_variables_tf, lstm2_variables_tf = model.build_generator() sess = tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True)) with tf.device('/cpu:0'): saver = tf.train.Saver() saver.restore(sess, model_path) for ind, row in enumerate(lstm1_variables_tf): if ind % 4 == 0: assign_op = row.assign(tf.mul(row,1-0.5)) sess.run(assign_op) for ind, row in enumerate(lstm2_variables_tf): if ind % 4 == 0: assign_op = row.assign(tf.mul(row,1-0.5)) sess.run(assign_op) [pred_sent, gt_sent] = testing_all(sess, test_data, ixtoword,video_tf, video_mask_tf, caption_tf, pos_mask_tf) #np.savez('Att_result/'+model_path.split('/')[1],gt = gt_sent,pred=pred_sent) scorer = COCOScorer() total_score = scorer.score(gt_sent, pred_sent, range(len(pred_sent))) return total_score
def __call__(self, x_placeholder, h_prev, C_prev): with tf.variable_scope(self.scope, reuse=True): embedding = tf.get_variable('embedding') W = tf.get_variable('weight') x_embedding = tf.nn.embedding_lookup(embedding, x_placeholder) if self.is_training: x_embedding = tf.nn.dropout(x_embedding, self.keep_prob) # forget gate concat_input = tf.concat(1, [h_prev, x_embedding]) gates = tf.matmul(concat_input, W) m_f, m_i, m_C_update, m_o = tf.split(1, 4, gates) # forget gate f = tf.sigmoid(m_f) # input gate i = tf.sigmoid(m_i) # output gate o = tf.sigmoid(m_o) # Cell update C_update = tf.tanh(m_C_update) # cell after update # Add a dropout layer. C = tf.mul(f, C_prev) + tf.mul(i, C_update) # output h = tf.mul(o, tf.tanh(C)) return h, C
def sparse_hermitian_product(emb, tuples): """ Compute the Hermitian inner product between selected complex embeddings This corresponds to the usual dot product applied on the conjugate of the first vector: <conj(x), y> where conj is the complex conjugate (obtained by inverting the imaginary part) We consider that the embedding dimension is twice the rank, where the first part is in emb[:,:rk] and the imaginary part is in emb[:,rk:]. It computes S[i] = <conj(E[I[i,1]], E[I[i,2]]> Usage: S = sparse_hermitian_product(E, I): :param emb: embedding matrix of size [n_emb, 2 * r] containing float numbers where r is the complex rank :param tuples: tuple matrix of size [n_t, 2] containing integers that correspond to the indices of the embeddings :return: a pair containing the real and imaginary parts of the Hermitian dot products """ rk = emb.get_shape()[1].value // 2 emb_re = emb[:, :rk] emb_im = emb[:, rk:] emb_sel_a_re = tf.gather(emb_re, tuples[:, 0]) emb_sel_a_im = tf.gather(emb_im, tuples[:, 0]) emb_sel_b_re = tf.gather(emb_re, tuples[:, 1]) emb_sel_b_im = tf.gather(emb_im, tuples[:, 1]) pred_re = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_re) + tf.mul(emb_sel_a_im, emb_sel_b_im), 1) pred_im = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_im) - tf.mul(emb_sel_a_im, emb_sel_b_re), 1) return pred_re, pred_im
def __call__(self, inputs, state, scope=None): """Gated recurrent unit (GRU) with nunits cells.""" with vs.variable_scope(scope or type(self).__name__): if self._dropMaskInput.get_shape()[1:] != inputs.get_shape()[1:]: print("error: "+str(self._dropMaskInput.get_shape()[1:])+" != "+str(inputs.get_shape()[1:])) assert(False) if self._dropMaskState.get_shape()[1:] != state.get_shape()[1:]: print("error: "+str(self._dropMaskState.get_shape()[1:])+" != "+str(state.get_shape()[1:])) assert(False) dropin = tf.mul(self._dropMaskInput, inputs) dropst = tf.mul(self._dropMaskState, state) with vs.variable_scope("Gates"): # Reset gate and update gate. # We start with bias of 1.0 to not reset and not update. concat = rnn_cell._linear([dropin, dropst], 2 * self._num_units, True, 1.0) r, u = tf.split(1, 2, concat) r, u = tf.sigmoid(r), tf.sigmoid(u) with vs.variable_scope("Candidate"): htilda = self._activation(rnn_cell._linear([dropin, r * dropst], self._num_units, True)) new_h = u * dropst + (1 - u) * htilda return new_h, new_h
def mean_var(x, mask, mean, num): x_mask = tf.mul(x, mask) residual = x_mask - mean res_mask = tf.mul(residual ,mask) res_mask_sq = tf.mul(res_mask, res_mask) var = tf.reduce_sum(res_mask_sq,0,keep_dims=True)*1.0/(num+1e-7) return tf.reduce_sum(var)
def __init__(self, env, render, debug, sess, action_policy, num_features, batch_size, max_num_steps, n_iter, algo_discount): super(REINFORCE, self).__init__(env, render, debug, sess, action_policy, num_features, batch_size, max_num_steps, n_iter) # params specific to the policy gradient algo self.algo_discount = algo_discount with tf.variable_scope("policy"): self.actions = tf.placeholder(tf.int32, [None, 1], "actions") self.returns = tf.placeholder(tf.float32, [None, 1], "returns") num_actions = self.env.action_space.n action_mask = tf.one_hot(indices=self.actions, depth=num_actions) # TODO: why are we using softmax here? self.log_probs = tf.nn.log_softmax(self.action_policy.network.logits) self.policy_probs = tf.reduce_sum( \ tf.mul(self.log_probs, action_mask), reduction_indices = 1) # negative since we are maximizing self.loss = -tf.reduce_sum(tf.mul(self.policy_probs, utils.standardize(self.returns))) self.opt = tf.train.AdamOptimizer(self.action_policy.learning_rate).minimize(self.loss)
def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or type(self).__name__): # Conveniently the concatenation of all hidden states at t-1 h_star_t_prev = state u_g = tf.get_variable("u_g", [self.state_size], initializer=tf.random_uniform_initializer(-0.1, 0.1)) cur_state_pos = 0 cur_inp = inputs new_states = [] for i, cell in enumerate(self._cells): with tf.variable_scope("Cell%d" % i): cur_state = array_ops.slice( state, [0, cur_state_pos], [-1, cell.state_size]) with tf.variable_scope("Global Reset"): w_g = tf.get_variable("w_g", cell.state_size, initializer=tf.random_uniform_initializer(-0.1, 0.1)) g = tf.sigmoid(tf.mul(w_g, cur_state) + tf.mul(u_g, h_star_t_prev)) U = tf.get_variable("U", [cell.state_size], initializer=tf.random_uniform_initializer(-0.1, 0.1)) cur_state = tf.reduce_sum(g * tf.matmul(cur_state, U)) cur_state_pos += cell.state_size cur_inp, new_state = cell(cur_inp, cur_state) new_states.append(new_state) return cur_inp, array_ops.concat(1, new_states)
def outer_product(*inputs): """Computes outer product. Args: inputs: a list of 1-D `Tensor` (vector) """ inputs = list(inputs) order = len(inputs) for idx, input_ in enumerate(inputs): if len(input_.get_shape()) == 1: inputs[idx] = tf.reshape(input_, [-1, 1] if idx % 2 == 0 else [1, -1]) if order == 2: output = tf.mul(inputs[0], inputs[1]) elif order == 3: size = [] idx = 1 for i in xrange(order): size.append(inputs[i].get_shape()[0]) output = tf.zeros(size) u, v, w = inputs[0], inputs[1], inputs[2] uv = tf.mul(inputs[0], inputs[1]) for i in xrange(self.size[-1]): output = tf.scatter_add(output, [0,0,i], uv) return output
def _loss_x_entropy(self, x, z, noise=None): with tf.name_scope("xentropy_loss"): z_clipped = tf.clip_by_value(z, FLAGS.zero_bound, FLAGS.one_bound) z_minus_1_clipped = tf.clip_by_value((1.0 - z), FLAGS.zero_bound, FLAGS.one_bound) x_clipped = tf.clip_by_value(x, FLAGS.zero_bound, FLAGS.one_bound) x_minus_1_clipped = tf.clip_by_value((1.0 - x), FLAGS.zero_bound, FLAGS.one_bound) # cross_entropy = x * log(z) + (1 - x) * log(1 - z) cross_entropy = tf.add(tf.mul(tf.log(z_clipped), x_clipped), tf.mul(tf.log(z_minus_1_clipped), x_minus_1_clipped), name='X-Entr') if noise: with tf.name_scope("Given_Emphasis"): a, b = self._get_emph_params corrupted = tf.select(noise, cross_entropy, tf.zeros_like(cross_entropy), name='Corrupted_Emphasis') # OR -- tf.select(tf.logical_not(noisy_points), cross_entropy, tf.zeros_like(cross_entropy), name='Uncorrupted_Emphasis') uncorrupted = tf.select(noise, tf.zeros_like(cross_entropy), cross_entropy, name='Uncorrupted_Emphasis') loss = a * (-1 * tf.reduce_sum(corrupted, 1)) + b * (-1 * tf.reduce_sum(uncorrupted, 1)) else: # Sum the cost for each example loss = -1 * tf.reduce_sum(cross_entropy, 1) # Reduce mean to find the overall cost of the loss cross_entropy_mean = tf.reduce_mean(loss, name='xentropy_mean') return cross_entropy_mean
def IoU(bbox, gt): # bbox = [ x , y , w , h ] ( x , y left up) shape = [-1, 1] x1 = tf.maximum(tf.cast(bbox[0], tf.float32), tf.reshape(tf.cast(gt[:,0], tf.float32), shape)) y1 = tf.maximum(tf.cast(bbox[1], tf.float32), tf.reshape(tf.cast(gt[:,1], tf.float32), shape)) x2 = tf.minimum(tf.cast(bbox[2] + bbox[0], tf.float32), tf.reshape(tf.cast(gt[:,2] + gt[:,0], tf.float32), shape)) y2 = tf.minimum(tf.cast(bbox[3] + bbox[1], tf.float32), tf.reshape(tf.cast(gt[:,3] + gt[:,1], tf.float32), shape)) inter_w = tf.sub(x2,x1) inter_h = tf.sub(y2,y1) inter = tf.cast(inter_w * inter_h, tf.float32) bounding_box = tf.cast(tf.mul(bbox[2],bbox[3]), tf.float32) ground_truth = tf.reshape(tf.cast(tf.mul(gt[:,2],gt[:,3]), tf.float32), shape) #iou = tf.div(inter,tf.sub(tf.add(bounding_box,tf.reshape(ground_truth,shape)),inter)) iou = inter / (bounding_box + ground_truth - inter) # limit the iou range between 0 and 1 mask_less = tf.cast(tf.logical_not(tf.less(iou, tf.zeros_like(iou))), tf.float32) #mask_great = tf.cast(tf.logical_not(tf.greater(iou, tf.ones_like(iou))), tf.float32) iou = tf.mul(iou, mask_less) #iou = tf.mul(iou, positive_mask) return iou
def setUp(self): """Test setup. Structure of the forward graph: f | | ----- ----- | | d e | | | | --- --------- --- | | | a b c Construct a backward graph using the GradientDescentOptimizer. """ self.a = tf.Variable(1.0, name="a") self.b = tf.Variable(2.0, name="b") self.c = tf.Variable(4.0, name="c") self.d = tf.mul(self.a, self.b, name="d") self.e = tf.mul(self.b, self.c, name="e") self.f = tf.mul(self.d, self.e, name="f") # Gradient descent optimizer that minimizes g. tf.train.GradientDescentOptimizer(0.01).minimize(self.f, name="optim") self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def build_node(self, x_in, c_in, h_in, scope="lstm_cell"): #print (x_in, c_in, h_in, scope) #print [type(thing) for thing in (x_in, c_in, h_in, scope)] # print [(item.name, item.dtype) for thing in (h_in, c_in) for item in thing] # print (x_in.name, x_in.dtype) with tf.variable_scope(scope): # print x.shape # print h_in.get_shape() x_with_h = tf.concat(2, [x_in, h_in]) ones_for_bias = tf.constant(np.ones([batch_size,1,1]), name="b", dtype=tf.float32) x_h_concat = tf.concat(2, [ones_for_bias, x_with_h]) # forget gate layer # print "w_f: ", self.w_f.get_shape() # print "x_h_concat: ", x_h_concat.get_shape() f = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_f)) # candidate values i = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_i)) candidate_c = tf.tanh(tf.batch_matmul(x_h_concat, self.w_c)) # new cell state (hidden) # forget old values of c old_c_to_keep = tf.mul(f, c_in) # scaled candidate values of c new_c_to_keep = tf.mul(i, candidate_c) c = tf.add(old_c_to_keep, new_c_to_keep) # new scaled output o = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_o)) h = tf.mul(o, tf.tanh(c)) return (c, h)
import tensorflow as tf x_data = [1., 2., 3.] y_data = [1., 2., 3.] X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) W = tf.Variable(tf.random_uniform([1], -10.0, 10.0)) hypothesis = tf.mul(X, W) cost = tf.reduce_sum(tf.square(hypothesis - Y)) decent = W - tf.mul(0.1, tf.reduce_mean(tf.mul((tf.mul(W, X) - Y), X))) update = W.assign(decent) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) for step in xrange(100): sess.run(update, feed_dict={X: x_data, Y: y_data}) print step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)
def build_optimizer(self): self.lr_input = tf.placeholder(tf.float32, shape=[], name="LearningRate") self.loss_alpha_input = tf.placeholder(tf.float32, shape=[], name="Alpha") mse = tf.reduce_mean(tf.square(self.y_ - self.y), name="Loss1") if self.debug: mse = tf.Print(mse, [mse], message="MSE: ") if self.loss_alpha == 0.0 or self.inference_depth == 0: loss = mse else: loss1_mse = self.inference_depth * [None] for i in range(0, self.inference_depth): inference_sub = tf.sub(self.y, self.Y2_conv[i], name="Loss1_%d_sub" % i) inference_square = tf.square(inference_sub, name="Loss1_%d_squ" % i) loss1_mse[i] = tf.reduce_mean(inference_square, name="Loss1_%d" % i) loss1 = loss1_mse[0] for i in range(1, self.inference_depth): if i == self.inference_depth: loss1 = tf.add(loss1, loss1_mse[i], name="Loss1") else: loss1 = tf.add(loss1, loss1_mse[i], name="Loss1_%d_add" % i) loss1 = tf.mul(1.0 / self.inference_depth, loss1, name="Loss1_weight") loss2 = mse if self.visualize: tf.summary.scalar("L1:" + self.model_name, loss1) tf.summary.scalar("L2:" + self.model_name, loss2) loss1 = tf.mul(self.loss_alpha_input, loss1, name="Loss1_alpha") loss2 = tf.mul(1 - self.loss_alpha_input, loss2, name="Loss2_alpha") if self.loss_beta > 0.0: with tf.name_scope('Loss3') as scope: loss3 = tf.nn.l2_loss(self.Wm1_conv) + tf.nn.l2_loss(self.W0_conv) \ + tf.nn.l2_loss(self.W_conv) + tf.nn.l2_loss(self.WD1_conv) \ + tf.nn.l2_loss(self.WD2_conv) loss3 *= self.loss_beta if self.visualize: tf.summary.scalar("L3:" + self.model_name, loss3) loss = loss1 + loss2 + loss3 else: loss = loss1 + loss2 if self.visualize: tf.summary.scalar("Loss:" + self.model_name, loss) self.loss = loss self.mse = mse self.train_step = self.add_optimizer_op(loss, self.lr_input)
def __init__(self, m,n,dim,n_iteratations=100,alpha=.3,sigma=None): # m by n dimensions of map # n_iter for training # dim is dimensionality of training input -> 4 # alpha is inital learning rate # sigma is inital neighborhood value self._m = m self._n = n self._n_iterations = n_iteratations if sigma is None: sigma = max(m,n) / 2.0 self._graph = tf.Graph() with self._graph.as_default(): # randomly initialize weightage vectors x each neuron self._weightage_vects = tf.Variable(tf.random_normal([m*n,dim])) #grid location of neurons self._location_vects = tf.constant(np.array( list(self._neuron_locations(m,n)))) #training vector placeholder self._vect_input = tf.placeholder('float',[dim]) self._iter_input = tf.placeholder('float') #best matching unit -> calculate euclidean distance bectween #each weightage vector and input, return index neuron w/ lowest bmu_index = tf.argmin( tf.sqrt( tf.reduce_sum( tf.pow( tf.sub(self._weightage_vects, tf.pack([self._vect_input for i in range(m*n)] )), 2 ), 1 ) ), 0 ) #extract the location of bmu based on bmu index slice_input = tf.pad( tf.reshape(bmu_index, [1]), np.array([[0,1]]) ) bmu_loc = tf.reshape( tf.slice(self._location_vects, slice_input, tf.constant(np.array([1,2]))), [2]) # adjust alpha, sigma based on iter learning_rate_op = tf.sub(1.0, tf.div( self._iter_input, self._n_iterations )) _alpha_op = tf.mul(alpha,learning_rate_op) _sigma_op = tf.mul(sigma, learning_rate_op) # operation that generates vectors w/ learning rates # for all neurons bmu_distance_squares = tf.reduce_sum( tf.pow( tf.sub(self._location_vects, tf.pack( [bmu_loc for i in range(m*n)] ) ), 2 ), 1 ) neighborhood_func = tf.exp( tf.neg( tf.div( tf.cast( bmu_distance_squares, "float32"), tf.pow(_sigma_op,2) ) ) ) learning_rate_op = tf.mul(_alpha_op, neighborhood_func) # operation to update the weightage vectors based on input learning_rate_multiplier = tf.pack([tf.tile( tf.slice(learning_rate_op, np.array([i]), np.array([1]) ), [dim] ) for i in range(m*n)]) weightage_delta = tf.mul( learning_rate_multiplier, tf.sub( tf.pack([self._vect_input for i in range(m*n)]), self._weightage_vects ) ) new_weightages_op = tf.add( self._weightage_vects, weightage_delta ) self._training_op = tf.assign( self._weightage_vects, new_weightages_op ) # init session, vars self._sess = tf.Session() init_op = tf.initialize_all_variables() self._sess.run(init_op)
def build_translation_model(self, sample_size): self.options['sample_size'] = sample_size options = self.options source_sentence = tf.placeholder( 'int32', [options['batch_size'], options['sample_size']], name='source_sentence') target_sentence = tf.placeholder( 'int32', [options['batch_size'], options['sample_size'] + 1], name='target_sentence') self.source_masked = tf.nn.embedding_lookup(self.input_mask, source_sentence, name="source_masked") self.source_masked_d = tf.slice(self.source_masked, [0, 0, 0], [ options['batch_size'], options['sample_size'], options['residual_channels'] ], name='source_masked_d') source_embedding = tf.nn.embedding_lookup(self.w_source_embedding, source_sentence) # MASK EMBEDDING BEYOND SOURCE LENGTH source_embedding = tf.mul(source_embedding, self.source_masked, name="source_embedding") # Input to the decoder target_sentence1 = tf.slice( target_sentence, [0, 0], [options['batch_size'], options['sample_size']], name='target_sentence1') target1_embedding = tf.nn.embedding_lookup(self.w_target_embedding, target_sentence1, name="target1_embedding") # Output of the decoder target_sentence2 = tf.slice( target_sentence, [0, 1], [options['batch_size'], options['sample_size']], name='target_sentence2') # FOR MASKING LOSS BEYOND THE TARGET LENGTH self.target_masked = tf.nn.embedding_lookup(self.output_mask, target_sentence2, name="target_masked") encoder_output = self.encoder(source_embedding) decoder_output = self.decoder(target1_embedding, encoder_output) loss = self.loss(decoder_output, target_sentence2) tf.summary.scalar('loss', loss) flat_logits = tf.reshape(decoder_output, [-1, options['n_target_quant']]) prediction = tf.argmax(flat_logits, 1) variables = tf.trainable_variables() merged_summary = tf.merge_all_summaries() # first_column_decoder = t tensors = { 'source_sentence': source_sentence, 'target_sentence': target_sentence, 'loss': loss, 'prediction': prediction, 'variables': variables, 'merged_summary': merged_summary, 'source_embedding': source_embedding, 'encoder_output': encoder_output, 'target_masked': self.target_masked, 'source_masked': self.source_masked, 'source_gradient': tf.gradients(loss, [source_embedding]), 'target_gradient': tf.gradients(loss, [target1_embedding]), 'probs': tf.nn.softmax(flat_logits) } return tensors
############################################################### # test 0 test_sense_indices = tf.placeholder(tf.int32, sense_dim, name='test_word_indices') test_word_index = tf.placeholder(tf.int32, 1, name='test_word_index') #s_in_l2 = tf.sqrt(tf.reduce_sum(tf.mul(s_in, s_in), 1)) #s_in_l2 = tf.reduce_mean(tf.reshape(s_in_l2, [vocab_size, sense_dim]), 1) s_in_l2 = tf.reshape(s_in, [vocab_size, sense_dim, sense_embedding_dim]) s_in_l2 = tf.transpose(s_in_l2, perm=[0, 2, 1]) s_in_l2_element = tf.sub( s_in_l2, tf.batch_matmul(tf.reduce_mean(s_in_l2, 2, keep_dims=True), tf.ones([vocab_size, 1, sense_dim]))) s_in_l2_element = tf.mul(s_in_l2_element, s_in_l2_element) s_in_l2_element = tf.sqrt(tf.reduce_mean(s_in_l2_element, 2)) s_in_var = tf.reduce_mean(s_in_l2_element, 1) #[sense_dim, embedding_dim] test_input_var = tf.nn.embedding_lookup(s_in_var, test_word_index) embedded_test_input_norm = tf.nn.embedding_lookup(s_in_norm, test_sense_indices) embedded_test_input = tf.nn.embedding_lookup(s_in, test_sense_indices) # [sense_dim, sense_size] test_input_cosine = tf.matmul(embedded_test_input_norm, s_in_norm, transpose_b=True) test_output_logit = tf.matmul(embedded_test_input, s_out, transpose_b=True) #test_all_cosine = tf.matmul(embedded_test_all,n_s_all,transpose_b = True) test_input_l2_norm = tf.sqrt(
def add_input_distortions(flip_left_right, random_crop, random_scale, random_brightness): """Creates the operations to apply the specified distortions. During training it can help to improve the results if we run the images through simple distortions like crops, scales, and flips. These reflect the kind of variations we expect in the real world, and so can help train the model to cope with natural data more effectively. Here we take the supplied parameters and construct a network of operations to apply them to an image. Cropping ~~~~~~~~ Cropping is done by placing a bounding box at a random position in the full image. The cropping parameter controls the size of that box relative to the input image. If it's zero, then the box is the same size as the input and no cropping is performed. If the value is 50%, then the crop box will be half the width and height of the input. In a diagram it looks like this: < width > +---------------------+ | | | width - crop% | | < > | | +------+ | | | | | | | | | | | | | | +------+ | | | | | +---------------------+ Scaling ~~~~~~~ Scaling is a lot like cropping, except that the bounding box is always centered and its size varies randomly within the given range. For example if the scale percentage is zero, then the bounding box is the same size as the input and no scaling is applied. If it's 50%, then the bounding box will be in a random range between half the width and height and full size. Args: flip_left_right: Boolean whether to randomly mirror images horizontally. random_crop: Integer percentage setting the total margin used around the crop box. random_scale: Integer percentage of how much to vary the scale by. random_brightness: Integer range to randomly multiply the pixel values by. graph. Returns: The jpeg input layer and the distorted result tensor. """ jpeg_data = tf.placeholder(tf.string, name='DistortJPGInput') decoded_image = tf.image.decode_jpeg(jpeg_data) decoded_image_as_float = tf.cast(decoded_image, dtype=tf.float32) decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0) margin_scale = 1.0 + (random_crop / 100.0) resize_scale = 1.0 + (random_scale / 100.0) margin_scale_value = tf.constant(margin_scale) resize_scale_value = tf.random_uniform(tensor_shape.scalar(), minval=1.0, maxval=resize_scale) scale_value = tf.mul(margin_scale_value, resize_scale_value) precrop_width = tf.mul(scale_value, MODEL_INPUT_WIDTH) precrop_height = tf.mul(scale_value, MODEL_INPUT_HEIGHT) precrop_shape = tf.pack([precrop_height, precrop_width]) precrop_shape_as_int = tf.cast(precrop_shape, dtype=tf.int32) precropped_image = tf.image.resize_bilinear(decoded_image_4d, precrop_shape_as_int) precropped_image_3d = tf.squeeze(precropped_image, squeeze_dims=[0]) cropped_image = tf.random_crop( precropped_image_3d, [MODEL_INPUT_HEIGHT, MODEL_INPUT_WIDTH, MODEL_INPUT_DEPTH]) if flip_left_right: flipped_image = tf.image.random_flip_left_right(cropped_image) else: flipped_image = cropped_image brightness_min = 1.0 - (random_brightness / 100.0) brightness_max = 1.0 + (random_brightness / 100.0) brightness_value = tf.random_uniform(tensor_shape.scalar(), minval=brightness_min, maxval=brightness_max) brightened_image = tf.mul(flipped_image, brightness_value) distort_result = tf.expand_dims(brightened_image, 0, name='DistortResult') return jpeg_data, distort_result
# coding: UTF-8 from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf var1 = tf.Variable(0) holder2 = tf.placeholder(tf.int32) add_op = tf.add(var1, holder2) update_var1 = tf.assign(var1, add_op) mul_op = tf.mul(add_op, update_var1) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) result = sess.run(mul_op, feed_dict={holder2: 5}) print(result)
def __init__(self, embedding_mat, non_static, hidden_unit, sequence_length, max_pool_size, num_classes, embedding_size, filter_sizes, num_filters, l2_reg_lambda=0.0): self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name='input_x') self.input_y = tf.placeholder(tf.float32, [None, num_classes], name='input_y') self.dropout_keep_prob = tf.placeholder(tf.float32, name='dropout_keep_prob') self.batch_size = tf.placeholder(tf.int32) self.pad = tf.placeholder(tf.float32, [None, 1, embedding_size, 1], name='pad') self.real_len = tf.placeholder(tf.int32, [None], name='real_len') l2_loss = tf.constant(0.0) with tf.device('/cpu:0'), tf.name_scope('embedding'): if not non_static: W = tf.constant(embedding_mat, name='W') else: W = tf.Variable(embedding_mat, name='W') self.embedded_chars = tf.nn.embedding_lookup(W, self.input_x) emb = tf.expand_dims(self.embedded_chars, -1) pooled_concat = [] reduced = np.int32(np.ceil((sequence_length) * 1.0 / max_pool_size)) for i, filter_size in enumerate(filter_sizes): with tf.name_scope('conv-maxpool-%s' % filter_size): # Zero paddings so that the convolution output have dimension batch x sequence_length x emb_size x channel num_prio = (filter_size - 1) // 2 num_post = (filter_size - 1) - num_prio pad_prio = tf.concat(1, [self.pad] * num_prio) pad_post = tf.concat(1, [self.pad] * num_post) emb_pad = tf.concat(1, [pad_prio, emb, pad_post]) filter_shape = [filter_size, embedding_size, 1, num_filters] W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name='W') b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name='b') conv = tf.nn.conv2d(emb_pad, W, strides=[1, 1, 1, 1], padding='VALID', name='conv') h = tf.nn.relu(tf.nn.bias_add(conv, b), name='relu') # Maxpooling over the outputs pooled = tf.nn.max_pool(h, ksize=[1, max_pool_size, 1, 1], strides=[1, max_pool_size, 1, 1], padding='SAME', name='pool') pooled = tf.reshape(pooled, [-1, reduced, num_filters]) pooled_concat.append(pooled) pooled_concat = tf.concat(2, pooled_concat) pooled_concat = tf.nn.dropout(pooled_concat, self.dropout_keep_prob) # lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=hidden_unit) lstm_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_unit) lstm_cell = tf.nn.rnn_cell.DropoutWrapper( lstm_cell, output_keep_prob=self.dropout_keep_prob) self._initial_state = lstm_cell.zero_state(self.batch_size, tf.float32) inputs = [ tf.squeeze(input_, [1]) for input_ in tf.split(1, reduced, pooled_concat) ] outputs, state = tf.nn.rnn(lstm_cell, inputs, initial_state=self._initial_state, sequence_length=self.real_len) # Collect the appropriate last words into variable output (dimension = batch x embedding_size) output = outputs[0] with tf.variable_scope('Output'): tf.get_variable_scope().reuse_variables() one = tf.ones([1, hidden_unit], tf.float32) for i in range(1, len(outputs)): ind = self.real_len < (i + 1) ind = tf.to_float(ind) ind = tf.expand_dims(ind, -1) mat = tf.matmul(ind, one) output = tf.add(tf.mul(output, mat), tf.mul(outputs[i], 1.0 - mat)) with tf.name_scope('output'): self.W = tf.Variable(tf.truncated_normal( [hidden_unit, num_classes], stddev=0.1), name='W') b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name='b') l2_loss += tf.nn.l2_loss(W) l2_loss += tf.nn.l2_loss(b) self.scores = tf.nn.xw_plus_b(output, self.W, b, name='scores') self.predictions = tf.argmax(self.scores, 1, name='predictions') with tf.name_scope('loss'): losses = tf.nn.softmax_cross_entropy_with_logits( self.scores, self.input_y) self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss with tf.name_scope('accuracy'): correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name='accuracy') with tf.name_scope('num_correct'): correct = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) self.num_correct = tf.reduce_sum(tf.cast(correct, 'float'))
#!/usr/bin/env python #coding=utf-8 import tensorflow as tf #2 state = tf.Variable(0, name="counter") one = tf.constant(1) new_value = tf.add(state, one) update = tf.assign(state, new_value) init_op = tf.initialize_all_variables() #3 input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.mul(input1, input2) input3 = tf.constant(5.0) intermed = tf.add(input2, input3) mul = tf.mul(input1, intermed) with tf.Session() as sess: print sess.run([output], feed_dict={input1: [7.], input2: [2.]}) # for _ in range(3): # sess.run(update) # print sess.run(state)
def l1_reg(tensor, weight=1.0, scope=None): with tf.op_scope([tensor], scope, 'L1Regularizer'): l1_weight = tf.convert_to_tensor(weight, dtype=tensor.dtype.base_dtype, name='weight') return tf.mul(l1_weight, tf.reduce_sum(tf.abs(tensor)), name='value')
# %% Let's multiply the two to get a 2d gaussian z_2d = tf.matmul(tf.reshape(z, [n_values, 1]), tf.reshape(z, [1, n_values])) # In[ ]: # %% Execute the graph and store the value that `out` represents in `result`. plt.imshow(z_2d.eval()) # In[ ]: # %% For fun let's create a gabor patch: x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1]) y = tf.reshape(tf.ones_like(x), [1, n_values]) z = tf.mul(tf.matmul(x, y), z_2d) plt.imshow(z.eval()) # In[ ]: # %% We can also list all the operations of a graph: ops = tf.get_default_graph().get_operations() print([op.name for op in ops]) # In[ ]: # %% Lets try creating a generic function for computing the same thing: def gabor(n_values=32, sigma=1.0, mean=0.0): x = tf.linspace(-3.0, 3.0, n_values)
def __build_generic_data_tensor(self, raw_images, raw_targets, shuffle, augmentation): """ Creates the input pipeline and performs some preprocessing. """ # load the data from numpy into our queue in blocks of feed_size samples set_size, width, height, channels = raw_images.shape image_input = tf.placeholder( tf.float32, shape=[self.feed_size, width, height, channels]) target_input = tf.placeholder( tf.float32, shape=[self.feed_size, self.NUMBER_OF_CLASSES]) self.queue = tf.FIFOQueue(self.feed_queue_capacity, [tf.float32, tf.float32], shapes=[[width, height, channels], [self.NUMBER_OF_CLASSES]]) enqueue_op = self.queue.enqueue_many([image_input, target_input]) image, target = self.queue.dequeue() # Data Augmentation if augmentation: # TODO # make sure after further preprocessing it is [0 1] pass # convert the given [0, 1] to [-1, 1] image = tf.sub(image, 0.5) image = tf.mul(image, 2.0) if shuffle: images_batch, target_batch = tf.train.shuffle_batch( [image, target], batch_size=self.batch_size, capacity=self.batch_queue_capacity, min_after_dequeue=self.min_after_dequeue) else: images_batch, target_batch = tf.train.batch( [image, target], batch_size=self.batch_size, capacity=self.batch_queue_capacity) def enqueue(sess): under = 0 max = len(raw_images) while not self.coord.should_stop(): upper = under + self.feed_size if upper <= max: curr_data = raw_images[under:upper] curr_target = raw_targets[under:upper] under = upper else: rest = upper - max curr_data = np.concatenate( (raw_images[under:max], raw_images[0:rest])) curr_target = np.concatenate( (raw_targets[under:max], raw_targets[0:rest])) under = rest sess.run(enqueue_op, feed_dict={ image_input: curr_data, target_input: curr_target }) enqueue_thread = threading.Thread(target=enqueue, args=[self.sess]) self.coord = tf.train.Coordinator() self.threads = tf.train.start_queue_runners(coord=self.coord, sess=self.sess) enqueue_thread.isDaemon() enqueue_thread.start() return images_batch, target_batch
def build_graph(self): """ Build the computation graph. """ self._left = tf.placeholder(dtype=tf.int64, shape=[None, self.sent_len], name='input_left') self._middle = tf.placeholder(dtype=tf.int64, shape=[None, self.sent_len], name='input_middle') self._right = tf.placeholder(dtype=tf.int64, shape=[None, self.sent_len], name='input_right') self._labels = tf.placeholder(dtype=tf.float32, shape=[None, self.num_classes], name='input_y') self._attention = tf.placeholder(dtype=tf.float32, shape=[None, 1], name='attention') losses = [] with tf.variable_scope('embedding-left') as scope: self._W_emb_left = _variable_on_cpu( name=scope.name, shape=[self.vocab_size, self.emb_size], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0)) sent_batch_left = tf.nn.embedding_lookup(params=self._W_emb_left, ids=self._left) input_left = tf.expand_dims(sent_batch_left, -1) with tf.variable_scope('embedding-middle') as scope: self._W_emb_middle = _variable_on_cpu( name=scope.name, shape=[self.vocab_size, self.emb_size], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0)) sent_batch_middle = tf.nn.embedding_lookup( params=self._W_emb_middle, ids=self._middle) input_middle = tf.expand_dims(sent_batch_middle, -1) with tf.variable_scope('embedding-right') as scope: self._W_emb_right = _variable_on_cpu( name=scope.name, shape=[self.vocab_size, self.emb_size], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0)) sent_batch_right = tf.nn.embedding_lookup(params=self._W_emb_right, ids=self._right) input_right = tf.expand_dims(sent_batch_right, -1) # conv + pooling layer contexts = [] for contextwise_input, context in zip( [input_left, input_middle, input_right], ['left', 'middle', 'right']): conv_losses, pool_flat = self.conv_layer(contextwise_input, context) losses.extend(conv_losses) contexts.append(pool_flat) # Combine context tensors num_filters = self.max_window - self.min_window + 1 pool_size = num_filters * self.num_kernel # 300 concat_context = tf.concat(1, contexts, name='concat') flat_context = tf.reshape(concat_context, [-1, pool_size * 3]) # drop out layer if self.is_train and self.dropout > 0: pool_dropout = tf.nn.dropout(flat_context, 1 - self.dropout) else: pool_dropout = flat_context # fully-connected layer with tf.variable_scope('output') as scope: W, wd = _variable_with_weight_decay( 'W', shape=[pool_size * 3, self.num_classes], initializer=tf.truncated_normal_initializer(stddev=0.05), wd=self.l2_reg) losses.append(wd) biases = _variable_on_cpu( 'bias', shape=[self.num_classes], initializer=tf.constant_initializer(0.01)) self.logits = tf.nn.bias_add(tf.matmul(pool_dropout, W), biases, name='logits') # loss with tf.variable_scope('loss') as scope: if self.multi_label: cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits( self.logits, self._labels, name='cross_entropy_per_example') else: cross_entropy = tf.nn.softmax_cross_entropy_with_logits( self.logits, self._labels, name='cross_entropy_per_example') if self.is_train and self.multi_instance: # apply attention cross_entropy_loss = tf.reduce_sum(tf.mul( cross_entropy, self._attention), name='cross_entropy_loss') else: cross_entropy_loss = tf.reduce_mean(cross_entropy, name='cross_entropy_loss') losses.append(cross_entropy_loss) self._total_loss = tf.add_n(losses, name='total_loss') # eval with auc-pr metric with tf.variable_scope('evaluation') as scope: precision = [] recall = [] for threshold in range(10, -1, -1): pre, rec = _auc_pr(self._labels, self.logits, threshold * 0.1) precision.append(pre) recall.append(rec) self._eval_op = zip(precision, recall) # train on a batch self._lr = tf.Variable(0.0, trainable=False) if self.is_train: if self.optimizer == 'adadelta': opt = tf.train.AdadeltaOptimizer(self._lr) elif self.optimizer == 'adagrad': opt = tf.train.AdagradOptimizer(self._lr) elif self.optimizer == 'adam': opt = tf.train.AdamOptimizer(self._lr) elif self.optimizer == 'sgd': opt = tf.train.GradientDescentOptimizer(self._lr) else: raise ValueError("Optimizer not supported.") grads = opt.compute_gradients(self._total_loss) self._train_op = opt.apply_gradients(grads) for var in tf.trainable_variables(): tf.histogram_summary(var.op.name, var) else: self._train_op = tf.no_op() return
def model(X, w): return tf.mul(X, w) # lr is just X*w so this model line is pretty simple
def main(c): ''' params: c: config dictionary ''' # Data --------------------------------------------------------------------------------------------------- data_portion = None # 2 * batch_size train_set = Dstc2('data/dstc2/data.dstc2.train.json', sample_unk=0.01, first_n=data_portion) valid_set = Dstc2('data/dstc2/data.dstc2.dev.json', first_n=data_portion, sample_unk=0, max_dial_len=train_set.max_dial_len, words_vocab=train_set.words_vocab, labels_vocab=train_set.labels_vocab, labels_vocab_separate=train_set.labels_vocab_separate) test_set = Dstc2('data/dstc2/data.dstc2.test.json', first_n=data_portion, sample_unk=0, max_dial_len=train_set.max_dial_len, words_vocab=train_set.words_vocab, labels_vocab=train_set.labels_vocab, labels_vocab_separate=train_set.labels_vocab_separate) stats(train_set, valid_set, test_set) vocab_size = len(train_set.words_vocab) output_dim = max(np.unique(train_set.labels)) + 1 n_train_batches = len(train_set.dialogs) // c.batch_size # output dimensions for each separate label output_dims = [] for i in range(3): o_d = max(np.unique(train_set.labels_separate[:, :, i])) + 1 output_dims.append(o_d) # Model ----------------------------------------------------------------------------------------------------- logging.info('Creating model') input_bt = tf.placeholder('int32', [c.batch_size, train_set.max_turn_len], name='input') turn_lens_b = tf.placeholder('int32', [c.batch_size], name='turn_lens') mask_b = tf.placeholder('int32', [c.batch_size], name='dial_mask') # labels_b = tf.placeholder('int64', [c.batch_size], name='labels') # onehot_labels_bo = tf.one_hot(indices=labels_b, # depth=output_dim, # on_value=1.0, # off_value=0.0, # axis=-1) # separate labels and their onehots labels0_b, onehot_labels0_bo0 = get_labels_with_onehot( c.batch_size, output_dims[0], 'labels0') labels1_b, onehot_labels1_bo1 = get_labels_with_onehot( c.batch_size, output_dims[1], 'labels1') labels2_b, onehot_labels2_bo2 = get_labels_with_onehot( c.batch_size, output_dims[2], 'labels2') is_first_turn = tf.placeholder(tf.bool) gru = GRUCell(c.hidden_state_dim) embeddings_we = tf.get_variable( 'word_embeddings', initializer=tf.random_uniform([vocab_size, c.embedding_dim], -1.0, 1.0)) embedded_input_bte = tf.nn.embedding_lookup(embeddings_we, input_bt) dialog_state_before_turn = tf.get_variable( 'dialog_state_before_turn', initializer=tf.zeros([c.batch_size, c.hidden_state_dim], dtype='float32'), trainable=False) before_state_bh = cond( is_first_turn, lambda: gru.zero_state(c.batch_size, dtype='float32'), lambda: dialog_state_before_turn) inputs = [ tf.squeeze(i, squeeze_dims=[1]) for i in tf.split(1, train_set.max_turn_len, embedded_input_bte) ] outputs, state_bh = tf.nn.rnn(cell=gru, inputs=inputs, initial_state=before_state_bh, sequence_length=turn_lens_b, dtype=tf.float32) dialog_state_before_turn.assign(state_bh) # projection_ho = tf.get_variable('project2labels', # initializer=tf.random_uniform([c.hidden_state_dim, output_dim], -1.0, 1.0)) # logits_bo = tf.matmul(state_bh, projection_ho) # tf.histogram_summary('logits', logits_bo) # probabilities_bo = tf.nn.softmax(logits_bo) # tf.histogram_summary('probabilities', probabilities_bo) # logits and probabilites and predictions from hidden state logits_bo0, probabilities_bo0, predict_b0 = get_logits_and_probabilities( state_bh, c.hidden_state_dim, output_dims[0], 'labels0') logits_bo1, probabilities_bo1, predict_b1 = get_logits_and_probabilities( state_bh, c.hidden_state_dim, output_dims[1], 'labels1') logits_bo2, probabilities_bo2, predict_b2 = get_logits_and_probabilities( state_bh, c.hidden_state_dim, output_dims[2], 'labels2') float_mask_b = tf.cast(mask_b, 'float32') # loss = tf.reduce_sum(tf.mul(float_mask_b, x_entropy(logits_bo, onehot_labels_bo))) / tf.reduce_sum(float_mask_b) # tf.scalar_summary('CCE loss', loss) # losses loss_0 = tf.reduce_sum( tf.mul(float_mask_b, x_entropy( logits_bo0, onehot_labels0_bo0))) / tf.reduce_sum(float_mask_b) loss_1 = tf.reduce_sum( tf.mul(float_mask_b, x_entropy( logits_bo1, onehot_labels1_bo1))) / tf.reduce_sum(float_mask_b) loss_2 = tf.reduce_sum( tf.mul(float_mask_b, x_entropy( logits_bo2, onehot_labels2_bo2))) / tf.reduce_sum(float_mask_b) loss = loss_0 + loss_1 + loss_2 tf.scalar_summary('CCE loss', loss) # predict_b = tf.argmax(logits_bo, 1) # correct = tf.cast(tf.equal(predict_b, labels_b), 'float32') # accuracy = tf.reduce_sum(tf.mul(correct, float_mask_b)) / tf.reduce_sum(float_mask_b) # tf.scalar_summary('Accuracy', accuracy) # correct correct_0 = tf.cast(tf.equal(predict_b0, labels0_b), 'float32') correct_1 = tf.cast(tf.equal(predict_b1, labels1_b), 'float32') correct_2 = tf.cast(tf.equal(predict_b2, labels2_b), 'float32') correct_all = tf.mul(tf.mul(correct_0, correct_1), correct_2) # accuracies accuracy_0 = get_accuracy(correct_0, float_mask_b) accuracy_1 = get_accuracy(correct_1, float_mask_b) accuracy_2 = get_accuracy(correct_2, float_mask_b) accuracy_all = get_accuracy(correct_all, float_mask_b) tf.scalar_summary('Accuracy all', accuracy_all) tf.scalar_summary('Accuracy label 0', accuracy_0) tf.scalar_summary('Accuracy label 1', accuracy_1) tf.scalar_summary('Accuracy label 2', accuracy_2) tb_info = tf.merge_all_summaries() # Optimizer ----------------------------------------------------------------------------------------------------- logging.info('Creating optimizer') optimizer = tf.train.AdamOptimizer(c.learning_rate) logging.info('Creating train_op') train_op = optimizer.minimize(loss) # Session ----------------------------------------------------------------------------------------------------- logging.info('Creating session') sess = tf.Session() logging.info('Initing variables') init = tf.initialize_all_variables() logging.info('Running session') sess.run(init) # TB --------------------------------------------------------------------------------------------------------- logging.info('See stats via tensorboard: $ tensorboard --logdir %s', c.log_dir) train_writer = tf.train.SummaryWriter(c.log_dir, sess.graph) # Train --------------------------------------------------------------------------------------------------------- train_summary = None step, stopper = 0, EarlyStopper(c.nbest_models, c.not_change_limit, c.name) try: for e in range(c.epochs): logging.info('------------------------------') logging.info('Epoch %d', e) total_loss = 0 total_acc = 0 batch_count = 0 for bid, (dialogs_bTt, lengths_bT, labels0_bT, labels1_bT, labels2_bT, masks_bT) in enumerate( next_batch(train_set, c.batch_size)): turn_loss = 0 turn_acc = 0 n_turns = 0 first_run = True for (turn_bt, label0_b, label1_b, label2_b, lengths_b, masks_b) in zip(dialogs_bTt.transpose([1, 0, 2]), labels0_bT.transpose([1, 0]), labels1_bT.transpose([1, 0]), labels2_bT.transpose([1, 0]), lengths_bT.transpose([1, 0]), masks_bT.transpose([1, 0])): if sum(masks_b) == 0: break _, batch_loss, batch_accuracy, train_summary = sess.run( [train_op, loss, accuracy_all, tb_info], feed_dict={ input_bt: turn_bt, turn_lens_b: lengths_b, mask_b: masks_b, labels0_b: label0_b, labels1_b: label1_b, labels2_b: label2_b, is_first_turn: first_run }) first_run = False turn_loss += batch_loss turn_acc += batch_accuracy n_turns += 1 step += 1 total_loss += turn_loss / n_turns total_acc += turn_acc / n_turns batch_count += 1 logging.info('Batch %d/%d\r', bid, n_train_batches) train_writer.add_summary(train_summary, e) logging.info('Train cost %f', total_loss / batch_count) logging.info('Train accuracy: %f', total_acc / batch_count) def monitor_stream(work_set, name): total_loss = 0 total_acc = 0 n_valid_batches = 0 for bid, (dialogs_bTt, lengths_bT, labels0_bT, labels1_bT, labels2_bT, masks_bT) in enumerate( next_batch(work_set, c.batch_size)): turn_loss = 0 turn_acc = 0 n_turns = 0 first_run = True for (turn_bt, label0_b, label1_b, label2_b, lengths_b, masks_b) in zip(dialogs_bTt.transpose([1, 0, 2]), labels0_bT.transpose([1, 0]), labels1_bT.transpose([1, 0]), labels2_bT.transpose([1, 0]), lengths_bT.transpose([1, 0]), masks_bT.transpose([1, 0])): if sum(masks_b) == 0: break input = np.pad(turn_bt, ((0, 0), (0, train_set.max_turn_len-turn_bt.shape[1])), 'constant', constant_values=0) if train_set.max_turn_len > turn_bt.shape[1]\ else turn_bt batch_loss, batch_acc, valid_summary = sess.run( [loss, accuracy_all, tb_info], feed_dict={ input_bt: input, turn_lens_b: lengths_b, labels0_b: label0_b, labels1_b: label1_b, labels2_b: label2_b, mask_b: masks_b, is_first_turn: first_run }) turn_loss += batch_loss turn_acc += batch_acc first_run = False n_turns += 1 total_loss += turn_loss / n_turns total_acc += turn_acc / n_turns n_valid_batches += 1 logging.info('%s cost: %f', name, total_loss / n_valid_batches) logging.info('%s accuracy: %f', name, total_acc / n_valid_batches) return total_loss / n_valid_batches stopper_reward = monitor_stream(valid_set, 'Valid') monitor_stream(test_set, 'Test') if not stopper.save_and_check(stopper_reward, step, sess): raise RuntimeError('Training not improving on dev set') finally: logging.info( 'Training stopped after %7d steps and %7.2f epochs. See logs for %s', step, step / len(train_set), c.log_name) logging.info( 'Saving current state. Please wait!\nBest model has reward %7.2f form step %7d is %s' % stopper.highest_reward()) stopper.saver.save(sess=sess, save_path='%s-FINAL-%.4f-step-%07d' % (stopper.saver_prefix, stopper_reward, step))
hidden = tf.nn.relu(tf.matmul(relu_flat, fc1_weights) + fc1_biases) return tf.matmul(hidden, fc2_weights) + fc2_biases sess = tf.InteractiveSession() sess.as_default() action_array_1 = network(state_input_1) tt = reward_input + terminal_input * (GAMMA * max_val_input) tt = tf.reshape(tt, (BATCH, 1)) target_prep = tf.tile(tt, [1, 4]) target = tf.select(action_input, target_prep, action_array_1) Qerror = tf.sub(target, action_array_1) loss = .5 * tf.reduce_sum(tf.mul(Qerror, Qerror)) optimizer = tf.train.GradientDescentOptimizer(1e-4).minimize(loss) saver = tf.train.Saver() tf.initialize_all_variables().run() checkpoint = tf.train.get_checkpoint_state("saved_networks") if checkpoint and checkpoint.model_checkpoint_path: saver.restore(sess, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) def see_action(action, i, j): if action == actions[0]:
def __init__(self, numActions, baseDir, args): self.numActions = numActions self.baseDir = baseDir self.saveModelFrequency = args.save_model_freq self.targetModelUpdateFrequency = args.target_model_update_freq self.normalizeWeights = args.normalize_weights self.batchCount = 0 self.staleSess = None tf.set_random_seed(123456) self.sess = tf.Session() assert (len(tf.all_variables()) == 0), "Expected zero variables" self.x, self.y = self.buildNetwork('policy', True, numActions) assert (len( tf.trainable_variables()) == 10), "Expected 10 trainable_variables" assert (len(tf.all_variables()) == 10), "Expected 10 total variables" self.x_target, self.y_target = self.buildNetwork( 'target', False, numActions) assert (len( tf.trainable_variables()) == 10), "Expected 10 trainable_variables" assert (len(tf.all_variables()) == 20), "Expected 20 total variables" # build the variable copy ops self.update_target = [] trainable_variables = tf.trainable_variables() all_variables = tf.all_variables() for i in range(0, len(trainable_variables)): self.update_target.append( all_variables[len(trainable_variables) + i].assign( trainable_variables[i])) self.a = tf.placeholder(tf.float32, shape=[None, numActions]) print('a %s' % (self.a.get_shape())) self.y_ = tf.placeholder(tf.float32, [None]) print('y_ %s' % (self.y_.get_shape())) self.y_a = tf.reduce_sum(tf.mul(self.y, self.a), reduction_indices=1) print('y_a %s' % (self.y_a.get_shape())) difference = tf.abs(self.y_a - self.y_) quadratic_part = tf.clip_by_value(difference, 0.0, 1.0) linear_part = difference - quadratic_part errors = (0.5 * tf.square(quadratic_part)) + linear_part self.loss = tf.reduce_sum(errors) #self.loss = tf.reduce_mean(tf.square(self.y_a - self.y_)) # (??) learning rate # Note tried gradient clipping with rmsprop with this particular loss function and it seemed to suck # Perhaps I didn't run it long enough #optimizer = GradientClippingOptimizer(tf.train.RMSPropOptimizer(args.learning_rate, decay=.95, epsilon=.01)) optimizer = tf.train.RMSPropOptimizer(args.learning_rate, decay=.95, epsilon=.01) self.train_step = optimizer.minimize(self.loss) self.saver = tf.train.Saver(max_to_keep=25) # Initialize variables self.sess.run(tf.initialize_all_variables()) self.sess.run(self.update_target) # is this necessary? self.summary_writer = tf.train.SummaryWriter( self.baseDir + '/tensorboard', self.sess.graph_def) if args.model is not None: print('Loading from model file %s' % (args.model)) self.saver.restore(self.sess, args.model)
def compile(self, compile_cfgs): # Constants dictating the learning rate schedule. RMSPROP_DECAY = 0.9 # Decay term for RMSProp. RMSPROP_MOMENTUM = 0.9 # Momentum in RMSProp. RMSPROP_EPSILON = 1.0 # Epsilon term for RMSProp. try: self.output = self.get_output(self.cfgs['optimization_node']) self.pred = self.get_output(self.cfgs['prediction_node']) # computation of performance metrics if compile_cfgs['metric'] == 'precision': self.t_pred = tf.cast( tf.greater(self.pred, compile_cfgs['global_threshold']), 'float') self.correct_pred = tf.mul(self.t_pred, self.labels) self.accuracy = tf.reduce_sum(self.correct_pred) / ( tf.reduce_sum(self.t_pred) + _EPSILON) elif compile_cfgs['metric'] == 'top-k accuracy': _, indices = tf.nn.top_k(self.pred, k=compile_cfgs['top_k']) dense_top_k = tf.one_hot( tf.to_int64(indices), tf.shape(self.labels)[len(self.labels.get_shape()) - 1], 1.0, 0.0, axis=-1) dense_top_k = tf.reshape( tf.reduce_sum(dense_top_k, 1, keep_dims=True), tf.shape(self.pred)) self.correct_pred = tf.mul(dense_top_k, self.labels) self.accuracy = tf.reduce_sum(self.correct_pred) / ( tf.reduce_sum(dense_top_k) + _EPSILON) #if # # Make a distribution out of labels # self.loss = tf.reduce_mean( loss_functions_dict[compile_cfgs['loss']](self.output, self.labels)) # # optimizer # global_step = tf.get_variable( 'global_step', [], initializer=tf.constant_initializer(0), trainable=False) # Calculate the learning rate schedule. decay_steps = compile_cfgs['stepsize'] # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay(compile_cfgs['lr'], global_step, decay_steps, compile_cfgs['decay'], staircase=True) # Create an optimizer that performs gradient descent. opt = optimizer_dict[compile_cfgs['optimizer']](lr) self.optimizer = opt.minimize(self.loss) # # Initializing the variables init = tf.initialize_all_variables() self.sess = tf.Session() self.sess.run(init) self.load_weights() # good to go self.init = True except Exception as err: self.io.print_error('Could not compile model {}'.format(err)) self.init = False
__author__ = 'jaehyek' import tensorflow as tf x_data = [ 1., 2., 3.] y_data = [ 1., 2., 3.] W = tf.Variable(tf.random_uniform([1], -10.0, 10.0)) X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) hypothesis = W * X cost = tf.reduce_mean(tf.square(hypothesis - Y)) descent = W - tf.mul(0.1, tf.reduce_mean(tf.mul((tf.mul(W,X) - Y), X ))) update = W.assign(descent) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) for step in xrange(100): sess.run(update, feed_dict={X:x_data, Y:y_data}) print step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(W)
def get_accuracy(correct, mask): return tf.reduce_sum(tf.mul(correct, mask)) / tf.reduce_sum(mask)
def prediction(self): num_steps = self.config.num_steps frame_size = self.config.num_input * self.config.embedding_size inputs = tf.nn.embedding_lookup(self.embedding, self._input_data) inputs = tf.reshape(inputs, [-1, num_steps, frame_size]) batch_size = tf.shape(inputs)[0] size = self.config.hidden_size num_class = self.config.num_class if self.config.model_type == "LSTM": cell = tf.nn.rnn_cell.BasicLSTMCell(size, forget_bias=1.0, state_is_tuple=True) #cell._activation = tf.nn.relu if self.is_training and self.config.keep_prob < 1: print 'hi' #inputs = tf.nn.dropout(inputs, self.config.keep_prob) #cell = rnn_cell.DropoutWrapper( #cell, output_keep_prob=self.config.keep_prob) # stack layers of LSTM num_layers = self.config.num_layers if num_layers > 1: cell = tf.nn.rnn_cell.MultiRNNCell([cell] * num_layers) self._initial_state = cell.zero_state(batch_size, tf.float32) outputs, state = tf.nn.rnn(cell, unpack_sequence(inputs), sequence_length=self._length, initial_state=self._initial_state) output_w = weight_variable([size, num_class]) output_b = bias_variable([num_class]) # Recurrent network. predictions = [] tag_trans = weight_variable([num_class, num_class]) previous = tf.zeros([batch_size, num_class]) # Recurrent network. for ids, x in enumerate(outputs): res = tf.matmul(x, output_w) + output_b deviation = tf.tile( tf.expand_dims(tf.reduce_min(previous, reduction_indices=1), 1), [1, num_class]) previous -= deviation focus = 1. res += tf.matmul(previous, tag_trans) * focus prediction = tf.nn.softmax(res) predictions.append(prediction) previous = prediction mask = tf.less( tf.mul(tf.ones([batch_size], dtype=tf.int32), ids + 1), self._length) mask = tf.expand_dims(mask, 1) masks = tf.tile(mask, [1, num_class]) element_mask = tf.select(masks, tf.ones([batch_size, num_class]), tf.zeros([batch_size, num_class])) previous = tf.mul(previous, element_mask) pred = pack_sequence(predictions) ''' shape of prediction should be: [batch, num_step, num_class]''' return pred
# Declare Zip code distance for a test zip and reference set zip_dist = tf.square(tf.sub(ref_zip, test_zip)) # Declare Edit distance for address address_dist = tf.edit_distance(test_address, ref_address, normalize=True) # Create similarity scores zip_max = tf.gather(tf.squeeze(zip_dist), tf.argmax(zip_dist, 1)) zip_min = tf.gather(tf.squeeze(zip_dist), tf.argmin(zip_dist, 1)) zip_sim = tf.div(tf.sub(zip_max, zip_dist), tf.sub(zip_max, zip_min)) address_sim = tf.sub(1., address_dist) # Combine distance functions address_weight = 0.5 zip_weight = 1. - address_weight weighted_sim = tf.add(tf.transpose(tf.mul(address_weight, address_sim)), tf.mul(zip_weight, zip_sim)) # Predict: Get max similarity entry top_match_index = tf.argmax(weighted_sim, 1) # Function to Create a character-sparse tensor from strings def sparse_from_word_vec(word_vec): num_words = len(word_vec) indices = [[xi, 0, yi] for xi, x in enumerate(word_vec) for yi, y in enumerate(x)] chars = list(''.join(word_vec)) return (tf.SparseTensorValue(indices, chars, [num_words, 1, 1]))
#unpool a = tf.transpose(risa_root, perm=[1, 2, 3, 0]) b = tf.reshape(a, [train_batch, sy, sx * nf1 / risa_pool, 1]) c = tf.tile(b, tf.to_int32(tf.constant(np.array([1, 1, 1, risa_pool])))) risa_unpool = tf.reshape(c, [train_batch, sy, sx, nf1]) ## error check # deconv_compute output_shape1 = [train_batch, sy, sx, nl] h_deconv_c = tf.nn.relu( conv2d_transpose(h_conv1 - b_risa1, W_risa1, output_shape1)) norm = tf.reduce_mean(tf.global_norm([h_deconv_c - x_image])) error = tf.add(tf.reduce_sum(risa_root), tf.mul(tf.to_float(tf.constant(lambda_r)), norm)) train_step = tf.train.AdamOptimizer(learning_rate).minimize(error) #deconv_ae h_deconv_ae = tf.nn.relu( conv2d_transpose(risa_unpool - b_risa1, W_risa1, output_shape1)) #initialize the variables init = tf.initialize_all_variables() #save and restore variables saver = tf.train.Saver() #launch Session sess = tf.InteractiveSession() sess.run(init)
def __init__(self, is_training, config, input_, dic_): self._input = input_ batch_size = input_.batch_size num_steps = input_.num_steps size = config.hidden_size vocab_size = config.vocab_size # Slightly better results can be obtained with forget gate biases # initialized to 1 but the hyperparameters of the model would need to be # different than reported in the paper. lstm_cell = rnn_cell.BasicLSTMCell(size, forget_bias=0.0, state_is_tuple=True) if is_training and config.keep_prob < 1: lstm_cell = tf.nn.rnn_cell.DropoutWrapper( lstm_cell, output_keep_prob=config.keep_prob) cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * config.num_layers, state_is_tuple=True) self._initial_state = cell.zero_state(batch_size, data_type()) with tf.device("/cpu:0"): self.embedding = tf.get_variable( "embedding", [vocab_size, size], dtype=data_type()) inputs = tf.nn.embedding_lookup(self.embedding, input_.input_data) left = tf.nn.embedding_lookup(self.embedding, dic_[0]) right_a = tf.nn.embedding_lookup(self.embedding, dic_[1]) right_b = tf.nn.embedding_lookup(self.embedding, dic_[2]) if is_training and config.keep_prob < 1: inputs = tf.nn.dropout(inputs, config.keep_prob) # Simplified version of tensorflow.models.rnn.rnn.py's rnn(). # This builds an unrolled LSTM for tutorial purposes only. # In general, use the rnn() or state_saving_rnn() from rnn.py. # # The alternative version of the code below is: # # inputs = [tf.squeeze(input_step, [1]) # for input_step in tf.split(1, num_steps, inputs)] # outputs, state = tf.nn.rnn(cell, inputs, initial_state=self._initial_state) outputs = [] state = self._initial_state with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step > 0: tf.get_variable_scope().reuse_variables() (cell_output, state) = cell(inputs[:, time_step, :], state) outputs.append(cell_output) output = tf.reshape(tf.concat(1, outputs), [-1, size]) softmax_w = tf.get_variable( "softmax_w", [size, vocab_size], dtype=data_type()) softmax_b = tf.get_variable("softmax_b", [vocab_size], dtype=data_type()) logits = tf.matmul(output, softmax_w) + softmax_b loss = tf.nn.seq2seq.sequence_loss_by_example( [logits], [tf.reshape(input_.targets, [-1])], [tf.ones([batch_size * num_steps], dtype=data_type())]) self._cost = cost = tf.reduce_sum(loss) / batch_size self._final_state = state if not is_training: return self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) self._train_op = optimizer.apply_gradients( zip(grads, tvars), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder( tf.float32, shape=[], name="new_learning_rate") self._lr_update = tf.assign(self._lr, self._new_lr) similarity_a = tf.reduce_sum(tf.mul(left, right_a),1) similarity_b = tf.reduce_sum(tf.mul(left, right_b),1) raw_scores = tf.sub(similarity_a, similarity_b, name = "raw_scores") self._score = tf.reduce_sum(tf.sign(raw_scores))
def __init__(self): self.session = tf.Session() ''' Training parameters: ''' self.w2v_dim = 30 self.num_feature = 400 self.batch_size = 32 self.num_epoch = 10000 self.num_hidden_1 = 50 self.num_hidden_2 = 3 self.number_of_layers = 1 #self.max_len = 50 self.max_len_title = 6 self.max_len_body = 38 #self.w2v_model=Word2Vec.load_word2vec_format('./data/word2vec/GoogleNews-vectors-negative300.bin', binary=True) self.w2v_model = Word2Vec.load('data/word2vec/w2v.model') self.index2word_set = set(self.w2v_model.index2word) #self.bigram = None #self.trigram =None self.bigram = Phrases.load('./data/bigram.dat') self.trigram = Phrases.load('./data/trigram.dat') # Model self.input_0 = tf.placeholder( tf.float32, [self.max_len_title, self.batch_size, self.w2v_dim]) self.input_1 = tf.placeholder( tf.float32, [self.max_len_title, self.batch_size, self.w2v_dim]) self.input_0_ = tf.placeholder( tf.float32, [self.max_len_body, self.batch_size, self.w2v_dim]) self.input_1_ = tf.placeholder( tf.float32, [self.max_len_body, self.batch_size, self.w2v_dim]) self.dropout_input = tf.placeholder(tf.float32) self.dropout_hidden = tf.placeholder(tf.float32) self.target = tf.placeholder(tf.float32, [self.batch_size, 3]) input_0 = array_ops.unpack(self.input_0) input_1 = array_ops.unpack(self.input_1) input_0_ = array_ops.unpack(self.input_0_) input_1_ = array_ops.unpack(self.input_1_) def _rnn(inputs, reverse=False): with tf.variable_scope("GRU_RNN") as scope: cell = rnn_cell.GRUCell(self.w2v_dim) cell = rnn_cell.DropoutWrapper( cell, output_keep_prob=self.dropout_input) stacked_cell = rnn_cell.MultiRNNCell([cell] * self.number_of_layers) state = stacked_cell.zero_state(self.batch_size, tf.float32) if reverse: inputs = reversed(inputs) for time, input_ in enumerate(inputs): if time > 0: scope.reuse_variables() output, state = stacked_cell(input_, state) return state with tf.variable_scope('Feature_Generator') as scope: state_0 = _rnn(input_0) scope.reuse_variables() state_1 = _rnn(input_1) state_0_ = _rnn(input_0_) state_1_ = _rnn(input_1_) ''' with tf.variable_scope('Feature_Generator_body') as scope: state_0_ = _rnn(input_0_) scope.reuse_variables() state_1_ = _rnn(input_1_) ''' ''' with tf.variable_scope('Feature_Generator_body_reverse') as scope: state_0_reverse = _rnn(input_0_, reverse=True) scope.reuse_variables() state_1_reverse = _rnn(input_1_, reverse=True) ''' ''' with tf.variable_scope('Feature_Generator_title') as scope: state_0 = _rnn(input_0) scope.reuse_variables() state_1 = _rnn(input_1) with tf.variable_scope('Feature_Generator_body') as scope: state_0_ = _rnn(input_0_) scope.reuse_variables() state_1_ = _rnn(input_1_) # state=tf.concat(1,[tf.abs(tf.sub(state_0,state_1)),tf.mul(state_0,state_1), # tf.abs(tf.sub(state_0_,state_1_)),tf.mul(state_0_,state_1_)]) # state=tf.concat(1,[state_0,state_1, state_0_, state_1_]) # state = tf.ones([32,10]) # state=tf.concat(1,[tf.abs(tf.sub(state_0,state_1)),tf.mul(state_0,state_1)]) ''' # 2-layer NN with tf.variable_scope("NN", initializer=tf.random_uniform_initializer( -1.0, 1.0)): self.W_mul = tf.get_variable( "W_mul", [state_0_.get_shape()[1] * 2, self.num_hidden_1]) self.W_sub = tf.get_variable( "W_sub", [state_0_.get_shape()[1] * 2, self.num_hidden_1]) self.b = tf.get_variable("b", [self.num_hidden_1]) self.W_softmax = tf.get_variable( "W_softmax", [self.num_hidden_1, self.num_hidden_2]) self.b_softmax = tf.get_variable("b_softmax", [self.num_hidden_2]) # h_mul = tf.mul(state_0,state_1) # h_sub = tf.abs(tf.sub(state_0,state_1)) h_mul = tf.concat( 1, [tf.mul(state_0, state_1), tf.mul(state_0_, state_1_)]) h_sub = tf.concat(1, [ tf.abs(tf.sub(state_0, state_1)), tf.abs(tf.sub(state_0_, state_1_)) ]) y_1 = tf.nn.sigmoid( tf.matmul(h_mul, self.W_mul) + tf.matmul(h_sub, self.W_sub) + self.b) y_2 = tf.matmul(y_1, self.W_softmax) + self.b_softmax # regularizers = (tf.nn.l2_loss(self.W_1) + tf.nn.l2_loss(self.b_1)+tf.nn.l2_loss(self.W_2) + tf.nn.l2_loss(self.b_2)) ''' state_0_title_normalized = tf.nn.l2_normalize(state_0, 1) state_1_title_normalized = tf.nn.l2_normalize(state_1, 1) state_0_body_normalized = tf.nn.l2_normalize(state_0_, 1) state_1_body_normalized = tf.nn.l2_normalize(state_1_, 1) dist_title_ = tf.mul(state_0_title_normalized, state_1_title_normalized) dist_body_ = tf.mul(state_0_body_normalized, state_1_body_normalized)s dist_title=tf.reduce_sum(dist_title_, 1, keep_dims=True) dist_body=tf.reduce_sum(dist_body_, 1, keep_dims=True) feature = tf.concat(1, [dist_title,dist_body]) with tf.variable_scope("log_reg", initializer=tf.random_uniform_initializer()): self.W = tf.get_variable("W", [feature.get_shape()[1],3]) self.b = tf.get_variable("b", [3]) y_2 = tf.matmul(feature, self.W)+self.b ''' ''' with tf.variable_scope("log_reg", initializer=tf.random_uniform_initializer()): self.W_1 = tf.get_variable("W_1", [state.get_shape()[1],self.num_hidden_1]) self.b_1 = tf.get_variable("b_1", [self.num_hidden_1]) self.W_2 = tf.get_variable("W_2", [self.num_hidden_1,self.num_hidden_2]) self.b_2 = tf.get_variable("b_2", [self.num_hidden_2]) ''' ''' # Create model def multilayer_perceptron(_X, _weights, _biases): layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1'])) #Hidden layer with RELU activation layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2'])) #Hidden layer with RELU activation return tf.matmul(layer_2, _weights['out']) + _biases['out'] # Store layers weight & bias weights = { 'h1': tf.Variable(tf.random_normal([10, 10])), 'h2': tf.Variable(tf.random_normal([10, 5])), 'out': tf.Variable(tf.random_normal([5, 3])) } biases = { 'b1': tf.Variable(tf.random_normal([10])), 'b2': tf.Variable(tf.random_normal([5])), 'out': tf.Variable(tf.random_normal([3])) } # Construct model self.y_pred = multilayer_perceptron(state, weights, biases) # Define loss and optimizer self.cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.y_pred, self.target)) # Softmax loss self.optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(self.cross_entropy) # Adam Optimizer ''' # self.W = tf.Variable(tf.zeros([10, 3])) # self.b = tf.Variable(tf.zeros([3])) # y_1 = tf.sigmoid(tf.matmul(state, self.W_1)+self.b_1) # y_2 = tf.sigmoid(tf.matmul(y_1, self.W_2)+self.b_2) # self.y_pred = tf.nn.softmax(tf.nn.sigmoid(tf.add(tf.matmul(state, self.W),self.b))) self.y_pred = tf.nn.softmax(y_2) # self.y_pred = tf.nn.softmax(tf.nn.sigmoid(tf.matmul(state, self.W_1)+self.b_1)) self.cross_entropy = -tf.reduce_mean(self.target * tf.log(self.y_pred)) # self.optimizer = tf.train.AdamOptimizer().minimize(self.cross_entropy) # self.optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(self.cross_entropy) # self.optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(self.cross_entropy) # self.gradstep = self.optimizer.compute_gradients(self.cross_entropy) # Optimizer. global_step = tf.Variable(0) # optimizer = tf.train.GradientDescentOptimizer(0.1) optimizer = tf.train.AdagradOptimizer(0.1) gradients, v = zip(*optimizer.compute_gradients(self.cross_entropy)) gradients, _ = tf.clip_by_global_norm(gradients, 10) self.optimizer = optimizer.apply_gradients(zip(gradients, v), global_step=global_step) print('Model has been built!')
print("train count:\t%d" % (trainSet.shape[0])) print("test count:\t%d" % (testSet.shape[0])) print("=" * 20) # matrix factorization u = tf.placeholder(tf.int32, [None, 1]) v = tf.placeholder(tf.int32, [None, 1]) r = tf.placeholder(tf.float32, [None, 1]) U = tf.Variable(tf.random_uniform([userCount, k], -0.05, 0.05)) V = tf.Variable(tf.random_uniform([itemCount, k], -0.05, 0.05)) uFactor = tf.reshape(tf.nn.embedding_lookup(U, u), [-1, k]) vFactor = tf.reshape(tf.nn.embedding_lookup(V, v), [-1, k]) y = tf.reduce_sum(tf.mul(uFactor, vFactor), 1, keep_dims=True) rmse = tf.sqrt(tf.reduce_mean(tf.square(r - y))) mae = tf.reduce_mean(tf.abs(r - y)) # loss function config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.InteractiveSession(config=config) sess.run(tf.global_variables_initializer()) uFactorRegular = tf.reduce_sum(tf.square(uFactor), 1, keep_dims=True) vFactorRegular = tf.reduce_sum(tf.square(vFactor), 1, keep_dims=True) loss = tf.reduce_mean( tf.square(r - y) + reLambda * (uFactorRegular + vFactorRegular)) trainStep = tf.train.GradientDescentOptimizer(learnRate).minimize(loss)
def __init__(self, checkpoint_path="deep_q_pong_networks", playback_mode=False, verbose_logging=False): """ Example of deep q network for pong :param checkpoint_path: directory to store checkpoints in :type checkpoint_path: str :param playback_mode: if true games runs in real time mode and demos itself running :type playback_mode: bool :param verbose_logging: If true then extra log information is printed to std out :type verbose_logging: bool """ self._time = 0 self._playback_mode = playback_mode super(DeepQPongPlayer, self).__init__(force_game_fps=8, run_real_time=playback_mode) self.verbose_logging = verbose_logging self._checkpoint_path = checkpoint_path self.convolution_weights_1 = tf.Variable( tf.truncated_normal([8, 8, DeepQPongPlayer.STATE_FRAMES, 32], stddev=0.01)) self.convolution_bias_1 = tf.Variable(tf.constant(0.01, shape=[32])) self.convolution_weights_2 = tf.Variable( tf.truncated_normal([4, 4, 32, 64], stddev=0.01)) self.convolution_bias_2 = tf.Variable(tf.constant(0.01, shape=[64])) self.convolution_weights_3 = tf.Variable( tf.truncated_normal([3, 3, 64, 64], stddev=0.01)) self.convolution_bias_3 = tf.Variable(tf.constant(0.01, shape=[64])) self.feed_forward_weights_1 = tf.Variable( tf.truncated_normal([256, 256], stddev=0.01)) self.feed_forward_bias_1 = tf.Variable(tf.constant(0.01, shape=[256])) self.feed_forward_weights_2 = tf.Variable( tf.truncated_normal([256, DeepQPongPlayer.ACTIONS_COUNT], stddev=0.01)) self.feed_forward_bias_2 = tf.Variable( tf.constant(0.01, shape=[DeepQPongPlayer.ACTIONS_COUNT])) self.target_convolution_weights_1 = tf.Variable( tf.truncated_normal([8, 8, DeepQPongPlayer.STATE_FRAMES, 32], stddev=0.01)) self.target_convolution_bias_1 = tf.Variable( tf.constant(0.01, shape=[32])) self.target_convolution_weights_2 = tf.Variable( tf.truncated_normal([4, 4, 32, 64], stddev=0.01)) self.target_convolution_bias_2 = tf.Variable( tf.constant(0.01, shape=[64])) self.target_convolution_weights_3 = tf.Variable( tf.truncated_normal([3, 3, 64, 64], stddev=0.01)) self.target_convolution_bias_3 = tf.Variable( tf.constant(0.01, shape=[64])) self.target_feed_forward_weights_1 = tf.Variable( tf.truncated_normal([256, 256], stddev=0.01)) self.target_feed_forward_bias_1 = tf.Variable( tf.constant(0.01, shape=[256])) self.target_feed_forward_weights_2 = tf.Variable( tf.truncated_normal([256, DeepQPongPlayer.ACTIONS_COUNT], stddev=0.01)) self.target_feed_forward_bias_2 = tf.Variable( tf.constant(0.01, shape=[DeepQPongPlayer.ACTIONS_COUNT])) # network weights self.input_layer = tf.placeholder("float", [ None, DeepQPongPlayer.RESIZED_SCREEN_X, DeepQPongPlayer.RESIZED_SCREEN_Y, DeepQPongPlayer.STATE_FRAMES ]) hidden_convolutional_layer_1 = tf.nn.relu( tf.nn.conv2d(self.input_layer, self.convolution_weights_1, strides=[1, 4, 4, 1], padding="SAME") + self.convolution_bias_1) hidden_max_pooling_layer_1 = tf.nn.max_pool( hidden_convolutional_layer_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") hidden_convolutional_layer_2 = tf.nn.relu( tf.nn.conv2d(hidden_max_pooling_layer_1, self.convolution_weights_2, strides=[1, 2, 2, 1], padding="SAME") + self.convolution_bias_2) hidden_max_pooling_layer_2 = tf.nn.max_pool( hidden_convolutional_layer_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") hidden_convolutional_layer_3 = tf.nn.relu( tf.nn.conv2d(hidden_max_pooling_layer_2, self.convolution_weights_3, strides=[1, 1, 1, 1], padding="SAME") + self.convolution_bias_3) hidden_max_pooling_layer_3 = tf.nn.max_pool( hidden_convolutional_layer_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") hidden_convolutional_layer_3_flat = tf.reshape( hidden_max_pooling_layer_3, [-1, 256]) final_hidden_activations = tf.nn.relu( tf.matmul(hidden_convolutional_layer_3_flat, self.feed_forward_weights_1) + self.feed_forward_bias_1) fiction_dropout = tf.nn.dropout(final_hidden_activations, keep_prob=0.5) self.output_layer = tf.matmul( fiction_dropout, self.feed_forward_weights_2) + self.feed_forward_bias_2 # return input_layer, output_layer # @staticmethod # def _create_target_network(): # network weights self.target_input_layer = tf.placeholder("float", [ None, DeepQPongPlayer.RESIZED_SCREEN_X, DeepQPongPlayer.RESIZED_SCREEN_Y, DeepQPongPlayer.STATE_FRAMES ]) target_hidden_convolutional_layer_1 = tf.nn.relu( tf.nn.conv2d(self.target_input_layer, self.target_convolution_weights_1, strides=[1, 4, 4, 1], padding="SAME") + self.target_convolution_bias_1) target_hidden_max_pooling_layer_1 = tf.nn.max_pool( target_hidden_convolutional_layer_1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") target_hidden_convolutional_layer_2 = tf.nn.relu( tf.nn.conv2d(target_hidden_max_pooling_layer_1, self.target_convolution_weights_2, strides=[1, 2, 2, 1], padding="SAME") + self.target_convolution_bias_2) target_hidden_max_pooling_layer_2 = tf.nn.max_pool( target_hidden_convolutional_layer_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") target_hidden_convolutional_layer_3 = tf.nn.relu( tf.nn.conv2d(target_hidden_max_pooling_layer_2, self.target_convolution_weights_3, strides=[1, 1, 1, 1], padding="SAME") + self.target_convolution_bias_3) target_hidden_max_pooling_layer_3 = tf.nn.max_pool( target_hidden_convolutional_layer_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") target_hidden_convolutional_layer_3_flat = tf.reshape( target_hidden_max_pooling_layer_3, [-1, 256]) target_final_hidden_activations = tf.nn.relu( tf.matmul(target_hidden_convolutional_layer_3_flat, self.target_feed_forward_weights_1) + self.target_feed_forward_bias_1) self.target_output_layer = tf.matmul( target_final_hidden_activations, self.target_feed_forward_weights_2 ) + self.target_feed_forward_bias_2 # self._input_layer, self._output_layer, self._target_input_layer, self._target_output_layer = self._create_network(self._time) # self._target_input_layer, self._target_output_layer = DeepQPongPlayer._create_target_network() self._action = tf.placeholder("float", [None, self.ACTIONS_COUNT]) self._target = tf.placeholder("float", [None]) readout_action = tf.reduce_sum(tf.mul(self.output_layer, self._action), reduction_indices=1) cost = tf.reduce_mean(tf.square(self._target - readout_action)) self._train_operation = tf.train.AdamOptimizer( self.LEARN_RATE).minimize(cost) self._observations = deque() self._last_scores = deque() # set the first action to do nothing self._last_action = np.zeros(self.ACTIONS_COUNT) self._last_action[1] = 1 self._last_state = None self._probability_of_random_action = self.INITIAL_RANDOM_ACTION_PROB self._session.run(tf.initialize_all_variables()) if not os.path.exists(self._checkpoint_path): os.mkdir(self._checkpoint_path) self._saver = tf.train.Saver() # self._saver.restore(self._session, "deep_q_pong_networks/network-400000") checkpoint = tf.train.get_checkpoint_state(self._checkpoint_path) if checkpoint and checkpoint.model_checkpoint_path: self._saver.restore(self._session, checkpoint.model_checkpoint_path) print("Loaded checkpoints %s" % checkpoint.model_checkpoint_path) elif playback_mode: raise Exception("Could not load checkpoints for playback")
def L2(self, tensor, wd=0.001): return tf.mul(tf.nn.l2_loss(tensor), wd, name='L2-Loss')
def build_training(self): # TODO add training function! # the label of image self.x_ = tf.placeholder( tf.float32, [None, 7, 7, 2 ]) # the first dimension (None) will index the images self.y_ = tf.placeholder(tf.float32, [None, 7, 7, 2]) self.w_ = tf.placeholder(tf.float32, [None, 7, 7, 2]) self.h_ = tf.placeholder(tf.float32, [None, 7, 7, 2]) self.C_ = tf.placeholder(tf.float32, [None, 7, 7, 2]) self.p_ = tf.placeholder(tf.float32, [None, 7, 7, 20]) self.obj = tf.placeholder(tf.float32, [None, 7, 7, 2]) self.objI = tf.placeholder(tf.float32, [None, 7, 7]) self.noobj = tf.placeholder(tf.float32, [None, 7, 7, 2]) #output network output = self.fc_32 nb_image = tf.shape(self.x_)[0] class_probs = tf.reshape(output[0:nb_image, 0:980], (nb_image, 7, 7, 20)) scales = tf.reshape(output[0:nb_image, 980:1078], (nb_image, 7, 7, 2)) boxes = tf.reshape(output[0:nb_image, 1078:], (nb_image, 7, 7, 2, 4)) boxes0 = boxes[:, :, :, :, 0] boxes1 = boxes[:, :, :, :, 1] boxes2 = boxes[:, :, :, :, 2] boxes3 = boxes[:, :, :, :, 3] # loss funtion self.subX = tf.sub(boxes0, self.x_) self.subY = tf.sub(boxes1, self.y_) self.subW = tf.sub(tf.sqrt(tf.abs(boxes2)), tf.sqrt(self.w_)) self.subH = tf.sub(tf.sqrt(tf.abs(boxes3)), tf.sqrt(self.h_)) self.subC = tf.sub(scales, self.C_) self.subP = tf.sub(class_probs, self.p_) self.lossX = tf.mul( self.lambdacoord, tf.reduce_sum(tf.mul(self.obj, tf.mul(self.subX, self.subX)), axis=[1, 2, 3])) self.lossY = tf.mul( self.lambdacoord, tf.reduce_sum(tf.mul(self.obj, tf.mul(self.subY, self.subY)), axis=[1, 2, 3])) self.lossW = tf.mul( self.lambdacoord, tf.reduce_sum(tf.mul(self.obj, tf.mul(self.subW, self.subW)), axis=[1, 2, 3])) self.lossH = tf.mul( self.lambdacoord, tf.reduce_sum(tf.mul(self.obj, tf.mul(self.subH, self.subH)), axis=[1, 2, 3])) self.lossCObj = tf.reduce_sum(tf.mul(self.obj, tf.mul(self.subC, self.subC)), axis=[1, 2, 3]) self.lossCNobj = tf.mul( self.lambdanoobj, tf.reduce_sum(tf.mul(self.noobj, tf.mul(self.subC, self.subC)), axis=[1, 2, 3])) self.lossP = tf.reduce_sum(tf.mul( self.objI, tf.reduce_sum(tf.mul(self.subP, self.subP), axis=3)), axis=[1, 2]) self.loss = tf.add_n((self.lossX, self.lossY, self.lossW, self.lossH, self.lossCObj, self.lossCNobj, self.lossP)) self.loss = tf.reduce_mean(self.loss) #variable for the training global_step = tf.Variable(0, trainable=False) starter_learning_rate = 0.001 decay = 0.0005 end_learning_rate = 0.01 self.epoch = tf.placeholder(tf.int32) # Different case of learning rate def lr1(): return tf.train.polynomial_decay( starter_learning_rate, global_step, decay, end_learning_rate=end_learning_rate, power=1.0) def lr2(): return tf.constant(0.01) def lr3(): return tf.constant(0.001) def lr4(): return tf.constant(0.0001) lr = tf.case( { tf.less_equal(self.epoch, 1): lr1, tf.logical_and(tf.greater(self.epoch, 76), tf.less_equal(self.epoch, 106)): lr2, tf.logical_and(tf.greater(self.epoch, 106), tf.less_equal(self.epoch, 136)): lr3, tf.greater(self.epoch, 136): lr4 }, lr4, exclusive=True) self.train_step = tf.train.MomentumOptimizer( learning_rate=lr, momentum=0.9).minimize(self.loss, global_step=global_step) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def deconv2d(self, filter_size, output_channels, stride=1, padding='SAME', activation_fn=tf.nn.relu, b_value=0.0, s_value=1.0, bn=True): """ :param filter_size: int. assumes square filter :param output_channels: int :param stride: int :param padding: 'VALID' or 'SAME' :param activation_fn: tf.nn function :param b_value: float :param s_value: float """ self.count['deconv'] += 1 scope = 'deconv_' + str(self.count['deconv']) with tf.variable_scope(scope): input_channels = self.input.get_shape()[3] output_shape = [ filter_size, filter_size, output_channels, input_channels ] w = self.weight_variable(name='weights', shape=output_shape) batch_size = tf.shape(self.input)[0] input_height = tf.shape(self.input)[1] input_width = tf.shape(self.input)[2] filter_height = tf.shape(w)[0] filter_width = tf.shape(w)[1] out_channels = tf.shape(w)[2] row_stride = stride col_stride = stride if padding == "VALID": out_rows = (input_height - 1) * row_stride + filter_height out_cols = (input_width - 1) * col_stride + filter_width else: # padding == "SAME": out_rows = input_height * row_stride out_cols = input_width * col_stride out_shape = tf.pack([batch_size, out_rows, out_cols, out_channels]) self.input = tf.nn.conv2d_transpose(self.input, w, out_shape, [1, stride, stride, 1], padding) if bn is True: self.input = self.conv_batch_norm(self.input) if b_value is not None: b = self.const_variable(name='bias', shape=[output_channels], value=b_value) self.input = tf.add(self.input, b) if s_value is not None: s = self.const_variable(name='scale', shape=[output_channels], value=s_value) self.input = tf.mul(self.input, s) if activation_fn is not None: self.input = activation_fn(self.input) self.print_log(scope + ' output: ' + str(self.input.get_shape()))