本文整理汇总了Python中tensorflow.device函数的典型用法代码示例。如果您正苦于以下问题:Python device函数的具体用法?Python device怎么用?Python device使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了device函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(
self,
remote_device,
local_device,
top_delta_size=64,
top_delta_layers=2,
compute_h_size=64,
compute_h_layers=1,
delta_dim=32,
num_grad_channels=4,
normalize_epsilon=1.,
):
self.local_device = local_device
self.remote_device = remote_device
self.top_delta_size = top_delta_size
self.top_delta_layers = top_delta_layers
self.compute_h_size = compute_h_size
self.compute_h_layers = compute_h_layers
self.delta_dim = delta_dim
self.num_grad_channels = num_grad_channels
self.normalize_epsilon = normalize_epsilon,
with tf.device(local_device):
self.opt = optimizers.UnrollableGradientDescentRollingOptimizer(
learning_rate=1e-4)
# lazily initialized for readouts
self.readout_mods = {}
super(MoreLocalWeightUpdateProcess,
self).__init__(name='MoreLocalWeightUpdateProcess')
with tf.device(remote_device):
self()
示例2: _apply_drop_path
def _apply_drop_path(self, net):
"""Apply drop_path regularization to net."""
drop_path_keep_prob = self._drop_path_keep_prob
if drop_path_keep_prob < 1.0:
# Scale keep prob by layer number
assert self._cell_num != -1
# The added 2 is for the reduction cells
num_cells = self._total_num_cells
layer_ratio = (self._cell_num + 1)/float(num_cells)
with tf.device('/cpu:0'):
tf.summary.scalar('layer_ratio', layer_ratio)
drop_path_keep_prob = 1 - layer_ratio * (1 - drop_path_keep_prob)
# Decrease the keep probability over time
current_step = tf.cast(tf.train.get_or_create_global_step(),
tf.float32)
drop_path_burn_in_steps = self._total_training_steps
current_ratio = (
current_step / drop_path_burn_in_steps)
current_ratio = tf.minimum(1.0, current_ratio)
with tf.device('/cpu:0'):
tf.summary.scalar('current_ratio', current_ratio)
drop_path_keep_prob = (
1 - current_ratio * (1 - drop_path_keep_prob))
with tf.device('/cpu:0'):
tf.summary.scalar('drop_path_keep_prob', drop_path_keep_prob)
net = drop_path(net, drop_path_keep_prob)
return net
示例3: __init__
def __init__(self, session, np_matrix, rank,
learning_rate=0.1):
matrix = tf.constant(np_matrix, dtype=tf.float32)
scale = 2 * np.sqrt(np_matrix.mean() / rank)
initializer = tf.random_uniform_initializer(maxval=scale)
with tf.device('/job:ps/task:0'):
self.matrix_W = tf.get_variable(
"W", (np_matrix.shape[0], rank), initializer=initializer
)
with tf.device("/job:ps/task:1"):
self.matrix_H = tf.get_variable(
"H", (rank, np_matrix.shape[1]), initializer=initializer
)
matrix_WH = tf.matmul(self.matrix_W, self.matrix_H)
f_norm = tf.reduce_sum(tf.pow(matrix - matrix_WH, 2))
nn_w = tf.reduce_sum(tf.abs(self.matrix_W) - self.matrix_W)
nn_h = tf.reduce_sum(tf.abs(self.matrix_H) - self.matrix_H)
constraint = INFINITY * (nn_w + nn_h)
self.loss = f_norm + constraint
self.constraint = constraint
self.session = session
self.optimizer = tf.train.GradientDescentOptimizer(
learning_rate
).minimize(self.loss)
示例4: prepare_networks
def prepare_networks(gpu,image_batch, nb_cl, nb_groups):
mean_img = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean')
scores = []
with tf.variable_scope('ResNet18'):
with tf.device('/gpu:' + gpu):
score = utils_resnet.ResNet18(image_batch-mean_img, phase='train',num_outputs=nb_cl*nb_groups)
scores.append(score)
scope = tf.get_variable_scope()
scope.reuse_variables()
# First score and initialization
variables_graph = tf.get_collection(tf.GraphKeys.WEIGHTS, scope='ResNet18')
scores_stored = []
with tf.variable_scope('store_ResNet18'):
with tf.device('/gpu:' + gpu):
score = utils_resnet.ResNet18(image_batch-mean_img, phase='test',num_outputs=nb_cl*nb_groups)
scores_stored.append(score)
scope = tf.get_variable_scope()
scope.reuse_variables()
variables_graph2 = tf.get_collection(tf.GraphKeys.WEIGHTS, scope='store_ResNet18')
return variables_graph,variables_graph2,scores,scores_stored
示例5: testAnalysisAndAllocations
def testAnalysisAndAllocations(self):
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
config = tf.ConfigProto(device_count={'CPU': 3})
with tf.Session(config=config) as sess:
with tf.device('/cpu:0'):
const1 = tf.constant(1.0, name='const1')
with tf.device('/cpu:1'):
const2 = tf.constant(2.0, name='const2')
with tf.device('/cpu:2'):
result = const1 + const2 + const1 * const2
sess.run(result, options=run_options, run_metadata=run_metadata)
self.assertTrue(run_metadata.HasField('step_stats'))
tl = timeline.Timeline(run_metadata.step_stats)
step_analysis = tl.analyze_step_stats()
ctf = step_analysis.chrome_trace.format_to_string()
self._validateTrace(ctf)
maximums = step_analysis.allocator_maximums
self.assertTrue('cpu' in maximums)
cpu_max = maximums['cpu']
# At least const1 + const2, both float32s (4 bytes each)
self.assertGreater(cpu_max.num_bytes, 8)
self.assertGreater(cpu_max.timestamp, 0)
self.assertTrue('const1' in cpu_max.tensors)
self.assertTrue('const2' in cpu_max.tensors)
示例6: testReturnsSingleCheckpointIfOneShardedCheckpoint
def testReturnsSingleCheckpointIfOneShardedCheckpoint(self):
checkpoint_dir = os.path.join(self.get_temp_dir(),
'one_checkpoint_found_sharded')
if not tf.gfile.Exists(checkpoint_dir):
tf.gfile.MakeDirs(checkpoint_dir)
global_step = tf.contrib.framework.get_or_create_global_step()
# This will result in 3 different checkpoint shard files.
with tf.device('/cpu:0'):
tf.Variable(10, name='v0')
with tf.device('/cpu:1'):
tf.Variable(20, name='v1')
saver = tf.train.Saver(sharded=True)
with tf.Session(
target='',
config=tf.ConfigProto(device_count={'CPU': 2})) as session:
session.run(tf.initialize_all_variables())
save_path = os.path.join(checkpoint_dir, 'model.ckpt')
saver.save(session, save_path, global_step=global_step)
num_found = 0
for _ in tf.contrib.training.checkpoints_iterator(
checkpoint_dir, timeout=0):
num_found += 1
self.assertEqual(num_found, 1)
示例7: pack_range
def pack_range(key, packing, grad_vars, rng):
"""Form the concatenation of a specified range of gradient tensors.
Args:
key: Value under which to store meta-data in packing that will be used
later to restore the grad_var list structure.
packing: Dict holding data describing packed ranges of small tensors.
grad_vars: List of (grad, var) pairs for one tower.
rng: A pair of integers giving the first, last indices of a consecutive
range of tensors to be packed.
Returns:
A tensor that is the concatenation of all the specified small tensors.
"""
to_pack = grad_vars[rng[0]:rng[1] + 1]
members = []
variables = []
restore_shapes = []
with tf.name_scope('pack'):
for g, v in to_pack:
variables.append(v)
restore_shapes.append(g.shape)
with tf.device(g.device):
members.append(tf.reshape(g, [-1]))
packing[key] = GradPackTuple(
indices=range(rng[0], rng[1] + 1),
vars=variables,
shapes=restore_shapes)
with tf.device(members[0].device):
return tf.concat(members, 0)
示例8: extract_features
def extract_features(ids, path, output_path, extractor, batch_size=64):
images_names = dict()
for p in listdir(path):
image_id = int(p.split('_')[-1].split('.')[0])
if image_id in ids:
images_names[image_id] = p
batch,names = [],[]
with open(output_path,'w') as output_file:
for idx,n in enumerate(images_names):
p = join(path, images_names[n])
batch.append(load_image(p))
names.append(n)
if len(batch)==batch_size:
batch = np.stack(batch)
feed_dict = {images: batch}
with tf.device('/gpu:0'):
features = sess.run(extractor, feed_dict=feed_dict)
for n,f in zip(names,features):
output_file.write("%s;%s\n" % (n, " ".join(str(x) for x in f)))
print("%d/%d" % (idx,len(images_names)))
batch, names = [],[]
output_file.flush()
if len(batch)>0:
batch = np.stack(batch)
feed_dict = {images: batch}
with tf.device('/gpu:0'):
features = sess.run(extractor, feed_dict=feed_dict)
for n,f in zip(names,features):
output_file.write("%s;%s\n" % (n, " ".join(str(x) for x in f)))
print("%d/%d" % (idx,len(images_names)))
output_file.flush()
示例9: __call__
def __call__(self, getter, name, *args, **kwargs):
staging_ops = self.variable_mgr.staging_vars_on_devices[self.device_num]
if name in staging_ops:
put_op, get_op = staging_ops[name]
return get_op
real_var = getter(name, *args, **kwargs)
shape = kwargs['shape']
dtype = kwargs['dtype']
trainable = kwargs['trainable']
if self.cpu_device:
with tf.device(self.cpu_device):
# This helps copying the weights from the parameter to this server only
# once.
if name in self.variable_mgr.staged_vars_on_cpu:
cpu_var = self.variable_mgr.staged_vars_on_cpu[name]
else:
cpu_var = tf.identity(real_var)
self.variable_mgr.staged_vars_on_cpu[name] = cpu_var
var_to_stage = cpu_var
else:
var_to_stage = tf.identity(real_var) # de-reference the variable.
with tf.device(self.devices[self.device_num]):
staging_area = data_flow_ops.StagingArea([dtype], shapes=[shape])
put_op = staging_area.put([var_to_stage])
get_op = staging_area.get()[0]
staging_ops[name] = (put_op, get_op)
if trainable:
# For trainable variables, they are managed separatedly through
# apply_gradients.
return get_op
else:
# For other shadow variables, the access is decoupled through a wrapper
# class.
return StagedModelVariable(real_var, get_op, self.variable_mgr)
示例10: create_weight_variables
def create_weight_variables(shape, seed, name, use_gpu=False):
"""
Create gaussian random neurons with mean 0 and std 0.1
**Paramters**
shape: Shape of the layer
"""
#import ipdb; ipdb.set_trace()
if len(shape) == 4:
in_out = shape[0] * shape[1] * shape[2] + shape[3]
else:
in_out = shape[0] + shape[1]
import math
stddev = math.sqrt(3.0 / in_out) # XAVIER INITIALIZER (GAUSSIAN)
initializer = tf.truncated_normal(shape, stddev=stddev, seed=seed)
if use_gpu:
with tf.device("/gpu"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
else:
with tf.device("/cpu"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
示例11: all_sync_params
def all_sync_params(tower_params, devices, usenccl=True):
"""Assigns the params from the first tower to all others"""
if len(devices) == 1:
return tf.no_op()
sync_ops = []
if have_nccl and usenccl:
for param_on_devices in zip(*tower_params):
# print('PARAM_ON_DEVICES: {}'.format(param_on_devices)) # DEBUG
# Note: param_on_devices is [paramX_gpu0, paramX_gpu1, ...]
param0 = param_on_devices[0]
send_op, received_tensors = nccl.broadcast(param0, devices[1:])
sync_ops.append(send_op)
for device, param, received in zip(devices[1:],
param_on_devices[1:],
received_tensors):
with tf.device(device):
sync_op = param.assign(received)
sync_ops.append(sync_op)
else:
params0 = tower_params[0]
for device, params in zip(devices, tower_params):
with tf.device(device):
for param, param0 in zip(params, params0):
sync_op = param.assign(param0.read_value())
sync_ops.append(sync_op)
return tf.group(*sync_ops)
示例12: train
def train():
hyperparams = {'batch_size': 50,
'learning_rate': 0.0001,
'grad_decay': 0.95,
'grad_epsilon': 0.01,
'num_updates': 20000,
'grad_norm_clip': 5}
with tf.device('/cpu:0'):
model = TradingSystemsModel(hyperparams)
loss = tb.Crossentropy(hyperparams)
acc = tb.CatAcc(hyperparams)
evaluator = tb.Evaluator(hyperparams, loss, acc)
optim = tb.RMSPropOptim(hyperparams)
trainer = tb.Trainer(model, hyperparams, loss, optim, evaluator)
split = 90000
data = np.load('data/trading-systems.npz')
print(data['ticks'].shape)
train_xs = {'ticks': data['ticks'][:split]}
train_y = data['targets'][:split]
val_xs = {'ticks': data['ticks'][split:]}
val_y = data['targets'][split:]
with tf.device('/cpu:0'):
trainer.train(train_xs, train_y,
val_xs, val_y,
val_cmp=True)
evaluator.eval(model, val_xs, val_y)
示例13: all_reduce_gradients
def all_reduce_gradients(tower_grads, devices):
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
split_grads = []
assert len(grad_and_vars) == FLAGS.num_workers
# Each GPU splits its own grad
for i, (g, _) in enumerate(grad_and_vars):
with tf.device(devices[i]):
split_grads.append(tf.split(0, FLAGS.num_workers, g))
# Each GPU gatheres slices of grad from other GPUs to do average.
for i, dev in enumerate(devices):
with tf.device(dev):
x = split_grads[i][i]
for j in range(FLAGS.num_workers):
if i == j:
continue
x += split_grads[j][i]
grads.append(x / FLAGS.num_workers)
grad = tf.concat(0, grads)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
示例14: _add_shared_train_op
def _add_shared_train_op(self):
"""Sets self._train_op, the op to run for training."""
# Take gradients of the trainable variables w.r.t. the loss function to minimize
if self._hps.rl_training or self._hps.ac_training:
loss_to_minimize = self._reinforce_shared_loss
if self._hps.coverage:
loss_to_minimize = self._reinforce_cov_total_loss
else:
loss_to_minimize = self._pgen_loss
if self._hps.coverage:
loss_to_minimize = self._pointer_cov_total_loss
tvars = tf.trainable_variables()
gradients = tf.gradients(loss_to_minimize, tvars, aggregation_method=tf.AggregationMethod.EXPERIMENTAL_TREE)
# Clip the gradients
with tf.device("/gpu:{}".format(self._hps.gpu_num)):
grads, global_norm = tf.clip_by_global_norm(gradients, self._hps.max_grad_norm)
# Add a summary
tf.summary.scalar('global_norm', global_norm)
# Apply adagrad optimizer
optimizer = tf.train.AdagradOptimizer(self._hps.lr, initial_accumulator_value=self._hps.adagrad_init_acc)
with tf.device("/gpu:{}".format(self._hps.gpu_num)):
self._shared_train_op = optimizer.apply_gradients(zip(grads, tvars), global_step=self.global_step, name='train_step')
示例15: _build_word_embeddings
def _build_word_embeddings(self):
n_tokens_vocab = self.options['n_tokens_vocab']
batch_size = self.options['batch_size']
unroll_steps = self.options['unroll_steps']
# LSTM options
projection_dim = self.options['lstm']['projection_dim']
# the input token_ids and word embeddings
self.token_ids = tf.placeholder(DTYPE_INT,
shape=(batch_size, unroll_steps),
name='token_ids')
# the word embeddings
with tf.device("/cpu:0"):
self.embedding_weights = tf.get_variable(
"embedding", [n_tokens_vocab, projection_dim],
dtype=DTYPE,
)
self.embedding = tf.nn.embedding_lookup(self.embedding_weights,
self.token_ids)
# if a bidirectional LM then make placeholders for reverse
# model and embeddings
if self.bidirectional:
self.token_ids_reverse = tf.placeholder(DTYPE_INT,
shape=(batch_size, unroll_steps),
name='token_ids_reverse')
with tf.device("/cpu:0"):
self.embedding_reverse = tf.nn.embedding_lookup(
self.embedding_weights, self.token_ids_reverse)