本文整理汇总了Python中tensorflow.variable方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.variable方法的具体用法?Python tensorflow.variable怎么用?Python tensorflow.variable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.variable方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _end_training
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def _end_training(self):
"""Performs clean up tasks after training.
"""
# Reset this variable as it is reused during evaluation phase
if self.is_filtered and self.eval_dataset_handle is not None:
# cleanup the evaluation data (deletion of tables
self.eval_dataset_handle.cleanup()
self.eval_dataset_handle = None
if self.train_dataset_handle is not None:
self.train_dataset_handle.cleanup()
self.train_dataset_handle = None
self.is_filtered = False
self.eval_config = {}
# close the tf session
if self.sess_train is not None:
self.sess_train.close()
# set is_fitted to true to indicate that the model fitting is completed
self.is_fitted = True
示例2: get_variable
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def get_variable(self, name, shape, noisy_params=None,
noise_rate=0.05, **kwargs):
"""Get a tensorflow variable for the model. Add noise if required.
Args:
name: The name of the variable.
shape: The shape of the variable.
noisy_params: (dict<parameter_name, tf.variable>) A dictionary of noisy parameters to add.
noise_rate: (float) A value specify how much noise to add.
Returns:
A tf.Tensor
"""
var = tf.get_variable(name, shape, **kwargs)
self.model_parameters[var.name] = var
if noisy_params is not None and var.name in noisy_params:
var = var + noisy_params[var.name] * noise_rate
return var
示例3: build
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def build(self, input_list, noisy_params=None,
noise_rate=0.05, is_training=False, **kwargs):
""" Create the Linear model
Args:
input_list: (list<tf.tensor>) A list of tensors containing the features
for a list of documents.
noisy_params: (dict<parameter_name, tf.variable>) A dictionary of noisy parameters to add.
noise_rate: (float) A value specify how much noise to add.
is_training: (bool) A flag indicating whether the model is running in training mode.
Returns:
A list of tf.Tensor containing the ranking scores for each instance in input_list.
"""
with tf.variable_scope(tf.get_variable_scope(), initializer=self.initializer,
reuse=tf.AUTO_REUSE):
input_data = tf.concat(input_list, axis=0)
output_data = input_data
output_sizes = [1]
if self.layer_norm is None and self.hparams.norm in BaseRankingModel.NORM_FUNC_DIC:
self.layer_norm = []
for j in range(len(output_sizes)):
self.layer_norm.append(BaseRankingModel.NORM_FUNC_DIC[self.hparams.norm](
name="layer_norm_%d" % j))
current_size = output_data.get_shape()[-1].value
for j in range(len(output_sizes)):
if self.layer_norm is not None:
output_data = self.layer_norm[j](
output_data, training=is_training)
expand_W = self.get_variable(
"linear_W_%d" % j, [current_size, output_sizes[j]], noisy_params=noisy_params, noise_rate=noise_rate)
expand_b = self.get_variable(
"linear_b_%d" % j, [output_sizes[j]], noisy_params=noisy_params, noise_rate=noise_rate)
output_data = tf.nn.bias_add(
tf.matmul(output_data, expand_W), expand_b)
return tf.split(output_data, len(input_list), axis=0)
示例4: build
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def build(self, input_list, noisy_params=None,
noise_rate=0.05, is_training=False, **kwargs):
""" Create the model
Args:
input_list: (list<tf.tensor>) A list of tensors containing the features
for a list of documents.
noisy_params: (dict<parameter_name, tf.variable>) A dictionary of noisy parameters to add.
noise_rate: (float) A value specify how much noise to add.
is_training: (bool) A flag indicating whether the model is running in training mode.
Returns:
A list of tf.Tensor containing the ranking scores for each instance in input_list.
"""
pass
示例5: build
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def build(self, input_list, noisy_params=None,
noise_rate=0.05, is_training=False, **kwargs):
""" Create the SetRank model (no supports for noisy parameters)
Args:
input_list: (list<tf.tensor>) A list of tensors containing the features
for a list of documents.
noisy_params: (dict<parameter_name, tf.variable>) A dictionary of noisy parameters to add.
noise_rate: (float) A value specify how much noise to add.
is_training: (bool) A flag indicating whether the model is running in training mode.
Returns:
A list of tf.Tensor containing the ranking scores for each instance in input_list.
"""
with tf.variable_scope(tf.get_variable_scope() or "transformer", reuse=tf.AUTO_REUSE, initializer=self.initializer):
sco_cur = tf.get_variable_scope()
print(sco_cur.name, "sco_cur.name")
mask = None
batch_size = tf.shape(input_list[0])[0]
feature_size = tf.shape(input_list[0])[1]
list_size = len(input_list)
ind = list(range(0, list_size))
random.shuffle(ind)
# input_list=[input_list[i] for i in ind ]
x = [tf.expand_dims(e, 1)for e in input_list]
x = tf.concat(axis=1, values=x) # [batch,len_seq,feature_size]
x = self.Encoder_layer(x, is_training, mask) # [batch,len_seq,1]
output = []
for i in range(list_size):
output.append(x[:, i, :])
# reind_output=[None]*list_size
# for i in range(list_size):
# reind_output[ind[i]]=output[i]
# output=reind_output
return output # [len_seq,batch,1]
示例6: _load_model_from_trained_params
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def _load_model_from_trained_params(self):
"""Load the model from trained params.
While restoring make sure that the order of loaded parameters match the saved order.
It's the duty of the embedding model to load the variables correctly.
This method must be overridden if the model has any other parameters (apart from entity-relation embeddings).
This function also set's the evaluation mode to do lazy loading of variables based on the number of
distinct entities present in the graph.
"""
# Generate the batch size based on entity length and batch_count
self.batch_size = int(np.ceil(len(self.ent_to_idx) / self.batches_count))
if len(self.ent_to_idx) > ENTITY_THRESHOLD:
self.dealing_with_large_graphs = True
logger.warning('Your graph has a large number of distinct entities. '
'Found {} distinct entities'.format(len(self.ent_to_idx)))
logger.warning('Changing the variable loading strategy to use lazy loading of variables...')
logger.warning('Evaluation would take longer than usual.')
if not self.dealing_with_large_graphs:
# (We use tf.variable for future - to load and continue training)
self.ent_emb = tf.Variable(self.trained_model_params[0], dtype=tf.float32)
else:
# Embeddings of all the corruptions entities will not fit on GPU.
# During training we loaded batch_size*2 embeddings on GPU as only 2* batch_size unique
# entities can be present in one batch.
# During corruption generation in eval mode, one side(s/o) is fixed and only the other side varies.
# Hence we use a batch size of 2 * training_batch_size for corruption generation i.e. those many
# corruption embeddings would be loaded per batch on the GPU. In other words, those corruptions
# would be processed as a batch.
self.corr_batch_size = self.batch_size * 2
# Load the entity embeddings on the cpu
self.ent_emb_cpu = self.trained_model_params[0]
# (We use tf.variable for future - to load and continue training)
# create empty variable on GPU.
# we initialize it with zeros because the actual embeddings will be loaded on the fly.
self.ent_emb = tf.Variable(np.zeros((self.corr_batch_size, self.internal_k)), dtype=tf.float32)
# (We use tf.variable for future - to load and continue training)
self.rel_emb = tf.Variable(self.trained_model_params[1], dtype=tf.float32)
示例7: build
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import variable [as 别名]
def build(self, input_list, noisy_params=None,
noise_rate=0.05, is_training=False, **kwargs):
""" Create the DNN model
Args:
input_list: (list<tf.tensor>) A list of tensors containing the features
for a list of documents.
noisy_params: (dict<parameter_name, tf.variable>) A dictionary of noisy parameters to add.
noise_rate: (float) A value specify how much noise to add.
is_training: (bool) A flag indicating whether the model is running in training mode.
Returns:
A list of tf.Tensor containing the ranking scores for each instance in input_list.
"""
with tf.variable_scope(tf.get_variable_scope(), initializer=self.initializer,
reuse=tf.AUTO_REUSE):
input_data = tf.concat(input_list, axis=0)
output_data = input_data
output_sizes = self.hparams.hidden_layer_sizes + [1]
if self.layer_norm is None and self.hparams.norm in BaseRankingModel.NORM_FUNC_DIC:
self.layer_norm = []
for j in range(len(output_sizes)):
self.layer_norm.append(BaseRankingModel.NORM_FUNC_DIC[self.hparams.norm](
name="layer_norm_%d" % j))
current_size = output_data.get_shape()[-1].value
for j in range(len(output_sizes)):
if self.layer_norm is not None:
output_data = self.layer_norm[j](
output_data, training=is_training)
expand_W = self.get_variable(
"dnn_W_%d" % j, [current_size, output_sizes[j]], noisy_params=noisy_params, noise_rate=noise_rate)
expand_b = self.get_variable("dnn_b_%d" % j, [
output_sizes[j]], noisy_params=noisy_params, noise_rate=noise_rate)
output_data = tf.nn.bias_add(
tf.matmul(output_data, expand_W), expand_b)
# Add activation if it is a hidden layer
if j != len(output_sizes) - 1:
output_data = self.act_func(output_data)
current_size = output_sizes[j]
return tf.split(output_data, len(input_list), axis=0)