Python tensorflow.assign() Examples
The following are 30
code examples of tensorflow.assign().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: mpi_util.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def sync_from_root(sess, variables, comm=None): """ Send the root node's parameters to every worker. Arguments: sess: the TensorFlow session. variables: all parameter variables including optimizer's """ if comm is None: comm = MPI.COMM_WORLD rank = comm.Get_rank() for var in variables: if rank == 0: comm.Bcast(sess.run(var)) else: import tensorflow as tf returned_var = np.empty(var.shape, dtype='float32') comm.Bcast(returned_var) sess.run(tf.assign(var, returned_var))
Example #2
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def finalize_autosummaries(): global _autosummary_finalized if _autosummary_finalized: return _autosummary_finalized = True init_uninited_vars([var for vars in _autosummary_vars.values() for var in vars]) with tf.device(None), tf.control_dependencies(None): for name, vars in _autosummary_vars.items(): id = name.replace('/', '_') with absolute_name_scope('Autosummary/' + id): sum = tf.add_n(vars) avg = sum[0] / sum[1] with tf.control_dependencies([avg]): # read before resetting reset_ops = [tf.assign(var, tf.zeros(2)) for var in vars] with tf.name_scope(None), tf.control_dependencies(reset_ops): # reset before reporting tf.summary.scalar(name, avg) # Internal helper for creating autosummary accumulators.
Example #3
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def _create_autosummary_var(name, value_expr): assert not _autosummary_finalized v = tf.cast(value_expr, tf.float32) if v.shape.ndims is 0: v = [v, np.float32(1.0)] elif v.shape.ndims is 1: v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)] else: v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))] v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2)) with tf.control_dependencies(None): var = tf.Variable(tf.zeros(2)) # [numerator, denominator] update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v)) if name in _autosummary_vars: _autosummary_vars[name].append(var) else: _autosummary_vars[name] = [var] return update_op #---------------------------------------------------------------------------- # Call filewriter.add_summary() with all summaries in the default graph, # automatically finalizing and merging them on the first call.
Example #4
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def init_uninited_vars(vars=None): if vars is None: vars = tf.global_variables() test_vars = []; test_ops = [] with tf.control_dependencies(None): # ignore surrounding control_dependencies for var in vars: assert is_tf_expression(var) try: tf.get_default_graph().get_tensor_by_name(var.name.replace(':0', '/IsVariableInitialized:0')) except KeyError: # Op does not exist => variable may be uninitialized. test_vars.append(var) with absolute_name_scope(var.name.split(':')[0]): test_ops.append(tf.is_variable_initialized(var)) init_vars = [var for var, inited in zip(test_vars, run(test_ops)) if not inited] run([var.initializer for var in init_vars]) #---------------------------------------------------------------------------- # Set the values of given tf.Variables. # Equivalent to the following, but more efficient and does not bloat the tf graph: # tfutil.run([tf.assign(var, value) for var, value in var_to_value_dict.items()]
Example #5
Source File: collect.py From fine-lm with MIT License | 6 votes |
def simulate(self, action): # There is subtlety here. We need to collect data # obs, action = policy(obs), done, reward = env(abs, action) # Thus we need to enqueue data before assigning new observation reward, done = self._batch_env.simulate(action) with tf.control_dependencies([reward, done]): enqueue_op = self.speculum.enqueue( [self._observ.read_value(), reward, done, action]) with tf.control_dependencies([enqueue_op]): assign = self._observ.assign(self._batch_env.observ) with tf.control_dependencies([assign]): return tf.identity(reward), tf.identity(done)
Example #6
Source File: ddpg.py From lirpg with MIT License | 6 votes |
def setup_popart(self): # See https://arxiv.org/pdf/1602.07714.pdf for details. self.old_std = tf.placeholder(tf.float32, shape=[1], name='old_std') new_std = self.ret_rms.std self.old_mean = tf.placeholder(tf.float32, shape=[1], name='old_mean') new_mean = self.ret_rms.mean self.renormalize_Q_outputs_op = [] for vs in [self.critic.output_vars, self.target_critic.output_vars]: assert len(vs) == 2 M, b = vs assert 'kernel' in M.name assert 'bias' in b.name assert M.get_shape()[-1] == 1 assert b.get_shape()[-1] == 1 self.renormalize_Q_outputs_op += [M.assign(M * self.old_std / new_std)] self.renormalize_Q_outputs_op += [b.assign((b * self.old_std + self.old_mean - new_mean) / new_std)]
Example #7
Source File: mpi_util.py From HardRLWithYoutube with MIT License | 6 votes |
def get_local_rank_size(comm): """ Returns the rank of each process on its machine The processes on a given machine will be assigned ranks 0, 1, 2, ..., N-1, where N is the number of processes on this machine. Useful if you want to assign one gpu per machine """ this_node = platform.node() ranks_nodes = comm.allgather((comm.Get_rank(), this_node)) node2rankssofar = defaultdict(int) local_rank = None for (rank, node) in ranks_nodes: if rank == comm.Get_rank(): local_rank = node2rankssofar[node] node2rankssofar[node] += 1 assert local_rank is not None return local_rank, node2rankssofar[this_node]
Example #8
Source File: ddpg.py From HardRLWithYoutube with MIT License | 6 votes |
def setup_popart(self): # See https://arxiv.org/pdf/1602.07714.pdf for details. self.old_std = tf.placeholder(tf.float32, shape=[1], name='old_std') new_std = self.ret_rms.std self.old_mean = tf.placeholder(tf.float32, shape=[1], name='old_mean') new_mean = self.ret_rms.mean self.renormalize_Q_outputs_op = [] for vs in [self.critic.output_vars, self.target_critic.output_vars]: assert len(vs) == 2 M, b = vs assert 'kernel' in M.name assert 'bias' in b.name assert M.get_shape()[-1] == 1 assert b.get_shape()[-1] == 1 self.renormalize_Q_outputs_op += [M.assign(M * self.old_std / new_std)] self.renormalize_Q_outputs_op += [b.assign((b * self.old_std + self.old_mean - new_mean) / new_std)]
Example #9
Source File: mobilenet_v1_train.py From DeepLab_v3 with MIT License | 6 votes |
def get_checkpoint_init_fn(): """Returns the checkpoint init_fn if the checkpoint is provided.""" if FLAGS.fine_tune_checkpoint: variables_to_restore = slim.get_variables_to_restore() global_step_reset = tf.assign(tf.train.get_or_create_global_step(), 0) # When restoring from a floating point model, the min/max values for # quantized weights and activations are not present. # We instruct slim to ignore variables that are missing during restoration # by setting ignore_missing_vars=True slim_init_fn = slim.assign_from_checkpoint_fn( FLAGS.fine_tune_checkpoint, variables_to_restore, ignore_missing_vars=True) def init_fn(sess): slim_init_fn(sess) # If we are restoring from a floating point model, we need to initialize # the global step to zero for the exponential decay to result in # reasonable learning rates. sess.run(global_step_reset) return init_fn else: return None
Example #10
Source File: GCN.py From nettack with MIT License | 6 votes |
def set_variables(self, var_dict): """ Set the model's variables to those provided in var_dict. This is e.g. used to restore the best seen parameters after training with patience. Parameters ---------- var_dict: dict Dictionary of the form {var_name: var_value} to assign the variables in the model. Returns ------- None. """ with self.graph.as_default(): if not hasattr(self, 'assign_placeholders'): self.assign_placeholders = {v.name: tf.placeholder(v.dtype, shape=v.get_shape()) for v in self.varlist} self.assign_ops = {v.name: tf.assign(v, self.assign_placeholders[v.name]) for v in self.varlist} to_namespace = list(var_dict.keys())[0].split("/")[0] self.session.run(list(self.assign_ops.values()), feed_dict = {val: var_dict[self.convert_varname(key, to_namespace)] for key, val in self.assign_placeholders.items()})
Example #11
Source File: convert_weights.py From Tensorflow-YOLOv3 with MIT License | 6 votes |
def load_batch_norm(idx, variables, weights, assign_ops, offset): """Loads kernel, gamma, beta, mean, variance for Batch Normalization""" kernel = variables[idx] gamma, beta, mean, variance = variables[idx + 1:idx + 5] batch_norm_vars = [beta, gamma, mean, variance] for var in batch_norm_vars: shape = var.shape.as_list() num_params = np.prod(shape) var_weights = weights[offset:offset + num_params].reshape(shape) offset += num_params assign_ops.append(tf.assign(var, var_weights)) shape = kernel.shape.as_list() num_params = np.prod(shape) var_weights = weights[offset:offset + num_params].reshape((shape[3], shape[2], shape[0], shape[1])) var_weights = np.transpose(var_weights, (2, 3, 1, 0)) offset += num_params assign_ops.append(tf.assign(kernel, var_weights)) return assign_ops, offset
Example #12
Source File: policies.py From evolution-strategies-starter with MIT License | 6 votes |
def __init__(self, *args, **kwargs): self.args, self.kwargs = args, kwargs self.scope = self._initialize(*args, **kwargs) self.all_variables = tf.get_collection(tf.GraphKeys.VARIABLES, self.scope.name) self.trainable_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.scope.name) self.num_params = sum(int(np.prod(v.get_shape().as_list())) for v in self.trainable_variables) self._setfromflat = U.SetFromFlat(self.trainable_variables) self._getflat = U.GetFlat(self.trainable_variables) logger.info('Trainable variables ({} parameters)'.format(self.num_params)) for v in self.trainable_variables: shp = v.get_shape().as_list() logger.info('- {} shape:{} size:{}'.format(v.name, shp, np.prod(shp))) logger.info('All variables') for v in self.all_variables: shp = v.get_shape().as_list() logger.info('- {} shape:{} size:{}'.format(v.name, shp, np.prod(shp))) placeholders = [tf.placeholder(v.value().dtype, v.get_shape().as_list()) for v in self.all_variables] self.set_all_vars = U.function( inputs=placeholders, outputs=[], updates=[tf.group(*[v.assign(p) for v, p in zip(self.all_variables, placeholders)])] )
Example #13
Source File: mpi_util.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def get_local_rank_size(comm): """ Returns the rank of each process on its machine The processes on a given machine will be assigned ranks 0, 1, 2, ..., N-1, where N is the number of processes on this machine. Useful if you want to assign one gpu per machine """ this_node = platform.node() ranks_nodes = comm.allgather((comm.Get_rank(), this_node)) node2rankssofar = defaultdict(int) local_rank = None for (rank, node) in ranks_nodes: if rank == comm.Get_rank(): local_rank = node2rankssofar[node] node2rankssofar[node] += 1 assert local_rank is not None return local_rank, node2rankssofar[this_node]
Example #14
Source File: ddpg_learner.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def setup_popart(self): # See https://arxiv.org/pdf/1602.07714.pdf for details. self.old_std = tf.placeholder(tf.float32, shape=[1], name='old_std') new_std = self.ret_rms.std self.old_mean = tf.placeholder(tf.float32, shape=[1], name='old_mean') new_mean = self.ret_rms.mean self.renormalize_Q_outputs_op = [] for vs in [self.critic.output_vars, self.target_critic.output_vars]: assert len(vs) == 2 M, b = vs assert 'kernel' in M.name assert 'bias' in b.name assert M.get_shape()[-1] == 1 assert b.get_shape()[-1] == 1 self.renormalize_Q_outputs_op += [M.assign(M * self.old_std / new_std)] self.renormalize_Q_outputs_op += [b.assign((b * self.old_std + self.old_mean - new_mean) / new_std)]
Example #15
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def set_vars(var_to_value_dict): ops = [] feed_dict = {} for var, value in var_to_value_dict.items(): assert is_tf_expression(var) try: setter = tf.get_default_graph().get_tensor_by_name(var.name.replace(':0', '/setter:0')) # look for existing op except KeyError: with absolute_name_scope(var.name.split(':')[0]): with tf.control_dependencies(None): # ignore surrounding control_dependencies setter = tf.assign(var, tf.placeholder(var.dtype, var.shape, 'new_value'), name='setter') # create new setter ops.append(setter) feed_dict[setter.op.inputs[1]] = value run(ops, feed_dict) #---------------------------------------------------------------------------- # Autosummary creates an identity op that internally keeps track of the input # values and automatically shows up in TensorBoard. The reported value # represents an average over input components. The average is accumulated # constantly over time and flushed when save_summaries() is called. # # Notes: # - The output tensor must be used as an input for something else in the # graph. Otherwise, the autosummary op will not get executed, and the average # value will not get accumulated. # - It is perfectly fine to include autosummaries with the same name in # several places throughout the graph, even if they are executed concurrently. # - It is ok to also pass in a python scalar or numpy array. In this case, it # is added to the average immediately.
Example #16
Source File: ddpg_learner.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 5 votes |
def get_perturbed_actor_updates(actor, perturbed_actor, param_noise_stddev): assert len(actor.vars) == len(perturbed_actor.vars) assert len(actor.perturbable_vars) == len(perturbed_actor.perturbable_vars) updates = [] for var, perturbed_var in zip(actor.vars, perturbed_actor.vars): if var in actor.perturbable_vars: logger.info(' {} <- {} + noise'.format(perturbed_var.name, var.name)) updates.append(tf.assign(perturbed_var, var + tf.random_normal(tf.shape(var), mean=0., stddev=param_noise_stddev))) else: logger.info(' {} <- {}'.format(perturbed_var.name, var.name)) updates.append(tf.assign(perturbed_var, var)) assert len(updates) == len(actor.vars) return tf.group(*updates)
Example #17
Source File: help.py From Traffic_sign_detection_YOLO with MIT License | 5 votes |
def load_old_graph(self, ckpt): ckpt_loader = create_loader(ckpt) self.say(old_graph_msg.format(ckpt)) for var in tf.global_variables(): name = var.name.split(':')[0] args = [name, var.get_shape()] val = ckpt_loader(args) assert val is not None, \ 'Cannot find and load {}'.format(var.name) shp = val.shape plh = tf.placeholder(tf.float32, shp) op = tf.assign(var, plh) self.sess.run(op, {plh: val})
Example #18
Source File: gan.py From adagan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, opts, data, weights): # Losses of the copied discriminator network self._d_loss_cp = None self._d_optim_cp = None # Rolling back ops (assign variable values fo true # to copied discriminator network) self._roll_back = None ImageGan.__init__(self, opts, data, weights)
Example #19
Source File: gan.py From adagan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, opts, data, weights): # Losses of the copied discriminator network self._d_loss_cp = None self._d_optim_cp = None # Rolling back ops (assign variable values fo true # to copied discriminator network) self._roll_back = None Gan.__init__(self, opts, data, weights) # Architecture used in unrolled gan paper
Example #20
Source File: ddpg_learner.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 5 votes |
def get_target_updates(vars, target_vars, tau): logger.info('setting up target updates ...') soft_updates = [] init_updates = [] assert len(vars) == len(target_vars) for var, target_var in zip(vars, target_vars): logger.info(' {} <- {}'.format(target_var.name, var.name)) init_updates.append(tf.assign(target_var, var)) soft_updates.append(tf.assign(target_var, (1. - tau) * target_var + tau * var)) assert len(init_updates) == len(vars) assert len(soft_updates) == len(vars) return tf.group(*init_updates), tf.group(*soft_updates)
Example #21
Source File: ner_model_bilstm.py From deepnlp with MIT License | 5 votes |
def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size self.num_steps = num_steps = config.num_steps self.is_training = is_training size = config.hidden_size vocab_size = config.vocab_size # Define input and target tensors self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size], dtype=data_type()) inputs = tf.nn.embedding_lookup(embedding, self._input_data) if (config.bi_direction): # BiLSTM self._cost, self._logits = _bilstm_model(inputs, self._targets, config) else: # LSTM self._cost, self._logits, self._final_state, self._initial_state = _lstm_model(inputs, self._targets, config) # Gradients and SGD update operation for training the model. self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) self._train_op = optimizer.apply_gradients( zip(grads, tvars), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder(data_type(), shape=[], name="new_learning_rate") self._lr_update = tf.assign(self._lr, self._new_lr) self.saver = tf.train.Saver(tf.global_variables())
Example #22
Source File: ner_model_bilstm_crf.py From deepnlp with MIT License | 5 votes |
def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size self.num_steps = num_steps = config.num_steps self.is_training = is_training self.crf_layer = config.crf_layer # if the model has the final CRF decoding layer size = config.hidden_size vocab_size = config.vocab_size # Define input and target tensors self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size], dtype=data_type()) inputs = tf.nn.embedding_lookup(embedding, self._input_data) # BiLSTM CRF model self._cost, self._logits, self._transition_params = _bilstm_crf_model(inputs, self._targets, config) # Gradients and SGD update operation for training the model. self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) self._train_op = optimizer.apply_gradients( zip(grads, tvars), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder(data_type(), shape=[], name="new_learning_rate") self._lr_update = tf.assign(self._lr, self._new_lr) self.saver = tf.train.Saver(tf.global_variables())
Example #23
Source File: pos_model_bilstm_crf.py From deepnlp with MIT License | 5 votes |
def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size self.num_steps = num_steps = config.num_steps self.is_training = is_training self.crf_layer = config.crf_layer # if the model has the final CRF decoding layer size = config.hidden_size vocab_size = config.vocab_size # Define input and target tensors self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size], dtype=data_type()) inputs = tf.nn.embedding_lookup(embedding, self._input_data) # BiLSTM CRF model self._cost, self._logits, self._transition_params = _bilstm_crf_model(inputs, self._targets, config) # Gradients and SGD update operation for training the model. self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) # vanila SGD self._train_op = optimizer.apply_gradients( zip(grads, tvars), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder(data_type(), shape=[], name="new_learning_rate") self._lr_update = tf.assign(self._lr, self._new_lr) self.saver = tf.train.Saver(tf.global_variables())
Example #24
Source File: pos_model_bilstm.py From deepnlp with MIT License | 5 votes |
def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size self.num_steps = num_steps = config.num_steps self.is_training = is_training size = config.hidden_size vocab_size = config.vocab_size # Define input and target tensors self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size], dtype=data_type()) inputs = tf.nn.embedding_lookup(embedding, self._input_data) if (config.bi_direction): # BiLSTM self._cost, self._logits = _bilstm_model(inputs, self._targets, config) else: # LSTM self._cost, self._logits, self._final_state, self._initial_state = _lstm_model(inputs, self._targets, config) # Gradients and SGD update operation for training the model. self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars), config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) # vanila SGD self._train_op = optimizer.apply_gradients( zip(grads, tvars), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder(data_type(), shape=[], name="new_learning_rate") self._lr_update = tf.assign(self._lr, self._new_lr) self.saver = tf.train.Saver(tf.global_variables())
Example #25
Source File: ops_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def testReuseVars(self): height, width = 3, 3 with self.test_session() as sess: image_shape = (10, height, width, 3) image_values = np.random.rand(*image_shape) expected_mean = np.mean(image_values, axis=(0, 1, 2)) expected_var = np.var(image_values, axis=(0, 1, 2)) images = tf.constant(image_values, shape=image_shape, dtype=tf.float32) output = ops.batch_norm(images, decay=0.1, is_training=False) update_ops = tf.get_collection(ops.UPDATE_OPS_COLLECTION) with tf.control_dependencies(update_ops): output = tf.identity(output) # Initialize all variables sess.run(tf.global_variables_initializer()) moving_mean = variables.get_variables('BatchNorm/moving_mean')[0] moving_variance = variables.get_variables('BatchNorm/moving_variance')[0] mean, variance = sess.run([moving_mean, moving_variance]) # After initialization moving_mean == 0 and moving_variance == 1. self.assertAllClose(mean, [0] * 3) self.assertAllClose(variance, [1] * 3) # Simulate assigment from saver restore. init_assigns = [tf.assign(moving_mean, expected_mean), tf.assign(moving_variance, expected_var)] sess.run(init_assigns) for _ in range(10): sess.run([output], {images: np.random.rand(*image_shape)}) mean = moving_mean.eval() variance = moving_variance.eval() # Although we feed different images, the moving_mean and moving_variance # shouldn't change. self.assertAllClose(mean, expected_mean) self.assertAllClose(variance, expected_var)
Example #26
Source File: policies.py From evolution-strategies-starter with MIT License | 5 votes |
def _initialize(self, ob_space, ac_space, ac_bins, ac_noise_std, nonlin_type, hidden_dims, connection_type): self.ac_space = ac_space self.ac_bins = ac_bins self.ac_noise_std = ac_noise_std self.hidden_dims = hidden_dims self.connection_type = connection_type assert len(ob_space.shape) == len(self.ac_space.shape) == 1 assert np.all(np.isfinite(self.ac_space.low)) and np.all(np.isfinite(self.ac_space.high)), \ 'Action bounds required' self.nonlin = {'tanh': tf.tanh, 'relu': tf.nn.relu, 'lrelu': U.lrelu, 'elu': tf.nn.elu}[nonlin_type] with tf.variable_scope(type(self).__name__) as scope: # Observation normalization ob_mean = tf.get_variable( 'ob_mean', ob_space.shape, tf.float32, tf.constant_initializer(np.nan), trainable=False) ob_std = tf.get_variable( 'ob_std', ob_space.shape, tf.float32, tf.constant_initializer(np.nan), trainable=False) in_mean = tf.placeholder(tf.float32, ob_space.shape) in_std = tf.placeholder(tf.float32, ob_space.shape) self._set_ob_mean_std = U.function([in_mean, in_std], [], updates=[ tf.assign(ob_mean, in_mean), tf.assign(ob_std, in_std), ]) # Policy network o = tf.placeholder(tf.float32, [None] + list(ob_space.shape)) a = self._make_net(tf.clip_by_value((o - ob_mean) / ob_std, -5.0, 5.0)) self._act = U.function([o], a) return scope
Example #27
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def setup_as_moving_average_of(self, src_net, beta=0.99, beta_nontrainable=0.0): assert isinstance(src_net, Network) with absolute_name_scope(self.scope): with tf.name_scope('MovingAvg'): ops = [] for name, var in self.vars.items(): if name in src_net.vars: cur_beta = beta if name in self.trainables else beta_nontrainable new_value = lerp(src_net.vars[name], var, cur_beta) ops.append(var.assign(new_value)) return tf.group(*ops) # Run this network for the given NumPy array(s), and return the output(s) as NumPy array(s).
Example #28
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def create_sync_ops(self, host_device): """Create an assignment operation for each weight on all devices. The weight is assigned the value of the copy on the `host_device'. """ sync_ops = [] host_params = self.params_device[host_device] for device, params in (self.params_device).iteritems(): if device == host_device: continue for k in self.params_names: if isinstance(params[k], tf.Variable): sync_ops += [tf.assign(params[k], host_params[k])] return sync_ops
Example #29
Source File: test_serial.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_save_and_load_var(): """ Tests that we can save and load a PicklableVariable with joblib """ sess = tf.Session() with sess.as_default(): x = np.ones(1) xv = PicklableVariable(x) xv.var.initializer.run() save("/tmp/var.joblib", xv) sess.run(tf.assign(xv.var, np.ones(1) * 2)) new_xv = load("/tmp/var.joblib") assert np.allclose(sess.run(xv.var), np.ones(1) * 2) assert np.allclose(sess.run(new_xv.var), np.ones(1))
Example #30
Source File: tensorFactorisation.py From decompose with MIT License | 5 votes |
def __set__(self, obj, values): if self.fset is None: raise AttributeError("can't set attribute") newValues = [] for f, value in enumerate(values): if obj.transform and (f == 0): name = f"{f}tr" else: name = f"{f}" with tf.variable_scope("U", reuse=tf.AUTO_REUSE): var = tf.get_variable(name, dtype=obj.dtype) value = tf.assign(var, value) newValues.append(value) value = tuple(newValues) self.fset(obj, value)