def replace(self, episodes, length, rows=None): """Replace full episodes. Args: episodes: Tuple of transition quantities with batch and time dimensions. length: Batch of sequence lengths. rows: Episodes to replace, defaults to all. Returns: Operation. """ rows = tf.range(self._capacity) if rows is None else rows assert rows.shape.ndims == 1 assert_capacity = tf.assert_less( rows, self._capacity, message='capacity exceeded') with tf.control_dependencies([assert_capacity]): assert_max_length = tf.assert_less_equal( length, self._max_length, message='max length exceeded') replace_ops = [] with tf.control_dependencies([assert_max_length]): for buffer_, elements in zip(self._buffers, episodes): replace_op = tf.scatter_update(buffer_, rows, elements) replace_ops.append(replace_op) with tf.control_dependencies(replace_ops): return tf.scatter_update(self._length, rows, length)
def build_init_cell(self): with tf.variable_scope("init_cell"): # always zero dummy = tf.placeholder(tf.float32, [1, 1], name='dummy') # memory M_init_linear = tf.tanh(Linear(dummy, self.mem_size * self.mem_dim, name='M_init_linear')) M_init = tf.reshape(M_init_linear, [self.mem_size, self.mem_dim]) # read weights read_w_init = tf.Variable(tf.zeros([self.read_head_size, self.mem_size])) read_init = tf.Variable(tf.zeros([self.read_head_size, 1, self.mem_dim])) for idx in xrange(self.read_head_size): # initialize bias distribution with `tf.range(mem_size-2, 0, -1)` read_w_linear_idx = Linear(dummy, self.mem_size, is_range=True, name='read_w_linear_%s' % idx) read_w_init = tf.scatter_update(read_w_init, [idx], tf.nn.softmax(read_w_linear_idx)) read_init_idx = tf.tanh(Linear(dummy, self.mem_dim, name='read_init_%s' % idx)) read_init = tf.scatter_update(read_init, [idx], tf.reshape(read_init_idx, [1, 1, self.mem_dim])) # write weights write_w_init = tf.Variable(tf.zeros([self.write_head_size, self.mem_size])) for idx in xrange(self.write_head_size): write_w_linear_idx = Linear(dummy, self.mem_size, is_range=True, name='write_w_linear_%s' % idx) write_w_init = tf.scatter_update(write_w_init, [idx], tf.nn.softmax(write_w_linear_idx)) # controller state output_init = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim])) hidden_init = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim])) for idx in xrange(self.controller_layer_size): output_init = tf.scatter_update(output_init, [idx], tf.reshape( tf.tanh(Linear(dummy, self.controller_dim, name='output_init_%s' % idx)), [1, self.controller_dim] ) ) hidden_init = tf.scatter_update(hidden_init, [idx], tf.reshape( tf.tanh(Linear(dummy, self.controller_dim, name='hidden_init_%s' % idx)), [1, self.controller_dim] ) ) new_output= tf.tanh(Linear(dummy, self.output_dim, name='new_output')) inputs = { 'input': dummy, } outputs = { 'new_output': new_output, 'M': M_init, 'read_w': read_w_init, 'write_w': write_w_init, 'read': tf.reshape(read_init, [self.read_head_size, self.mem_dim]), 'output': output_init, 'hidden': hidden_init } return inputs, outputs
def loop_body(j): ns1 = tf.scatter_update(select1, j, 10.0) ns2 = tf.scatter_update(select2, j, 10.0) nj = tf.add(j, 1) op = control_flow_ops.group(ns1, ns2) nj = control_flow_ops.with_dependencies([op], nj) return [nj]
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 shortlist_insert(): larger_ids = tf.boolean_mask(tf.to_int64(ids), larger_scores) larger_score_values = tf.boolean_mask(scores, larger_scores) shortlist_ids, new_ids, new_scores = self.ops.top_n_insert( self.sl_ids, self.sl_scores, larger_ids, larger_score_values) u1 = tf.scatter_update(self.sl_ids, shortlist_ids, new_ids) u2 = tf.scatter_update(self.sl_scores, shortlist_ids, new_scores) return tf.group(u1, u2)
def build_update(self): """Perform sampling and exchange. """ # Sample by Metropolis-Hastings for each replica. replica_sample = [] replica_accept = [] for i in range(self.n_replica): sample_, accept_ = self._mh_sample(self.replica_vars[i], self.inverse_temperatures[i]) replica_sample.append(sample_) replica_accept.append(accept_) accept = replica_accept[0] # Variable to store order of replicas after exchange new_replica_idx = tf.Variable(tf.range(self.n_replica)) new_replica_idx = tf.assign(new_replica_idx, tf.range(self.n_replica)) # Variable to store ratio of current samples replica_ratio = tf.Variable(tf.zeros( self.n_replica, dtype=list(self.latent_vars)[0].dtype)) replica_ratio = self._replica_ratio(replica_ratio, replica_sample) # Exchange adjacent replicas at frequency of exchange_freq u = tf.random_uniform([]) exchange = u < self.exchange_freq new_replica_idx = tf.cond( exchange, lambda: self._replica_exchange( new_replica_idx, replica_ratio), lambda: new_replica_idx) # New replica sorted by new_replica_idx new_replica_sample = [] for i in range(self.n_replica): new_replica_sample.append( {z: tf.case({tf.equal(tf.gather(new_replica_idx, i), j): _stateful_lambda(replica_sample[j][z]) for j in range(self.n_replica)}, default=lambda: replica_sample[0][z], exclusive=True) for z, qz in six.iteritems(self.latent_vars)}) assign_ops = [] # Update Empirical random variables. for z, qz in six.iteritems(self.latent_vars): variable = qz.get_variables()[0] assign_ops.append(tf.scatter_update(variable, self.t, new_replica_sample[0][z])) for i in range(self.n_replica): for z, qz in six.iteritems(self.replica_vars[i]): variable = qz.get_variables()[0] assign_ops.append(tf.scatter_update(variable, self.t, new_replica_sample[i][z])) # Increment n_accept (if accepted). assign_ops.append(self.n_accept.assign_add(tf.where(accept, 1, 0))) return tf.group(*assign_ops)
def _reset_non_empty(self, indices): op_zero = tf.scatter_update( self._time_elapsed, indices, tf.gather(tf.zeros((len(self),), tf.int32), indices)) # pylint: disable=protected-access new_values = self._batch_env._reset_non_empty(indices) # pylint: enable=protected-access assign_op = tf.scatter_update(self._observ, indices, new_values) with tf.control_dependencies([op_zero, assign_op]): return tf.gather(self.observ, indices)
def testBooleanScatterUpdate(self): with self.test_session(use_gpu=False) as session: var = tf.Variable([True, False]) update0 = tf.scatter_update(var, 1, True) update1 = tf.scatter_update(var, tf.constant(0, dtype=tf.int64), False) var.initializer.run() session.run([update0, update1]) self.assertAllEqual([False, True], var.eval())
def build_controller(self, input, read_prev, output_prev, hidden_prev): with tf.variable_scope("controller"): output = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim])) hidden = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim])) for layer_idx in xrange(self.controller_layer_size): if self.controller_layer_size == 1: o_prev = output_prev h_prev = hidden_prev else: o_prev = tf.reshape(tf.gather(output_prev, layer_idx), [1, -1]) h_prev = tf.reshape(tf.gather(hidden_prev, layer_idx), [1, -1]) if layer_idx == 0: def new_gate(gate_name): in_modules = [ Linear(input, self.controller_dim, name='%s_gate_1_%s' % (gate_name, layer_idx)), Linear(o_prev, self.controller_dim, name='%s_gate_2_%s' % (gate_name, layer_idx)), ] if self.read_head_size == 1: in_modules.append( Linear(read_prev, self.controller_dim, name='%s_gate_3_%s' % (gate_name, layer_idx)) ) else: for read_idx in xrange(self.read_head_size): vec = tf.reshape(tf.gather(read_prev, read_idx), [1, -1]) in_modules.append( Linear(vec, self.controller_dim, name='%s_gate_3_%s_%s' % (gate_name, layer_idx, read_idx)) ) return tf.add_n(in_modules) else: def new_gate(gate_name): return tf.add_n([ Linear(tf.reshape(tf.gather(output, layer_idx-1), [1, -1]), self.controller_dim, name='%s_gate_1_%s' % (gate_name, layer_idx)), Linear(o_prev, self.controller_dim, name='%s_gate_2_%s' % (gate_name, layer_idx)), ]) # input, forget, and output gates for LSTM i = tf.sigmoid(new_gate('input')) f = tf.sigmoid(new_gate('forget')) o = tf.sigmoid(new_gate('output')) update = tf.tanh(new_gate('update')) # update the sate of the LSTM cell hidden = tf.scatter_update(hidden, [layer_idx], tf.add_n([f * h_prev, i * update])) output = tf.scatter_update(output, [layer_idx], o * tf.tanh(tf.gather(hidden,layer_idx))) return output, hidden
def viterbi_inference(self, obs_seq): # length of observed sequence self.N = len(obs_seq) # shape path Variables shape = [self.N, self.S] # observed sequence x = tf.constant(obs_seq, dtype=tf.int32, name='observation_sequence') with tf.name_scope('Init_viterbi_variables'): # Initialize variables pathStates, pathScores, states_seq = self.initialize_viterbi_variables(shape) with tf.name_scope('Emission_seq_'): # log probability of emission sequence obs_prob_seq = tf.log(tf.gather(self.E, x)) obs_prob_list = tf.split(0, self.N, obs_prob_seq) with tf.name_scope('Starting_log-priors'): # initialize with state starting log-priors pathScores = tf.scatter_update(pathScores, 0, tf.log(self.T0) + tf.squeeze(obs_prob_list[0])) with tf.name_scope('Belief_Propagation'): for step, obs_prob in enumerate(obs_prob_list[1:]): with tf.name_scope('Belief_Propagation_step_%s' %step): # propagate state belief belief = self.belief_propagation(pathScores[step, :]) # the inferred state by maximizing global function # and update state and score matrices pathStates = tf.scatter_update(pathStates, step + 1, tf.argmax(belief, 0)) pathScores = tf.scatter_update(pathScores, step + 1, tf.reduce_max(belief, 0) + tf.squeeze(obs_prob)) with tf.name_scope('Max_Likelyhood_update'): # infer most likely last state states_seq = tf.scatter_update(states_seq, self.N-1, tf.argmax(pathScores[self.N-1, :], 0)) with tf.name_scope('Backtrack'): for step in range(self.N - 1, 0, -1): with tf.name_scope('Back_track_step_%s' %step): # for every timestep retrieve inferred state state = states_seq[step] idx = tf.reshape(tf.pack([step, state]), [1, -1]) state_prob = tf.gather_nd(pathStates, idx) states_seq = tf.scatter_update(states_seq, step - 1, state_prob[0]) return states_seq, tf.exp(pathScores) # turn scores back to probabilities
def add_val_to_col(var, col, val): vector_with_zeros = tf.Variable(tf.zeros(var.get_shape()[1]), dtype=tf.float32) vector_with_zeros = tf.scatter_update(vector_with_zeros,[col],[val]) vector_with_zeros = tf.reshape(vector_with_zeros, [1,var.get_shape().as_list()[1]]) return var+vector_with_zeros
def _reset_non_empty(self, indices): # pylint: disable=protected-access new_values = self._batch_env._reset_non_empty(indices) # pylint: enable=protected-access assign_op = tf.scatter_update(self._observ, indices, new_values) with tf.control_dependencies([assign_op]): return tf.identity(new_values)
def _reset_non_empty(self, indices): # pylint: disable=protected-access new_values = self._batch_env._reset_non_empty(indices) # pylint: enable=protected-access initial_frames = getattr(self._batch_env, "history_observations", None) if initial_frames is not None: # Using history buffer frames for initialization, if they are available. with tf.control_dependencies([new_values]): # Transpose to [batch, height, width, history, channels] and merge # history and channels into one dimension. initial_frames = tf.transpose(initial_frames, [0, 2, 3, 1, 4]) initial_frames = tf.reshape(initial_frames, (len(self),) + self.observ_shape) else: inx = tf.concat( [ tf.ones(tf.size(tf.shape(new_values)), dtype=tf.int64)[:-1], [self.history] ], axis=0) initial_frames = tf.tile(new_values, inx) assign_op = tf.scatter_update(self._observ, indices, initial_frames) with tf.control_dependencies([assign_op]): return tf.gather(self.observ, indices)
def _apply_dense(self, grad, var): memory = self.get_slot(var, "memory") memsum = tf.reduce_mean(memory, [0]) mem = tf.gather(memory, self.batch_ind) delta = grad - mem + memsum mem_op = tf.scatter_update(memory, self.batch_ind, grad) return tf.group(var.assign_sub(tf.mul(delta, self.learning_rate)), mem_op)
def test_state_grads(sess): v = tf.Variable([0., 0., 0.]) x = tf.ones((3,)) y0 = tf.assign(v, x) y1 = tf.assign_add(v, x) grad0 = tf.gradients(y0, [v, x]) grad1 = tf.gradients(y1, [v, x]) grad_vals = sess.run((grad0, grad1)) assert np.allclose(grad_vals[0][0], 0) assert np.allclose(grad_vals[0][1], 1) assert np.allclose(grad_vals[1][0], 1) assert np.allclose(grad_vals[1][1], 1) v = tf.Variable([0., 0., 0.]) x = tf.ones((1,)) y0 = tf.scatter_update(v, [0], x) y1 = tf.scatter_add(v, [0], x) grad0 = tf.gradients(y0, [v._ref(), x]) grad1 = tf.gradients(y1, [v._ref(), x]) grad_vals = sess.run((grad0, grad1)) assert np.allclose(grad_vals[0][0], [0, 1, 1]) assert np.allclose(grad_vals[0][1], 1) assert np.allclose(grad_vals[1][0], 1) assert np.allclose(grad_vals[1][1], 1)
def encode(self, x=None): if x is None: x = CharLSTMEmbeddings.create_placeholder(self.name) self.x = x with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE): Wch = tf.get_variable( "Wch", initializer=tf.constant_initializer(self.weights, dtype=tf.float32, verify_shape=True), shape=[self.vsz, self.dsz], trainable=True ) ech0 = tf.scatter_update(Wch, tf.constant(Offsets.PAD, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, self.dsz])) shape = tf.shape(x) B = shape[0] T = shape[1] W = shape[2] flat_chars = tf.reshape(x, [-1, W]) word_lengths = tf.reduce_sum(tf.cast(tf.equal(flat_chars, Offsets.PAD), tf.int32), axis=1) with tf.control_dependencies([ech0]): embed_chars = tf.nn.embedding_lookup(Wch, flat_chars) fwd_lstm = stacked_lstm(self.lstmsz // 2, self.pdrop, self.layers) bwd_lstm = stacked_lstm(self.lstmsz // 2, self.pdrop, self.layers) _, rnn_state = tf.nn.bidirectional_dynamic_rnn(fwd_lstm, bwd_lstm, embed_chars, sequence_length=word_lengths, dtype=tf.float32) result = tf.concat([rnn_state[0][-1].h, rnn_state[1][-1].h], axis=1) return tf.reshape(result, [B, T, self.lstmsz])
def _forward(self, obs_prob_seq): # initialize with state starting priors self.forward = tf.scatter_update(self.forward, 0, self.T0) # propagate belief for step in range(self.N): # previous state probability prev_prob = tf.reshape(self.forward[step, :], [1, -1]) # transition prior prior_prob = tf.matmul(prev_prob, self.T) # forward belief propagation forward_score = tf.multiply(prior_prob, tf.cast(obs_prob_seq[step, :], tf.float64)) # Normalize score into a probability forward_prob = tf.reshape(forward_score / tf.reduce_sum(forward_score), [-1]) # Update forward matrix self.forward = tf.scatter_update(self.forward, step + 1, forward_prob)
def build_update(self): """ Simulate Langevin dynamics using a discretized integrator. Its discretization error goes to zero as the learning rate decreases. """ old_sample = {z: tf.gather(qz.params, tf.maximum(self.t - 1, 0)) for z, qz in six.iteritems(self.latent_vars)} # Simulate Langevin dynamics. learning_rate = self.step_size / tf.cast(self.t + 1, tf.float32) grad_log_joint = tf.gradients(self._log_joint(old_sample), list(six.itervalues(old_sample))) sample = {} for z, qz, grad_log_p in \ zip(six.iterkeys(self.latent_vars), six.itervalues(self.latent_vars), grad_log_joint): event_shape = qz.get_event_shape() normal = Normal(mu=tf.zeros(event_shape), sigma=learning_rate * tf.ones(event_shape)) sample[z] = old_sample[z] + 0.5 * learning_rate * grad_log_p + \ normal.sample() # Update Empirical random variables. assign_ops = [] variables = {x.name: x for x in tf.get_default_graph().get_collection(tf.GraphKeys.VARIABLES)} for z, qz in six.iteritems(self.latent_vars): variable = variables[qz.params.op.inputs[0].op.inputs[0].name] assign_ops.append(tf.scatter_update(variable, self.t, sample[z])) # Increment n_accept. assign_ops.append(self.n_accept.assign_add(1)) return tf.group(*assign_ops)
def build_update(self): """Simulate Langevin dynamics using a discretized integrator. Its discretization error goes to zero as the learning rate decreases. #### Notes The updates assume each Empirical random variable is directly parameterized by `tf.Variable`s. """ old_sample = {z: tf.gather(qz.params, tf.maximum(self.t - 1, 0)) for z, qz in six.iteritems(self.latent_vars)} # Simulate Langevin dynamics. learning_rate = self.step_size / tf.cast(self.t + 1, tf.float32) grad_log_joint = tf.gradients(self._log_joint(old_sample), list(six.itervalues(old_sample))) sample = {} for z, grad_log_p in zip(six.iterkeys(old_sample), grad_log_joint): qz = self.latent_vars[z] event_shape = qz.event_shape normal = Normal(loc=tf.zeros(event_shape), scale=learning_rate * tf.ones(event_shape)) sample[z] = old_sample[z] + \ 0.5 * learning_rate * tf.convert_to_tensor(grad_log_p) + \ normal.sample() # Update Empirical random variables. assign_ops = [] for z, qz in six.iteritems(self.latent_vars): variable = qz.get_variables()[0] assign_ops.append(tf.scatter_update(variable, self.t, sample[z])) # Increment n_accept. assign_ops.append(self.n_accept.assign_add(1)) return tf.group(*assign_ops)
def deconv_pooling_n_filter(pool_s, pool_layer_scope, kheight=2, kwidth=2): with tf.variable_scope(pool_layer_scope, reuse=True) as scope: pool_shape = pool_s.get_shape().as_list() # if pool_shape[1] < 4 or pool_shape[2] < 4: # pool_s2 = tf.nn.dropout(pool_s, 0.5) # switches = tf.ones_like(pool_s2) # return pool_s2 # Recreate 1D switches for scatter update dim = 1 for d in pool_shape: dim *= d [pool_s2, ind] = tf.nn.max_pool_with_argmax( pool_s, ksize=[1, kheight, kwidth, 1], strides=[1, kheight, kwidth, 1], padding="SAME" ) _print_tensor_size(pool_s2) # ones_temp = tf.ones_like([(dim // kheight) // kwidth]) ones_temp = tf.ones_like(ind, dtype=tf.float32) # temp_zeros = switches = tf.Variable(tf.zeros([dim]), name="switches") switches = tf.assign(switches, tf.zeros([dim])) # set switches switches_out2 = tf.scatter_update(switches, ind, ones_temp) # reshape back to batches switches_out2 = tf.reshape(switches_out2, pool_shape) return pool_s2, switches_out2
def body(sequence_len, step, feature_pl, path_pl, flattened_idx_offset, contextual_features): begin = tf.get_variable("begin1",[3],dtype=tf.int32,initializer=tf.constant_initializer(0)) begin = tf.scatter_update(begin,1,step,use_locking=None) step_feature = tf.squeeze(tf.slice(feature_pl,begin,[-1,1,-1])) input_idx = tf.slice(path_pl, begin, [-1,1,1]) input_idx = tf.reshape(input_idx,[-1]) input_idx_flattened = flattened_idx_offset + input_idx max_seq_len = FLAGS.max_seq_len begin2 = tf.get_variable("begin2",[3],dtype=tf.int32,initializer=tf.constant_initializer(0)) begin2 = tf.scatter_update(begin2,1,step,use_locking=None) begin2 = tf.scatter_update(begin2,2,1,use_locking=None) tf.get_variable_scope().reuse_variables() contextual_features = tf.get_variable("contextual_features") # [max_seq_len * max_seq_len, encoding_nn_output_size], # dtype=tf.float32) step_contextual_features = tf.gather(contextual_features,input_idx_flattened) # use flattened indices1 inputs = tf.concat(1,[step_contextual_features,step_feature]) updated_contextual_vectors = single_layer_neural_network1(inputs) updated_contextual_vectors = tf.tanh(updated_contextual_vectors) output_idx = tf.reshape(tf.slice(path_pl, begin2, [-1,1, 1]),[-1]) output_idx_flattened = flattened_idx_offset + output_idx contextual_features = tf.scatter_add(contextual_features, output_idx_flattened, updated_contextual_vectors, use_locking=None) with tf.control_dependencies([contextual_features]): return (sequence_len, step+1, feature_pl, path_pl, flattened_idx_offset, contextual_features)
def _add(): num_adds_inc = self._num_adds_cs.execute(_increment_num_adds) current_pos = tf.mod(num_adds_inc - 1, self._buffer_size) update_ops = [] for name in self._tensors.keys(): update_ops.append( tf.scatter_update(self._tensors[name], current_pos, tensors[name])) return tf.group(*update_ops)
def encode(self, x=None): x = super(LearnedPositionalLookupTableEmbeddings, self).encode(x) l = tf.shape(x)[1] e0 = tf.scatter_update(self.pos, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, self.dsz])) with tf.control_dependencies([e0]): pos_embeddings = tf.nn.embedding_lookup(self.pos, tf.range(l, dtype=tf.int32)) return x + tf.expand_dims(pos_embeddings, 0)
def perform(self, agent_indices, observ): """Compute batch of actions and a summary for a batch of observation. Args: agent_indices: Tensor containing current batch indices. observ: Tensor of a batch of observations for all agents. Returns: Tuple of action batch tensor and summary tensor. """ with tf.name_scope('perform/'): observ = self._observ_filter.transform(observ) if self._last_state is None: state = None else: state = tools.nested.map( lambda x: tf.gather(x, agent_indices), self._last_state) with tf.device('/gpu:0' if self._use_gpu else '/cpu:0'): output = self._network( observ[:, None], tf.ones(observ.shape[0]), state) action = tf.cond( self._is_training, output.policy.sample, output.policy.mode) logprob = output.policy.log_prob(action)[:, 0] # pylint: disable=g-long-lambda summary = tf.cond(self._should_log, lambda: tf.summary.merge([ tf.summary.histogram('mode', output.policy.mode()[:, 0]), tf.summary.histogram('action', action[:, 0]), tf.summary.histogram('logprob', logprob)]), str) # Remember current policy to append to memory in the experience callback. if self._last_state is None: assign_state = tf.no_op() else: assign_state = utility.assign_nested_vars( self._last_state, output.state, agent_indices) remember_last_action = tf.scatter_update( self._last_action, agent_indices, action[:, 0]) policy_params = tools.nested.filter( lambda x: isinstance(x, tf.Tensor), output.policy.parameters) assert policy_params, 'Policy has no parameters to store.' remember_last_policy = tools.nested.map( lambda var, val: tf.scatter_update(var, agent_indices, val[:, 0]), self._last_policy, policy_params, flatten=True) with tf.control_dependencies(( assign_state, remember_last_action) + remember_last_policy): return action[:, 0], tf.identity(summary)
def _define_begin_episode(agent_indices): """Reset environments, intermediate scores and durations for new episodes. Args: agent_indices: Tensor containing batch indices starting an episode. Returns: Summary tensor. """ assert agent_indices.shape.ndims == 1 zero_scores = tf.zeros_like(agent_indices, tf.float32) zero_durations = tf.zeros_like(agent_indices) reset_ops = [ batch_env.reset(agent_indices), tf.scatter_update(score, agent_indices, zero_scores), tf.scatter_update(length, agent_indices, zero_durations)] with tf.control_dependencies(reset_ops): return algo.begin_episode(agent_indices)
def scatter_update(cls, factor, indices, values, sharding_func): """Helper function for doing sharded scatter update.""" assert isinstance(factor, list) if len(factor) == 1: with ops.colocate_with(factor[0]): # TODO(agarwal): assign instead of scatter update for full batch update. return tf.scatter_update(factor[0], indices, values).op else: num_shards = len(factor) assignments, new_ids = sharding_func(indices) assert assignments is not None assignments = tf.cast(assignments, tf.int32) sharded_ids = tf.dynamic_partition(new_ids, assignments, num_shards) sharded_values = tf.dynamic_partition(values, assignments, num_shards) updates = [] for i in xrange(num_shards): updates.append(tf.scatter_update(factor[i], sharded_ids[i], sharded_values[i])) return tf.group(*updates)
def _backward(self, obs_prob_seq): # initialize with state ending priors self.backward = tf.scatter_update(self.backward, self.N, tf.ones([self.S], dtype=tf.float64)) for step in range(self.N, 0, -1): # next state probability next_prob = tf.reshape(self.backward[step, :], [-1, 1]) # observation emission probabilities obs_prob = tf.diag(obs_prob_seq[step - 1, :]) # transition prior prior_prob = tf.matmul(self.T, obs_prob) # backward belief propagation backward_score = tf.matmul(prior_prob, next_prob) # Normalize score into a probability backward_prob = tf.reshape(backward_score / tf.reduce_sum(backward_score), [-1]) # Update backward matrix self.backward = tf.scatter_update(self.backward, step - 1, backward_prob)
def remove(self, ids): """Remove the ids (and their associated scores) from the TopN.""" with tf.control_dependencies(self.last_ops): scatter_op = tf.scatter_update(self.id_to_score, ids, tf.ones_like(ids, dtype=tf.float32) * tf.float32.min) # We assume that removed ids are almost always in the shortlist, # so it makes no sense to hide the Op behind a tf.cond shortlist_ids_to_remove, new_length = tensor_forest_ops.top_n_remove(self.sl_ids, ids) u1 = tf.scatter_update( self.sl_ids, tf.concat_v2([[0], shortlist_ids_to_remove], 0), tf.concat_v2([new_length, tf.ones_like(shortlist_ids_to_remove) * -1], 0), ) u2 = tf.scatter_update( self.sl_scores, shortlist_ids_to_remove, tf.float32.min * tf.ones_like(shortlist_ids_to_remove, dtype=tf.float32), ) self.last_ops = [scatter_op, u1, u2]
def gibbs(beta, mu, S): for param in xrange(p): vark = 1.0 / S[param, param] span = tf.reshape(S[param, :], (1, p)) muk = mu[param, :] - vark * ( tf.reshape(tf.matmul(span, beta - mu), [1]) - S[param, param] * (beta[param, :] - mu[param, :]) ) beta = tf.scatter_update(beta, param, tf.random_normal([1], mean=muk, stddev=tf.sqrt(vark))) return beta
def encode(self, x=None): if x is None: x = CharConvEmbeddings.create_placeholder(self.name) self.x = x ech0 = tf.scatter_update(self.Wch, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, self.dsz])) char_comp, self.wsz = pool_chars(self.x, self.Wch, ech0, self.dsz, self.nfeat_factor, self.cfiltsz, self.max_feat, self.gating, self.num_gates, self.activation, self.wsz) return char_comp
def call(self, inputs, training=None, mask=None): """ 不需要训练 生成训练roi用的数据 总体过程: 1. 计算 rois 与 gt_bboxes(即输入数据中的bbox)的iou 2. 设置与 gt_bboxes 的 max_iou > pos_iou_threshold 的 roi 为正例,设置 max_iou < neg_iou_threshold 的 roi 为反例 3. 对正例、反例有数量限制: 正例数量不大于 max_pos_samples 正例反例总数不超过 max_pos_samples 反例数量如果过少,则通过 numpy.random.choice 随机填充 4. 最终输出5个结果: 1)rois [128, 4] 2)每个 roi 对应的 label [128,],如果我为0则表示为反例,>0则表示为正例 3)每个 roi 对应的 txtytwth [128, num_classes * 4] 4)计算 smooth l1 loss时的 bbox_inside_weights [128, num_classes * 4] 5)计算 smooth l1 loss时的 bbox_outside_weights [128, num_classes * 4] :param inputs: :param training: :param mask: :return: """ rois, gt_bboxes, gt_labels = inputs iou = pairwise_iou(rois, gt_bboxes) # [rois_size, gt_bboxes_size] max_overlaps = tf.reduce_max(iou, axis=1) # [rois_size, ] gt_assignment = tf.argmax(iou, axis=1) # [rois_size, ] labels = tf.gather(gt_labels, gt_assignment) # [rois_size, ] # 根据条件获取 前景 背景 fg_inds = tf.where(max_overlaps >= self._pos_iou_threshold)[:, 0] bg_inds = tf.where( tf.logical_and(max_overlaps < self._pos_iou_threshold, max_overlaps >= self._neg_iou_threshold))[:, 0] # 筛选 前景/背景 if tf.size(fg_inds) > self._max_pos_samples: fg_inds = tf.random_shuffle(fg_inds)[:self._max_pos_samples] if tf.size(bg_inds) > self._total_num_samples - tf.size(fg_inds): # 如果bg sample的数量多于要求值,则随机筛选 bg_inds = tf.random_shuffle(bg_inds)[:(self._total_num_samples - tf.size(fg_inds))] elif tf.size(bg_inds).numpy() == (self._total_num_samples - tf.size(fg_inds)).numpy(): pass else: # 如果bg sample的数量少于要求数值,则重复获取 target_size = (self._total_num_samples - tf.size(fg_inds)).numpy() bg_inds = np.random.choice(bg_inds.numpy(), size=int(target_size), replace=True) tf.logging.debug('proposal target generate %d fgs and %d bgs.' % (tf.size(fg_inds), tf.size(bg_inds))) keep_inds = tf.concat([fg_inds, bg_inds], axis=0) final_rois = tf.gather(rois, keep_inds) # rois[keep_inds] final_labels = tf.gather(labels, keep_inds) # labels[keep_inds] # labels[fg_inds_size:] = 0 final_labels = tf.scatter_update( tf.Variable(final_labels), tf.range(tf.size(fg_inds), tf.size(keep_inds), dtype=tf.int32), 0) # inside weights 只有正例才会设置,其他均为0 bbox_inside_weights = tf.zeros( (tf.size(keep_inds), self._num_classes, 4), dtype=tf.float32) if tf.size(fg_inds) > 0: # memory leak bug for tf.scatter_nd_update # https://github.com/tensorflow/tensorflow/issues/27288 # cur_index = tf.stack([tf.range(tf.size(fg_inds)), tf.gather(labels, fg_inds)], axis=1) # bbox_inside_weights = tf.scatter_nd_update(tf.Variable(bbox_inside_weights), # cur_index, # tf.ones([tf.size(fg_inds), 4])) bbox_inside_weights = bbox_inside_weights.numpy() for idx, fg_ind in enumerate(fg_inds.numpy()): bbox_inside_weights[idx, labels[idx]] = 1 bbox_inside_weights = tf.reshape(bbox_inside_weights, [-1, self._num_classes * 4]) # final bbox target 只有正例才会设置,其他均为0 final_bbox_targets = tf.zeros( (tf.size(keep_inds), self._num_classes, 4), dtype=tf.float32) if tf.size(fg_inds) > 0: bbox_targets = encode_bboxes( tf.gather(final_rois, tf.range(tf.size(fg_inds))), tf.gather(gt_bboxes, tf.gather(gt_assignment, fg_inds))) # memory leak bug for tf.scatter_nd_update # https://github.com/tensorflow/tensorflow/issues/27288 # final_bbox_targets = tf.scatter_nd_update(tf.Variable(final_bbox_targets), # tf.stack([tf.range(tf.size(fg_inds)), # tf.gather(labels, fg_inds)], axis=1), bbox_targets) final_bbox_targets = final_bbox_targets.numpy() bbox_targets = bbox_targets.numpy() for idx, fg_ind in enumerate(fg_inds.numpy()): final_bbox_targets[idx, labels[idx]] = bbox_targets[idx] final_bbox_targets = tf.reshape(final_bbox_targets, [-1, self._num_classes * 4]) # 这个好像没啥用 bbox_outside_weights = tf.ones_like(bbox_inside_weights, dtype=tf.float32) return tf.stop_gradient(final_rois), tf.stop_gradient(final_labels), tf.stop_gradient(final_bbox_targets), \ tf.stop_gradient(bbox_inside_weights), tf.stop_gradient(bbox_outside_weights)
def step(index, scores_sum, scores_num): """Single step.""" index %= epoch_length # Only needed in eval runs. # Note - the only way to ensure making a copy of tensor is to run simple # operation. We are waiting for tf.copy: # https://github.com/tensorflow/tensorflow/issues/11186 obs_copy = batch_env.observ + 0 value_fun_shape = (num_agents, ) if distributional_size > 1: value_fun_shape = (num_agents, distributional_size) def env_step(arg1, arg2, arg3): # pylint: disable=unused-argument """Step of the environment.""" (logits, value_function) = get_policy(obs_copy, ppo_hparams, batch_env.action_space, distributional_size) action = common_layers.sample_with_temperature( logits, sampling_temp) action = tf.cast(action, tf.int32) action = tf.reshape(action, shape=(num_agents, )) reward, done = batch_env.simulate(action) pdf = tfp.distributions.Categorical(logits=logits).prob(action) pdf = tf.reshape(pdf, shape=(num_agents, )) value_function = tf.reshape(value_function, shape=value_fun_shape) done = tf.reshape(done, shape=(num_agents, )) with tf.control_dependencies([reward, done]): return tf.identity(pdf), tf.identity(value_function), \ tf.identity(done) # TODO(piotrmilos): while_body is executed at most once, # thus should be replaced with tf.cond pdf, value_function, top_level_done = tf.while_loop( lambda _1, _2, _3: tf.equal(speculum.size(), 0), env_step, [ tf.constant(0.0, shape=(num_agents, )), tf.constant(0.0, shape=value_fun_shape), tf.constant(False, shape=(num_agents, )) ], parallel_iterations=1, back_prop=False, ) with tf.control_dependencies([pdf, value_function]): obs, reward, done, action = speculum.dequeue() to_save = [obs, reward, done, action, pdf, value_function] save_ops = [ tf.scatter_update(memory_slot, index, value) for memory_slot, value in zip(memory, to_save) ] cumulate_rewards_op = cumulative_rewards.assign_add(reward) agent_indices_to_reset = tf.where(top_level_done)[:, 0] with tf.control_dependencies([cumulate_rewards_op]): # TODO(piotrmilos): possibly we need cumulative_rewards.read_value() scores_sum_delta = tf.reduce_sum( tf.gather(cumulative_rewards.read_value(), agent_indices_to_reset)) scores_num_delta = tf.count_nonzero(done, dtype=tf.int32) with tf.control_dependencies(save_ops + [scores_sum_delta, scores_num_delta]): reset_env_op = batch_env.reset(agent_indices_to_reset) reset_cumulative_rewards_op = tf.scatter_update( cumulative_rewards, agent_indices_to_reset, tf.gather(zeros_tensor, agent_indices_to_reset)) with tf.control_dependencies( [reset_env_op, reset_cumulative_rewards_op]): return [ index + 1, scores_sum + scores_sum_delta, scores_num + scores_num_delta ]
def construct_model_graph(self, reviews, config, init_model=None, training=True, offset=None, poccur=None): review_size, movie_size, dim_atts = self.get_problem_sizes( reviews, config) self.initialize_model(review_size, movie_size, dim_atts, config, init_model, training, offset, poccur) if (config["exposure"]): if (config['use_covariates']): occur_probs = self.hsm([ self.input_att["feat_" + str(i)] for i in range(len(dim_atts)) ]) if (config['assoc_plasticity']): beta = self.betassoc([ self.input_att["feat_" + str(i)] for i in range(len(dim_atts)) ]) else: ### If there is no exposure component and no covariates for SDM then there is no association plasticity occur_probs = self.intercept else: occur_probs = tf.zeros((tf.shape(self.input_ind)[0], movie_size), tf.float32) # number of non-zeros nnz = tf.reduce_sum(tf.cast(tf.not_equal(self.input_label, 0), tf.float32), 1, keepdims=True) #prepare embedding of context rate = tf.cast(self.input_label, tf.float32) alpha_select = tf.gather(self.alpha, self.input_ind, name='context_alpha') alpha_weighted = alpha_select * tf.expand_dims(rate, 2) alpha_sum = tf.reduce_sum(alpha_weighted, keepdims=True, reduction_indices=1) asum_zero = alpha_sum / tf.maximum( tf.expand_dims(nnz, 2), 1) ##divide by the true number of non zeros for each sample asum_nonz = (alpha_sum - alpha_weighted) / tf.expand_dims( tf.maximum(nnz - 1, 1), 1) ###When only one non-zero entry this returns 0 ''' Config['intercept'] is deprecated ''' if ( config['intercept'] == True ): ### Abundance is also controlled by intrinsic characteristics of the species such as its reproduction rate for instance ### In this case we add a bias term to the context that will be multiplied by its rho vector asum_zero = tf.add( asum_zero, tf.ones(shape=tf.shape(asum_zero), dtype=tf.float32, name="bias")) asum_nonz = tf.add( asum_nonz, tf.ones(shape=tf.shape(asum_nonz), dtype=tf.float32, name="bias")) ### Here before feeding the contexts to the generator, add the weights by betassoc ### if config['assoc_plasticity']: asum_zero = asum_zero * tf.expand_dims(beta, 1) asum_nonz = asum_nonz * tf.expand_dims(beta, 1) llh_zero, sind, emb, flag, _ = self.logprob_zero( asum_zero, config, occur_probs, training) llh_nonz, emb_logp_nz, emb_logp_z, log_mean = self.logprob_nonz( alpha_emb=asum_nonz, occur=occur_probs, config=config, training=training) # combine logprob of single instances if training: sum_llh_zero = tf.cond(tf.equal(tf.shape(sind)[0], 0), true_fn=lambda: tf.cast(0, tf.float32), false_fn=lambda: tf.reduce_mean(llh_zero) * tf.cast(tf.shape(sind)[0], tf.float32)) sum_llh = tf.reduce_sum(llh_nonz) + sum_llh_zero # training does not keep llh for each entry ins_llh = None pos_llh = None else: canevas = tf.Variable(tf.zeros([config['batch_size'] * movie_size], dtype=tf.float32), validate_shape=False) sind_ext = sind[:, 0] * movie_size + sind[:, 1] ins_llh = tf.scatter_update(canevas, sind_ext, llh_zero) pos_llh = emb_logp_nz - tf.log(1 - tf.exp(emb_logp_z)) sum_llh = tf.reduce_sum(llh_nonz) + tf.reduce_sum(llh_zero) # random choose weight vectors to get a noisy estimation of the regularization term rsize = max(1, int(movie_size * config['sample_ratio'])) rind = tf.random_shuffle(tf.range(movie_size))[0:rsize] if (config['use_reg']): if (config['prior'] == "gaussian"): ##L2 regularization if (config['bias'] == True): reg_bias = tf.reduce_sum( tf.square(tf.gather(self.invmu, rind))) else: reg_bias = 0 if ((config['exposure'] == True) & (config['use_covariates'] == False) & (config['fixedoccur'] == False)): reg_intercept = tf.reduce_sum( tf.square(tf.gather(self.intercept, rind))) else: reg_intercept = 0 regularizer = (tf.reduce_sum(tf.square(tf.gather(self.rho, rind))) \ + tf.reduce_sum(tf.square(tf.gather(self.alpha, rind))) + reg_bias + reg_intercept) \ * (0.5 * movie_size / (config['ar_sigma2'] * rsize * review_size)) elif (config['prior'] == "lasso"): ##L1 regularization if (config['bias'] == True): reg_bias = tf.reduce_sum( tf.abs(tf.gather(self.invmu, rind))) else: reg_bias = 0 if ((config['exposure'] == True) & (config['use_covariates'] == False) & (config['fixedoccur'] == False)): reg_intercept = tf.reduce_sum( tf.abs(tf.gather(self.intercept, rind))) else: reg_intercept = 0 reg_rho = tf.reduce_sum(tf.abs(tf.gather(self.rho, rind))) \ * (movie_size / (config['ar_sigma2'] * rsize * review_size)) reg_alpha = tf.reduce_sum(tf.abs(tf.gather(self.alpha, rind))) \ * (movie_size / (config['ar_sigma2'] * rsize * review_size)) regularizer = config['lambda_lasso'] * ( reg_rho + reg_alpha + reg_intercept + reg_bias) else: regularizer = 0 if (config['use_penalty']): pen = 0 ##TODO: add other penalties here else: pen = 0 objective = regularizer + pen - sum_llh inputs = {'input_ind': self.input_ind, 'input_label': self.input_label} inputs.update(self.input_att) outputs = { 'objective': objective, 'llh': sum_llh, 'ins_llh': ins_llh, 'pos_llh': pos_llh, 'debugv': [], #[train_writer,valid_writer], 'exposure': occur_probs, 'means': log_mean, 'meansneg': emb, 'sindzero': sind, 'flag': flag } model_param = { 'alpha': self.alpha, 'rho': self.rho, 'invmu': self.invmu, 'std': self.std, 'intercept': self.intercept, 'nbr': self.nbr, 'hsm_params': self.hsm } if config['assoc_plasticity']: model_param.update(dict(betassoc_params=self.betassoc)) return inputs, outputs, model_param
def embedding_layer(self): # Get constant values passage_shape = tf.shape(self.passage_char_inputs) ques_shape = tf.shape(self.question_char_inputs) batch_size_passage, num_words_passage, passage_max_word_len = passage_shape[ 0], passage_shape[1], passage_shape[2] batch_size_ques, num_words_ques, question_max_word_len = ques_shape[ 0], ques_shape[1], ques_shape[2] # For char embeddings # char CNN with tf.variable_scope("char_embeddings") as scope: char_embedd_matrix = tf.get_variable( 'char_embedd', dtype=tf.float32, initializer=tf.constant(CHAR_EMBEDD_MATRIX) ) # Shape = (all_chars, CHAR_EMBEDD_SIZE) clear_char_embedding_padding = tf.scatter_update( char_embedd_matrix, [0], tf.constant(0.0, dtype=tf.float32, shape=[1, CHAR_EMBEDD_SIZE])) # Embedding lookup using character indices passage_char_embed = tf.nn.embedding_lookup( char_embedd_matrix, self.passage_char_inputs) passage_char_embed = tf.reshape( passage_char_embed, [-1, passage_max_word_len, CHAR_EMBEDD_SIZE]) question_char_embed = tf.nn.embedding_lookup( char_embedd_matrix, self.question_char_inputs) question_char_embed = tf.reshape( question_char_embed, [-1, question_max_word_len, CHAR_EMBEDD_SIZE]) # Final shape = (batch_size*num_words, word_len, CHAR_EMBEDD_SIZE) passage_charCNN_output, cnn_output_size = charCNN(passage_char_embed) question_charCNN_output, cnn_output_size = charCNN(question_char_embed, reuse_scope=True) # Final shape = (batch_size*num_words, CNN_OUTPUT_SIZE) passage_charCNN_output = tf.reshape( passage_charCNN_output, [batch_size_passage, num_words_passage, -1]) question_charCNN_output = tf.reshape( question_charCNN_output, [batch_size_ques, num_words_ques, -1]) # Final shape = (batch_size, num_words, CNN_OUTPUT_SIZE) # print passage_charCNN_output.get_shape().as_list() # Concatenate GloVE, POS, and Character embeddings self.passage_embedd = tf.concat([ self.passage_word_embedd, self.passage_POS_embedd, passage_charCNN_output ], axis=2) self.question_embedd = tf.concat([ self.question_word_embedd, self.question_POS_embedd, question_charCNN_output ], axis=2) final_embedd_shape = VECTOR_DIM + POS_VECTOR_DIM + cnn_output_size # Now for using named entities information if self.use_named_ent_info: if self.use_NER and not self.use_NET_info: # Use only NER information # Simply concatenate NER embeddings self.passage_embedd = tf.concat( [self.passage_embedd, self.passage_NE_embedd_1], axis=2) self.question_embedd = tf.concat( [self.question_embedd, self.question_NE_embedd_1], axis=2) else: # Merge NER and NET info # Fusion unit passage_NE_embed = NE_fusion(self.passage_NE_embedd_1, self.passage_NE_embedd_2) question_NE_embed = NE_fusion(self.question_NE_embedd_1, self.question_NE_embedd_2, reuse_scope=True) # Finally concatenate with previous embeddings self.passage_embedd = tf.concat( [self.passage_embedd, passage_NE_embed], axis=2) self.question_embedd = tf.concat( [self.question_embedd, question_NE_embed], axis=2) final_embedd_shape += NE_VECTOR_DIM # Explicitly set shapes for both tensors # This is required for running dynamic rnns later self.passage_embedd.set_shape([None, None, final_embedd_shape]) self.question_embedd.set_shape([None, None, final_embedd_shape])
def I_fgaba(o, V): o_ = tf.Variable([0.0] * n_n**2, dtype=tf.float64) ind = tf.boolean_mask(tf.range(n_n**2), fgaba_mat.reshape(-1) == 1) o_ = tf.scatter_update(o_, ind, o) o_ = tf.transpose(tf.reshape(o_, (n_n, n_n))) return tf.reduce_sum(tf.transpose((o_ * (V - E_fgaba)) * g_fgaba), 1)
def __call__(self, rgb, label, constructor=None, store_timesteps=False): """ load variable from npy to build the VGG :param rgb: rgb image [batch, height, width, 3] values scaled [0, 1] """ self.gammanet_constructor = constructor X_shape = rgb.get_shape().as_list() self.N = X_shape[0] self.dtype = rgb.dtype self.input = rgb self.ff_reuse = self.scope_reuse self.conv1_1 = self.conv_layer(self.input, "conv1_1") self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2") self.pool1 = self.max_pool(self.conv1_2, 'pool1') self.conv2_1 = self.conv_layer(self.pool1, "conv2_1") self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2") X_shape = self.conv2_2.get_shape().as_list() self.prepare_tensors(X_shape, allow_resize=False) self.create_hidden_states(constructor=self.gammanet_constructor, shapes=self.layer_shapes, recurrent_ff=self.recurrent_ff, init=self.hidden_init, dtype=self.dtype) # self.fgru_0 = tf.get_variable(name="perturb_viz", initializer=self.conv2_2, trainable=True) if self.perturb is not None: # Load weights for deriving tuning curves moments_file = "../undo_bias/neural_models/linear_moments/INSILICO_BSDS_vgg_gratings_simple_tb_feature_matrix.npz" model_file = "../undo_bias/neural_models/linear_models/INSILICO_BSDS_vgg_gratings_simple_tb_model.joblib.npy" moments = np.load(moments_file) means = moments["means"] stds = moments["stds"] clf = np.load(model_file).astype(np.float32) clf_sq = clf.T.dot(clf) inv_clf = np.linalg.pinv(clf_sq).astype( np.float32) # Precompute inversion # Get target units bs, h, w, _ = self.fgru_0.get_shape().as_list() hh, hw = h // 2, w // 2 # sel_units_raw = self.fgru_0[:, hh - 2: hh + 2, hw - 2: hw + 2, :] # sel_units = tf.reshape(sel_units_raw, [bs, -1]) # Squeeze to a matrix # # Normalize activities # sel_units = (sel_units - means) / stds # # Transform to population tuning curves # tc = tf.matmul(tf.matmul(inv_clf, clf, transpose_b=True), sel_units, transpose_b=True) # Reweight tuning curves # tc = tc * self.perturb if 0: # self.perturb <= 1: sel_units_raw = self.fgru_0[:, hh - 2:hh + 2, hw - 2:hw + 2, :] sel_units = tf.reshape(sel_units_raw, [bs, -1]) # Squeeze to a matrix # # Normalize activities sel_units = (sel_units - means) / stds # # Transform to population tuning curves tc = tf.matmul(tf.matmul(inv_clf, clf, transpose_b=True), sel_units, transpose_b=True) # Reweight tuning curves tc = tc + self.perturb ### tc = tf.transpose(label * self.perturb) elif 0: tc = tf.transpose(label) else: # Triganometric perturbations a = 3. b = 0. bin_size = 30. mu = tf.cast(tf.argmax(tf.squeeze(label)), tf.float32) * bin_size # PASS THE TUNING CENTER orientations = np.arange(180).astype(np.float32) perturbation = tf.cos( (np.pi * (orientations - (b + mu)) / 180)) perturbation = tf.nn.relu(perturbation) perturbation = self.perturb * (perturbation**a) # bin here binned_perturbation = tf.reduce_mean( tf.reshape(perturbation, [6, -1]), -1) B = tf.expand_dims(binned_perturbation, 1) A = tf.reshape(label, [-1, 1]) Z = tf.matmul(A, A, transpose_b=True) M = tf.matmul(tf.matmul(B, A, transpose_b=True), pinv(Z)) tc = tf.matmul( M, label, transpose_b=True) # This is out perturbed activity # tc = tf.transpose(tc) """ # Get gradient of tuning curves wrt target units tc_grad = tf.gradients(tc, sel_units)[0] tc_grad = tf.reshape(tc_grad, sel_units_raw.get_shape().as_list()) """ # Invert the inverted model # tc_inv = tf.matmul(tf.matmul(inv_clf, clf, transpose_b=True), tc, transpose_a=True) # Numerical issues inv_inv = np.linalg.pinv(clf.dot(clf.T)) tc_inv = tf.matmul( tf.matmul( tf.matmul(tf.matmul(tc, clf.T, transpose_a=True), clf), clf.T), inv_inv ) # predictions.T @ clf.T @ clf @ clf.T @ np.linalg.pinv(clf @ clf.T) tc_inv = tf.reshape(tc_inv, [-1]) # Unnormalize activities tc_inv = tc_inv * stds + means # Weight the target units with the gradient perturb_mask = np.ones(self.fgru_0.get_shape().as_list(), dtype=np.float32) perturb_idx = np.zeros(self.fgru_0.get_shape().as_list(), dtype=np.float32) perturb_bias = tf.zeros(self.fgru_0.get_shape().as_list(), dtype=tf.float32) self.center_h = self.fgru_0.get_shape().as_list()[1] // 2 self.center_w = self.fgru_0.get_shape().as_list()[2] // 2 perturb_mask[:, hh - 2:hh + 2, hw - 2:hw + 2] = 0. # BG needs to be ignored via stopgrad # bg = tf.cast(tf.greater_equal(tf.reduce_mean(self.fgru_0 ** 2, reduction_indices=[-1], keep_dims=True), 10.), tf.float32) # self.fgru_0 = self.fgru_0 * tf.stop_gradient(bg) bg = tf.reduce_mean(self.conv2_2**2, reduction_indices=[-1], keep_dims=True) bg = tf.cast(tf.greater(bg, tf.reduce_mean(bg)), tf.float32) bg = erosion2d(img=bg, extent=9) if 0: # self.perturb < 0: raise RuntimeError("Negative perturbs dont make sense.") else: perturb_idx[:, hh - 2:hh + 2, hw - 2:hw + 2] = 1. perturb_idxs = np.where(perturb_idx.reshape(-1)) perturb_bias_shape = perturb_bias.get_shape().as_list() perturb_bias = tf.get_variable(name="perturb_bias", initializer=tf.reshape( perturb_bias, [-1]), dtype=tf.float32, trainable=False) perturb_bias = tf.scatter_update(perturb_bias, perturb_idxs[0], tc_inv) perturb_bias = tf.stop_gradient( tf.reshape(perturb_bias, perturb_bias_shape)) # Fixed perturbation if self.perturb_method == "kernel": ks = 21 # Paramaterize the learned hidden state as conv2_2 * kernel self.perturb_mask = tf.get_variable(name="perturb_mask", initializer=perturb_mask, trainable=False) # self.perturb_bias = tf.get_variable(name="perturb_bias", initializer=perturb_bias, trainable=False) # fgru_kernel = tf.get_variable(name="perturb_viz", shape=(ks, ks, 128, 1), initializer=tf.compat.v1.keras.initializers.Orthogonal, trainable=True) fgru_kernel = tf.get_variable( name="perturb_viz", initializer=np.ones( (ks, ks, 128, 1), dtype=np.float32) / (ks * ks), trainable=True) # if self.perturb > 1: # fgru_kernel = fgru_kernel + 1 # Center around 1 """ fgru_kernel = tf.get_variable(name="perturb_viz", shape=(ks, ks, 1, 1), initializer=tf.compat.v1.keras.initializers.Orthogonal, trainable=True) sf = tf.split(self.fgru_0, self.fgru_0.get_shape().as_list()[-1], axis=-1) of = [] for f in sf: of.append(tf.nn.conv2d(f, fgru_kernel, strides=[1, 1, 1, 1], padding="SAME")) self.fgru_0 = tf.concat(of, -1) """ # self.fgru_0 = tf.nn.depthwise_conv2d(tf.stop_gradient(self.conv2_2), fgru_kernel, strides=[1, 1, 1, 1], padding="SAME") pre_kernel = tf.stop_gradient( self.conv2_2) * perturb_mask + perturb_bias self.fgru_0 = tf.nn.depthwise_conv2d(pre_kernel, fgru_kernel, strides=[1, 1, 1, 1], padding="SAME") # self.fgru_0 = self.fgru_0 * self.perturb_mask + self.perturb_bias # Add the bias back in! # self.fgru_0 = bg * (perturb_bias + (self.fgru_0 * perturb_mask)) + tf.stop_gradient(self.fgru_0 * (1 - bg)) elif self.perturb_method == "hidden_state": # self.perturb_mask = tf.get_variable(name="perturb_mask", initializer=perturb_mask, trainable=False) mult = tf.get_variable( name="perturb_viz_mult", initializer=np.ones( (self.conv2_2.get_shape().as_list())).astype( np.float32), trainable=True) # Perturbed fgru add = tf.get_variable( name="perturb_viz_add", initializer=np.zeros( (self.conv2_2.get_shape().as_list())).astype( np.float32), trainable=True) # Perturbed fgru # mult = mult * perturb_mask + tf.stop_gradient(0. * perturb_mask) # add = add * perturb_mask + tf.stop_gradient(0. * perturb_mask) self.fgru_0 = (mult * self.conv2_2 * perturb_mask + perturb_bias) + tf.stop_gradient(1 - perturb_mask) # add = add + self.conv2_2 # self.fgru_0 = perturb_bias + mult + add # self.fgru_0 = perturb_bias + (self.conv2_2 * self.fgru_0) # + tf.stop_gradient(self.conv2_2 * (1 - bg)) # noqa Set center to perturbed value. Mask out middle value in conv2_2. Take middle value from conv2_2 and add a bias. Place this into conv2_2. # Mask the BG self.fgru_0 = bg * self.fgru_0 + tf.stop_gradient( (1 - bg) * self.fgru_0) # self.fgru_0 = perturb_bias else: self.fgru_0 = self.conv2_2 ta = [] for idx in range(self.timesteps): act = self.build(i0=idx) self.ff_reuse = tf.AUTO_REUSE if store_timesteps: ta += [self.fgru_0] if self.downsampled: return act if store_timesteps: return ta
def KernelHyperParameterLearning(trainingX, trainingY): numDataPoints = len(trainingY) numDimension = len(trainingX[0]) # Input and Output Data Declaration for TensorFlow obsX = tf.placeholder(tf.float32, [numDataPoints, numDimension]) obsY = tf.placeholder(tf.float32, [numDataPoints, 1]) # Learning Parameter Variable Declaration for TensorFlow theta0 = tf.Variable(1.0) theta1 = tf.Variable(1.0) theta2 = tf.Variable(1.0) theta3 = tf.Variable(1.0) beta = tf.Variable(1.0) # Kernel building matCovarianceLinear = [] for i in range(numDataPoints): for j in range(numDataPoints): kernelEvaluationResult = KernelFunctionWithTensorFlow( theta0, theta1, theta2, theta3, tf.slice(obsX, [i, 0], [1, numDimension]), tf.slice(obsX, [j, 0], [1, numDimension])) if i != j: matCovarianceLinear.append(kernelEvaluationResult) if i == j: matCovarianceLinear.append(kernelEvaluationResult + tf.div(1.0, beta)) matCovarianceCombined = tf.pack(matCovarianceLinear) matCovariance = tf.reshape(matCovarianceCombined, [numDataPoints, numDataPoints]) matCovarianceInv = tf.inv(matCovariance) # Prediction for calculating sum of sqaured error sumsquarederror = 0.0 for i in range(numDataPoints): k = tf.Variable(tf.ones([numDataPoints])) for j in range(numDataPoints): kernelEvaluationResult = KernelFunctionWithTensorFlow( theta0, theta1, theta2, theta3, tf.slice(obsX, [i, 0], [1, numDimension]), tf.slice(obsX, [j, 0], [1, numDimension])) indices = tf.constant([j]) tempTensor = tf.Variable(tf.zeros([1])) tempTensor = tf.add(tempTensor, kernelEvaluationResult) tf.scatter_update(k, tf.reshape(indices, [1, 1]), tempTensor) c = tf.Variable(tf.zeros([1, 1])) kernelEvaluationResult = KernelFunctionWithTensorFlow( theta0, theta1, theta2, theta3, tf.slice(obsX, [i, 0], [1, numDimension]), tf.slice(obsX, [i, 0], [1, numDimension])) c = tf.div(tf.add(tf.add(c, kernelEvaluationResult), 1), beta) k = tf.reshape(k, [1, numDataPoints]) predictionMu = tf.matmul(k, tf.matmul(matCovarianceInv, obsY)) predictionVar = tf.sub( c, tf.matmul(k, tf.matmul(matCovarianceInv, tf.transpose(k)))) sumsquarederror = tf.add( sumsquarederror, tf.pow(tf.sub(predictionMu, tf.slice(obsY, [i, 0], [1, 1])), 2)) # Training session declaration training = tf.train.GradientDescentOptimizer(0.0001).minimize( sumsquarederror) # Session initialization gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) init = tf.initialize_all_variables() sess.run(init) # Session Running for Training for i in range(100): sess.run(training, feed_dict={obsX: trainingX, obsY: trainingY}) trainedTheta = [] trainedTheta.append( sess.run(theta0, feed_dict={ obsX: trainingX, obsY: trainingY })) trainedTheta.append( sess.run(theta1, feed_dict={ obsX: trainingX, obsY: trainingY })) trainedTheta.append( sess.run(theta2, feed_dict={ obsX: trainingX, obsY: trainingY })) trainedTheta.append( sess.run(theta3, feed_dict={ obsX: trainingX, obsY: trainingY })) trainedBeta = sess.run(beta, feed_dict={ obsX: trainingX, obsY: trainingY }) # print "---------------------- Iteration ",i," -----------------" # print "Sum of Squared Error : ",sess.run(sumsquarederror, feed_dict={obsX: trainingX, obsY: trainingY}) # print "Theta : ",trainedTheta # print "Beta : ",trainedBeta # Return Learning Result return trainedTheta, trainedBeta
def __init__(self): SegBase.__init__(self) self.dtype = tf.float32 # 参数初始化 self.skip_window_left = constant.LSTM_SKIP_WINDOW_LEFT self.skip_window_right = constant.LSTM_SKIP_WINDOW_RIGHT self.window_size = self.skip_window_left + self.skip_window_right + 1 self.embed_size = 100 self.hidden_units = 150 self.tag_count = 4 self.concat_embed_size = self.window_size * self.embed_size self.vocab_size = constant.VOCAB_SIZE self.alpha = 0.02 self.lam = 0.001 self.eta = 0.02 self.dropout_rate = 0.2 # 数据初始化 trans = TransformDataLSTM() self.words_batch = trans.words_batch self.tags_batch = trans.labels_batch self.dictionary = trans.dictionary # 模型定义和初始化 self.sess = tf.Session() self.optimizer = tf.train.GradientDescentOptimizer(self.alpha) self.x = tf.placeholder(self.dtype, shape=[1, None, self.concat_embed_size]) self.embeddings = tf.Variable(tf.truncated_normal( [self.vocab_size, self.embed_size], stddev=-1.0 / math.sqrt(self.embed_size), dtype=self.dtype), dtype=self.dtype, name='embeddings') self.w = tf.Variable(tf.truncated_normal( [self.tags_count, self.hidden_units], stddev=1.0 / math.sqrt(self.concat_embed_size)), dtype=self.dtype, name='w') self.b = tf.Variable(tf.zeros([self.tag_count, 1]), dtype=self.dtype, name='b') self.A = tf.Variable(tf.random_uniform( [self.tag_count, self.tag_count], -0.05, 0.05), dtype=self.dtype, name='A') self.Ap = tf.placeholder(self.dtype, shape=self.A.get_shape()) self.init_A = tf.Variable(tf.random_uniform([self.tag_count], -0.05, 0.05), dtype=self.dtype, name='init_A') self.init_Ap = tf.placeholder(self.dtype, shape=self.init_A.get_shape()) self.update_A_op = (1 - self.lam) * self.A.assign_add( self.alpha * self.Ap) self.update_init_A_op = (1 - self.lam) * self.init_A.assign_add( self.alpha * self.init_Ap) self.sentence_holder = tf.placeholder(tf.int32, shape=[None, self.window_size]) self.lookup_op = tf.nn.embedding_lookup(self.embeddings, self.sentence_holder) self.indices = tf.placeholder(tf.int32, shape=[None, 2]) self.shape = tf.placeholder(tf.int32, shape=[2]) self.values = tf.placeholder(self.dtype, shape=[None]) self.map_matrix_op = tf.sparse_to_dense(self.indices, self.shape, self.values, validate_indices=False) self.map_matrix = tf.placeholder(self.dtype, shape=[self.tag_count, None]) self.lstm = tf.contrib.rnn.LSTMCell(self.hidden_units) self.lstm_output, self.lstm_out_state = tf.nn.dynamic_rnn( self.lstm, self.x, dtype=self.dtype) tf.global_variables_initializer().run(session=self.sess) self.word_scores = tf.matmul(self.w, tf.transpose( self.lstm_output[0])) + self.b self.loss_scores = tf.multiply(self.map_matrix, self.word_scores) self.loss = tf.reduce_sum(self.loss_scores) self.lstm_variable = [ v for v in tf.global_variables() if v.name.startswith('rnn') ] self.params = [self.w, self.b] + self.lstm_variable self.regularization = list( map(lambda p: tf.assign_sub(p, self.lam * p), self.params)) self.train = self.optimizer.minimize(self.loss, var_list=self.params) self.embedp = tf.placeholder(self.dtype, shape=[None, self.embed_size]) self.embed_index = tf.placeholder(tf.int32, shape=[None]) self.update_embed_op = tf.scatter_update(self.embeddings, self.embed_index, self.embedp) self.sentence_length = 1 #tf.placeholder(tf.int32, shape=[1]) self.grad_embed = tf.gradients( tf.split(self.loss_scores, self.sentence_length), tf.split(self.x, self.sentence_length, 1)) self.saver = tf.train.Saver(self.params + [self.embeddings, self.A, self.init_A], max_to_keep=100) self.regu = tf.contrib.layers.apply_regularization( tf.contrib.layers.l2_regularizer(self.lam), self.params + [self.A, self.init_A])
def _build_single_target(self, anchors, valid_flags, gt_boxes, gt_class_ids): '''Compute targets per instance. Args --- anchors: [num_anchors, (y1, x1, y2, x2)] valid_flags: [num_anchors] gt_class_ids: [num_gt_boxes] gt_boxes: [num_gt_boxes, (y1, x1, y2, x2)] Returns --- target_matchs: [num_anchors] target_deltas: [num_rpn_deltas, (dy, dx, log(dh), log(dw))] ''' gt_boxes, _ = trim_zeros(gt_boxes) target_matchs = tf.zeros(anchors.shape[0], dtype=tf.int32) # Compute overlaps [num_anchors, num_gt_boxes] overlaps = geometry.compute_overlaps(anchors, gt_boxes) # Match anchors to GT Boxes # If an anchor overlaps a GT box with IoU >= 0.7 then it's positive. # If an anchor overlaps a GT box with IoU < 0.3 then it's negative. # Neutral anchors are those that don't match the conditions above, # and they don't influence the loss function. # However, don't keep any GT box unmatched (rare, but happens). Instead, # match it to the closest anchor (even if its max IoU is < 0.3). neg_values = tf.constant([0, -1]) pos_values = tf.constant([0, 1]) # 1. Set negative anchors first. They get overwritten below if a GT box is # matched to them. anchor_iou_argmax = tf.argmax(overlaps, axis=1) anchor_iou_max = tf.reduce_max(overlaps, reduction_indices=[1]) target_matchs = tf.where(anchor_iou_max < self.neg_iou_thr, -tf.ones(anchors.shape[0], dtype=tf.int32), target_matchs) # filter invalid anchors target_matchs = tf.where(tf.equal(valid_flags, 1), target_matchs, tf.zeros(anchors.shape[0], dtype=tf.int32)) # 2. Set anchors with high overlap as positive. target_matchs = tf.where(anchor_iou_max >= self.pos_iou_thr, tf.ones(anchors.shape[0], dtype=tf.int32), target_matchs) # 3. Set an anchor for each GT box (regardless of IoU value). gt_iou_argmax = tf.argmax(overlaps, axis=0) target_matchs = tf.scatter_update(tf.Variable(target_matchs), gt_iou_argmax, 1) # Subsample to balance positive and negative anchors # Don't let positives be more than half the anchors ids = tf.where(tf.equal(target_matchs, 1)) ids = tf.squeeze(ids, 1) extra = ids.shape.as_list()[0] - int( self.num_rpn_deltas * self.positive_fraction) if extra > 0: # Reset the extra ones to neutral ids = tf.random_shuffle(ids)[:extra] target_matchs = tf.scatter_update(target_matchs, ids, 0) # Same for negative proposals ids = tf.where(tf.equal(target_matchs, -1)) ids = tf.squeeze(ids, 1) extra = ids.shape.as_list()[0] - (self.num_rpn_deltas - tf.reduce_sum( tf.cast(tf.equal(target_matchs, 1), tf.int32))) if extra > 0: # Rest the extra ones to neutral ids = tf.random_shuffle(ids)[:extra] target_matchs = tf.scatter_update(target_matchs, ids, 0) # For positive anchors, compute shift and scale needed to transform them # to match the corresponding GT boxes. ids = tf.where(tf.equal(target_matchs, 1)) a = tf.gather_nd(anchors, ids) anchor_idx = tf.gather_nd(anchor_iou_argmax, ids) gt = tf.gather(gt_boxes, anchor_idx) target_deltas = transforms.bbox2delta(a, gt, self.target_means, self.target_stds) padding = tf.maximum(self.num_rpn_deltas - tf.shape(target_deltas)[0], 0) target_deltas = tf.pad(target_deltas, [(0, padding), (0, 0)]) return target_matchs, target_deltas
def build_init_cell(self): with tf.variable_scope("init_cell"): # always zero dummy = tf.placeholder(tf.float32, [1, 1], name='dummy') # memory M_init_linear = tf.tanh( Linear(dummy, self.mem_size * self.mem_dim, name='M_init_linear')) M_init = tf.reshape(M_init_linear, [self.mem_size, self.mem_dim]) # read weights read_w_init = tf.Variable( tf.zeros([self.read_head_size, self.mem_size])) read_init = tf.Variable( tf.zeros([self.read_head_size, 1, self.mem_dim])) for idx in xrange(self.read_head_size): # initialize bias distribution with `tf.range(mem_size-2, 0, -1)` read_w_linear_idx = Linear(dummy, self.mem_size, is_range=True, name='read_w_linear_%s' % idx) read_w_init = tf.scatter_update( read_w_init, [idx], tf.nn.softmax(read_w_linear_idx)) read_init_idx = tf.tanh( Linear(dummy, self.mem_dim, name='read_init_%s' % idx)) read_init = tf.scatter_update( read_init, [idx], tf.reshape(read_init_idx, [1, 1, self.mem_dim])) # write weights write_w_init = tf.Variable( tf.zeros([self.write_head_size, self.mem_size])) for idx in xrange(self.write_head_size): write_w_linear_idx = Linear(dummy, self.mem_size, is_range=True, name='write_w_linear_%s' % idx) write_w_init = tf.scatter_update( write_w_init, [idx], tf.nn.softmax(write_w_linear_idx)) # controller state output_init = tf.Variable( tf.zeros([self.controller_layer_size, self.controller_dim])) hidden_init = tf.Variable( tf.zeros([self.controller_layer_size, self.controller_dim])) for idx in xrange(self.controller_layer_size): output_init = tf.scatter_update( output_init, [idx], tf.reshape( tf.tanh( Linear(dummy, self.controller_dim, name='output_init_%s' % idx)), [1, self.controller_dim])) hidden_init = tf.scatter_update( hidden_init, [idx], tf.reshape( tf.tanh( Linear(dummy, self.controller_dim, name='hidden_init_%s' % idx)), [1, self.controller_dim])) new_output = tf.tanh( Linear(dummy, self.output_dim, name='new_output')) inputs = { 'input': dummy, } outputs = { 'new_output': new_output, 'M': M_init, 'read_w': read_w_init, 'write_w': write_w_init, 'read': tf.reshape(read_init, [self.read_head_size, self.mem_dim]), 'output': output_init, 'hidden': hidden_init } return inputs, outputs
def build_controller(self, input, read_prev, output_prev, hidden_prev): with tf.variable_scope("controller"): output = tf.Variable( tf.zeros([self.controller_layer_size, self.controller_dim])) hidden = tf.Variable( tf.zeros([self.controller_layer_size, self.controller_dim])) for layer_idx in xrange(self.controller_layer_size): if self.controller_layer_size == 1: o_prev = output_prev h_prev = hidden_prev else: o_prev = tf.reshape(tf.gather(output_prev, layer_idx), [1, -1]) h_prev = tf.reshape(tf.gather(hidden_prev, layer_idx), [1, -1]) if layer_idx == 0: def new_gate(gate_name): in_modules = [ Linear(input, self.controller_dim, name='%s_gate_1_%s' % (gate_name, layer_idx)), Linear(o_prev, self.controller_dim, name='%s_gate_2_%s' % (gate_name, layer_idx)), ] if self.read_head_size == 1: in_modules.append( Linear(read_prev, self.controller_dim, name='%s_gate_3_%s' % (gate_name, layer_idx))) else: for read_idx in xrange(self.read_head_size): vec = tf.reshape( tf.gather(read_prev, read_idx), [1, -1]) in_modules.append( Linear(vec, self.controller_dim, name='%s_gate_3_%s_%s' % (gate_name, layer_idx, read_idx))) return tf.add_n(in_modules) else: def new_gate(gate_name): return tf.add_n([ Linear(tf.reshape(tf.gather(output, layer_idx - 1), [1, -1]), self.controller_dim, name='%s_gate_1_%s' % (gate_name, layer_idx)), Linear(o_prev, self.controller_dim, name='%s_gate_2_%s' % (gate_name, layer_idx)), ]) # input, forget, and output gates for LSTM i = tf.sigmoid(new_gate('input')) f = tf.sigmoid(new_gate('forget')) o = tf.sigmoid(new_gate('output')) update = tf.tanh(new_gate('update')) # update the sate of the LSTM cell hidden = tf.scatter_update(hidden, [layer_idx], tf.add_n([f * h_prev, i * update])) output = tf.scatter_update( output, [layer_idx], o * tf.tanh(tf.gather(hidden, layer_idx))) return output, hidden
def collect_experience(tf_env, agent, meta_agent, state_preprocess, replay_buffer, meta_replay_buffer, action_fn, meta_action_fn, environment_steps, num_episodes, num_resets, episode_rewards, episode_meta_rewards, store_context, disable_agent_reset): """Collect experience in a tf_env into a replay_buffer using action_fn. Args: tf_env: A TFEnvironment. agent: A UVF agent. meta_agent: A Meta Agent. replay_buffer: A Replay buffer to collect experience in. meta_replay_buffer: A Replay buffer to collect meta agent experience in. action_fn: A function to produce actions given current state. meta_action_fn: A function to produce meta actions given current state. environment_steps: A variable to count the number of steps in the tf_env. num_episodes: A variable to count the number of episodes. num_resets: A variable to count the number of resets. store_context: A boolean to check if store context in replay. disable_agent_reset: A boolean that disables agent from resetting. Returns: A collect_experience_op that excute an action and store into the replay_buffers """ tf_env.start_collect() state = tf_env.current_obs() state_repr = state_preprocess(state) action = action_fn(state, context=None) with tf.control_dependencies([state]): transition_type, reward, discount = tf_env.step(action) def increment_step(): return environment_steps.assign_add(1) def increment_episode(): return num_episodes.assign_add(1) def increment_reset(): return num_resets.assign_add(1) def update_episode_rewards(context_reward, meta_reward, reset): new_episode_rewards = tf.concat( [episode_rewards[:1] + context_reward, episode_rewards[1:]], 0) new_episode_meta_rewards = tf.concat( [episode_meta_rewards[:1] + meta_reward, episode_meta_rewards[1:]], 0) return tf.group( episode_rewards.assign( tf.cond(reset, lambda: tf.concat([[0.], episode_rewards[:-1]], 0), lambda: new_episode_rewards)), episode_meta_rewards.assign( tf.cond(reset, lambda: tf.concat([[0.], episode_meta_rewards[:-1]], 0), lambda: new_episode_meta_rewards))) def no_op_int(): return tf.constant(0, dtype=tf.int64) step_cond = agent.step_cond_fn(state, action, transition_type, environment_steps, num_episodes) reset_episode_cond = agent.reset_episode_cond_fn( state, action, transition_type, environment_steps, num_episodes) reset_env_cond = agent.reset_env_cond_fn(state, action, transition_type, environment_steps, num_episodes) increment_step_op = tf.cond(step_cond, increment_step, no_op_int) increment_episode_op = tf.cond(reset_episode_cond, increment_episode, no_op_int) increment_reset_op = tf.cond(reset_env_cond, increment_reset, no_op_int) increment_op = tf.group(increment_step_op, increment_episode_op, increment_reset_op) with tf.control_dependencies([increment_op, reward, discount]): next_state = tf_env.current_obs() next_state_repr = state_preprocess(next_state) next_reset_episode_cond = tf.logical_or( agent.reset_episode_cond_fn( state, action, transition_type, environment_steps, num_episodes), tf.equal(discount, 0.0)) if store_context: context = [tf.identity(var) + tf.zeros_like(var) for var in agent.context_vars] meta_context = [tf.identity(var) + tf.zeros_like(var) for var in meta_agent.context_vars] else: context = [] meta_context = [] with tf.control_dependencies([next_state] + context + meta_context): if disable_agent_reset: collect_experience_ops = [tf.no_op()] # don't reset agent else: collect_experience_ops = agent.cond_begin_episode_op( tf.logical_not(reset_episode_cond), [state, action, reward, next_state, state_repr, next_state_repr], mode='explore', meta_action_fn=meta_action_fn) context_reward, meta_reward = collect_experience_ops collect_experience_ops = list(collect_experience_ops) collect_experience_ops.append( update_episode_rewards(tf.reduce_sum(context_reward), meta_reward, reset_episode_cond)) meta_action_every_n = agent.tf_context.meta_action_every_n with tf.control_dependencies(collect_experience_ops): transition = [state, action, reward, discount, next_state] meta_action = tf.to_float( tf.concat(context, -1)) # Meta agent action is low-level context meta_end = tf.logical_and( # End of meta-transition. tf.equal(agent.tf_context.t % meta_action_every_n, 1), agent.tf_context.t > 1) with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE): states_var = tf.get_variable('states_var', [meta_action_every_n, state.shape[-1]], state.dtype) actions_var = tf.get_variable('actions_var', [meta_action_every_n, action.shape[-1]], action.dtype) state_var = tf.get_variable('state_var', state.shape, state.dtype) reward_var = tf.get_variable('reward_var', reward.shape, reward.dtype) meta_action_var = tf.get_variable('meta_action_var', meta_action.shape, meta_action.dtype) meta_context_var = [ tf.get_variable('meta_context_var%d' % idx, meta_context[idx].shape, meta_context[idx].dtype) for idx in range(len(meta_context))] actions_var_upd = tf.scatter_update( actions_var, (agent.tf_context.t - 2) % meta_action_every_n, action) with tf.control_dependencies([actions_var_upd]): actions = tf.identity(actions_var) + tf.zeros_like(actions_var) meta_reward = tf.identity(meta_reward) + tf.zeros_like(meta_reward) meta_reward = tf.reshape(meta_reward, reward.shape) reward = 0.1 * meta_reward meta_transition = [state_var, meta_action_var, reward_var + reward, discount * (1 - tf.to_float(next_reset_episode_cond)), next_state] meta_transition.extend([states_var, actions]) if store_context: # store current and next context into replay transition += context + list(agent.context_vars) meta_transition += meta_context_var + list(meta_agent.context_vars) meta_step_cond = tf.squeeze(tf.logical_and(step_cond, tf.logical_or(next_reset_episode_cond, meta_end))) collect_experience_op = tf.group( replay_buffer.maybe_add(transition, step_cond), meta_replay_buffer.maybe_add(meta_transition, meta_step_cond), ) with tf.control_dependencies([collect_experience_op]): collect_experience_op = tf.cond(reset_env_cond, tf_env.reset, tf_env.current_time_step) meta_period = tf.equal(agent.tf_context.t % meta_action_every_n, 1) states_var_upd = tf.scatter_update( states_var, (agent.tf_context.t - 1) % meta_action_every_n, next_state) state_var_upd = tf.assign( state_var, tf.cond(meta_period, lambda: next_state, lambda: state_var)) reward_var_upd = tf.assign( reward_var, tf.cond(meta_period, lambda: tf.zeros_like(reward_var), lambda: reward_var + reward)) meta_action = tf.to_float(tf.concat(agent.context_vars, -1)) meta_action_var_upd = tf.assign( meta_action_var, tf.cond(meta_period, lambda: meta_action, lambda: meta_action_var)) meta_context_var_upd = [ tf.assign( meta_context_var[idx], tf.cond(meta_period, lambda: meta_agent.context_vars[idx], lambda: meta_context_var[idx])) for idx in range(len(meta_context))] return tf.group( collect_experience_op, states_var_upd, state_var_upd, reward_var_upd, meta_action_var_upd, *meta_context_var_upd)
#tf.reduce_sum(input_h_neg*(input_t_neg-input_r_neg),axis=1) loss = tf.reduce_sum(tf.nn.relu(score_hrt_pos - score_hrt_neg + margin_)) #+regularizer_weight*regularizer_loss #optimizer=tf.train.GradientDescentOptimizer(learning_rate=lr) optimizer = tf.train.AdadeltaOptimizer(learning_rate=lr) grads = optimizer.compute_gradients(loss, trainable) op_train = optimizer.apply_gradients(grads) idx_e = tf.placeholder(tf.int32, [None]) idx_r = tf.placeholder(tf.int32, [None]) normedE = tf.nn.l2_normalize(tf.nn.embedding_lookup(ent_embedding, idx_e), axis=1) normedR = tf.nn.l2_normalize(tf.nn.embedding_lookup(rel_embedding, idx_r), axis=1) updateE = tf.scatter_update(ent_embedding, idx_e, normedE) updateR = tf.scatter_update(rel_embedding, idx_r, normedR) saver = tf.train.Saver() # 启动图 (graph) init = tf.global_variables_initializer() init_local_op = tf.initialize_local_variables() loss_sum = 0 with tf.Session() as sess: sess.run(init) sess.run(init_local_op) ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: print("success! %s." % ckpt.model_checkpoint_path)
def __call__(self, frame0, pos_2d_new): batch_size = 1 #tf.shape(frame0)[0] height = 376 #tf.shape(frame0)[1] width = 672 #tf.shape(frame0)[2] num_channels = 3 #tf.shape(frame0)[3] output_height = height output_width = width height_float = tf.cast(height, dtype='float32') width_float = tf.cast(width, dtype='float32') x_s = tf.slice(pos_2d_new, [0, 1, 0], [-1, 1, -1]) x = tf.cast(tf.reshape(x_s, [-1]), dtype='float32') x = x * (width_float) y_s = tf.slice(pos_2d_new, [0, 0, 0], [-1, 1, -1]) y = tf.cast(tf.reshape(y_s, [-1]), dtype='float32') y = y * (height_float) x0 = tf.cast(tf.floor(x), 'int32') x1 = x0 + 1 y0 = tf.cast(tf.floor(y), 'int32') y1 = y0 + 1 max_y = tf.cast(height - 1, dtype='int32') max_x = tf.cast(width - 1, dtype='int32') zero = tf.zeros([], dtype='int32') x0 = tf.clip_by_value(x0, zero, max_x) x1 = tf.clip_by_value(x1, zero + 1, max_x + 1) y0 = tf.clip_by_value(y0, zero, max_y) y1 = tf.clip_by_value(y1, zero + 1, max_y + 1) x_ref = tf.range(1, width + 1) y_ref = tf.range(1, height + 1) x_ref, y_ref = tf.meshgrid(x_ref, y_ref) x_ref = self._repeat(x_ref, batch_size) y_ref = self._repeat(y_ref, batch_size) flat_image_dimensions = width * height pixels_batch = tf.range(batch_size) * flat_image_dimensions flat_output_dimensions = output_height * output_width base = self._repeat(pixels_batch, flat_output_dimensions) base_y0 = base + y0 * width base_y1 = base + y1 * width indices_a = base_y0 + x0 indices_b = base_y1 + x0 indices_c = base_y0 + x1 indices_d = base_y1 + x1 base_y_ref = base + y_ref * width indices_ref = base_y_ref + x_ref flat_image = tf.reshape(frame0, shape=(-1, num_channels)) flat_image = tf.cast(flat_image, dtype='float32') pixel_values_ref = tf.gather(flat_image, indices_ref) x0 = tf.cast(x0, 'float32') x1 = tf.cast(x1, 'float32') y0 = tf.cast(y0, 'float32') y1 = tf.cast(y1, 'float32') area_a = tf.expand_dims(((x1 - x) * (y1 - y)), 1) area_b = tf.expand_dims(((x1 - x) * (y - y0)), 1) area_c = tf.expand_dims(((x - x0) * (y1 - y)), 1) area_d = tf.expand_dims(((x - x0) * (y - y0)), 1) #x1,y1 indicies_d : area_a*pixel_values_ref #x1,y0 indicies_c : area_b*pixel_values_ref #x0,y1 indicies_b : area_c*pixel_values_ref #x0,y0 indicies_a : area_d*pixel_values_ref pixel_x1y1 = area_a * pixel_values_ref pixel_x1y0 = area_b * pixel_values_ref pixel_x0y1 = area_c * pixel_values_ref pixel_x0y0 = area_d * pixel_values_ref if not self.transformed_image: self.transformed_image = tf.Variable(tf.zeros( [batch_size * width * height, 3]), trainable=False) self.transformed_image = tf.assign( self.transformed_image, tf.zeros([batch_size * width * height, 3])) self.transformed_image = tf.scatter_update(self.transformed_image, indices_d, pixel_x1y1) self.transformed_image = tf.scatter_add(self.transformed_image, indices_c, pixel_x1y0) self.transformed_image = tf.scatter_add(self.transformed_image, indices_b, pixel_x0y1) self.transformed_image = tf.scatter_add(self.transformed_image, indices_a, pixel_x0y0) transformed_image = tf.reshape(self.transformed_image, shape=(-1, output_height, output_width, num_channels)) return transformed_image
def inference_graph(self, char_vocab_size, word_vocab_size, char_embed_size=15, batch_size=20, num_highway_layers=2, num_lstm_layers=2, rnn_size=650, max_word_length=65, kernels=[1, 2, 3, 4, 5, 6, 7], kernel_features=[50, 100, 150, 200, 200, 200, 200], num_unroll_steps=35, dropout=0.0): assert len(kernels) == len( kernel_features), 'Kernel and Features must have the same size' self.input = tf.placeholder( tf.int32, shape=[batch_size, num_unroll_steps, max_word_length], name="input") ''' First, embed characters ''' with tf.variable_scope('Embedding'): char_embedding = tf.get_variable( 'char_embedding', [char_vocab_size, char_embed_size]) if self.projector_config: embedding = self.projector_config.embeddings.add() embedding.tensor_name = char_embedding.name embedding.metadata_path = self.metadata ''' this op clears embedding vector of first symbol (symbol at position 0, which is by convention the position of the padding symbol). It can be used to mimic Torch7 embedding operator that keeps padding mapped to zero embedding vector and ignores gradient updates. For that do the following in TF: 1. after parameter initialization, apply this op to zero out padding embedding vector 2. after each gradient update, apply this op to keep padding at zero''' self.clear_char_embedding_padding = tf.scatter_update( char_embedding, [0], tf.constant(0.0, shape=[1, char_embed_size])) # [batch_size x max_word_length, num_unroll_steps, char_embed_size] input_embedded = tf.nn.embedding_lookup(char_embedding, self.input) input_embedded = tf.reshape(input_embedded, [-1, max_word_length, char_embed_size]) ''' Second, apply convolutions ''' # [batch_size x num_unroll_steps, cnn_size] where cnn_size=sum(kernel_features) output_cnn = self.conv2dLayers(input_embedded, kernels, kernel_features) if num_highway_layers > 0: output_cnn = self.highway(output_cnn, output_cnn.get_shape()[-1], num_layers=num_highway_layers) ''' Finally, do LSTM ''' with tf.variable_scope('LSTM'): def create_rnn_cell(): cell = tf.contrib.rnn.BasicLSTMCell(rnn_size, state_is_tuple=True, forget_bias=0.0) if dropout: cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=1. - dropout) return cell cell = tf.contrib.rnn.MultiRNNCell( [create_rnn_cell() for _ in range(num_lstm_layers)], state_is_tuple=True) self.initial_rnn_state = cell.zero_state(batch_size, dtype=tf.float32) output_cnn = tf.reshape(output_cnn, [batch_size, num_unroll_steps, -1]) output_cnn2 = [ tf.squeeze(x, [1]) for x in tf.split(output_cnn, num_unroll_steps, 1) ] outputs, self.final_rnn_state = tf.contrib.rnn.static_rnn( cell, output_cnn2, initial_state=self.initial_rnn_state, dtype=tf.float32) # linear projection onto output (word) vocab self.logits = [] with tf.variable_scope('WordEmbedding') as scope: for idx, output in enumerate(outputs): if idx > 0: scope.reuse_variables() self.logits.append(self.linear(output, word_vocab_size))
def _create_dilation_layer(self, input_batch, layer_index, dilation,local_condition_batch,global_condition_batch): # input_batch는 train mode에서는 길이 줄어드는 것을 대비하여 padding이 되어 있다. with tf.variable_scope('dilation_layer'): residual = input_batch if self.train_mode: # padding padding = (self.filter_width - 1)*dilation input_batch = tf.pad(input_batch, tf.constant([(0, 0), (padding, 0), (0, 0)])) else: self.dilation_queue[layer_index] = tf.scatter_update(self.dilation_queue[layer_index],tf.range(self.batch_size),tf.concat([self.dilation_queue[layer_index][:,1:,:],input_batch],axis=1) ) input_batch = self.dilation_queue[layer_index] input_batch = tf.layers.dropout(input_batch,rate=self.drop_rate,training=self.train_mode) dilation_layer = tf.layers.Conv1D(filters=self.dilation_channels*2,kernel_size=self.filter_width,dilation_rate=dilation,padding='valid',use_bias=self.use_biases,name='conv_filter_gate') if self.train_mode: conv = dilation_layer(input_batch) conv_filter, conv_gate = tf.split(conv,2,axis=-1) else: dilation_layer.build((self.batch_size,1,input_batch.shape.as_list()[-1])) # shape의 마지막만 중요함. kernel을 잡는데 마지막 차원만 사용됨 linearized_weights = tf.reshape(dilation_layer.kernel,(-1,self.dilation_channels*2)) input_batch = input_batch[:, 0::dilation, :] temp = tf.matmul(tf.reshape(input_batch,(self.batch_size,-1)), linearized_weights) if self.use_biases: temp = tf.nn.bias_add(temp, dilation_layer.bias) conv_filter, conv_gate = tf.split(tf.expand_dims(temp,1),2,axis=-1) if global_condition_batch is not None: conv_filter += tf.layers.conv1d(global_condition_batch,filters=self.dilation_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="gc_filter") conv_gate += tf.layers.conv1d(global_condition_batch,filters=self.dilation_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="gc_gate") if local_condition_batch is not None: local_filter = tf.layers.conv1d(local_condition_batch,filters=self.dilation_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="lc_filter") local_gate = tf.layers.conv1d(local_condition_batch,filters=self.dilation_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="lc_gate") conv_filter += local_filter conv_gate += local_gate out = tf.tanh(conv_filter) * tf.sigmoid(conv_gate) # The 1x1 conv to produce the residual output == FC transformed = tf.layers.conv1d(out,filters=self.residual_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="dense") # The 1x1 conv to produce the skip output skip_contribution = tf.layers.conv1d(out,filters=self.skip_channels,kernel_size=1,padding="same",use_bias=self.use_biases,name="skip") # residual + transformed: 다음 단계의 입력으로 들어감 if self.residual_legacy: out = (residual + transformed) * np.sqrt(0.5) else: out = residual + transformed return skip_contribution, out # skip_contribution: 결과값으로 쌓임.
def perform(self, agent_indices, observ): """Compute batch of actions and a summary for a batch of observation. Args: agent_indices: Tensor containing current batch indices. observ: Tensor of a batch of observations for all agents. Returns: Tuple of action batch tensor and summary tensor. """ with tf.name_scope('perform/'): observ = self._observ_filter.transform(observ) if self._last_state is None: state = None else: state = tools.nested.map(lambda x: tf.gather(x, agent_indices), self._last_state) with tf.device('/gpu:0' if self._use_gpu else '/cpu:0'): output = self._network(observ[:, None], tf.ones(observ.shape[0]), state) # policy sample_ = tf.concat([ tf.zeros(shape=[observ.shape[0], 1, 13], dtype=tf.float32), output.policy[ACT['DEF_DASH']].sample() ], axis=2) mode_ = tf.concat([ tf.zeros(shape=[observ.shape[0], 1, 13], dtype=tf.float32), output.policy[ACT['DEF_DASH']].mode() ], axis=2) action = tf.where(self._is_training, sample_, mode_) logprob = output.policy[ACT['DEF_DASH']].log_prob(action[:, :, 13:23])[:, 0] # pylint: disable=g-long-lambda summary = tf.cond( self._should_log, lambda: tf.summary.merge([ tf.summary.histogram('mode', mode_[:, 0, 13:23]), tf.summary.histogram('DEF_DASH', action[:, 0, 13:23]), tf.summary.histogram('logprob', logprob) ]), str) # Remember current policy to append to memory in the experience callback. if self._last_state is None: assign_state = tf.no_op() else: assign_state = utility.assign_nested_vars( self._last_state, output.state, agent_indices) remember_last_action = tf.scatter_update(self._last_action, agent_indices, action[:, 0]) def is_tensor(x): return isinstance(x, tf.Tensor) policy_params = [] remember_last_policy = tuple() for i in range(1): policy_params.append( tools.nested.filter(is_tensor, output.policy[i].parameters)) remember_last_policy += tools.nested.map( lambda var, val: tf.scatter_update(var, agent_indices, val[:, 0]), self._last_policy[i], policy_params[i], flatten=True) assert policy_params, 'Policy has no parameters to store.' with tf.control_dependencies((assign_state, remember_last_action) + remember_last_policy): return action[:, 0], tf.identity(summary)
def inference_graph(char_vocab_size, char_embed_size=20, batch_size=20, num_highway_layers=2, num_rnn_layers=2, rnn_size=50, max_word_length=65, kernels=[2] * 20 + [3] * 10, kernel_features=[32] * 30, dropout=0.0, embed_dimension=32): assert len(kernels) == len( kernel_features), 'Kernel and Features must have the same size' with tf.variable_scope('Encoder'): input_ = tf.placeholder(tf.int32, shape=[batch_size, max_word_length], name="input") input_len = tf.placeholder(tf.int32, shape=[batch_size], name="input") ''' First, embed characters ''' with tf.variable_scope('Embedding'): char_embedding = tf.get_variable( 'char_embedding', [char_vocab_size, char_embed_size]) ''' this op clears embedding vector of first symbol (symbol at position 0, which is by convention the position of the padding symbol). It can be used to mimic Torch7 embedding operator that keeps padding mapped to zero embedding vector and ignores gradient updates. For that do the following in TF: 1. after parameter initialization, apply this op to zero out padding embedding vector 2. after each gradient update, apply this op to keep padding at zero''' clear_char_embedding_padding = tf.scatter_update( char_embedding, [0], tf.constant(0.0, shape=[1, char_embed_size])) # [batch_size x max_word_length, num_unroll_steps, char_embed_size] input_embedded = tf.nn.embedding_lookup(char_embedding, input_) ''' Second, apply convolutions ''' input_cnn = tdnn(input_embedded, kernels, kernel_features) input_cnn = tf.layers.batch_normalization(input_cnn) ''' Maybe apply Highway ''' if num_highway_layers > 0: input_cnn = tf.reshape(input_cnn, [batch_size * max_word_length, -1]) input_cnn = highway(input_cnn, input_cnn.get_shape()[-1], num_layers=num_highway_layers) input_cnn = tf.reshape(input_cnn, [batch_size, max_word_length, -1]) input_cnn = tf.layers.batch_normalization(input_cnn) ''' Finally, do LSTM ''' with tf.variable_scope('LSTM'): def create_rnn_cell(): cell = tf.contrib.rnn.BasicLSTMCell(rnn_size, state_is_tuple=True, forget_bias=0.0, reuse=False) if dropout > 0.0: cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=1. - dropout) return cell if num_rnn_layers > 1: cell = tf.contrib.rnn.MultiRNNCell( [create_rnn_cell() for _ in range(num_rnn_layers)], state_is_tuple=True) else: cell = create_rnn_cell() initial_rnn_state = cell.zero_state(batch_size, dtype=tf.float32) input_cnn = tf.reshape(input_cnn, [batch_size, max_word_length, -1]) # input_cnn2 = [tf.squeeze(x, [1]) for x in tf.split(input_cnn, max_word_length, 1)] outputs, final_rnn_state = tf.nn.dynamic_rnn( cell, input_cnn, sequence_length=input_len, initial_state=initial_rnn_state, dtype=tf.float32) outputs = tf.layers.batch_normalization(outputs) # outputs = tf.concat([tf.expand_dims(output, 1) for output in outputs], 1) outputs = tf.reshape(outputs, [batch_size * max_word_length, -1]) embed_output = linear(outputs, embed_dimension, scope='out_linear') embed_output = tf.reshape( embed_output, [batch_size, max_word_length, embed_dimension]) return adict(input=input_, input_len_g=input_len, clear_char_embedding_padding=clear_char_embedding_padding, input_embedded=input_embedded, input_cnn=input_cnn, initial_rnn_state_g=initial_rnn_state, final_rnn_state_g=final_rnn_state, rnn_outputs=outputs, embed_output=embed_output)
def params(self, labels, word_vec, char_vec, mxlen, maxw, rnntype, wsz, hsz, filtsz, num_filt=64, kernel_size=3, num_layers=4, num_iterations=3, crf=False): self.num_iterations = num_iterations self.num_layers = num_layers self.kernel_size = kernel_size self.num_filt = num_filt self.crf = crf char_dsz = char_vec.dsz nc = len(labels) self.num_classes = nc self.x = tf.placeholder(tf.int32, [None, mxlen], name="x") self.xch = tf.placeholder(tf.int32, [None, mxlen, maxw], name="xch") self.y = tf.placeholder(tf.int32, [None, mxlen], name="y") self.intermediate_probs = tf.placeholder( tf.int32, [None, mxlen, nc, num_iterations + 2], name="y") self.pkeep = tf.placeholder(tf.float32, name="pkeep") self.word_keep = tf.placeholder(tf.float32, name="word_keep") self.labels = labels self.y_lut = revlut(labels) self.phase = tf.placeholder(tf.bool, name="phase") self.l2_loss = tf.constant(0.0) self.word_vocab = {} if word_vec is not None: self.word_vocab = word_vec.vocab self.char_vocab = char_vec.vocab self.char_dsz = char_dsz self.wsz = wsz self.mxlen = mxlen self.drop_penalty = 0.001 self.A = tf.get_variable("transitions", [self.num_classes, self.num_classes]) # if num_filt != nc: # raise RuntimeError('number of filters needs to be equal to number of classes!') self.filtsz = [int(filt) for filt in filtsz.split(',')] with tf.variable_scope('output/'): W = tf.Variable(tf.truncated_normal([self.num_filt, nc], stddev=0.1), name="W") # W = tf.get_variable('W', initializer=tf.contrib.layers.xavier_initializer(), shape=[num_filt, nc]) b = tf.Variable(tf.constant(0.0, shape=[1, nc]), name="b") intermediates = [] if word_vec is not None: with tf.name_scope("WordLUT"): self.Ww = tf.Variable(tf.constant(word_vec.weights, dtype=tf.float32), name="W") self.we0 = tf.scatter_update( self.Ww, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, word_vec.dsz])) with tf.name_scope("CharLUT"): self.Wc = tf.Variable(tf.constant(char_vec.weights, dtype=tf.float32), name="W") self.ce0 = tf.scatter_update( self.Wc, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, self.char_dsz])) self.input_dropout_keep_prob = self.word_keep self.middle_dropout_keep_prob = 1.00 self.hidden_dropout_keep_prob = self.pkeep self.intermediate_probs, self.probs = self.forward( self.hidden_dropout_keep_prob, self.input_dropout_keep_prob, self.middle_dropout_keep_prob, reuse=False) self.loss = self.createLoss()
def params(self, labels, word_vec, char_vec, mxlen, maxw, rnntype, wsz, hsz, filtsz, crf=False): self.crf = crf char_dsz = char_vec.dsz nc = len(labels) self.x = tf.placeholder(tf.int32, [None, mxlen], name="x") self.xch = tf.placeholder(tf.int32, [None, mxlen, maxw], name="xch") self.y = tf.placeholder(tf.int32, [None, mxlen], name="y") self.pkeep = tf.placeholder(tf.float32, name="pkeep") self.labels = labels self.word_vocab = {} if word_vec is not None: self.word_vocab = word_vec.vocab self.char_vocab = char_vec.vocab filtsz = [int(filt) for filt in filtsz.split(',')] if word_vec is not None: with tf.name_scope("WordLUT"): Ww = tf.Variable(tf.constant(word_vec.weights, dtype=tf.float32), name="W") we0 = tf.scatter_update( Ww, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, word_vec.dsz])) with tf.control_dependencies([we0]): wembed = tf.nn.embedding_lookup(Ww, self.x, name="embeddings") with tf.name_scope("CharLUT"): Wc = tf.Variable(tf.constant(char_vec.weights, dtype=tf.float32), name="W") ce0 = tf.scatter_update(Wc, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, char_dsz])) with tf.control_dependencies([ce0]): xch_seq = tensor2seq(self.xch) cembed_seq = [] for i, xch_i in enumerate(xch_seq): cembed_seq.append( shared_char_word(Wc, xch_i, maxw, filtsz, char_dsz, wsz, None if i == 0 else True)) word_char = seq2tensor(cembed_seq) # List to tensor, reform as (T, B, W) # Join embeddings along the third dimension joint = word_char if word_vec is None else tf.concat_v2( values=[wembed, word_char], axis=2) joint = tf.nn.dropout(joint, self.pkeep) with tf.name_scope("Recurrence"): embedseq = tensor2seq(joint) if rnntype == 'blstm': rnnfwd = tf.contrib.rnn.BasicLSTMCell(hsz) rnnbwd = tf.contrib.rnn.BasicLSTMCell(hsz) # Primitive will wrap the fwd and bwd, reverse signal for bwd, unroll rnnseq, _, __ = tf.contrib.rnn.static_bidirectional_rnn( rnnfwd, rnnbwd, embedseq, dtype=tf.float32) else: rnnfwd = tf.contrib.rnn.BasicLSTMCell(hsz) # Primitive will wrap RNN and unroll in time rnnseq, _ = tf.contrib.rnn.static_rnn(rnnfwd, embedseq, dtype=tf.float32) with tf.name_scope("output"): # Converts seq to tensor, back to (B,T,W) if rnntype == 'blstm': hsz *= 2 W = tf.Variable(tf.truncated_normal([hsz, nc], stddev=0.1), name="W") b = tf.Variable(tf.constant(0.0, shape=[1, nc]), name="b") preds = [tf.matmul(rnnout, W) + b for rnnout in rnnseq] self.probs = seq2tensor(preds) self.best = tf.argmax(self.probs, 2)
def step(index, scores_sum, scores_num): """Single step.""" index %= hparams.epoch_length # Only needed in eval runs. # Note - the only way to ensure making a copy of tensor is to run simple # operation. We are waiting for tf.copy: # https://github.com/tensorflow/tensorflow/issues/11186 obs_copy = batch_env.observ + 0 def env_step(arg1, arg2, arg3): # pylint: disable=unused-argument """Step of the environment.""" actor_critic = get_policy(tf.expand_dims(obs_copy, 0), hparams) policy = actor_critic.policy if policy_to_actions_lambda: action = policy_to_actions_lambda(policy) else: action = tf.cond(eval_phase, policy.mode, policy.sample) postprocessed_action = actor_critic.action_postprocessing(action) reward, done = batch_env.simulate(postprocessed_action[0, ...]) pdf = policy.prob(action)[0] value_function = actor_critic.value[0] pdf = tf.reshape(pdf, shape=(hparams.num_agents,)) value_function = tf.reshape(value_function, shape=(hparams.num_agents,)) done = tf.reshape(done, shape=(hparams.num_agents,)) with tf.control_dependencies([reward, done]): return tf.identity(pdf), tf.identity(value_function), \ tf.identity(done) # TODO(piotrmilos): while_body is executed at most once, # thus should be replaced with tf.cond pdf, value_function, top_level_done = tf.while_loop( lambda _1, _2, _3: tf.equal(speculum.size(), 0), env_step, [ tf.constant(0.0, shape=(hparams.num_agents,)), tf.constant(0.0, shape=(hparams.num_agents,)), tf.constant(False, shape=(hparams.num_agents,)) ], parallel_iterations=1, back_prop=False, ) with tf.control_dependencies([pdf, value_function]): obs, reward, done, action = speculum.dequeue() to_save = [obs, reward, done, action, pdf, value_function] save_ops = [tf.scatter_update(memory_slot, index, value) for memory_slot, value in zip(memory, to_save)] cumulate_rewards_op = cumulative_rewards.assign_add(reward) agent_indices_to_reset = tf.where(top_level_done)[:, 0] with tf.control_dependencies([cumulate_rewards_op]): # TODO(piotrmilos): possibly we need cumulative_rewards.read_value() scores_sum_delta = tf.reduce_sum( tf.gather(cumulative_rewards.read_value(), agent_indices_to_reset)) scores_num_delta = tf.count_nonzero(done, dtype=tf.int32) with tf.control_dependencies(save_ops + [scores_sum_delta, scores_num_delta]): reset_env_op = batch_env.reset(agent_indices_to_reset) reset_cumulative_rewards_op = tf.scatter_update( cumulative_rewards, agent_indices_to_reset, tf.gather(zeros_tensor, agent_indices_to_reset)) with tf.control_dependencies([reset_env_op, reset_cumulative_rewards_op]): return [index + 1, scores_sum + scores_sum_delta, scores_num + scores_num_delta]
def testLoss(self): """ Tests the loss of the FasterRCNN """ # Create prediction_dict's structure prediction_dict_random = { 'rpn_prediction': {}, 'classification_prediction': { 'rcnn': { 'cls_score': None, 'bbox_offsets': None }, 'target': {}, '_debug': { 'losses': {} } } } prediction_dict_perf = { 'rpn_prediction': {}, 'classification_prediction': { 'rcnn': { 'cls_score': None, 'bbox_offsets': None }, 'target': {}, '_debug': { 'losses': {} } } } # Set seeds for stable results rand_seed = 13 target_seed = 43 image_size = (60, 80) num_anchors = 1000 config = EasyDict(self.config) config.model.rpn.l2_regularization_scale = 0.0 config.model.rcnn.l2_regularization_scale = 0.0 config.model.base_network.arg_scope.weight_decay = 0.0 # RPN # Random generation of cls_targets for rpn # where: # {-1}: Ignore # { 0}: Background # { 1}: Object rpn_cls_target = tf.floor( tf.random_uniform([num_anchors], minval=-1, maxval=2, dtype=tf.float32, seed=target_seed, name=None)) # Creation of cls_scores with: # score 100 in correct class # score 0 in wrong class # Generation of opposite cls_score for rpn rpn_cls_score = tf.cast( tf.one_hot(tf.cast(tf.mod(tf.identity(rpn_cls_target) + 1, 2), tf.int32), depth=2, on_value=10), tf.float32) # Generation of correct cls_score for rpn rpn_cls_perf_score = tf.cast( tf.one_hot(tf.cast(tf.identity(rpn_cls_target), tf.int32), depth=2, on_value=100), tf.float32) # Random generation of target bbox deltas rpn_bbox_target = tf.floor( tf.random_uniform([num_anchors, 4], minval=-1, maxval=1, dtype=tf.float32, seed=target_seed, name=None)) # Random generation of predicted bbox deltas rpn_bbox_predictions = tf.floor( tf.random_uniform([num_anchors, 4], minval=-1, maxval=1, dtype=tf.float32, seed=rand_seed, name=None)) prediction_dict_random['rpn_prediction'][ 'rpn_cls_score'] = rpn_cls_score prediction_dict_random['rpn_prediction'][ 'rpn_cls_target'] = rpn_cls_target prediction_dict_random['rpn_prediction'][ 'rpn_bbox_target'] = rpn_bbox_target prediction_dict_random['rpn_prediction'][ 'rpn_bbox_pred'] = rpn_bbox_predictions prediction_dict_perf['rpn_prediction'][ 'rpn_cls_score'] = rpn_cls_perf_score prediction_dict_perf['rpn_prediction'][ 'rpn_cls_target'] = rpn_cls_target prediction_dict_perf['rpn_prediction'][ 'rpn_bbox_target'] = rpn_bbox_target prediction_dict_perf['rpn_prediction'][ 'rpn_bbox_pred'] = rpn_bbox_target # RCNN # Set the number of classes num_classes = config.model.network.num_classes # Randomly generate the bbox_offsets for the correct class = 1 prediction_dict_random['classification_prediction']['target'] = { 'bbox_offsets': tf.random_uniform([1, 4], minval=-1, maxval=1, dtype=tf.float32, seed=target_seed, name=None), 'cls': [1] } # Set the same bbox_offsets and cls for the perfect prediction prediction_dict_perf['classification_prediction'][ 'target'] = prediction_dict_random['classification_prediction'][ 'target'].copy() # Generate random scores for the num_classes + the background class rcnn_cls_score = tf.random_uniform([1, num_classes + 1], minval=-100, maxval=100, dtype=tf.float32, seed=rand_seed, name=None) # Generate a perfect prediction with the correct class score = 100 # and the rest set to 0 rcnn_cls_perf_score = tf.cast( tf.one_hot([1], depth=num_classes + 1, on_value=100), tf.float32) # Generate the random delta prediction for each class rcnn_bbox_offsets = tf.random_uniform([1, num_classes * 4], minval=-1, maxval=1, dtype=tf.float32, seed=rand_seed, name=None) # Copy the random prediction and set the correct class prediction # as the target one target_bbox_offsets = prediction_dict_random[ 'classification_prediction']['target']['bbox_offsets'] initial_val = 1 * 4 # cls value * 4 rcnn_bbox_perf_offsets = tf.Variable( tf.reshape( tf.random_uniform([1, num_classes * 4], minval=-1, maxval=1, dtype=tf.float32, seed=target_seed, name=None), [-1])) rcnn_bbox_perf_offsets = tf.reshape( tf.scatter_update(rcnn_bbox_perf_offsets, tf.range(initial_val, initial_val + 4), tf.reshape(target_bbox_offsets, [-1])), [1, -1]) prediction_dict_random['classification_prediction']['rcnn'][ 'cls_score'] = rcnn_cls_score prediction_dict_random['classification_prediction']['rcnn'][ 'bbox_offsets'] = rcnn_bbox_offsets prediction_dict_perf['classification_prediction']['rcnn'][ 'cls_score'] = rcnn_cls_perf_score prediction_dict_perf['classification_prediction']['rcnn'][ 'bbox_offsets'] = rcnn_bbox_perf_offsets loss_perfect = self._get_losses(config, prediction_dict_perf, image_size) loss_random = self._get_losses(config, prediction_dict_random, image_size) loss_random_compare = { 'rcnn_cls_loss': 5, 'rcnn_reg_loss': 3, 'rpn_cls_loss': 5, 'rpn_reg_loss': 3, 'no_reg_loss': 16, 'regularization_loss': 0, 'total_loss': 22, } for loss in loss_random: self.assertGreaterEqual(loss_random[loss], loss_random_compare[loss], loss) self.assertEqual(loss_perfect[loss], 0, loss)
def _create_network(self, input_batch, local_condition_batch, global_condition_batch): '''Construct the WaveNet network.''' # global_condition_batch: (batch_size, 1, self.global_condition_channels) <--- 가운데 1은 크기 1짜리 data FC대신에 conv1d를 적용하기 위해 강제로 넣었다고 봐야 한다. if self.train_mode == False: self._create_queue() outputs = [] current_layer = input_batch # causal cut으로 길이 1이 줄어든 상태 if self.train_mode == False: self.causal_queue = tf.scatter_update( self.causal_queue, tf.range(self.batch_size), tf.concat([self.causal_queue[:, 1:, :], input_batch], axis=1)) current_layer = self.causal_queue self.local_condition_queue = tf.scatter_update( self.local_condition_queue, tf.range(self.batch_size), tf.concat([ self.local_condition_queue[:, 1:, :], local_condition_batch ], axis=1)) local_condition_batch = self.local_condition_queue # Pre-process the input with a regular convolution current_layer = self._create_causal_layer( current_layer) # conv1d를 통과 하면서, (filter_width-1)= 1 만큼 더 줄어 있다. # 아래의 output_width는 최대 SAMPLE_SIZE = 100,000까지 이고, 짧은 파일이나 파일의 끝부분이면 더 100,000 안 될 수 있다. if self.train_mode == True: output_width = tf.shape( input_batch )[1] - self.receptive_field + 1 # 모든 dilated convolution을 통과 한 이후 길이. +1을 하는 이유는 causal cut으로 줄어든 1만큼 다시 더해준다. else: output_width = 1 # Add all defined dilation layers. with tf.variable_scope('dilated_stack'): for layer_index, dilation in enumerate( self.dilations ): # [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512] with tf.variable_scope('layer{}'.format(layer_index)): if self.train_mode == False: self.dilation_queue[layer_index] = tf.scatter_update( self.dilation_queue[layer_index], tf.range(self.batch_size), tf.concat([ self.dilation_queue[layer_index][:, 1:, :], current_layer ], axis=1)) current_layer = self.dilation_queue[layer_index] output, current_layer = self._create_dilation_layer( current_layer, layer_index, dilation, local_condition_batch, global_condition_batch, output_width) outputs.append(output) with tf.name_scope('postprocessing'): # Perform (+) -> ReLU -> 1x1 conv -> ReLU -> 1x1 conv to # postprocess the output. # We skip connections from the outputs of each layer, adding them # all up here. total = sum( outputs ) # list를 sum하는 것이므로, sum 후, (N,output_width,'skip_channels') transformed1 = tf.nn.relu(total) conv1 = tf.layers.conv1d(transformed1, filters=self.skip_channels, kernel_size=1, padding="same", use_bias=self.use_biases) transformed2 = tf.nn.relu(conv1) if self.scalar_input: conv2 = tf.layers.conv1d(transformed2, filters=self.out_channels, kernel_size=1, padding="same", use_bias=self.use_biases) else: conv2 = tf.layers.conv1d(transformed2, filters=self.quantization_channels, kernel_size=1, padding="same", use_bias=self.use_biases) return conv2
def build_graph(cluster, image_url, return_list): prob_list = return_list num_workers = cluster.num_tasks('worker') # default picture for testing if image_url == None: image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Bow_bow.jpg/800px-Bow_bow.jpg" image_string = urllib.urlopen(image_url).read() #image_string = tf.read_file("/home/philiptkd/Downloads/Dependency_Tree.png") # I lost internet image_size = inception.inception_v1_dist.default_image_size # shared done list, ready list, and image with tf.device("/job:ps/task:0"): done_list = tf.get_variable("done_list", [num_workers+1], tf.int32, tf.zeros_initializer) ready_list = tf.get_variable("ready_list", [num_workers], tf.int32, tf.zeros_initializer) with tf.device("/job:worker/task:0"): # image image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) shared_image = tf.Variable(processed_images, name="shared_image") #download the inception v1 checkpoint if we need to url = "http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz" checkpoints_dir = '/tmp/checkpoints' if not tf.gfile.Exists(checkpoints_dir): tf.gfile.MakeDirs(checkpoints_dir) if not tf.gfile.Exists(checkpoints_dir+'/inception_v1_2016_08_28.tar.gz'): dataset_utils.download_and_uncompress_tarball(url, checkpoints_dir) # end download server = tf.train.Server(cluster, job_name="ps", task_index=0) sess = tf.Session(target=server.target) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v1_dist_arg_scope()): logits, _ = inception.inception_v1_dist(shared_image, num_workers, num_classes=1001, is_training=False, reuse=tf.AUTO_REUSE) probabilities = tf.nn.softmax(logits) # initialization function that uses saved parameters init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v1.ckpt'), slim.get_model_variables('InceptionV1')) sess.run(tf.initialize_variables([done_list, ready_list, shared_image])) # initialize variables that aren't model parameters init_fn(sess) # wait for workers to acknowledge variables have been initialized while sess.run(tf.reduce_sum(ready_list)) < num_workers: pass # do the thing print("before getting probs") run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() np_image, probabilities = sess.run([shared_image, probabilities], options=run_options, run_metadata=run_metadata) print("after getting probs") # see who did what for device in run_metadata.step_stats.dev_stats: print(device.device) for node in device.node_stats: print(" ", node.node_name) # indicate that the ps task is done sess.run(tf.scatter_update(done_list, [0], 1)) # wait until all tasks are done num_done = 1 while num_done < num_workers+1: num_done = sess.run(tf.reduce_sum(done_list)) sess.close() probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])] names = imagenet.create_readable_names_for_imagenet_labels() for i in range(5): index = sorted_inds[i] probability = 'Probability %0.2f%% => [%s]' % (probabilities[index] * 100, names[index]) prob_list.append(probability) print(probability)
def build_update(self): """Draw sample from proposal conditional on last sample. Then accept or reject the sample based on the ratio, .. math:: \\text{ratio} = \log p(x, z^{\\text{new}}) - \log p(x, z^{\\text{old}}) + \log g(z^{\\text{new}} \mid z^{\\text{old}}) - \log g(z^{\\text{old}} \mid z^{\\text{new}}) Notes ----- The updates assume each Empirical random variable is directly parameterized by ``tf.Variable``s. """ old_sample = {z: tf.gather(qz.params, tf.maximum(self.t - 1, 0)) for z, qz in six.iteritems(self.latent_vars)} old_sample = OrderedDict(old_sample) # Form dictionary in order to replace conditioning on prior or # observed variable with conditioning on a specific value. dict_swap = {} for x, qx in six.iteritems(self.data): if isinstance(x, RandomVariable): if isinstance(qx, RandomVariable): qx_copy = copy(qx, scope='conditional') dict_swap[x] = qx_copy.value() else: dict_swap[x] = qx dict_swap_old = dict_swap.copy() dict_swap_old.update(old_sample) scope_old = 'inference_' + str(id(self)) + '/old' scope_new = 'inference_' + str(id(self)) + '/new' # Draw proposed sample and calculate acceptance ratio. new_sample = old_sample.copy() # copy to ensure same order ratio = 0.0 for z, proposal_z in six.iteritems(self.proposal_vars): # Build proposal g(znew | zold). proposal_znew = copy(proposal_z, dict_swap_old, scope=scope_old) # Sample znew ~ g(znew | zold). new_sample[z] = proposal_znew.value() # Increment ratio. ratio += tf.reduce_sum(proposal_znew.log_prob(new_sample[z])) dict_swap_new = dict_swap.copy() dict_swap_new.update(new_sample) for z, proposal_z in six.iteritems(self.proposal_vars): # Build proposal g(zold | znew). proposal_zold = copy(proposal_z, dict_swap_new, scope=scope_new) # Increment ratio. ratio -= tf.reduce_sum(proposal_zold.log_prob(dict_swap_old[z])) for z in six.iterkeys(self.latent_vars): # Build priors p(znew) and p(zold). znew = copy(z, dict_swap_new, scope=scope_new) zold = copy(z, dict_swap_old, scope=scope_old) # Increment ratio. ratio += tf.reduce_sum(znew.log_prob(dict_swap_new[z])) ratio -= tf.reduce_sum(zold.log_prob(dict_swap_old[z])) for x in six.iterkeys(self.data): if isinstance(x, RandomVariable): # Build likelihoods p(x | znew) and p(x | zold). x_znew = copy(x, dict_swap_new, scope=scope_new) x_zold = copy(x, dict_swap_old, scope=scope_old) # Increment ratio. ratio += tf.reduce_sum(x_znew.log_prob(dict_swap[x])) ratio -= tf.reduce_sum(x_zold.log_prob(dict_swap[x])) # Accept or reject sample. u = Uniform().sample() accept = tf.log(u) < ratio sample_values = tf.cond(accept, lambda: list(six.itervalues(new_sample)), lambda: list(six.itervalues(old_sample))) if not isinstance(sample_values, list): # ``tf.cond`` returns tf.Tensor if output is a list of size 1. sample_values = [sample_values] sample = {z: sample_value for z, sample_value in zip(six.iterkeys(new_sample), sample_values)} # Update Empirical random variables. assign_ops = [] for z, qz in six.iteritems(self.latent_vars): variable = qz.get_variables()[0] assign_ops.append(tf.scatter_update(variable, self.t, sample[z])) # Increment n_accept (if accepted). assign_ops.append(self.n_accept.assign_add(tf.where(accept, 1, 0))) return tf.group(*assign_ops)
def clip_norm_ref(embedding, idxs): with tf.name_scope('clip_norm_ref') as scope: to_update = tf.nn.embedding_lookup(embedding, idxs) to_update = tf.clip_by_norm(to_update, self.norm_cap, axes=2) return tf.scatter_update(embedding, idxs, to_update)
import tensorflow as tf import tensorflow.contrib.opt as opt X = tf.Variable([1.0, 2.0]) X0 = tf.Variable([3.0]) Y = tf.constant([2.0, -3.0]) scatter = tf.scatter_update(X, [0], X0) with tf.control_dependencies([scatter]): loss = tf.reduce_sum(tf.squared_difference(X, Y)) opt = opt.ScipyOptimizerInterface(loss, [X0]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) opt.minimize(sess) print("X: {}".format(X.eval())) print("X0: {}".format(X0.eval()))
def reset(self, indices): initial_frames = tf.gather(self.get_initial_observations(), indices) scatter_op = tf.scatter_update(self._history_buff, indices, initial_frames) with tf.control_dependencies([scatter_op]): return self._history_buff.read_value()
def build_update(self): """ Simulate Hamiltonian dynamics using a numerical integrator. Correct for the integrator's discretization error using an acceptance ratio. """ old_sample = { z: tf.gather(qz.params, tf.maximum(self.t - 1, 0)) for z, qz in six.iteritems(self.latent_vars) } # Sample momentum. old_r_sample = {} for z, qz in six.iteritems(self.latent_vars): event_shape = qz.get_event_shape() normal = Normal(mu=tf.zeros(event_shape), sigma=tf.ones(event_shape)) old_r_sample[z] = normal.sample() # Simulate Hamiltonian dynamics. new_sample = old_sample new_r_sample = old_r_sample for _ in range(self.n_steps): new_sample, new_r_sample = leapfrog(old_sample, old_r_sample, self.step_size, self.log_joint) # Calculate acceptance ratio. ratio = tf.reduce_sum( [0.5 * tf.square(r) for r in six.itervalues(old_r_sample)]) ratio -= tf.reduce_sum( [0.5 * tf.square(r) for r in six.itervalues(new_r_sample)]) ratio += self.log_joint(new_sample) ratio -= self.log_joint(old_sample) # Accept or reject sample. u = Uniform().sample() accept = tf.log(u) < ratio sample_values = tf.cond(accept, lambda: list(six.itervalues(new_sample)), lambda: list(six.itervalues(old_sample))) if not isinstance(sample_values, list): # ``tf.cond`` returns tf.Tensor if output is a list of size 1. sample_values = [sample_values] sample = { z: sample_value for z, sample_value in zip(six.iterkeys(new_sample), sample_values) } # Update Empirical random variables. assign_ops = [] variables = { x.name: x for x in tf.get_default_graph().get_collection( tf.GraphKeys.VARIABLES) } for z, qz in six.iteritems(self.latent_vars): variable = variables[qz.params.op.inputs[0].op.inputs[0].name] assign_ops.append(tf.scatter_update(variable, self.t, sample[z])) # Increment n_accept (if accepted). assign_ops.append(self.n_accept.assign_add(tf.select(accept, 1, 0))) return tf.group(*assign_ops)
def test_scatter_update(): a = tf.Variable(initial_value=[2, 5, -4, 0]) b = tf.scatter_update(a, [2, 2], [9, 100]) return b