Python tensorflow.Variable() Examples
The following are 30
code examples of tensorflow.Variable().
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: model.py From Neural-LP with MIT License | 6 votes |
def _build_input(self): self.tails = tf.placeholder(tf.int32, [None]) self.heads = tf.placeholder(tf.int32, [None]) self.targets = tf.one_hot(indices=self.heads, depth=self.num_entity) if not self.query_is_language: self.queries = tf.placeholder(tf.int32, [None, self.num_step]) self.query_embedding_params = tf.Variable(self._random_uniform_unit( self.num_query + 1, # <END> token self.query_embed_size), dtype=tf.float32) rnn_inputs = tf.nn.embedding_lookup(self.query_embedding_params, self.queries) else: self.queries = tf.placeholder(tf.int32, [None, self.num_step, self.num_word]) self.vocab_embedding_params = tf.Variable(self._random_uniform_unit( self.num_vocab + 1, # <END> token self.vocab_embed_size), dtype=tf.float32) embedded_query = tf.nn.embedding_lookup(self.vocab_embedding_params, self.queries) rnn_inputs = tf.reduce_mean(embedded_query, axis=2) return rnn_inputs
Example #2
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 #3
Source File: dataset.py From disentangling_conditional_gans with MIT License | 6 votes |
def __init__(self, resolution=1024, num_channels=3, dtype='uint8', dynamic_range=[0,255], label_size=0, label_dtype='float32'): self.resolution = resolution self.resolution_log2 = int(np.log2(resolution)) self.shape = [num_channels, resolution, resolution] self.dtype = dtype self.dynamic_range = dynamic_range self.label_size = label_size self.label_dtype = label_dtype self._tf_minibatch_var = None self._tf_lod_var = None self._tf_minibatch_np = None self._tf_labels_np = None assert self.resolution == 2 ** self.resolution_log2 with tf.name_scope('Dataset'): self._tf_minibatch_var = tf.Variable(np.int32(0), name='minibatch_var') self._tf_lod_var = tf.Variable(np.int32(0), name='lod_var')
Example #4
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def set_input_shape(self, input_shape): batch_size, rows, cols, input_channels = input_shape kernel_shape = tuple(self.kernel_shape) + (input_channels, self.output_channels) assert len(kernel_shape) == 4 assert all(isinstance(e, int) for e in kernel_shape), kernel_shape init = tf.random_normal(kernel_shape, dtype=tf.float32) init = init / tf.sqrt(1e-7 + tf.reduce_sum(tf.square(init), axis=(0, 1, 2))) self.kernels = tf.Variable(init) self.b = tf.Variable( np.zeros((self.output_channels,)).astype('float32')) input_shape = list(input_shape) input_shape[0] = 1 dummy_batch = tf.zeros(input_shape) dummy_output = self.fprop(dummy_batch) output_shape = [int(e) for e in dummy_output.get_shape()] output_shape[0] = batch_size self.output_shape = tuple(output_shape)
Example #5
Source File: resnet_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _decay(self): """L2 weight decay loss.""" if self.decay_cost is not None: return self.decay_cost costs = [] if self.device_name is None: for var in tf.trainable_variables(): if var.op.name.find(r'DW') > 0: costs.append(tf.nn.l2_loss(var)) else: for layer in self.layers: for var in layer.params_device[self.device_name].values(): if (isinstance(var, tf.Variable) and var.op.name.find(r'DW') > 0): costs.append(tf.nn.l2_loss(var)) self.decay_cost = tf.multiply(self.hps.weight_decay_rate, tf.add_n(costs)) return self.decay_cost
Example #6
Source File: mnist_histogram.py From deep-learning-note with MIT License | 6 votes |
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu): # 同一层神经网络放在一个统一的命名空间下 with tf.name_scope(layer_name): with tf.name_scope('weights'): # 权重及监控变量 weights = tf.Variable(tf.truncated_normal([input_dim, output_dim], stddev=0.1)) variable_summaries(weights, layer_name+'/weights') with tf.name_scope('biases'): # 偏置及监控变量 biases = tf.Variable(tf.constant(0.0, shape=[output_dim])) variable_summaries(biases, layer_name + '/biases') with tf.name_scope('Wx_plus_b'): preactivate = tf.matmul(input_tensor, weights) + biases # 记录神经网络输出节点在经过激活函数之前的分布 tf.summary.histogram(layer_name + '/pre_activations', preactivate) activations = act(preactivate, name='activation') # 记录神经网络输出节点在经过激活函数之后的分布 tf.summary.histogram(layer_name + '/activations', activations) return activations
Example #7
Source File: 2_tf_linear.py From deep-learning-note with MIT License | 6 votes |
def createLinearModel(dimension): np.random.seed(1024) # 定义 x 和 y x = tf.placeholder(tf.float64, shape=[None, dimension], name='x') # 写成矩阵形式会大大加快运算速度 y = tf.placeholder(tf.float64, shape=[None, 1], name='y') # 定义参数估计值和预测值 betaPred = tf.Variable(np.random.random([dimension, 1])) yPred = tf.matmul(x, betaPred, name='y_pred') # 定义损失函数 loss = tf.reduce_mean(tf.square(yPred - y)) model = { 'loss_function': loss, 'independent_variable': x, 'dependent_variable': y, 'prediction': yPred, 'model_params': betaPred } return model
Example #8
Source File: model_deploy_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def testPS(self): deploy_config = model_deploy.DeploymentConfig(num_clones=1, num_ps_tasks=1) self.assertDeviceEqual(deploy_config.clone_device(0), '/job:worker/device:GPU:0') self.assertEqual(deploy_config.clone_scope(0), '') self.assertDeviceEqual(deploy_config.optimizer_device(), '/job:worker/device:CPU:0') self.assertDeviceEqual(deploy_config.inputs_device(), '/job:worker/device:CPU:0') with tf.device(deploy_config.variables_device()): a = tf.Variable(0) b = tf.Variable(0) c = tf.no_op() d = slim.variable('a', [], caching_device=deploy_config.caching_device()) self.assertDeviceEqual(a.device, '/job:ps/task:0/device:CPU:0') self.assertDeviceEqual(a.device, a.value().device) self.assertDeviceEqual(b.device, '/job:ps/task:0/device:CPU:0') self.assertDeviceEqual(b.device, b.value().device) self.assertDeviceEqual(c.device, '') self.assertDeviceEqual(d.device, '/job:ps/task:0/device:CPU:0') self.assertDeviceEqual(d.value().device, '')
Example #9
Source File: model_deploy_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def testVariablesPS(self): deploy_config = model_deploy.DeploymentConfig(num_ps_tasks=2) with tf.device(deploy_config.variables_device()): a = tf.Variable(0) b = tf.Variable(0) c = tf.no_op() d = slim.variable('a', [], caching_device=deploy_config.caching_device()) self.assertDeviceEqual(a.device, '/job:ps/task:0/device:CPU:0') self.assertDeviceEqual(a.device, a.value().device) self.assertDeviceEqual(b.device, '/job:ps/task:1/device:CPU:0') self.assertDeviceEqual(b.device, b.value().device) self.assertDeviceEqual(c.device, '') self.assertDeviceEqual(d.device, '/job:ps/task:0/device:CPU:0') self.assertDeviceEqual(d.value().device, '')
Example #10
Source File: cifar10.py From DOTA_models with Apache License 2.0 | 6 votes |
def _variable_with_weight_decay(name, shape, stddev, wd): """Helper to create an initialized Variable with weight decay. Note that the Variable is initialized with a truncated normal distribution. A weight decay is added only if one is specified. Args: name: name of the variable shape: list of ints stddev: standard deviation of a truncated Gaussian wd: add L2Loss weight decay multiplied by this float. If None, weight decay is not added for this Variable. Returns: Variable Tensor """ dtype = tf.float16 if FLAGS.use_fp16 else tf.float32 var = _variable_on_cpu( name, shape, tf.truncated_normal_initializer(stddev=stddev, dtype=dtype)) if wd is not None: weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss') tf.add_to_collection('losses', weight_decay) return var
Example #11
Source File: graph_builder.py From DOTA_models with Apache License 2.0 | 6 votes |
def _create_learning_rate(hyperparams, step_var): """Creates learning rate var, with decay and switching for CompositeOptimizer. Args: hyperparams: a GridPoint proto containing optimizer spec, particularly learning_method to determine optimizer class to use. step_var: tf.Variable, global training step. Returns: a scalar `Tensor`, the learning rate based on current step and hyperparams. """ if hyperparams.learning_method != 'composite': base_rate = hyperparams.learning_rate else: spec = hyperparams.composite_optimizer_spec switch = tf.less(step_var, spec.switch_after_steps) base_rate = tf.cond(switch, lambda: tf.constant(spec.method1.learning_rate), lambda: tf.constant(spec.method2.learning_rate)) return tf.train.exponential_decay( base_rate, step_var, hyperparams.decay_steps, hyperparams.decay_base, staircase=hyperparams.decay_staircase)
Example #12
Source File: accountant.py From DOTA_models with Apache License 2.0 | 6 votes |
def __init__(self, total_examples, moment_orders=32): """Initialize a MomentsAccountant. Args: total_examples: total number of examples. moment_orders: the order of moments to keep. """ assert total_examples > 0 self._total_examples = total_examples self._moment_orders = (moment_orders if isinstance(moment_orders, (list, tuple)) else range(1, moment_orders + 1)) self._max_moment_order = max(self._moment_orders) assert self._max_moment_order < 100, "The moment order is too large." self._log_moments = [tf.Variable(numpy.float64(0.0), trainable=False, name=("log_moments-%d" % moment_order)) for moment_order in self._moment_orders]
Example #13
Source File: deep_cnn.py From DOTA_models with Apache License 2.0 | 6 votes |
def _variable_with_weight_decay(name, shape, stddev, wd): """Helper to create an initialized Variable with weight decay. Note that the Variable is initialized with a truncated normal distribution. A weight decay is added only if one is specified. Args: name: name of the variable shape: list of ints stddev: standard deviation of a truncated Gaussian wd: add L2Loss weight decay multiplied by this float. If None, weight decay is not added for this Variable. Returns: Variable Tensor """ var = _variable_on_cpu(name, shape, tf.truncated_normal_initializer(stddev=stddev)) if wd is not None: weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss') tf.add_to_collection('losses', weight_decay) return var
Example #14
Source File: variables.py From DOTA_models with Apache License 2.0 | 6 votes |
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.GLOBAL_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)
Example #15
Source File: siamese_network_semantic.py From deep-siamese-text-similarity with MIT License | 5 votes |
def __init__( self, sequence_length, vocab_size, embedding_size, hidden_units, l2_reg_lambda, batch_size, trainableEmbeddings): # Placeholders for input, output and dropout self.input_x1 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x1") self.input_x2 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x2") self.input_y = tf.placeholder(tf.float32, [None], name="input_y") self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob") # Keeping track of l2 regularization loss (optional) l2_loss = tf.constant(0.0, name="l2_loss") # Embedding layer with tf.name_scope("embedding"): self.W = tf.Variable( tf.constant(0.0, shape=[vocab_size, embedding_size]), trainable=trainableEmbeddings,name="W") self.embedded_words1 = tf.nn.embedding_lookup(self.W, self.input_x1) self.embedded_words2 = tf.nn.embedding_lookup(self.W, self.input_x2) print self.embedded_words1 # Create a convolution + maxpool layer for each filter size with tf.name_scope("output"): self.out1=self.stackedRNN(self.embedded_words1, self.dropout_keep_prob, "side1", embedding_size, sequence_length, hidden_units) self.out2=self.stackedRNN(self.embedded_words2, self.dropout_keep_prob, "side2", embedding_size, sequence_length, hidden_units) self.distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(self.out1,self.out2)),1,keep_dims=True)) self.distance = tf.div(self.distance, tf.add(tf.sqrt(tf.reduce_sum(tf.square(self.out1),1,keep_dims=True)),tf.sqrt(tf.reduce_sum(tf.square(self.out2),1,keep_dims=True)))) self.distance = tf.reshape(self.distance, [-1], name="distance") with tf.name_scope("loss"): self.loss = self.contrastive_loss(self.input_y,self.distance, batch_size) #### Accuracy computation is outside of this class. with tf.name_scope("accuracy"): self.temp_sim = tf.subtract(tf.ones_like(self.distance),tf.rint(self.distance), name="temp_sim") #auto threshold 0.5 correct_predictions = tf.equal(self.temp_sim, self.input_y) self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
Example #16
Source File: siamese_network.py From deep-siamese-text-similarity with MIT License | 5 votes |
def __init__( self, sequence_length, vocab_size, embedding_size, hidden_units, l2_reg_lambda, batch_size): # Placeholders for input, output and dropout self.input_x1 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x1") self.input_x2 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x2") self.input_y = tf.placeholder(tf.float32, [None], name="input_y") self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob") # Keeping track of l2 regularization loss (optional) l2_loss = tf.constant(0.0, name="l2_loss") # Embedding layer with tf.name_scope("embedding"): self.W = tf.Variable( tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), trainable=True,name="W") self.embedded_chars1 = tf.nn.embedding_lookup(self.W, self.input_x1) #self.embedded_chars_expanded1 = tf.expand_dims(self.embedded_chars1, -1) self.embedded_chars2 = tf.nn.embedding_lookup(self.W, self.input_x2) #self.embedded_chars_expanded2 = tf.expand_dims(self.embedded_chars2, -1) # Create a convolution + maxpool layer for each filter size with tf.name_scope("output"): self.out1=self.BiRNN(self.embedded_chars1, self.dropout_keep_prob, "side1", embedding_size, sequence_length, hidden_units) self.out2=self.BiRNN(self.embedded_chars2, self.dropout_keep_prob, "side2", embedding_size, sequence_length, hidden_units) self.distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(self.out1,self.out2)),1,keep_dims=True)) self.distance = tf.div(self.distance, tf.add(tf.sqrt(tf.reduce_sum(tf.square(self.out1),1,keep_dims=True)),tf.sqrt(tf.reduce_sum(tf.square(self.out2),1,keep_dims=True)))) self.distance = tf.reshape(self.distance, [-1], name="distance") with tf.name_scope("loss"): self.loss = self.contrastive_loss(self.input_y,self.distance, batch_size) #### Accuracy computation is outside of this class. with tf.name_scope("accuracy"): self.temp_sim = tf.subtract(tf.ones_like(self.distance),tf.rint(self.distance), name="temp_sim") #auto threshold 0.5 correct_predictions = tf.equal(self.temp_sim, self.input_y) self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
Example #17
Source File: baseop.py From Traffic_sign_detection_YOLO with MIT License | 5 votes |
def _shape(tensor): # work for both tf.Tensor & np.ndarray if type(tensor) in [tf.Variable, tf.Tensor]: return tensor.get_shape() else: return tensor.shape
Example #18
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def is_tf_expression(x): return isinstance(x, tf.Tensor) or isinstance(x, tf.Variable) or isinstance(x, tf.Operation)
Example #19
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def get_loss_scaling_var(self, device): if not self.use_loss_scaling: return None if device not in self._dev_ls_var: with absolute_name_scope(self.scope + '/LossScalingVars'), tf.control_dependencies(None): self._dev_ls_var[device] = tf.Variable(np.float32(self.loss_scaling_init), name='loss_scaling_var') return self._dev_ls_var[device] # Apply dynamic loss scaling for the given expression.
Example #20
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 5 votes |
def list_layers(self): patterns_to_ignore = ['/Setter', '/new_value', '/Shape', '/strided_slice', '/Cast', '/concat'] all_ops = tf.get_default_graph().get_operations() all_ops = [op for op in all_ops if not any(p in op.name for p in patterns_to_ignore)] layers = [] def recurse(scope, parent_ops, level): prefix = scope + '/' ops = [op for op in parent_ops if op.name == scope or op.name.startswith(prefix)] # Does not contain leaf nodes => expand immediate children. if level == 0 or all('/' in op.name[len(prefix):] for op in ops): visited = set() for op in ops: suffix = op.name[len(prefix):] if '/' in suffix: suffix = suffix[:suffix.index('/')] if suffix not in visited: recurse(prefix + suffix, ops, level + 1) visited.add(suffix) # Otherwise => interpret as a layer. else: layer_name = scope[len(self.scope)+1:] layer_output = ops[-1].outputs[0] layer_trainables = [op.outputs[0] for op in ops if op.type.startswith('Variable') and self.get_var_localname(op.name) in self.trainables] layers.append((layer_name, layer_output, layer_trainables)) recurse(self.scope, all_ops, 0) return layers # Print a summary table of the network structure.
Example #21
Source File: model.py From jiji-with-tensorflow-example with MIT License | 5 votes |
def __setup_model(self): column_size = Model.COLUMN_SIZE w1 = tf.Variable(tf.truncated_normal([column_size, Estimator.HIDDEN_UNIT_SIZE], stddev=0.1)) b1 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE])) h1 = tf.nn.relu(tf.matmul(self.trade_data, w1) + b1) w2 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE, Estimator.HIDDEN_UNIT_SIZE2], stddev=0.1)) b2 = tf.Variable(tf.constant(0.1, shape=[Estimator.HIDDEN_UNIT_SIZE2])) h2 = tf.nn.relu(tf.matmul(h1, w2) + b2) h2_drop = tf.nn.dropout(h2, self.keep_prob) w2 = tf.Variable(tf.truncated_normal([Estimator.HIDDEN_UNIT_SIZE2, 2], stddev=0.1)) b2 = tf.Variable(tf.constant(0.1, shape=[2])) self.output = tf.nn.softmax(tf.matmul(h2_drop, w2) + b2)
Example #22
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _weight_variable(shape,name=None): """weight_variable generates a weigh t variable of a given shape.""" initial = tf.truncated_normal(shape, stddev=0.01)+0.01 return tf.Variable(initial,name=name)
Example #23
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _bias_variable1(shape,name=None): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.5, shape=shape) return tf.Variable(initial,name=name)
Example #24
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _bias_variable(shape,name=None): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.5, shape=shape) return tf.Variable(initial,name=name)
Example #25
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _weight_variable(shape,name=None): """weight_variable generates a weigh t variable of a given shape.""" initial = tf.truncated_normal(shape, stddev=0.01)+0.01 return tf.Variable(initial,name=name)
Example #26
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _bias_variable1(shape,name=None): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.5, shape=shape) return tf.Variable(initial,name=name)
Example #27
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _weight_variable(shape,name=None): """weight_variable generates a weigh t variable of a given shape.""" initial = tf.truncated_normal(shape, stddev=0.01)+0.01 return tf.Variable(initial,name=name)
Example #28
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _bias_variable1(shape,name=None): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.5, shape=shape) return tf.Variable(initial,name=name)
Example #29
Source File: tf_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _bias_variable(shape,name=None): """bias_variable generates a bias variable of a given shape.""" initial = tf.constant(0.5, shape=shape) return tf.Variable(initial,name=name)
Example #30
Source File: madry_mnist_model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)