我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.get_collection()。
def _get_summary(self): # build the self.summ_op for tensorboard # This function could be overwritten if not self.flags.visualize or self.flags.visualize=='none': return summ_collection = "{} {} {} summaries".format(self.flags.comp, self.flags.sol, self.flags.run_name) if len(tf.get_collection(tf.GraphKeys.SCALARS)): self.scaler_op = tf.summary.merge(tf.get_collection(tf.GraphKeys.SCALARS)) if len(tf.get_collection(tf.GraphKeys.IMAGES)): self.image_op = tf.summary.merge(tf.get_collection(tf.GraphKeys.IMAGES)) for i in tf.get_collection(tf.GraphKeys.SCALARS): tf.add_to_collection(summ_collection, i) for i in tf.get_collection(tf.GraphKeys.WEIGHTS): tf.add_to_collection(summ_collection, i) for i in tf.get_collection(tf.GraphKeys.FEATURE_MAPS): tf.add_to_collection(summ_collection, i) for i in tf.get_collection(tf.GraphKeys.IMAGES): tf.add_to_collection(summ_collection, i) for i in tf.get_collection(tf.GraphKeys.GRADIENTS): tf.add_to_collection(summ_collection, i) for i in tf.get_collection(tf.GraphKeys.EMBEDDINGS): tf.add_to_collection(summ_collection, i) self.summ_op = tf.summary.merge(tf.get_collection(summ_collection))
def _add_loss_summaries(total_loss): """Add summaries for losses. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summmary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.summary.scalar(l.op.name +' (raw)', l) tf.summary.scalar(l.op.name, loss_averages.average(l)) return loss_averages_op
def __init__(self, ob_space, ac_space, layers=[256], **kwargs): self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) rank = len(ob_space) if rank == 3: # pixel input for i in range(4): x = tf.nn.elu(conv2d(x, 32, "c{}".format(i + 1), [3, 3], [2, 2])) elif rank == 1: # plain features #x = tf.nn.elu(linear(x, 256, "l1", normalized_columns_initializer(0.01))) pass else: raise TypeError("observation space must have rank 1 or 3, got %d" % rank) x = flatten(x) for i, layer in enumerate(layers): x = tf.nn.elu(linear(x, layer, "l{}".format(i + 1), tf.contrib.layers.xavier_initializer())) self.logits = linear(x, ac_space, "action", tf.contrib.layers.xavier_initializer()) self.vf = tf.reshape(linear(x, 1, "value", tf.contrib.layers.xavier_initializer()), [-1]) self.sample = categorical_sample(self.logits, ac_space)[0, :] self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name) self.state_in = []
def __init__(self, ob_space, ac_space, size=256, **kwargs): self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space)) for i in range(4): x = tf.nn.elu(conv2d(x, 32, "l{}".format(i + 1), [3, 3], [2, 2])) # introduce a "fake" batch dimension of 1 after flatten so that we can do GRU over time dim x = tf.expand_dims(flatten(x), 1) gru = rnn.GRUCell(size) h_init = np.zeros((1, size), np.float32) self.state_init = [h_init] h_in = tf.placeholder(tf.float32, [1, size]) self.state_in = [h_in] gru_outputs, gru_state = tf.nn.dynamic_rnn( gru, x, initial_state=h_in, sequence_length=[size], time_major=True) x = tf.reshape(gru_outputs, [-1, size]) self.logits = linear(x, ac_space, "action", normalized_columns_initializer(0.01)) self.vf = tf.reshape(linear(x, 1, "value", normalized_columns_initializer(1.0)), [-1]) self.state_out = [gru_state[:1]] self.sample = categorical_sample(self.logits, ac_space)[0, :] self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def scope_vars(scope, trainable_only=False): """ Get variables inside a scope The scope can be specified as a string Parameters ---------- scope: str or VariableScope scope in which the variables reside. trainable_only: bool whether or not to return only the variables that were marked as trainable. Returns ------- vars: [tf.Variable] list of variables in `scope`. """ return tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES if trainable_only else tf.GraphKeys.GLOBAL_VARIABLES, scope=scope if isinstance(scope, str) else scope.name )
def loss(logits, labels): """Add L2Loss to all the trainable variables. Add summary for for "Loss" and "Loss/avg". Args: logits: Logits from inference(). labels: Labels from distorted_inputs or inputs(). 1-D tensor of shape [batch_size] Returns: Loss tensor of type float. """ # Calculate the average cross entropy loss across the batch. labels = tf.cast(labels, tf.int64) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') tf.add_to_collection('losses', cross_entropy_mean) # The total loss is defined as the cross entropy loss plus all of the weight # decay terms (L2 loss). return tf.add_n(tf.get_collection('losses'), name='total_loss')
def _add_loss_summaries(total_loss): """Add summaries for losses in CIFAR-10 model. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.summary.scalar(l.op.name +' (raw)', l) tf.summary.scalar(l.op.name, loss_averages.average(l)) return loss_averages_op
def loss(c_fuse, s_fuse, labels): """Add L2Loss to all the trainable variables. Add summary for "Loss" and "Loss/avg". Args: c_fuse: Contours output map from inference(). s_fuse: Segments output map from inference(). labels: Labels from distorted_inputs or inputs(). Returns: Loss tensor of type float. """ # Calculate the average cross entropy loss across the batch. # Split the labels tensor into contours and segments image tensors # Each has shape [FLAGS.batch_size, 696, 520, 1] contours_labels, segments_labels = tf.split(labels, 2, 3) _add_cross_entropy(contours_labels, c_fuse, 'c') _add_cross_entropy(segments_labels, s_fuse, 's') return tf.add_n(tf.get_collection('losses'), name='total_loss')
def _add_loss_summaries(total_loss): """Add summaries for losses in BBBC006 model. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.summary.scalar(l.op.name + '_raw', l) tf.summary.scalar(l.op.name, loss_averages.average(l)) return loss_averages_op
def add_variable(var, restore=True): """Adds a variable to the MODEL_VARIABLES collection. Optionally it will add the variable to the VARIABLES_TO_RESTORE collection. Args: var: a variable. restore: whether the variable should be added to the VARIABLES_TO_RESTORE collection. """ collections = [MODEL_VARIABLES] if restore: collections.append(VARIABLES_TO_RESTORE) for collection in collections: if var not in tf.get_collection(collection): tf.add_to_collection(collection, var)
def get_unique_variable(name): """Gets the variable uniquely identified by that name. Args: name: a name that uniquely identifies the variable. Returns: a tensorflow variable. Raises: ValueError: if no variable uniquely identified by the name exists. """ candidates = tf.get_collection(tf.GraphKeys.VARIABLES, name) if not candidates: raise ValueError('Couldnt find variable %s' % name) for candidate in candidates: if candidate.op.name == name: return candidate raise ValueError('Variable %s does not uniquely identify a variable', name)
def compute_gradients(self, loss, *args, **kwargs): train_vars = None if self.trainable_names is not None: log.info('All trainable vars:\n'+str([var.name for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)])) train_vars = [] for scope_name in self.trainable_names: new_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope_name) if len(new_vars) == 0: raise ValueError('The scope name, {}, you specified does not contain any trainable variables.'.format(scope_name)) train_vars.extend(new_vars) log.info('Variables to be trained:\n'+str([var.name for var in train_vars])) if train_vars is not None: self.var_list = train_vars gvs = self._optimizer.compute_gradients(loss, var_list=train_vars, *args, **kwargs) if self.clip: # gradient clipping. Some gradients returned are 'None' because # no relation between the variable and loss; so we skip those. gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs if grad is not None] return gvs
def __init__(self, session, model_scope, result_dir, result_file, k=1): """ Args: model_scope: The variable_scope used for the trained model to be restored. session: The TensorFlow session used to run the prediction. result_dir: The full path to the folder in which the result file locates. result_file: The file that saves the training results. k: Optional. Number of elements to be predicted. """ tf.train.import_meta_graph(os.path.join(result_dir, result_file + ".meta")) all_vars = tf.global_variables() model_vars = [var for var in all_vars if var.name.startswith(model_scope)] saver = tf.train.Saver(model_vars) saver.restore(session, os.path.join(result_dir, result_file)) # Retrieve the Ops we 'remembered'. logits = tf.get_collection(model_scope+"logits")[0] self.images_placeholder = tf.get_collection(model_scope+"images")[0] self.keep_prob_placeholder = tf.get_collection(model_scope+"keep_prob")[0] # Add an Op that chooses the top k predictions. Apply softmax so that # we can have the probabilities (percentage) in the output. self.eval_op = tf.nn.top_k(tf.nn.softmax(logits), k=k) self.session = session
def test_vgg(): vgg = Vgg16() image_tensor = tf.placeholder(tf.float32) with tf.Session() as sess: vgg.build(image_tensor) init = tf.initialize_all_variables() sess.run(init) load_feature_layer_params('/Users/dtong/code/data/tf-image-interpreter/pretrain/vgg16_weights.npz', sess) for v in tf.get_collection(tf.GraphKeys.VARIABLES): print_op = tf.Print(v, [v], message=v.name, first_n=10) sess.run(print_op) roidb = RoiDb('val.txt', 2007) batch_gen = BatchGenerator(roidb) for i in range(10): image, scale, bboxes = batch_gen.next_batch() print(sess.run(vgg.conv5_3, feed_dict={image_tensor: image}))
def loss(logits, label_batch): """ Add L2Loss to all the trainable variables. Add summary for "Loss" and "Loss/avg". Args: logits -> logits from inference() label_batch -> 1D tensor of [batch_size] Rtns: total_loss -> float tensor """ # Calculate the average cross entropy loss across the batch. label_batch = tf.cast(label_batch,tf.int64) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, label_batch,name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') tf.add_to_collection('losses',cross_entropy_mean) # The total loss is defined as the cross entropy loss plus all of the weight # decay terms (L2 loss). return tf.add_n(tf.get_collection('losses'), name='total_loss')
def add_loss_summaries(total_loss): """ Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss -> total loss from loss() Rtns: loss_averages_op -> op for generating moving averages of losses """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.summary.scalar(l.op.name +' (raw)', l) tf.summary.scalar(l.op.name,loss_averages.average(l)) return loss_averages_op
def training_step(loss, optimizer_handle, learning_rate, **kwargs): ''' Creates the optimisation operation which is executed in each training iteration of the network :param loss: The loss to be minimised :param optimizer_handle: A handle to one of the tf optimisers :param learning_rate: Learning rate :param momentum: Optionally, you can also pass a momentum term to the optimiser. :return: The training operation ''' if 'momentum' in kwargs: momentum = kwargs.get('momentum') optimizer = optimizer_handle(learning_rate=learning_rate, momentum=momentum) else: optimizer = optimizer_handle(learning_rate=learning_rate) # The with statement is needed to make sure the tf contrib version of batch norm properly performs its updates update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss) return train_op
def __init__(self, sess, action_dim, action_bound, learning_rate, t_replace_iter): self.sess = sess self.a_dim = action_dim self.action_bound = action_bound self.lr = learning_rate self.t_replace_iter = t_replace_iter self.t_replace_counter = 0 with tf.variable_scope('Actor'): # input s, output a self.a = self._build_net(S, scope='eval_net', trainable=True) # input s_, output a, get a_ for critic self.a_ = self._build_net(S_, scope='target_net', trainable=False) self.e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/eval_net') self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Actor/target_net')
def tower_loss(name_scope, autoencoder, clips): # calculate reconstruction loss rec_loss = tf.reduce_mean(tf.abs(clips-autoencoder.rec_vid)) weight_decay_loss_list = tf.get_collection('losses', name_scope) weight_decay_loss = 0.0 if len(weight_decay_loss_list) > 0: weight_decay_loss = tf.add_n(weight_decay_loss_list) tf.add_to_collection('losses', rec_loss) losses = tf.get_collection('losses', name_scope) # Calculate the total loss for the current tower. total_loss = tf.add_n(losses, name='total_loss') return total_loss, rec_loss, weight_decay_loss
def tower_loss(name_scope, mfb, use_pretrained_encoder, encoder_gradient_ratio=1.0): # get reconstruction and ground truth ac_loss = mfb.ac_loss weight_decay_loss_list = tf.get_collection('losses', name_scope) if use_pretrained_encoder: if encoder_gradient_ratio == 0.0: weight_decay_loss_list = [var for var in weight_decay_loss_list \ if 'c3d' not in var.name and 'mapping' not in var.name] weight_decay_loss = 0.0 if len(weight_decay_loss_list) > 0: weight_decay_loss = tf.add_n(weight_decay_loss_list) total_loss = weight_decay_loss * 100 + ac_loss return total_loss, ac_loss, weight_decay_loss
def get_trainable_variables(trainable_scopes): """Returns a list of variables to train. Returns: A list of variables to train by the optimizer. """ if trainable_scopes is None: return tf.trainable_variables() trainable_scopes = [scope.strip() for scope in trainable_scopes] variables_to_train = [] for scope in trainable_scopes: variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope) variables_to_train.extend(variables) return variables_to_train
def _create_summaries(self): base_summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) with tf.name_scope('summaries/train'): train_loss = tf.summary.scalar('loss', self.loss) train_histo_loss = tf.summary.histogram( 'histogram_loss', self.loss ) train_summaries = [train_loss, train_histo_loss] train_summaries.extend(base_summaries) self.train_summary_op = tf.summary.merge(train_summaries) with tf.name_scope('summaries/valid'): valid_loss = tf.summary.scalar('loss', self.loss) valid_histo_loss = tf.summary.histogram( 'histogram_loss', self.loss ) valid_summaries = [valid_loss, valid_histo_loss] valid_summaries.extend(base_summaries) self.valid_summary_op = tf.summary.merge(valid_summaries)
def _add_loss_summaries(total_loss): """Add summaries for losses in CIFAR-10 model. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.scalar_summary(l.op.name +' (raw)', l) tf.scalar_summary(l.op.name, loss_averages.average(l)) return loss_averages_op
def update_prmt_dqn(scope_main): q_prmts = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES , scope_main + "/q_network" ) target_prmts = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope_main + "/target_network" ) sess.run( [tf.assign(t , q)for t,q in zip(target_prmts , q_prmts)]) #*** print("updating target-network parmeters...") # # def local2global(): # def global2local(): # ========= Error Raise =========
def _loss_shared(logits, labels): """Add L2Loss to all the trainable variables. Add summary for "Loss" and "Loss/avg". Args: logits: Logits from inference(). labels: Labels from distorted_inputs or inputs(). 1-D tensor of shape [batch_size] Returns: Loss tensor of type float. """ # Calculate the average cross entropy loss across the batch. labels = tf.cast(labels, tf.int64) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits, labels, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') tf.add_to_collection('losses', cross_entropy_mean) # The total loss is defined as the cross entropy loss plus all of the weight # decay terms (L2 loss). return tf.add_n(tf.get_collection('losses'), name='total_loss')
def _add_loss_summaries(total_loss): """Add summaries for losses. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.scalar_summary(l.op.name + ' (raw)', l) tf.scalar_summary(l.op.name, loss_averages.average(l)) return loss_averages_op
def inference(images, batch_size, train): """Build the ocr model. Args: images: Images returned from distorted_inputs() or inputs(). Returns: Logits. """ features, timesteps = convolutional_layers(images, batch_size, train) logits = get_lstm_layers(features, timesteps, batch_size) return logits, timesteps # The total loss is defined as the cross entropy loss plus all of the weight # decay terms (L2 loss). # return tf.add_n(tf.get_collection('losses'), name='total_loss')
def _add_loss_summaries(total_loss): """Add summaries for losses in ocr model. Generates moving average for all losses and associated summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). Returns: loss_averages_op: op for generating moving averages of losses. """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') losses = tf.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Name each loss as '(raw)' and name the moving average version of the loss # as the original loss name. tf.summary.scalar(l.op.name + ' (raw)', l) tf.summary.scalar(l.op.name, loss_averages.average(l)) return loss_averages_op
def train_simple(total_loss, global_step): with tf.variable_scope('train_op'): # Variables that affect learning rate. num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size decay_steps = int(num_batches_per_epoch * NUM_EPOCHS_PER_DECAY) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, decay_steps, LEARNING_RATE_DECAY_FACTOR, staircase=True) tf.summary.scalar('learning_rate', lr) # update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # with tf.control_dependencies(update_ops): # opt = tf.train.MomentumOptimizer(lr, 0.9).minimize(total_loss, global_step=global_step) opt = tf.train.AdamOptimizer(lr).minimize(total_loss, global_step=global_step) tf.summary.scalar(total_loss.op.name + ' (raw)', total_loss) return opt, lr
def collect_vars(scope, start=None, end=None, prepend_scope=None): vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) var_dict = OrderedDict() if isinstance(start, str): for i, var in enumerate(vars): var_name = remove_first_scope(var.op.name) if var_name.startswith(start): start = i break if isinstance(end, str): for i, var in enumerate(vars): var_name = remove_first_scope(var.op.name) if var_name.startswith(end): end = i break for var in vars[start:end]: var_name = remove_first_scope(var.op.name) if prepend_scope is not None: var_name = os.path.join(prepend_scope, var_name) var_dict[var_name] = var return var_dict
def testCreateLogisticClassifier(self): g = tf.Graph() with g.as_default(): tf.set_random_seed(0) tf_inputs = tf.constant(self._inputs, dtype=tf.float32) tf_labels = tf.constant(self._labels, dtype=tf.float32) model_fn = LogisticClassifier clone_args = (tf_inputs, tf_labels) deploy_config = model_deploy.DeploymentConfig(num_clones=1) self.assertEqual(slim.get_variables(), []) clones = model_deploy.create_clones(deploy_config, model_fn, clone_args) clone = clones[0] self.assertEqual(len(slim.get_variables()), 2) for v in slim.get_variables(): self.assertDeviceEqual(v.device, 'CPU:0') self.assertDeviceEqual(v.value().device, 'CPU:0') self.assertEqual(clone.outputs.op.name, 'LogisticClassifier/fully_connected/Sigmoid') self.assertEqual(clone.scope, '') self.assertDeviceEqual(clone.device, '') self.assertEqual(len(slim.losses.get_losses()), 1) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) self.assertEqual(update_ops, [])
def testCreateSingleclone(self): g = tf.Graph() with g.as_default(): tf.set_random_seed(0) tf_inputs = tf.constant(self._inputs, dtype=tf.float32) tf_labels = tf.constant(self._labels, dtype=tf.float32) model_fn = BatchNormClassifier clone_args = (tf_inputs, tf_labels) deploy_config = model_deploy.DeploymentConfig(num_clones=1) self.assertEqual(slim.get_variables(), []) clones = model_deploy.create_clones(deploy_config, model_fn, clone_args) clone = clones[0] self.assertEqual(len(slim.get_variables()), 5) for v in slim.get_variables(): self.assertDeviceEqual(v.device, 'CPU:0') self.assertDeviceEqual(v.value().device, 'CPU:0') self.assertEqual(clone.outputs.op.name, 'BatchNormClassifier/fully_connected/Sigmoid') self.assertEqual(clone.scope, '') self.assertDeviceEqual(clone.device, '') self.assertEqual(len(slim.losses.get_losses()), 1) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) self.assertEqual(len(update_ops), 2)
def testCreateLogisticClassifier(self): g = tf.Graph() with g.as_default(): tf.set_random_seed(0) tf_inputs = tf.constant(self._inputs, dtype=tf.float32) tf_labels = tf.constant(self._labels, dtype=tf.float32) model_fn = LogisticClassifier clone_args = (tf_inputs, tf_labels) deploy_config = model_deploy.DeploymentConfig(num_clones=1) self.assertEqual(slim.get_variables(), []) clones = model_deploy.create_clones(deploy_config, model_fn, clone_args) self.assertEqual(len(slim.get_variables()), 2) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) self.assertEqual(update_ops, []) optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) total_loss, grads_and_vars = model_deploy.optimize_clones(clones, optimizer) self.assertEqual(len(grads_and_vars), len(tf.trainable_variables())) self.assertEqual(total_loss.op.name, 'total_loss') for g, v in grads_and_vars: self.assertDeviceEqual(g.device, '') self.assertDeviceEqual(v.device, 'CPU:0')
def testCreateSingleclone(self): g = tf.Graph() with g.as_default(): tf.set_random_seed(0) tf_inputs = tf.constant(self._inputs, dtype=tf.float32) tf_labels = tf.constant(self._labels, dtype=tf.float32) model_fn = BatchNormClassifier clone_args = (tf_inputs, tf_labels) deploy_config = model_deploy.DeploymentConfig(num_clones=1) self.assertEqual(slim.get_variables(), []) clones = model_deploy.create_clones(deploy_config, model_fn, clone_args) self.assertEqual(len(slim.get_variables()), 5) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) self.assertEqual(len(update_ops), 2) optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) total_loss, grads_and_vars = model_deploy.optimize_clones(clones, optimizer) self.assertEqual(len(grads_and_vars), len(tf.trainable_variables())) self.assertEqual(total_loss.op.name, 'total_loss') for g, v in grads_and_vars: self.assertDeviceEqual(g.device, '') self.assertDeviceEqual(v.device, 'CPU:0')
def _get_variables_to_train(): """Returns a list of variables to train. Returns: A list of variables to train by the optimizer. """ if FLAGS.trainable_scopes is None: return tf.trainable_variables() else: scopes = [scope.strip() for scope in FLAGS.trainable_scopes.split(',')] variables_to_train = [] for scope in scopes: variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope) variables_to_train.extend(variables) return variables_to_train
def get_collection(self, global_step): W1 = tf.get_collection("W1")[0] W2 = tf.get_collection("W2")[0] observations = tf.get_collection("observations")[0] probability = tf.get_collection("probability")[0] advantages = tf.get_collection("advantages")[0] W1Grad = tf.get_collection("W1Grad")[0] updateGrads = tf.get_collection("updateGrads")[0] W2Grad = tf.get_collection("W2Grad")[0] newGrads1 = tf.get_collection("newGrads1")[0] newGrads2 = tf.get_collection("newGrads2")[0] newGrads = [newGrads1, newGrads2] self.global_step = global_step self.W1 = W1 self.W2 = W2 self.observations = observations self.probability = probability self.advantages = advantages self.W1Grad = W1Grad self.updateGrads = updateGrads self.W2Grad = W2Grad self.newGrads = newGrads # Before training, any initialization code
def __init__(self, ob_dim, ac_dim): #pylint: disable=W0613 X = tf.placeholder(tf.float32, shape=[None, ob_dim*2+ac_dim*2+2]) # batch of observations vtarg_n = tf.placeholder(tf.float32, shape=[None], name='vtarg') wd_dict = {} h1 = tf.nn.elu(dense(X, 64, "h1", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict)) h2 = tf.nn.elu(dense(h1, 64, "h2", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict)) vpred_n = dense(h2, 1, "hfinal", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict)[:,0] sample_vpred_n = vpred_n + tf.random_normal(tf.shape(vpred_n)) wd_loss = tf.get_collection("vf_losses", None) loss = U.mean(tf.square(vpred_n - vtarg_n)) + tf.add_n(wd_loss) loss_sampled = U.mean(tf.square(vpred_n - tf.stop_gradient(sample_vpred_n))) self._predict = U.function([X], vpred_n) optim = kfac.KfacOptimizer(learning_rate=0.001, cold_lr=0.001*(1-0.9), momentum=0.9, \ clip_kl=0.3, epsilon=0.1, stats_decay=0.95, \ async=1, kfac_update=2, cold_iter=50, \ weight_decay_dict=wd_dict, max_grad_norm=None) vf_var_list = [] for var in tf.trainable_variables(): if "vf" in var.name: vf_var_list.append(var) update_op, self.q_runner = optim.minimize(loss, loss_sampled, var_list=vf_var_list) self.do_update = U.function([X, vtarg_n], update_op) #pylint: disable=E1101 U.initialize() # Initialize uninitialized TF variables
def __init__(self, num_layers, size_layer, dimension_input, len_noise, sequence_size, learning_rate): self.noise = tf.placeholder(tf.float32, [None, None, len_noise]) self.fake_input = tf.placeholder(tf.float32, [None, None, dimension_input]) self.true_sentence = tf.placeholder(tf.float32, [None, None, dimension_input]) self.initial_layer = generator_encode(self.noise, num_layers, size_layer, len_noise) self.final_outputs = generator_sentence(self.fake_input, self.initial_layer, num_layers, size_layer, dimension_input) fake_logits = discriminator(self.final_outputs, num_layers, size_layer, dimension_input) true_logits = discriminator(self.true_sentence, num_layers, size_layer, dimension_input, reuse = True) d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = true_logits, labels = tf.ones_like(true_logits))) d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = fake_logits, labels = tf.zeros_like(fake_logits))) self.g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = fake_logits, labels = tf.ones_like(fake_logits))) self.d_loss = d_loss_real + d_loss_fake d_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope = 'discriminator') g_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope = 'generator_encode') + tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope = 'generator_sentence') self.d_train_opt = tf.train.AdamOptimizer(learning_rate, beta1 = 0.5).minimize(self.d_loss, var_list = d_vars) self.g_train_opt = tf.train.AdamOptimizer(learning_rate, beta1 = 0.5).minimize(self.g_loss, var_list = g_vars)
def build(self): self.add_placeholders() xavier = tf.contrib.layers.xavier_initializer(seed=1234) inputs, output_embed_matrix = self.add_input_op(xavier) # the encoder with tf.variable_scope('RNNEnc', initializer=xavier): enc_hidden_states, enc_final_state = self.add_encoder_op(inputs=inputs) self.final_encoder_state = enc_final_state # the training decoder with tf.variable_scope('RNNDec', initializer=xavier): train_preds = self.add_decoder_op(enc_final_state=enc_final_state, enc_hidden_states=enc_hidden_states, output_embed_matrix=output_embed_matrix, training=True) self.loss = self.add_loss_op(train_preds) + self.add_regularization_loss() self.train_op = self.add_training_op(self.loss) # the inference decoder with tf.variable_scope('RNNDec', initializer=xavier, reuse=True): eval_preds = self.add_decoder_op(enc_final_state=enc_final_state, enc_hidden_states=enc_hidden_states, output_embed_matrix=output_embed_matrix, training=False) self.pred = self.finalize_predictions(eval_preds) self.eval_loss = self.add_loss_op(eval_preds) weights = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) size = 0 def get_size(w): shape = w.get_shape() if shape.ndims == 2: return int(shape[0])*int(shape[1]) else: assert shape.ndims == 1 return int(shape[0]) for w in weights: sz = get_size(w) print('weight', w, sz) size += sz print('total model size', size)
def add_regularization_loss(self): weights = [w for w in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if w.name.split('/')[-1] in ('kernel:0', 'weights:0')] if self.config.l2_regularization == 0.0: return 0 return tf.contrib.layers.apply_regularization(tf.contrib.layers.l2_regularizer(self.config.l2_regularization), weights)
def _add_loss_summaries(total_loss): """Add summaries for losses Generates loss summaries for visualizing the performance of the network. Args: total_loss: Total loss from loss(). """ losses = tf.get_collection('losses') # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: tf.summary.scalar(l.op.name, l)
def check_video_id(): tf.set_random_seed(0) # for reproducibility with tf.Graph().as_default(): # convert feature_names and feature_sizes to lists of values feature_names, feature_sizes = utils.GetListOfFeatureNamesAndSizes( FLAGS.feature_names, FLAGS.feature_sizes) # prepare a reader for each single model prediction result all_readers = [] all_patterns = FLAGS.eval_data_patterns all_patterns = map(lambda x: x.strip(), all_patterns.strip().strip(",").split(",")) for i in xrange(len(all_patterns)): reader = readers.EnsembleReader( feature_names=feature_names, feature_sizes=feature_sizes) all_readers.append(reader) input_reader = None input_data_pattern = None if FLAGS.input_data_pattern is not None: input_reader = readers.EnsembleReader( feature_names=["mean_rgb","mean_audio"], feature_sizes=[1024,128]) input_data_pattern = FLAGS.input_data_pattern if FLAGS.eval_data_patterns is "": raise IOError("'eval_data_patterns' was not specified. " + "Nothing to evaluate.") build_graph( all_readers=all_readers, input_reader=input_reader, input_data_pattern=input_data_pattern, all_eval_data_patterns=all_patterns, batch_size=FLAGS.batch_size) logging.info("built evaluation graph") video_id_equal = tf.get_collection("video_id_equal")[0] input_distance = tf.get_collection("input_distance")[0] check_loop(video_id_equal, input_distance, all_patterns)
def __init__(self, checkpoint_file): checkpoint_dir = os.path.dirname(checkpoint_file) hparams_file = os.path.join(checkpoint_dir, "hparams.txt") hparams_dict = {} if os.path.isfile(hparams_file): with open(hparams_file) as f: hparams_dict = ast.literal_eval(f.read()) self.hparams = TensorflowClassifierHparams(**hparams_dict) self.graph = tf.Graph() with self.graph.as_default(): print("loading from file {}".format(checkpoint_file)) config = tf.ConfigProto( device_count={'GPU': 0}, ) config.gpu_options.visible_device_list = "" self.session = tf.Session(config=config) new_saver = tf.train.import_meta_graph(checkpoint_file + ".meta", clear_devices=True) new_saver.restore(self.session, checkpoint_file) self.features = {} if self.hparams.use_image: self.features["image"] = self.graph.get_tensor_by_name("image:0") if self.hparams.use_observation: self.features["observation"] = self.graph.get_tensor_by_name("observation:0") if self.hparams.use_action: self.features["action"] = self.graph.get_tensor_by_name("action:0") self.prediction = tf.get_collection('prediction')[0] self.loss = tf.get_collection('loss')[0] self.threshold = tf.get_collection('threshold')[0]