本文整理匯總了Python中sugartensor.sg_context方法的典型用法代碼示例。如果您正苦於以下問題:Python sugartensor.sg_context方法的具體用法?Python sugartensor.sg_context怎麽用?Python sugartensor.sg_context使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sugartensor
的用法示例。
在下文中一共展示了sugartensor.sg_context方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: discriminator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def discriminator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('discriminator')]) > 0
with tf.sg_context(name='discriminator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
res = (tensor
.sg_conv(dim=64, name='conv1')
.sg_conv(dim=128, name='conv2')
.sg_flatten()
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=1, act='linear', bn=False, name='fc2')
.sg_squeeze())
return res
#
# inputs
#
# MNIST input tensor ( with QueueRunner )
示例2: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
# generator network
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
# random normal seed
示例3: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
#
# inputs
#
# target_number
示例4: get_loss
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def get_loss(opt):
# conv layers
with tf.sg_context(name='convs', act='relu', bn=True):
conv = (opt.input[opt.gpu_index]
.sg_conv(dim=16, name='conv1')
.sg_pool()
.sg_conv(dim=32, name='conv2')
.sg_pool()
.sg_conv(dim=32, name='conv3')
.sg_pool())
# fc layers
with tf.sg_context(name='fcs', act='relu', bn=True):
logit = (conv
.sg_flatten()
.sg_dense(dim=256, name='fc1')
.sg_dense(dim=10, act='linear', bn=False, name='fc2'))
# cross entropy loss with logit
return logit.sg_ce(target=opt.target[opt.gpu_index])
# parallel training ( same as single GPU training )
示例5: sg_res_block
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def sg_res_block(tensor, opt):
# default rate
opt += tf.sg_opt(size=3, rate=1, causal=False, is_first=False)
# input dimension
in_dim = tensor.get_shape().as_list()[-1]
with tf.sg_context(name='block_%d_%d' % (opt.block, opt.rate)):
# reduce dimension
input_ = (tensor
.sg_bypass(act='relu', ln=(not opt.is_first), name='bypass') # do not
.sg_conv1d(size=1, dim=in_dim/2, act='relu', ln=True, name='conv_in'))
# 1xk conv dilated
out = (input_
.sg_aconv1d(size=opt.size, rate=opt.rate, causal=opt.causal, act='relu', ln=True, name='aconv'))
# dimension recover and residual connection
out = out.sg_conv1d(size=1, dim=in_dim, name='conv_out') + tensor
return out
# inject residual multiplicative block
示例6: encode
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def encode(x):
with tf.sg_context(name='encoder'):
res = x
# loop dilated conv block
for i in range(num_blocks):
res = (res
.sg_res_block(size=5, block=i, rate=1, is_first=True)
.sg_res_block(size=5, block=i, rate=2)
.sg_res_block(size=5, block=i, rate=4)
.sg_res_block(size=5, block=i, rate=8)
.sg_res_block(size=5, block=i, rate=16))
return res
#
# decode graph ( causal convolution )
#
示例7: decode
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def decode(x, voca_size):
with tf.sg_context(name='decoder'):
res = x
# loop dilated causal conv block
for i in range(num_blocks):
res = (res
.sg_res_block(size=3, block=i, rate=1, causal=True, is_first=True)
.sg_res_block(size=3, block=i, rate=2, causal=True)
.sg_res_block(size=3, block=i, rate=4, causal=True)
.sg_res_block(size=3, block=i, rate=8, causal=True)
.sg_res_block(size=3, block=i, rate=16, causal=True))
# final fully convolution layer for softmax
res = res.sg_conv1d(size=1, dim=voca_size, name='conv_final')
return res
示例8: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='relu', bn=True, reuse=reuse):
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
#
# inputs
#
# target_number
示例9: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(x):
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
# generator network
res = (x.sg_dense(dim=1024, name='fc_1')
.sg_dense(dim=7*7*128, name='fc_2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv_1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv_2'))
return res
#
# create discriminator
#
示例10: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
# generator network
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
示例11: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='leaky_relu', bn=True, reuse=reuse):
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
示例12: _resnet_graph
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def _resnet_graph(x, opt, nums):
# default option
opt += tf.sg_opt(num_class=1000, conv_only=False, squeeze=True, act='relu')
# convolution layers ( residual net v2 arch )
with tf.sg_context(name=opt.name):
conv = (x
.sg_conv(dim=64, size=7, stride=2, bias=False, reuse=opt.reuse, name='conv1')
.sg_pool(size=3, stride=2, pad='SAME')
.sg_resnet_layer(dim=64, num=nums[0], stride=1, act=opt.act, reuse=opt.reuse, name='conv2')
.sg_resnet_layer(dim=128, num=nums[1], stride=2, act=opt.act, reuse=opt.reuse, name='conv3')
.sg_resnet_layer(dim=256, num=nums[2], stride=2, act=opt.act, reuse=opt.reuse, name='conv4')
.sg_resnet_layer(dim=512, num=nums[3], stride=2, act=opt.act, reuse=opt.reuse, name='conv5')
.sg_bypass(act=opt.act, bn=True, reuse=opt.reuse, name='final_act') # final activation
.sg_pool(size=7, stride=1, avg=True)) # global average pool
# fully convolution layers
fc = conv.sg_conv(dim=opt.num_class, size=1, act='linear', bn=False, reuse=opt.reuse, name='fc')
# return selectively
if opt.conv_only:
return conv
else:
if opt.squeeze:
return fc.sg_squeeze(axis=(1, 2))
else:
return fc
#
# Dense net utility
#
示例13: sg_parallel
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def sg_parallel(func):
r"""Decorates function as multiple gpu support towers.
Args:
func: function to decorate
"""
@wraps(func)
def wrapper(**kwargs):
r"""Manages arguments of `tf.sg_opt`.
Args:
kwargs: keyword arguments. The wrapped function will be provided with gpu_index argument.
"""
# parse option
opt = tf.sg_opt(kwargs)
# loop for all available GPUs
res = []
for i in range(sg_gpus()):
# specify device
with tf.device('/gpu:%d' % i):
# give new scope only to operation
with tf.name_scope('gpu_%d' % i):
# save reuse flag
with sg_context(reuse=(True if i > 0 else False)):
# call function
res.append(func(opt * tf.sg_opt(gpu_index=i)))
return res
return wrapper
#
# Command line argument util funcs
#
# noinspection PyProtectedMember
示例14: generator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def generator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('generator')]) > 0
with tf.sg_context(name='generator', size=4, stride=2, act='relu', bn=True, reuse=reuse):
res = (tensor
.sg_dense(dim=1024, name='fc1')
.sg_dense(dim=7*7*128, name='fc2')
.sg_reshape(shape=(-1, 7, 7, 128))
.sg_upconv(dim=64, name='conv1')
.sg_upconv(dim=1, act='sigmoid', bn=False, name='conv2'))
return res
示例15: discriminator
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_context [as 別名]
def discriminator(tensor):
# reuse flag
reuse = len([t for t in tf.global_variables() if t.name.startswith('discriminator')]) > 0
with tf.sg_context(name='discriminator', size=4, stride=2, act='leaky_relu', reuse=reuse):
# shared part
shared = (tensor
.sg_conv(dim=64, name='conv1')
.sg_conv(dim=128, name='conv2')
.sg_flatten()
.sg_dense(dim=1024, name='fc1'))
# discriminator end
disc = shared.sg_dense(dim=1, act='linear', name='disc').sg_squeeze()
# shared recognizer part
recog_shared = shared.sg_dense(dim=128, name='recog')
# categorical auxiliary classifier end
cat = recog_shared.sg_dense(dim=cat_dim, act='linear', name='cat')
# continuous auxiliary classifier end
con = recog_shared.sg_dense(dim=con_dim, act='sigmoid', name='con')
return disc, cat, con
#
# inputs
#
# MNIST input tensor ( with QueueRunner )