def feed_network(self,data,keep_prob,chunk_size,n_chunks,dynamic):
# This code is copied from tflearn
sequence_lengths = None
if dynamic:
sequence_lengths = net.calc_seqlenth(data if isinstance(data, tf.Tensor) else tf.stack(data))
batch_size = tf.shape(data)[0]
weight_dropout = tf.nn.dropout(self._layer_weights, keep_prob)
rnn_dropout = rnn.core_rnn_cell.DropoutWrapper(self._gru_cell,output_keep_prob=keep_prob)
# Calculation Begin
input_shape = data.get_shape().as_list()
ndim = len(input_shape)
axis = [1, 0] + list(range(2,ndim))
data = tf.transpose(data,(axis))
sequence = tf.unstack(data)
outputs, states = rnn.static_rnn(rnn_dropout, sequence, dtype=tf.float32, sequence_length = sequence_lengths)
if dynamic:
outputs = tf.transpose(tf.stack(outputs), [1, 0, 2])
output = net.advanced_indexing_op(outputs, sequence_lengths)
else:
output = outputs[-1]
output = tf.add(tf.matmul(output,weight_dropout), self._layer_biases)
return output
python类add()的实例源码
def triplet_loss(anchor, positive, negative, alpha):
"""Calculate the triplet loss according to the FaceNet paper
Args:
anchor: the embeddings for the anchor images.
positive: the embeddings for the positive images.
negative: the embeddings for the negative images.
Returns:
the triplet loss according to the FaceNet paper as a float tensor.
"""
with tf.variable_scope('triplet_loss'):
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)
basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)
return loss
def build_encoder(self):
"""Inference Network. q(h|X)"""
with tf.variable_scope("encoder"):
self.l1_lin = linear(tf.expand_dims(self.x, 0), self.embed_dim, bias=True, scope="l1")
self.l1 = tf.nn.relu(self.l1_lin)
self.l2_lin = linear(self.l1, self.embed_dim, bias=True, scope="l2")
self.l2 = tf.nn.relu(self.l2_lin)
self.mu = linear(self.l2, self.h_dim, bias=True, scope="mu")
self.log_sigma_sq = linear(self.l2, self.h_dim, bias=True, scope="log_sigma_sq")
self.eps = tf.random_normal((1, self.h_dim), 0, 1, dtype=tf.float32)
self.sigma = tf.sqrt(tf.exp(self.log_sigma_sq))
self.h = tf.add(self.mu, tf.mul(self.sigma, self.eps))
_ = tf.histogram_summary("mu", self.mu)
_ = tf.histogram_summary("sigma", self.sigma)
_ = tf.histogram_summary("h", self.h)
_ = tf.histogram_summary("mu + sigma", self.mu + self.sigma)
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:
# weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
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:
# weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def simulate_dynamics(initial_pos, initial_vel, stepsize, n_steps, energy_fn):
def leapfrog(pos, vel, step, i):
de_dp_ = tf.gradients(tf.reduce_sum(energy_fn(pos)), pos)[0]
new_vel_ = vel - step * de_dp_
new_pos_ = pos + step * new_vel_
return [new_pos_, new_vel_, step, tf.add(i, 1)]
def condition(pos, vel, step, i):
return tf.less(i, n_steps)
de_dp = tf.gradients(tf.reduce_sum(energy_fn(initial_pos)), initial_pos)[0]
vel_half_step = initial_vel - 0.5 * stepsize * de_dp
pos_full_step = initial_pos + stepsize * vel_half_step
i = tf.constant(0)
final_pos, new_vel, _, _ = tf.while_loop(condition, leapfrog, [pos_full_step, vel_half_step, stepsize, i])
de_dp = tf.gradients(tf.reduce_sum(energy_fn(final_pos)), final_pos)[0]
final_vel = new_vel - 0.5 * stepsize * de_dp
return final_pos, final_vel
def compile(self, in_x, train_feed, eval_feed):
n = np.product(self.in_d)
m, param_init_fn = [dom[i] for (dom, i) in zip(self.domains, self.chosen)]
#sc = np.sqrt(6.0) / np.sqrt(m + n)
#W = tf.Variable(tf.random_uniform([n, m], -sc, sc))
W = tf.Variable( param_init_fn( [n, m] ) )
b = tf.Variable(tf.zeros([m]))
# if the number of input dimensions is larger than one, flatten the
# input and apply the affine transformation.
if len(self.in_d) > 1:
in_x_flat = tf.reshape(in_x, shape=[-1, n])
out_y = tf.add(tf.matmul(in_x_flat, W), b)
else:
out_y = tf.add(tf.matmul(in_x, W), b)
return out_y
# computes the output dimension based on the padding scheme used.
# this comes from the tensorflow documentation
def compile(self, in_x, train_feed, eval_feed):
in_height, in_width, in_nchannels = self.in_d
nfilters, filter_len, stride, padding, param_init_fn = [dom[i]
for (dom, i) in zip(self.domains, self.chosen)]
# Creation and initialization of the parameters. Should take size of
# the filter into account.
W = tf.Variable(
param_init_fn( [filter_len, filter_len, in_nchannels, nfilters]) )
b = tf.Variable(tf.zeros([nfilters]))
# create the output and add the bias.
out_yaux = tf.nn.conv2d(in_x, W, strides=[1, stride, stride, 1], padding=padding)
out_y = tf.nn.bias_add(out_yaux, b)
#print(in_x.get_shape(), self.get_outdim(), out_y.get_shape())
return out_y
def _shortcut(inputs, x): # x = f(inputs)
# shortcut path
_, inputs_h, inputs_w, inputs_ch = inputs.shape.as_list()
_, x_h, x_w, x_ch = x.shape.as_list()
stride_h = int(round(inputs_h / x_h))
stride_w = int(round(inputs_w / x_w))
equal_ch = inputs_ch == x_ch
if stride_h>1 or stride_w>1 or not equal_ch:
shortcut = tcl.conv2d(inputs,
num_outputs = x_ch,
kernel_size = (1, 1),
stride = (stride_h, stride_w),
padding = 'VALID')
else:
shortcut = inputs
merged = tf.add(shortcut, x)
return merged
def l1_l2_regularizer(weight_l1=1.0, weight_l2=1.0, scope=None):
"""Define a L1L2 regularizer.
Args:
weight_l1: scale the L1 loss by this factor.
weight_l2: scale the L2 loss by this factor.
scope: Optional scope for op_scope.
Returns:
a regularizer function.
"""
def regularizer(tensor):
with tf.op_scope([tensor], scope, 'L1L2Regularizer'):
weight_l1_t = tf.convert_to_tensor(weight_l1,
dtype=tensor.dtype.base_dtype,
name='weight_l1')
weight_l2_t = tf.convert_to_tensor(weight_l2,
dtype=tensor.dtype.base_dtype,
name='weight_l2')
reg_l1 = tf.mul(weight_l1_t, tf.reduce_sum(tf.abs(tensor)),
name='value_l1')
reg_l2 = tf.mul(weight_l2_t, tf.nn.l2_loss(tensor),
name='value_l2')
return tf.add(reg_l1, reg_l2, name='value')
return regularizer
a2_transformer_classification.py 文件源码
项目:text_classification
作者: brightmart
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def inference(self):
""" building blocks:
encoder:6 layers.each layers has two sub-layers. the first is multi-head self-attention mechanism; the second is position-wise fully connected feed-forward network.
for each sublayer. use LayerNorm(x+Sublayer(x)). all dimension=512.
decoder:6 layers.each layers has three sub-layers. the second layer is performs multi-head attention over the ouput of the encoder stack.
for each sublayer. use LayerNorm(x+Sublayer(x)).
"""
# 1.embedding for encoder input & decoder input
# 1.1 position embedding for encoder input
input_x_embeded = tf.nn.embedding_lookup(self.Embedding,self.input_x) #[None,sequence_length, embed_size]
input_x_embeded=tf.multiply(input_x_embeded,tf.sqrt(tf.cast(self.d_model,dtype=tf.float32)))
input_mask=tf.get_variable("input_mask",[self.sequence_length,1],initializer=self.initializer)
input_x_embeded=tf.add(input_x_embeded,input_mask) #[None,sequence_length,embed_size].position embedding.
# 2. encoder
encoder_class=Encoder(self.d_model,self.d_k,self.d_v,self.sequence_length,self.h,self.batch_size,self.num_layer,input_x_embeded,input_x_embeded,dropout_keep_prob=self.dropout_keep_prob,use_residual_conn=self.use_residual_conn)
Q_encoded,K_encoded = encoder_class.encoder_fn() #K_v_encoder
Q_encoded=tf.reshape(Q_encoded,shape=(self.batch_size,-1)) #[batch_size,sequence_length*d_model]
with tf.variable_scope("output"):
logits = tf.matmul(Q_encoded, self.W_projection) + self.b_projection #logits shape:[batch_size*decoder_sent_length,self.num_classes]
print("logits:",logits)
return logits
def kSparse(self, x, topk):
print 'run regular k-sparse'
dim = int(x.get_shape()[1])
if topk > dim:
warnings.warn('Warning: topk should not be larger than dim: %s, found: %s, using %s' % (dim, topk, dim))
topk = dim
k = dim - topk
values, indices = tf.nn.top_k(-x, k) # indices will be [[0, 1], [2, 1]], values will be [[6., 2.], [5., 4.]]
# We need to create full indices like [[0, 0], [0, 1], [1, 2], [1, 1]]
my_range = tf.expand_dims(tf.range(0, tf.shape(indices)[0]), 1) # will be [[0], [1]]
my_range_repeated = tf.tile(my_range, [1, k]) # will be [[0, 0], [1, 1]]
full_indices = tf.stack([my_range_repeated, indices], axis=2) # change shapes to [N, k, 1] and [N, k, 1], to concatenate into [N, k, 2]
full_indices = tf.reshape(full_indices, [-1, 2])
to_reset = tf.sparse_to_dense(full_indices, tf.shape(x), tf.reshape(values, [-1]), default_value=0., validate_indices=False)
res = tf.add(x, to_reset)
return res
def __init__(self,T,train_mode=1,name='srResNet'):
with tf.variable_scope(name):
self.train_mode=train_mode
conv1=conv_layer(T,[5,5,3,64],1)
relu1=leaky_relu(conv1)
block=[]
for i in xrange(16):
block.append(self.residual_block(block[-1] if i else relu1))
conv2=conv_layer(block[-1],[3,3,64,64],1)
bn1=batch_norm(conv2) if self.train_mode else conv2
sum1=tf.add(bn1,relu1)
conv3=conv_layer(sum1,[3,3,64,256],1)
ps1=tf.depth_to_space(conv3,2) #pixel-shuffle
relu2=leaky_relu(ps1)
conv4=conv_layer(relu2,[3,3,64,256],1)
ps2=tf.depth_to_space(conv4,2)
relu3=leaky_relu(ps2)
self.conv5=conv_layer(relu3,[3,3,64,3],1)
def feed_network(self,data,keep_prob,chunk_size,n_chunks, dynamic):
# This code is copied from tflearn
sequence_lengths = None
if dynamic:
sequence_lengths = net.calc_seqlenth(data if isinstance(data, tf.Tensor) else tf.stack(data))
batch_size = tf.shape(data)[0]
weight_dropout = tf.nn.dropout(self._layer_weights, keep_prob)
rnn_dropout = rnn.core_rnn_cell.DropoutWrapper(self._lstm_cell,output_keep_prob=keep_prob)
# Calculation Begin
input_shape = data.get_shape().as_list()
ndim = len(input_shape)
axis = [1, 0] + list(range(2,ndim))
data = tf.transpose(data,(axis))
sequence = tf.unstack(data)
outputs, states = rnn.static_rnn(rnn_dropout, sequence, dtype=tf.float32, sequence_length = sequence_lengths)
if dynamic:
outputs = tf.transpose(tf.stack(outputs), [1, 0, 2])
output = net.advanced_indexing_op(outputs, sequence_lengths)
else:
output = outputs[-1]
output = tf.add(tf.matmul(output,weight_dropout), self._layer_biases)
return output
def variable_with_weight_decay(name, shape, stddev, wd):
"""
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.
Rtns:
var -> 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.mul(tf.nn.l2_loss(var),wd,name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def add_layers(inputs, in_size, out_size, layer_name, keep_prob, activation_function=None):
# add one more layer and return the output of this layer
weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
wx_plus_b = tf.matmul(inputs, weights) + biases
# here to dropout
# ? wx_plus_b ?drop?????
# keep_prob ??????drop?????? sess.run ? feed
wx_plus_b = tf.nn.dropout(wx_plus_b, keep_prob)
if activation_function is None:
outputs = wx_plus_b
else:
outputs = activation_function(wx_plus_b)
tf.histogram_summary(layer_name + '/outputs', outputs)
return outputs
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
# ?????????? layer???? ???
with tf.name_scope('layer'):
# ??????
with tf.name_scope('weights_1'):
weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
with tf.name_scope('biases_1'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
with tf.name_scope('wx_plus_b'):
wx_plus_b = tf.add(tf.matmul(inputs, weights), biases)
# here to dropout, ? wx_plus_b ?drop?????, keep_prob ??????drop?????? sess.run ? feed
wx_plus_b = tf.nn.dropout(wx_plus_b, keep_prob=1)
if activation_function is None:
outputs = wx_plus_b
else:
outputs = activation_function(wx_plus_b, )
return outputs
# define placeholder for inputs to network
# ?????????? inputs x?y
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['w1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Create a summary to visualize the first layer ReLU activation
tf.summary.histogram("relu1", layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['w2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Create another summary to visualize the second layer ReLU activation
tf.summary.histogram("relu2", layer_2)
# Output layer
out_layer = tf.add(tf.matmul(layer_2, weights['w3']), biases['b3'])
return out_layer
# Store layers weight & bias
def _get_layer(self, layer_input, size_last_layer, size_current_layer):
"""
Returns a layer with a batch normalized input, depending on the `batch_norm flag`
@param layer_input is the value used as an input to the layer.
@param size_last_layer is the size of the last layer (used in weight) or the size of the input
@param size_current_layer is the size of the current layer (used in weight and bias)
"""
weight = tf.Variable(tf.random_normal([size_last_layer, size_current_layer]))
bias = tf.Variable(tf.random_normal([size_current_layer]))
if not self.batch_norm:
return self.activation_func(tf.add(tf.matmul(layer_input, weight), bias))
layer_input = tf.contrib.layers.batch_norm(layer_input,
center=True, scale=True,
is_training=self.is_training,
scope='bn{}-{}'.format(size_last_layer, size_current_layer))
return self.activation_func(tf.add(tf.matmul(layer_input, weight), bias))
def test_forward_declarations(self):
# Define a simple expression data structure
nlit = lambda x: {'op': 'lit', 'val': x}
nadd = lambda x, y: {'op': 'add', 'left': x, 'right': y}
nexpr = nadd(nadd(nlit(3.0), nlit(5.0)), nlit(2.0))
# Define a recursive block using forward declarations
expr_fwd = tdb.ForwardDeclaration(tdt.PyObjectType(),
tdt.TensorType((), 'float32'))
lit_case = tdb.GetItem('val') >> tdb.Scalar()
add_case = (tdb.Record({'left': expr_fwd(), 'right': expr_fwd()})
>> tdb.Function(tf.add))
expr = tdb.OneOf(lambda x: x['op'], {'lit': lit_case, 'add': add_case})
expr_fwd.resolve_to(expr)
self.assertBuilds(10.0, expr, nexpr, max_depth=2)
def test_constant_network_with_tags(self):
shape1 = loom.TypeShape('int64', (3,), 'alpha')
shape2 = loom.TypeShape('int64', (3,), 'beta')
value1 = np.array([1, 2, 3], dtype='int64')
value2 = np.array([4, 5, 6], dtype='int64')
ops = {'add1': BinaryLoomOp(shape1, tf.add),
'add2': BinaryLoomOp(shape2, tf.add)}
the_loom = loom.Loom(named_ops=ops)
output_tensor1 = the_loom.output_tensor(shape1)
output_tensor2 = the_loom.output_tensor(shape2)
with self.test_session():
weaver = the_loom.make_weaver()
c1 = weaver(value1, tag='alpha')
c2 = weaver(value2, tag='beta')
result1 = output_tensor1.eval(
feed_dict=weaver.build_feed_dict([c2, c1]))
result2 = output_tensor2.eval(
feed_dict=weaver.build_feed_dict([c2, c1]))
self.assertTrue((result1[0] == value1).all())
self.assertTrue((result2[0] == value2).all())
def test_constant_network_with_tags_dry_run(self):
shape1 = loom.TypeShape('int64', (3,), 'alpha')
shape2 = loom.TypeShape('int64', (3,), 'beta')
value1 = np.array([1, 2, 3], dtype='int64')
value2 = np.array([4, 5, 6], dtype='int64')
ops = {'add1': BinaryLoomOp(shape1, tf.add),
'add2': BinaryLoomOp(shape2, tf.add)}
the_loom = loom.Loom(named_ops=ops, dry_run=True)
output_tensor1 = the_loom.output_tensor(shape1)
output_tensor2 = the_loom.output_tensor(shape2)
with self.test_session():
weaver = the_loom.make_weaver()
c1 = weaver(value1, tag='alpha')
c2 = weaver(value2, tag='beta')
result1 = output_tensor1.eval(
feed_dict=weaver.build_feed_dict([c2, c1]))
result2 = output_tensor2.eval(
feed_dict=weaver.build_feed_dict([c2, c1]))
zero_vec = np.zeros_like(value1)
self.assertTrue((result1[0] == zero_vec).all())
self.assertTrue((result2[0] == zero_vec).all())
def test_two_layer_sum_network(self):
shape = loom.TypeShape('int64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add)}
the_loom = loom.Loom(named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
c1 = weaver(np.array([1, 2, 3], dtype='int64'))
c2 = weaver(np.array([2, 4, 6], dtype='int64'))
c3 = weaver(np.array([3, 6, 9], dtype='int64'))
c4 = weaver(np.array([4, 8, 12], dtype='int64'))
sum_1_2 = weaver.add(c1, c2)
sum_3_4 = weaver.add(c3, c4)
sum_1_2_3_4 = weaver.add(sum_1_2, sum_3_4)
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([sum_1_2_3_4]))
self.assertTrue((result == np.array([[10, 20, 30]], dtype='int64')).all())
def test_three_layer_sum_network(self):
shape = loom.TypeShape('int64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add)}
the_loom = loom.Loom(named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
vals = [weaver(np.array([0, 1, 1 << k], dtype='int64'))
for k in range(8)]
for _ in xrange(3):
vals = [weaver.add(*args) for args in group_values(vals, 2)]
big_sum = vals[0]
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([big_sum]))
self.assertTrue((result == np.array([[0, 8, 255]], dtype='int64')).all())
def test_two_ops_network(self):
shape = loom.TypeShape('int64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
c1 = weaver(np.array([1, 2, 3], dtype='int64'))
c2 = weaver(np.array([2, 4, 6], dtype='int64'))
c3 = weaver(np.array([3, 6, 9], dtype='int64'))
sum_2_3 = weaver.add(c2, c3)
sum_12_13 = weaver.mul(c1, sum_2_3)
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([sum_12_13]))
self.assertTrue((result == np.array([[5, 20, 45]], dtype='int64')).all())
def test_two_ops_network_tagged_named_tensorx(self):
shape = loom.TypeShape('int64', (3,), tag='x')
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
named_tensors = {
'c1': (tf.constant(np.array([1, 2, 3], dtype='int64')), 'x'),
'c2': (tf.constant(np.array([2, 4, 6], dtype='int64')), 'x'),
'c3': (tf.constant(np.array([3, 6, 9], dtype='int64')), 'x')
}
the_loom = loom.Loom(named_ops=ops, named_tensors=named_tensors)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
sum_2_3 = weaver.add(weaver.c2, weaver.c3)
sum_12_13 = weaver.mul(weaver.c1, sum_2_3)
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([sum_12_13]))
self.assertTrue((result == np.array([[5, 20, 45]], dtype='int64')).all())
def test_gradient(self):
x_var = tf.Variable(tf.zeros([3], dtype='float64'), name='x')
shape = loom.TypeShape('float64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_tensors={'x': x_var}, named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
output = tf.reduce_sum(output_tensor)
gradient = tf.gradients(output, [x_var])[0]
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
weaver = the_loom.make_weaver()
m = weaver(np.array([1, 2, 3], dtype='float64'))
b = weaver(np.array([47, 9, -1], dtype='float64'))
mx = weaver.mul(m, weaver.x)
mx_plus_b = weaver.add(mx, b)
result = gradient.eval(feed_dict=weaver.build_feed_dict([mx_plus_b]))
self.assertTrue((result == np.array(
[1.0, 2.0, 3.0], dtype='float64')).all())
def test_gradient_with_direct_feed_dict(self):
x_var = tf.Variable(tf.zeros([3], dtype='float64'), name='x')
shape = loom.TypeShape('float64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_tensors={'x': x_var}, named_ops=ops,
direct_feed_dict=True)
output_tensor = the_loom.output_tensor(shape)
output = tf.reduce_sum(output_tensor)
gradient = tf.gradients(output, [x_var])[0]
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
weaver = the_loom.make_weaver()
m = weaver(np.array([1, 2, 3], dtype='float64'))
b = weaver(np.array([47, 9, -1], dtype='float64'))
mx = weaver.mul(m, weaver.x)
mx_plus_b = weaver.add(mx, b)
result = gradient.eval(feed_dict=weaver.build_feed_dict([mx_plus_b]))
self.assertTrue((result == np.array(
[1.0, 2.0, 3.0], dtype='float64')).all())
def create_generator_loss(disc_output, gene_output, features):
# I.e. did we fool the discriminator?
cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=disc_output, logits=tf.ones_like(disc_output))
gene_ce_loss = tf.reduce_mean(cross_entropy, name='gene_ce_loss')
# I.e. does the result look like the feature?
K = int(gene_output.get_shape()[1])//int(features.get_shape()[1])
assert K == 2 or K == 4 or K == 8
downscaled = _downscale(gene_output, K)
gene_l1_loss = tf.reduce_mean(tf.abs(downscaled - features), name='gene_l1_loss')
gene_loss = tf.add((1.0 - FLAGS.gene_l1_factor) * gene_ce_loss,
FLAGS.gene_l1_factor * gene_l1_loss, name='gene_loss')
return gene_loss
def make_feature_columns():
"""Retrieve the feature columns required for training."""
feature_columns = (make_query_feature_columns()
| make_candidate_feature_columns())
# Add feature column for the label.
target_rating_real_column = tf.contrib.layers.real_valued_column(
column_name=LABEL_RATING_SCORE, dtype=tf.float32)
feature_columns.add(target_rating_real_column)
# Ranking candidate movies used only in eval graph to rank candidate movie
# against.
ranking_candidate_movie_ids = (
tf.contrib.layers.sparse_column_with_integerized_feature(
column_name=RANKING_CANDIDATE_MOVIE_IDS,
bucket_size=MOVIE_VOCAB_SIZE))
feature_columns.add(ranking_candidate_movie_ids)
return feature_columns