desc
stringlengths 3
26.7k
| decl
stringlengths 11
7.89k
| bodies
stringlengths 8
553k
|
---|---|---|
'Instruction to download and extract the tarball from Flowers website.'
| def download_message(self):
| print(('Failed to find any ImageNet %s files' % self.subset))
print('')
print('If you have already downloaded and processed the data, then make sure to set --data_dir to point to the directory containing the location of the sharded TFRecords.\n')
print('If you have not downloaded and prepared the ImageNet data in the TFRecord format, you will need to do this at least once. This process could take several hours depending on the speed of your computer and network connection\n')
print('Please see README.md for instructions on how to build the ImageNet dataset using download_and_preprocess_imagenet.\n')
print('Note that the raw data size is 300 GB and the processed data size is 150 GB. Please ensure you have at least 500GB disk space.')
|
'Constructs classifier graph from inputs to classifier loss.
* Caches the VatxtInput object in `self.cl_inputs`
* Caches tensors: `cl_embedded`, `cl_logits`, `cl_loss`
Returns:
loss: scalar float.'
| def classifier_graph(self):
| inputs = _inputs('train', pretrain=False)
self.cl_inputs = inputs
embedded = self.layers['embedding'](inputs.tokens)
self.tensors['cl_embedded'] = embedded
(_, next_state, logits, loss) = self.cl_loss_from_embedding(embedded, return_intermediates=True)
tf.summary.scalar('classification_loss', loss)
self.tensors['cl_logits'] = logits
self.tensors['cl_loss'] = loss
acc = layers_lib.accuracy(logits, inputs.labels, inputs.weights)
tf.summary.scalar('accuracy', acc)
adv_loss = (self.adversarial_loss() * tf.constant(FLAGS.adv_reg_coeff, name='adv_reg_coeff'))
tf.summary.scalar('adversarial_loss', adv_loss)
total_loss = (loss + adv_loss)
tf.summary.scalar('total_classification_loss', total_loss)
with tf.control_dependencies([inputs.save_state(next_state)]):
total_loss = tf.identity(total_loss)
return total_loss
|
'Constructs LM graph from inputs to LM loss.
* Caches the VatxtInput object in `self.lm_inputs`
* Caches tensors: `lm_embedded`
Args:
compute_loss: bool, whether to compute and return the loss or stop after
the LSTM computation.
Returns:
loss: scalar float.'
| def language_model_graph(self, compute_loss=True):
| inputs = _inputs('train', pretrain=True)
self.lm_inputs = inputs
return self._lm_loss(inputs, compute_loss=compute_loss)
|
'Constructs classifier evaluation graph.
Args:
dataset: the labeled dataset to evaluate, {\'train\', \'test\', \'valid\'}.
Returns:
eval_ops: dict<metric name, tuple(value, update_op)>
var_restore_dict: dict mapping variable restoration names to variables.
Trainable variables will be mapped to their moving average names.'
| def eval_graph(self, dataset='test'):
| inputs = _inputs(dataset, pretrain=False)
embedded = self.layers['embedding'](inputs.tokens)
(_, next_state, logits, _) = self.cl_loss_from_embedding(embedded, inputs=inputs, return_intermediates=True)
eval_ops = {'accuracy': tf.contrib.metrics.streaming_accuracy(layers_lib.predictions(logits), inputs.labels, inputs.weights)}
with tf.control_dependencies([inputs.save_state(next_state)]):
(acc, acc_update) = eval_ops['accuracy']
acc_update = tf.identity(acc_update)
eval_ops['accuracy'] = (acc, acc_update)
var_restore_dict = make_restore_average_vars_dict()
return (eval_ops, var_restore_dict)
|
'Compute classification loss from embedding.
Args:
embedded: 3-D float Tensor [batch_size, num_timesteps, embedding_dim]
inputs: VatxtInput, defaults to self.cl_inputs.
return_intermediates: bool, whether to return intermediate tensors or only
the final loss.
Returns:
If return_intermediates is True:
lstm_out, next_state, logits, loss
Else:
loss'
| def cl_loss_from_embedding(self, embedded, inputs=None, return_intermediates=False):
| if (inputs is None):
inputs = self.cl_inputs
(lstm_out, next_state) = self.layers['lstm'](embedded, inputs.state, inputs.length)
logits = self.layers['cl_logits'](lstm_out)
loss = layers_lib.classification_loss(logits, inputs.labels, inputs.weights)
if return_intermediates:
return (lstm_out, next_state, logits, loss)
else:
return loss
|
'Compute adversarial loss based on FLAGS.adv_training_method.'
| def adversarial_loss(self):
| def random_perturbation_loss():
return adv_lib.random_perturbation_loss(self.tensors['cl_embedded'], self.cl_inputs.length, self.cl_loss_from_embedding)
def adversarial_loss():
return adv_lib.adversarial_loss(self.tensors['cl_embedded'], self.tensors['cl_loss'], self.cl_loss_from_embedding)
def virtual_adversarial_loss():
"Computes virtual adversarial loss.\n\n Uses lm_inputs and constructs the language model graph if it hasn't yet\n been constructed.\n\n Also ensures that the LM input states are saved for LSTM state-saving\n BPTT.\n\n Returns:\n loss: float scalar.\n "
if (self.lm_inputs is None):
self.language_model_graph(compute_loss=False)
def logits_from_embedding(embedded, return_next_state=False):
(_, next_state, logits, _) = self.cl_loss_from_embedding(embedded, inputs=self.lm_inputs, return_intermediates=True)
if return_next_state:
return (next_state, logits)
else:
return logits
(next_state, lm_cl_logits) = logits_from_embedding(self.tensors['lm_embedded'], return_next_state=True)
va_loss = adv_lib.virtual_adversarial_loss(lm_cl_logits, self.tensors['lm_embedded'], self.lm_inputs, logits_from_embedding)
with tf.control_dependencies([self.lm_inputs.save_state(next_state)]):
va_loss = tf.identity(va_loss)
return va_loss
def combo_loss():
return (adversarial_loss() + virtual_adversarial_loss())
adv_training_methods = {'rp': random_perturbation_loss, 'at': adversarial_loss, 'vat': virtual_adversarial_loss, 'atvat': combo_loss, '': (lambda : tf.constant(0.0)), None: (lambda : tf.constant(0.0))}
with tf.name_scope('adversarial_loss'):
return adv_training_methods[FLAGS.adv_training_method]()
|
'Constructs classifier graph from inputs to classifier loss.
* Caches the VatxtInput objects in `self.cl_inputs`
* Caches tensors: `cl_embedded` (tuple of forward and reverse), `cl_logits`,
`cl_loss`
Returns:
loss: scalar float.'
| def classifier_graph(self):
| inputs = _inputs('train', pretrain=False, bidir=True)
self.cl_inputs = inputs
(f_inputs, _) = inputs
embedded = [self.layers['embedding'](inp.tokens) for inp in inputs]
self.tensors['cl_embedded'] = embedded
(_, next_states, logits, loss) = self.cl_loss_from_embedding(embedded, return_intermediates=True)
tf.summary.scalar('classification_loss', loss)
self.tensors['cl_logits'] = logits
self.tensors['cl_loss'] = loss
acc = layers_lib.accuracy(logits, f_inputs.labels, f_inputs.weights)
tf.summary.scalar('accuracy', acc)
adv_loss = (self.adversarial_loss() * tf.constant(FLAGS.adv_reg_coeff, name='adv_reg_coeff'))
tf.summary.scalar('adversarial_loss', adv_loss)
total_loss = (loss + adv_loss)
tf.summary.scalar('total_classification_loss', total_loss)
saves = [inp.save_state(state) for (inp, state) in zip(inputs, next_states)]
with tf.control_dependencies(saves):
total_loss = tf.identity(total_loss)
return total_loss
|
'Constructs forward and reverse LM graphs from inputs to LM losses.
* Caches the VatxtInput objects in `self.lm_inputs`
* Caches tensors: `lm_embedded`, `lm_embedded_reverse`
Args:
compute_loss: bool, whether to compute and return the loss or stop after
the LSTM computation.
Returns:
loss: scalar float, sum of forward and reverse losses.'
| def language_model_graph(self, compute_loss=True):
| inputs = _inputs('train', pretrain=True, bidir=True)
self.lm_inputs = inputs
(f_inputs, r_inputs) = inputs
f_loss = self._lm_loss(f_inputs, compute_loss=compute_loss)
r_loss = self._lm_loss(r_inputs, emb_key='lm_embedded_reverse', lstm_layer='lstm_reverse', lm_loss_layer='lm_loss_reverse', loss_name='lm_loss_reverse', compute_loss=compute_loss)
if compute_loss:
return (f_loss + r_loss)
|
'Constructs classifier evaluation graph.
Args:
dataset: the labeled dataset to evaluate, {\'train\', \'test\', \'valid\'}.
Returns:
eval_ops: dict<metric name, tuple(value, update_op)>
var_restore_dict: dict mapping variable restoration names to variables.
Trainable variables will be mapped to their moving average names.'
| def eval_graph(self, dataset='test'):
| inputs = _inputs(dataset, pretrain=False, bidir=True)
embedded = [self.layers['embedding'](inp.tokens) for inp in inputs]
(_, next_states, logits, _) = self.cl_loss_from_embedding(embedded, inputs=inputs, return_intermediates=True)
(f_inputs, _) = inputs
eval_ops = {'accuracy': tf.contrib.metrics.streaming_accuracy(layers_lib.predictions(logits), f_inputs.labels, f_inputs.weights)}
saves = [inp.save_state(state) for (inp, state) in zip(inputs, next_states)]
with tf.control_dependencies(saves):
(acc, acc_update) = eval_ops['accuracy']
acc_update = tf.identity(acc_update)
eval_ops['accuracy'] = (acc, acc_update)
var_restore_dict = make_restore_average_vars_dict()
return (eval_ops, var_restore_dict)
|
'Compute classification loss from embedding.
Args:
embedded: Length 2 tuple of 3-D float Tensor
[batch_size, num_timesteps, embedding_dim].
inputs: Length 2 tuple of VatxtInput, defaults to self.cl_inputs.
return_intermediates: bool, whether to return intermediate tensors or only
the final loss.
Returns:
If return_intermediates is True:
lstm_out, next_states, logits, loss
Else:
loss'
| def cl_loss_from_embedding(self, embedded, inputs=None, return_intermediates=False):
| if (inputs is None):
inputs = self.cl_inputs
out = []
for (layer_name, emb, inp) in zip(['lstm', 'lstm_reverse'], embedded, inputs):
out.append(self.layers[layer_name](emb, inp.state, inp.length))
(lstm_outs, next_states) = zip(*out)
lstm_out = tf.concat(lstm_outs, 1)
logits = self.layers['cl_logits'](lstm_out)
(f_inputs, _) = inputs
loss = layers_lib.classification_loss(logits, f_inputs.labels, f_inputs.weights)
if return_intermediates:
return (lstm_out, next_states, logits, loss)
else:
return loss
|
'Compute adversarial loss based on FLAGS.adv_training_method.'
| def adversarial_loss(self):
| def random_perturbation_loss():
return adv_lib.random_perturbation_loss_bidir(self.tensors['cl_embedded'], self.cl_inputs[0].length, self.cl_loss_from_embedding)
def adversarial_loss():
return adv_lib.adversarial_loss_bidir(self.tensors['cl_embedded'], self.tensors['cl_loss'], self.cl_loss_from_embedding)
def virtual_adversarial_loss():
"Computes virtual adversarial loss.\n\n Uses lm_inputs and constructs the language model graph if it hasn't yet\n been constructed.\n\n Also ensures that the LM input states are saved for LSTM state-saving\n BPTT.\n\n Returns:\n loss: float scalar.\n "
if (self.lm_inputs is None):
self.language_model_graph(compute_loss=False)
def logits_from_embedding(embedded, return_next_state=False):
(_, next_states, logits, _) = self.cl_loss_from_embedding(embedded, inputs=self.lm_inputs, return_intermediates=True)
if return_next_state:
return (next_states, logits)
else:
return logits
lm_embedded = (self.tensors['lm_embedded'], self.tensors['lm_embedded_reverse'])
(next_states, lm_cl_logits) = logits_from_embedding(lm_embedded, return_next_state=True)
va_loss = adv_lib.virtual_adversarial_loss_bidir(lm_cl_logits, lm_embedded, self.lm_inputs, logits_from_embedding)
saves = [inp.save_state(state) for (inp, state) in zip(self.lm_inputs, next_states)]
with tf.control_dependencies(saves):
va_loss = tf.identity(va_loss)
return va_loss
def combo_loss():
return (adversarial_loss() + virtual_adversarial_loss())
adv_training_methods = {'rp': random_perturbation_loss, 'at': adversarial_loss, 'vat': virtual_adversarial_loss, 'atvat': combo_loss, '': (lambda : tf.constant(0.0)), None: (lambda : tf.constant(0.0))}
with tf.name_scope('adversarial_loss'):
return adv_training_methods[FLAGS.adv_training_method]()
|
'Construct VatxtInput.
Args:
batch: NextQueuedSequenceBatch.
state_name: str, name of state to fetch and save.
tokens: int Tensor, tokens. Defaults to batch\'s F_TOKEN_ID sequence.
num_states: int The number of states to store.
eos_id: int Id of end of Sequence.'
| def __init__(self, batch, state_name=None, tokens=None, num_states=0, eos_id=None):
| self._batch = batch
self._state_name = state_name
self._tokens = (tokens if (tokens is not None) else batch.sequences[data_utils.SequenceWrapper.F_TOKEN_ID])
self._num_states = num_states
w = batch.sequences[data_utils.SequenceWrapper.F_WEIGHT]
w = tf.transpose(w, [1, 0])
w = tf.reshape(w, [(-1)])
self._weights = w
l = batch.sequences[data_utils.SequenceWrapper.F_LABEL]
l = tf.transpose(l, [1, 0])
l = tf.reshape(l, [(-1)])
self._labels = l
self._eos_weights = None
if eos_id:
ew = tf.cast(tf.equal(self._tokens, eos_id), tf.float32)
ew = tf.transpose(ew, [1, 0])
ew = tf.reshape(ew, [(-1)])
self._eos_weights = ew
|
'Constructs Timestep from empty Features.'
| def __init__(self, token, label, weight, multivalent_tokens=False):
| self._token = token
self._label = label
self._weight = weight
self._multivalent_tokens = multivalent_tokens
self._fill_with_defaults()
|
'Constructs a VGSLImageModel.
Args:
mode: One of "train", "eval"
model_spec: Full model specification string, for reference only.
initial_learning_rate: Initial learning rate for the network.
final_learning_rate: Final learning rate for the network.
halflife: Number of steps over which to halve the difference between
initial and final learning rate for the network.'
| def __init__(self, mode, model_spec, initial_learning_rate, final_learning_rate, halflife):
| self.model_spec = model_spec
self.layers = None
self.mode = mode
self.initial_learning_rate = initial_learning_rate
self.final_learning_rate = final_learning_rate
self.decay_steps = (halflife / DECAY_STEPS_FACTOR)
self.decay_rate = DECAY_RATE
self.labels = None
self.sparse_labels = None
self.truths = None
self.loss = None
self.train_op = None
self.global_step = None
self.output = None
self.using_ctc = False
self.saver = None
|
'Builds the model from the separate input/layers/output spec strings.
Args:
input_pattern: File pattern of the data in tfrecords of TF Example format.
input_spec: Specification of the input layer:
batchsize,height,width,depth (4 comma-separated integers)
Training will run with batches of batchsize images, but runtime can
use any batch size.
height and/or width can be 0 or -1, indicating variable size,
otherwise all images must be the given size.
depth must be 1 or 3 to indicate greyscale or color.
NOTE 1-d image input, treating the y image dimension as depth, can
be achieved using S1(1x0)1,3 as the first op in the model_spec, but
the y-size of the input must then be fixed.
model_spec: Model definition. See vgslspecs.py
output_spec: Output layer definition:
O(2|1|0)(l|s|c)n output layer with n classes.
2 (heatmap) Output is a 2-d vector map of the input (possibly at
different scale).
1 (sequence) Output is a 1-d sequence of vector values.
0 (value) Output is a 0-d single vector value.
l uses a logistic non-linearity on the output, allowing multiple
hot elements in any output vector value.
s uses a softmax non-linearity, with one-hot output in each value.
c uses a softmax with CTC. Can only be used with s (sequence).
NOTE Only O1s and O1c are currently supported.
optimizer_type: One of \'GradientDescent\', \'AdaGrad\', \'Momentum\', \'Adam\'.
num_preprocess_threads: Number of threads to use for image processing.
reader: Function that returns an actual reader to read Examples from input
files. If None, uses tf.TFRecordReader().'
| def Build(self, input_pattern, input_spec, model_spec, output_spec, optimizer_type, num_preprocess_threads, reader):
| self.global_step = tf.Variable(0, name='global_step', trainable=False)
shape = _ParseInputSpec(input_spec)
(out_dims, out_func, num_classes) = _ParseOutputSpec(output_spec)
self.using_ctc = (out_func == 'c')
(images, heights, widths, labels, sparse, _) = vgsl_input.ImageInput(input_pattern, num_preprocess_threads, shape, self.using_ctc, reader)
self.labels = labels
self.sparse_labels = sparse
self.layers = vgslspecs.VGSLSpecs(widths, heights, (self.mode == 'train'))
last_layer = self.layers.Build(images, model_spec)
self._AddOutputs(last_layer, out_dims, out_func, num_classes)
if (self.mode == 'train'):
self._AddOptimizer(optimizer_type)
self.saver = tf.train.Saver()
|
'Runs a training step in the session.
Args:
sess: Session in which to train the model.
Returns:
loss, global_step.'
| def TrainAStep(self, sess):
| (_, loss, step) = sess.run([self.train_op, self.loss, self.global_step])
return (loss, step)
|
'Restores the model from the given checkpoint path into the session.
Args:
checkpoint_path: File pathname of the checkpoint.
sess: Session in which to restore the model.
Returns:
global_step of the model.'
| def Restore(self, checkpoint_path, sess):
| self.saver.restore(sess, checkpoint_path)
return tf.train.global_step(sess, self.global_step)
|
'Runs a step for eval in the session.
Args:
sess: Session in which to run the model.
Returns:
output tensor result, labels tensor result.'
| def RunAStep(self, sess):
| return sess.run([self.output, self.labels])
|
'Adds the output layer and loss function.
Args:
prev_layer: Output of last layer of main network.
out_dims: Number of output dimensions, 0, 1 or 2.
out_func: Output non-linearity. \'s\' or \'c\'=softmax, \'l\'=logistic.
num_classes: Number of outputs/size of last output dimension.'
| def _AddOutputs(self, prev_layer, out_dims, out_func, num_classes):
| height_in = shapes.tensor_dim(prev_layer, dim=1)
(logits, outputs) = self._AddOutputLayer(prev_layer, out_dims, out_func, num_classes)
if (self.mode == 'train'):
self.loss = self._AddLossFunction(logits, height_in, out_dims, out_func)
tf.summary.scalar('loss', self.loss)
elif (out_dims == 0):
self.labels = tf.slice(self.labels, [0, 0], [(-1), 1])
self.labels = tf.reshape(self.labels, [(-1)])
logging.info('Final output=%s', outputs)
logging.info('Labels tensor=%s', self.labels)
self.output = outputs
|
'Add the fully-connected logits and SoftMax/Logistic output Layer.
Args:
prev_layer: Output of last layer of main network.
out_dims: Number of output dimensions, 0, 1 or 2.
out_func: Output non-linearity. \'s\' or \'c\'=softmax, \'l\'=logistic.
num_classes: Number of outputs/size of last output dimension.
Returns:
logits: Pre-softmax/logistic fully-connected output shaped to out_dims.
outputs: Post-softmax/logistic shaped to out_dims.
Raises:
ValueError: if syntax is incorrect.'
| def _AddOutputLayer(self, prev_layer, out_dims, out_func, num_classes):
| batch_in = shapes.tensor_dim(prev_layer, dim=0)
height_in = shapes.tensor_dim(prev_layer, dim=1)
width_in = shapes.tensor_dim(prev_layer, dim=2)
depth_in = shapes.tensor_dim(prev_layer, dim=3)
if out_dims:
shaped = tf.reshape(prev_layer, [(-1), depth_in])
else:
shaped = tf.reshape(prev_layer, [(-1), ((height_in * width_in) * depth_in)])
logits = slim.fully_connected(shaped, num_classes, activation_fn=None)
if (out_func == 'l'):
raise ValueError('Logistic not yet supported!')
else:
output = tf.nn.softmax(logits)
if (out_dims == 2):
output_shape = [batch_in, height_in, width_in, num_classes]
elif (out_dims == 1):
output_shape = [batch_in, (height_in * width_in), num_classes]
else:
output_shape = [batch_in, num_classes]
output = tf.reshape(output, output_shape, name='Output')
logits = tf.reshape(logits, output_shape)
return (logits, output)
|
'Add the appropriate loss function.
Args:
logits: Pre-softmax/logistic fully-connected output shaped to out_dims.
height_in: Height of logits before going into the softmax layer.
out_dims: Number of output dimensions, 0, 1 or 2.
out_func: Output non-linearity. \'s\' or \'c\'=softmax, \'l\'=logistic.
Returns:
loss: That which is to be minimized.
Raises:
ValueError: if logistic is used.'
| def _AddLossFunction(self, logits, height_in, out_dims, out_func):
| if (out_func == 'c'):
ctc_input = tf.transpose(logits, [1, 0, 2])
widths = self.layers.GetLengths(dim=2, factor=height_in)
cross_entropy = tf.nn.ctc_loss(ctc_input, self.sparse_labels, widths)
elif (out_func == 's'):
if (out_dims == 2):
self.labels = _PadLabels3d(logits, self.labels)
elif (out_dims == 1):
self.labels = _PadLabels2d(shapes.tensor_dim(logits, dim=1), self.labels)
else:
self.labels = tf.slice(self.labels, [0, 0], [(-1), 1])
self.labels = tf.reshape(self.labels, [(-1)])
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=self.labels, name='xent')
else:
raise ValueError('Logistic not yet supported!')
return tf.reduce_sum(cross_entropy)
|
'Adds an optimizer with learning rate decay to minimize self.loss.
Args:
optimizer_type: One of \'GradientDescent\', \'AdaGrad\', \'Momentum\', \'Adam\'.
Raises:
ValueError: if the optimizer type is unrecognized.'
| def _AddOptimizer(self, optimizer_type):
| learn_rate_delta = (self.initial_learning_rate - self.final_learning_rate)
learn_rate_dec = tf.add(tf.train.exponential_decay(learn_rate_delta, self.global_step, self.decay_steps, self.decay_rate), self.final_learning_rate)
if (optimizer_type == 'GradientDescent'):
opt = tf.train.GradientDescentOptimizer(learn_rate_dec)
elif (optimizer_type == 'AdaGrad'):
opt = tf.train.AdagradOptimizer(learn_rate_dec)
elif (optimizer_type == 'Momentum'):
opt = tf.train.MomentumOptimizer(learn_rate_dec, momentum=0.9)
elif (optimizer_type == 'Adam'):
opt = tf.train.AdamOptimizer(learning_rate=learn_rate_dec)
else:
raise ValueError(('Invalid optimizer type: ' + optimizer_type))
tf.summary.scalar('learn_rate', learn_rate_dec)
self.train_op = opt.minimize(self.loss, global_step=self.global_step, name='train')
|
'Constructs a VGSLSpecs.
Args:
widths: Tensor of size batch_size of the widths of the inputs.
heights: Tensor of size batch_size of the heights of the inputs.
is_training: True if the graph should be build for training.'
| def __init__(self, widths, heights, is_training):
| self.model_str = None
self.is_training = is_training
self.widths = widths
self.heights = heights
self.reduction_factors = [1.0, 1.0, 1.0, 1.0]
self.valid_ops = [self.AddSeries, self.AddParallel, self.AddConvLayer, self.AddMaxPool, self.AddDropout, self.AddReShape, self.AddFCLayer, self.AddLSTMLayer]
self.transtab = maketrans('(,)', '___')
|
'Builds a network with input prev_layer from a VGSLSpecs description.
Args:
prev_layer: The input tensor.
model_str: Model definition similar to Tesseract as follows:
============ FUNCTIONAL OPS ============
C(s|t|r|l|m)[{name}]<y>,<x>,<d> Convolves using a y,x window, with no
shrinkage, SAME infill, d outputs, with s|t|r|l|m non-linear layer.
(s|t|r|l|m) specifies the type of non-linearity:
s = sigmoid
t = tanh
r = relu
l = linear (i.e., None)
m = softmax
F(s|t|r|l|m)[{name}]<d> Fully-connected with s|t|r|l|m non-linearity and
d outputs. Reduces height, width to 1. Input height and width must be
constant.
L(f|r|b)(x|y)[s][{name}]<n> LSTM cell with n outputs.
f runs the LSTM forward only.
r runs the LSTM reversed only.
b runs the LSTM bidirectionally.
x runs the LSTM in the x-dimension (on data with or without the
y-dimension).
y runs the LSTM in the y-dimension (data must have a y dimension).
s (optional) summarizes the output in the requested dimension,
outputting only the final step, collapsing the dimension to a
single element.
Examples:
Lfx128 runs a forward-only LSTM in the x-dimension with 128
outputs, treating any y dimension independently.
Lfys64 runs a forward-only LSTM in the y-dimension with 64 outputs
and collapses the y-dimension to 1 element.
NOTE that Lbxsn is implemented as (LfxsnLrxsn) since the summaries
need to be taken from opposite ends of the output
Do[{name}] Insert a dropout layer.
============ PLUMBING OPS ============
[...] Execute ... networks in series (layers).
(...) Execute ... networks in parallel, with their output concatenated
in depth.
S[{name}]<d>(<a>x<b>)<e>,<f> Splits one dimension, moves one part to
another dimension.
Splits input dimension d into a x b, sending the high part (a) to the
high side of dimension e, and the low part (b) to the high side of
dimension f. Exception: if d=e=f, then then dimension d is internally
transposed to bxa.
Either a or b can be zero, meaning whatever is left after taking out
the other, allowing dimensions to be of variable size.
Eg. S3(3x50)2,3 will split the 150-element depth into 3x50, with the 3
going to the most significant part of the width, and the 50 part
staying in depth.
This will rearrange a 3x50 output parallel operation to spread the 3
output sets over width.
Mp[{name}]<y>,<x> Maxpool the input, reducing the (y,x) rectangle to a
single vector value.
Returns:
Output tensor'
| def Build(self, prev_layer, model_str):
| self.model_str = model_str
(final_layer, _) = self.BuildFromString(prev_layer, 0)
return final_layer
|
'Returns the lengths of the batch of elements in the given dimension.
WARNING: The returned sizes may not exactly match TF\'s calculation.
Args:
dim: dimension to get the sizes of, in [1,2]. batch, depth not allowed.
factor: A scalar value to multiply by.
Returns:
The original heights/widths scaled by the current scaling of the model and
the given factor.
Raises:
ValueError: If the args are invalid.'
| def GetLengths(self, dim=2, factor=1):
| if (dim == 1):
lengths = self.heights
elif (dim == 2):
lengths = self.widths
else:
raise ValueError('Invalid dimension given to GetLengths')
lengths = tf.cast(lengths, tf.float32)
if (self.reduction_factors[dim] is not None):
lengths = tf.div(lengths, self.reduction_factors[dim])
else:
lengths = tf.ones_like(lengths)
if (factor != 1):
lengths = tf.multiply(lengths, tf.cast(factor, tf.float32))
return tf.cast(lengths, tf.int32)
|
'Adds the layers defined by model_str[index:] to the model.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, next model_str index.
Raises:
ValueError: If the model string is unrecognized.'
| def BuildFromString(self, prev_layer, index):
| index = self._SkipWhitespace(index)
for op in self.valid_ops:
(output_layer, next_index) = op(prev_layer, index)
if (output_layer is not None):
return (output_layer, next_index)
if (output_layer is not None):
return (output_layer, next_index)
raise ValueError(('Unrecognized model string:' + self.model_str[index:]))
|
'Builds a sequence of layers for a VGSLSpecs model.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor of the series, end index in model_str.
Raises:
ValueError: If [] are unbalanced.'
| def AddSeries(self, prev_layer, index):
| if (self.model_str[index] != '['):
return (None, None)
index += 1
while ((index < len(self.model_str)) and (self.model_str[index] != ']')):
(prev_layer, index) = self.BuildFromString(prev_layer, index)
if (index == len(self.model_str)):
raise ValueError(('Missing ] at end of series!' + self.model_str))
return (prev_layer, (index + 1))
|
'tf.concats outputs of layers that run on the same inputs.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor of the parallel, end index in model_str.
Raises:
ValueError: If () are unbalanced or the elements don\'t match.'
| def AddParallel(self, prev_layer, index):
| if (self.model_str[index] != '('):
return (None, None)
index += 1
layers = []
num_dims = 0
original_factors = self.reduction_factors
final_factors = None
while ((index < len(self.model_str)) and (self.model_str[index] != ')')):
self.reduction_factors = original_factors
(layer, index) = self.BuildFromString(prev_layer, index)
if (num_dims == 0):
num_dims = len(layer.get_shape())
elif (num_dims != len(layer.get_shape())):
raise ValueError('All elements of parallel must return same num dims')
layers.append(layer)
if final_factors:
if (final_factors != self.reduction_factors):
raise ValueError('All elements of parallel must scale the same')
else:
final_factors = self.reduction_factors
if (index == len(self.model_str)):
raise ValueError(('Missing ) at end of parallel!' + self.model_str))
return (tf.concat(axis=(num_dims - 1), values=layers), (index + 1))
|
'Add a single standard convolutional layer.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddConvLayer(self, prev_layer, index):
| pattern = re.compile('(C)(s|t|r|l|m)({\\w+})?(\\d+),(\\d+),(\\d+)')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
name = self._GetLayerName(m.group(0), index, m.group(3))
width = int(m.group(4))
height = int(m.group(5))
depth = int(m.group(6))
fn = self._NonLinearity(m.group(2))
return (slim.conv2d(prev_layer, depth, [height, width], activation_fn=fn, scope=name), m.end())
|
'Add a maxpool layer.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddMaxPool(self, prev_layer, index):
| pattern = re.compile('(Mp)({\\w+})?(\\d+),(\\d+)(?:,(\\d+),(\\d+))?')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
name = self._GetLayerName(m.group(0), index, m.group(2))
height = int(m.group(3))
width = int(m.group(4))
y_stride = (height if (m.group(5) is None) else m.group(5))
x_stride = (width if (m.group(6) is None) else m.group(6))
self.reduction_factors[1] *= y_stride
self.reduction_factors[2] *= x_stride
return (slim.max_pool2d(prev_layer, [height, width], [y_stride, x_stride], padding='SAME', scope=name), m.end())
|
'Adds a dropout layer.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddDropout(self, prev_layer, index):
| pattern = re.compile('(Do)({\\w+})?')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
name = self._GetLayerName(m.group(0), index, m.group(2))
layer = slim.dropout(prev_layer, 0.5, is_training=self.is_training, scope=name)
return (layer, m.end())
|
'Reshapes the input tensor by moving each (x_scale,y_scale) rectangle to.
the depth dimension. NOTE that the TF convention is that inputs are
[batch, y, x, depth].
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddReShape(self, prev_layer, index):
| pattern = re.compile('(S)(?:{(\\w)})?(\\d+)\\((\\d+)x(\\d+)\\)(\\d+),(\\d+)')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
name = self._GetLayerName(m.group(0), index, m.group(2))
src_dim = int(m.group(3))
part_a = int(m.group(4))
part_b = int(m.group(5))
dest_dim_a = int(m.group(6))
dest_dim_b = int(m.group(7))
if (part_a == 0):
part_a = (-1)
if (part_b == 0):
part_b = (-1)
prev_shape = tf.shape(prev_layer)
layer = shapes.transposing_reshape(prev_layer, src_dim, part_a, part_b, dest_dim_a, dest_dim_b, name=name)
result_shape = tf.shape(layer)
for i in xrange(len(self.reduction_factors)):
if (self.reduction_factors[i] is not None):
factor1 = tf.cast(self.reduction_factors[i], tf.float32)
factor2 = tf.cast(prev_shape[i], tf.float32)
divisor = tf.cast(result_shape[i], tf.float32)
self.reduction_factors[i] = tf.div(tf.multiply(factor1, factor2), divisor)
return (layer, m.end())
|
'Parse expression and add Fully Connected Layer.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddFCLayer(self, prev_layer, index):
| pattern = re.compile('(F)(s|t|r|l|m)({\\w+})?(\\d+)')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
fn = self._NonLinearity(m.group(2))
name = self._GetLayerName(m.group(0), index, m.group(3))
depth = int(m.group(4))
input_depth = ((shapes.tensor_dim(prev_layer, 1) * shapes.tensor_dim(prev_layer, 2)) * shapes.tensor_dim(prev_layer, 3))
shaped = tf.reshape(prev_layer, [(-1), input_depth], name=(name + '_reshape_in'))
output = slim.fully_connected(shaped, depth, activation_fn=fn, scope=name)
self.reduction_factors[1] = None
self.reduction_factors[2] = None
return (tf.reshape(output, [shapes.tensor_dim(prev_layer, 0), 1, 1, depth], name=(name + '_reshape_out')), m.end())
|
'Parse expression and add LSTM Layer.
Args:
prev_layer: Input tensor.
index: Position in model_str to start parsing
Returns:
Output tensor, end index in model_str.'
| def AddLSTMLayer(self, prev_layer, index):
| pattern = re.compile('(L)(f|r|b)(x|y)(s)?({\\w+})?(\\d+)')
m = pattern.match(self.model_str, index)
if (m is None):
return (None, None)
direction = m.group(2)
dim = m.group(3)
summarize = (m.group(4) == 's')
name = self._GetLayerName(m.group(0), index, m.group(5))
depth = int(m.group(6))
if ((direction == 'b') and summarize):
fwd = self._LSTMLayer(prev_layer, 'forward', dim, True, depth, (name + '_forward'))
back = self._LSTMLayer(prev_layer, 'backward', dim, True, depth, (name + '_reverse'))
return (tf.concat(axis=3, values=[fwd, back], name=(name + '_concat')), m.end())
if (direction == 'f'):
direction = 'forward'
elif (direction == 'r'):
direction = 'backward'
else:
direction = 'bidirectional'
outputs = self._LSTMLayer(prev_layer, direction, dim, summarize, depth, name)
if summarize:
if (dim == 'x'):
self.reduction_factors[2] = None
else:
self.reduction_factors[1] = None
return (outputs, m.end())
|
'Adds an LSTM layer with the given pre-parsed attributes.
Always maps 4-D to 4-D regardless of summarize.
Args:
prev_layer: Input tensor.
direction: \'forward\' \'backward\' or \'bidirectional\'
dim: \'x\' or \'y\', dimension to consider as time.
summarize: True if we are to return only the last timestep.
depth: Output depth.
name: Some string naming the op.
Returns:
Output tensor.'
| def _LSTMLayer(self, prev_layer, direction, dim, summarize, depth, name):
| if (dim == 'x'):
lengths = self.GetLengths(2, 1)
inputs = prev_layer
else:
lengths = self.GetLengths(1, 1)
inputs = tf.transpose(prev_layer, [0, 2, 1, 3], name=(name + '_ytrans_in'))
input_batch = shapes.tensor_dim(inputs, 0)
num_slices = shapes.tensor_dim(inputs, 1)
num_steps = shapes.tensor_dim(inputs, 2)
input_depth = shapes.tensor_dim(inputs, 3)
inputs = tf.reshape(inputs, [(-1), num_steps, input_depth], name=(name + '_reshape_in'))
tile_factor = (tf.to_float((input_batch * num_slices)) / tf.to_float(tf.shape(lengths)[0]))
lengths = tf.tile(lengths, [tf.cast(tile_factor, tf.int32)])
lengths = tf.cast(lengths, tf.int64)
outputs = nn_ops.rnn_helper(inputs, lengths, cell_type='lstm', num_nodes=depth, direction=direction, name=name, stddev=0.1)
if (direction == 'bidirectional'):
output_depth = (depth * 2)
else:
output_depth = depth
if summarize:
outputs = tf.slice(outputs, [0, (num_steps - 1), 0], [(-1), 1, (-1)], name=(name + '_sum_slice'))
outputs = tf.reshape(outputs, [input_batch, num_slices, 1, output_depth], name=(name + '_reshape_out'))
else:
outputs = tf.reshape(outputs, [input_batch, num_slices, num_steps, output_depth], name=(name + '_reshape_out'))
if (dim == 'y'):
outputs = tf.transpose(outputs, [0, 2, 1, 3], name=(name + '_ytrans_out'))
return outputs
|
'Returns the non-linearity function pointer for the given string code.
For forwards compatibility, allows the full names for stand-alone
non-linearities, as well as the single-letter names used in ops like C,F.
Args:
code: String code representing a non-linearity function.
Returns:
non-linearity function represented by the code.'
| def _NonLinearity(self, code):
| if (code in ['s', 'Sig']):
return tf.sigmoid
elif (code in ['t', 'Tanh']):
return tf.tanh
elif (code in ['r', 'Relu']):
return tf.nn.relu
elif (code in ['m', 'Smax']):
return tf.nn.softmax
return None
|
'Generates a name for the op, using a user-supplied name if possible.
Args:
op_str: String representing the parsed op.
index: Position in model_str of the start of the op.
name_str: User-supplied {name} with {} that need removing or None.
Returns:
Selected name.'
| def _GetLayerName(self, op_str, index, name_str):
| if name_str:
return name_str[1:(-1)]
else:
return ((op_str.translate(self.transtab) + '_') + str(index))
|
'Skips any leading whitespace in the model description.
Args:
index: Position in model_str to start parsing
Returns:
end index in model_str of whitespace.'
| def _SkipWhitespace(self, index):
| pattern = re.compile('([ \\t\\n]+)')
m = pattern.match(self.model_str, index)
if (m is None):
return index
return m.end()
|
'Tests that the simple CTC decoder drops nulls and duplicates.'
| def testCodesFromCTC(self):
| ctc_labels = [9, 9, 9, 1, 9, 2, 2, 3, 9, 9, 0, 0, 1, 9, 1, 9, 9, 9]
decode = decoder.Decoder(filename=None)
non_null_labels = decode._CodesFromCTC(ctc_labels, merge_dups=False, null_label=9)
self.assertEqual(non_null_labels, [1, 2, 2, 3, 0, 0, 1, 1])
idempotent_labels = decode._CodesFromCTC(non_null_labels, merge_dups=False, null_label=9)
self.assertEqual(idempotent_labels, non_null_labels)
collapsed_labels = decode._CodesFromCTC(ctc_labels, merge_dups=True, null_label=9)
self.assertEqual(collapsed_labels, [1, 2, 3, 0, 1, 1])
non_idempotent_labels = decode._CodesFromCTC(collapsed_labels, merge_dups=True, null_label=9)
self.assertEqual(non_idempotent_labels, [1, 2, 3, 0, 1])
|
'Tests that the decoder can decode sequences including multi-codes.'
| def testStringFromCTC(self):
| ctc_labels = [9, 6, 9, 1, 3, 9, 4, 9, 5, 5, 9, 5, 0, 2, 1, 3, 9, 4, 9]
decode = decoder.Decoder(filename=_testdata('charset_size_10.txt'))
text = decode.StringFromCTC(ctc_labels, merge_dups=True, null_label=9)
self.assertEqual(text, 'farm barn')
|
'Tests that the percent calculation works as expected.'
| def testComputeErrorRate(self):
| rate = ec.ComputeErrorRate(error_count=0, truth_count=0)
self.assertEqual(rate, 100.0)
rate = ec.ComputeErrorRate(error_count=1, truth_count=0)
self.assertEqual(rate, 100.0)
rate = ec.ComputeErrorRate(error_count=10, truth_count=1)
self.assertEqual(rate, 100.0)
rate = ec.ComputeErrorRate(error_count=0, truth_count=1)
self.assertEqual(rate, 0.0)
rate = ec.ComputeErrorRate(error_count=3, truth_count=12)
self.assertEqual(rate, 25.0)
|
'Tests that the error counter works as expected.'
| def testCountErrors(self):
| truth_str = 'farm barn'
counts = ec.CountErrors(ocr_text=truth_str, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=0, truth_count=9, test_count=9))
dot_str = 'farm barn.'
counts = ec.CountErrors(ocr_text=dot_str, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=1, truth_count=9, test_count=10))
counts = ec.CountErrors(ocr_text=truth_str, truth_text=dot_str)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=0, truth_count=10, test_count=9))
no_space = 'farmbarn'
counts = ec.CountErrors(ocr_text=no_space, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=0, truth_count=9, test_count=8))
counts = ec.CountErrors(ocr_text=truth_str, truth_text=no_space)
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=1, truth_count=8, test_count=9))
counts = ec.CountErrors(ocr_text='', truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=9, fp=0, truth_count=9, test_count=0))
counts = ec.CountErrors(ocr_text=truth_str, truth_text='')
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=9, truth_count=0, test_count=9))
|
'Tests that the error counter works as expected.'
| def testCountWordErrors(self):
| truth_str = 'farm barn'
counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=0, truth_count=2, test_count=2))
dot_str = 'farm barn.'
counts = ec.CountWordErrors(ocr_text=dot_str, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=1, truth_count=2, test_count=2))
counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=dot_str)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=1, truth_count=2, test_count=2))
no_space = 'farmbarn'
counts = ec.CountWordErrors(ocr_text=no_space, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=2, fp=1, truth_count=2, test_count=1))
counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=no_space)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=2, truth_count=1, test_count=2))
counts = ec.CountWordErrors(ocr_text='', truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=2, fp=0, truth_count=2, test_count=0))
counts = ec.CountWordErrors(ocr_text=truth_str, truth_text='')
self.assertEqual(counts, ec.ErrorCounts(fn=0, fp=2, truth_count=0, test_count=2))
sp_str = 'farm ba rn'
counts = ec.CountWordErrors(ocr_text=sp_str, truth_text=truth_str)
self.assertEqual(counts, ec.ErrorCounts(fn=1, fp=2, truth_count=2, test_count=3))
counts = ec.CountWordErrors(ocr_text=truth_str, truth_text=sp_str)
self.assertEqual(counts, ec.ErrorCounts(fn=2, fp=1, truth_count=3, test_count=2))
|
'The parser must return the numbers in the correct order.'
| def testParseInputSpec(self):
| shape = vgsl_model._ParseInputSpec(input_spec='32,42,256,3')
self.assertEqual(shape, vgsl_input.ImageShape(batch_size=32, height=42, width=256, depth=3))
shape = vgsl_model._ParseInputSpec(input_spec='1,0,0,3')
self.assertEqual(shape, vgsl_input.ImageShape(batch_size=1, height=None, width=None, depth=3))
|
'The parser must return the correct args in the correct order.'
| def testParseOutputSpec(self):
| (out_dims, out_func, num_classes) = vgsl_model._ParseOutputSpec(output_spec='O1c142')
self.assertEqual(out_dims, 1)
self.assertEqual(out_func, 'c')
self.assertEqual(num_classes, 142)
(out_dims, out_func, num_classes) = vgsl_model._ParseOutputSpec(output_spec='O2s99')
self.assertEqual(out_dims, 2)
self.assertEqual(out_func, 's')
self.assertEqual(num_classes, 99)
(out_dims, out_func, num_classes) = vgsl_model._ParseOutputSpec(output_spec='O0l12')
self.assertEqual(out_dims, 0)
self.assertEqual(out_func, 'l')
self.assertEqual(num_classes, 12)
|
'Must pad timesteps in labels to match logits.'
| def testPadLabels2d(self):
| with self.test_session() as sess:
ph_logits = tf.placeholder(tf.float32, shape=(None, None, 42))
ph_labels = tf.placeholder(tf.int64, shape=(None, None))
padded_labels = vgsl_model._PadLabels2d(tf.shape(ph_logits)[1], ph_labels)
real_logits = _rand(4, 97, 42)
real_labels = _rand(4, 85)
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (4, 97))
real_labels = _rand(4, 97)
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (4, 97))
real_labels = _rand(4, 100)
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (4, 97))
|
'Must pad height and width in labels to match logits.
The tricky thing with 3-d is that the rows and columns need to remain
intact, so we\'ll test it with small known data.'
| def testPadLabels3d(self):
| with self.test_session() as sess:
ph_logits = tf.placeholder(tf.float32, shape=(None, None, None, 42))
ph_labels = tf.placeholder(tf.int64, shape=(None, None, None))
padded_labels = vgsl_model._PadLabels3d(ph_logits, ph_labels)
real_logits = _rand(1, 3, 4, 42)
real_labels = np.arange(6).reshape((1, 2, 3))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 0], [3, 4, 5, 0], [0, 0, 0, 0]])
real_labels = np.arange(8).reshape((1, 2, 4))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [4, 5, 6, 7], [0, 0, 0, 0]])
real_labels = np.arange(10).reshape((1, 2, 5))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [5, 6, 7, 8], [0, 0, 0, 0]])
real_labels = np.arange(9).reshape((1, 3, 3))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 0], [3, 4, 5, 0], [6, 7, 8, 0]])
real_labels = np.arange(12).reshape((1, 3, 4))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
real_labels = np.arange(15).reshape((1, 3, 5))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [5, 6, 7, 8], [10, 11, 12, 13]])
real_labels = np.arange(12).reshape((1, 4, 3))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 0], [3, 4, 5, 0], [6, 7, 8, 0]])
real_labels = np.arange(16).reshape((1, 4, 4))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
real_labels = np.arange(20).reshape((1, 4, 5))
np_array = sess.run([padded_labels], feed_dict={ph_logits: real_logits, ph_labels: real_labels})[0]
self.assertEqual(tuple(np_array.shape), (1, 3, 4))
self.assertAllEqual(np_array[0, :, :], [[0, 1, 2, 3], [5, 6, 7, 8], [10, 11, 12, 13]])
|
'Tests that the output sizes match when training/running real 0d data.
Uses mnist with dual summarizing LSTMs to reduce to a single value.'
| def testEndToEndSizes0d(self):
| filename = _testdata('mnist-tiny')
with self.test_session() as sess:
model = vgsl_model.InitNetwork(filename, model_spec='4,0,0,1[Cr5,5,16 Mp3,3 Lfys16 Lfxs16]O0s12', mode='train')
tf.global_variables_initializer().run(session=sess)
coord = tf.train.Coordinator()
tf.train.start_queue_runners(sess=sess, coord=coord)
(_, step) = model.TrainAStep(sess)
self.assertEqual(step, 1)
(output, labels) = model.RunAStep(sess)
self.assertEqual(len(output.shape), 2)
self.assertEqual(len(labels.shape), 1)
self.assertEqual(output.shape[0], labels.shape[0])
self.assertEqual(output.shape[1], 12)
|
'Tests that the output sizes match when training with CTC.
Basic bidi LSTM on top of convolution and summarizing LSTM with CTC.'
| def testEndToEndSizes1dCTC(self):
| filename = _testdata('arial-32-tiny')
with self.test_session() as sess:
model = vgsl_model.InitNetwork(filename, model_spec='2,0,0,1[Cr5,5,16 Mp3,3 Lfys16 Lbx100]O1c105', mode='train')
tf.global_variables_initializer().run(session=sess)
coord = tf.train.Coordinator()
tf.train.start_queue_runners(sess=sess, coord=coord)
(_, step) = model.TrainAStep(sess)
self.assertEqual(step, 1)
(output, labels) = model.RunAStep(sess)
self.assertEqual(len(output.shape), 3)
self.assertEqual(len(labels.shape), 2)
self.assertEqual(output.shape[0], labels.shape[0])
self.assertLessEqual(labels.shape[1], output.shape[1])
self.assertEqual(output.shape[2], 105)
|
'Tests that the output sizes match when training/running 1 data.
Convolution, summarizing LSTM with fwd rev fwd to allow no CTC.'
| def testEndToEndSizes1dFixed(self):
| filename = _testdata('numbers-16-tiny')
with self.test_session() as sess:
model = vgsl_model.InitNetwork(filename, model_spec='8,0,0,1[Cr5,5,16 Mp3,3 Lfys16 Lfx64 Lrx64 Lfx64]O1s12', mode='train')
tf.global_variables_initializer().run(session=sess)
coord = tf.train.Coordinator()
tf.train.start_queue_runners(sess=sess, coord=coord)
(_, step) = model.TrainAStep(sess)
self.assertEqual(step, 1)
(output, labels) = model.RunAStep(sess)
self.assertEqual(len(output.shape), 3)
self.assertEqual(len(labels.shape), 2)
self.assertEqual(output.shape[0], labels.shape[0])
self.assertEqual(output.shape[1], labels.shape[1])
self.assertEqual(output.shape[2], 12)
|
'Tests that a tiled input can be reshaped to the batch dimension.'
| def testReshapeTile(self):
| fake = tf.placeholder(tf.float32, shape=(None, None, None, self.depth), name='inputs')
real = _rand(self.batch_size, self.im_height, self.im_width, self.depth)
with self.test_session() as sess:
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=3, part_b=(-1), dest_dim_a=0, dest_dim_b=2)
res_image = sess.run([outputs], feed_dict={fake: real})
self.assertEqual(tuple(res_image[0].shape), ((self.batch_size * 3), self.im_height, (self.im_width / 3), self.depth))
|
'Tests that depth can be reshaped to the x dimension.'
| def testReshapeDepth(self):
| fake = tf.placeholder(tf.float32, shape=(None, None, None, self.depth), name='inputs')
real = _rand(self.batch_size, self.im_height, self.im_width, self.depth)
with self.test_session() as sess:
outputs = shapes.transposing_reshape(fake, src_dim=3, part_a=4, part_b=(-1), dest_dim_a=2, dest_dim_b=3)
res_image = sess.run([outputs], feed_dict={fake: real})
self.assertEqual(tuple(res_image[0].shape), (self.batch_size, self.im_height, (self.im_width * 4), (self.depth / 4)))
|
'Case: dest_a == src, dest_b < src: Split with Least sig part going left.'
| def testTransposingReshape_2_2_3_2_1(self):
| with self.test_session() as sess:
fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs')
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=1)
real = np.arange(120).reshape((5, 2, 6, 2))
np_array = sess.run([outputs], feed_dict={fake: real})[0]
self.assertEqual(tuple(np_array.shape), (5, 6, 2, 2))
self.assertAllEqual(np_array[0, :, :, :], [[[0, 1], [6, 7]], [[12, 13], [18, 19]], [[2, 3], [8, 9]], [[14, 15], [20, 21]], [[4, 5], [10, 11]], [[16, 17], [22, 23]]])
|
'Case: dest_a == src, dest_b > src: Split with Least sig part going right.'
| def testTransposingReshape_2_2_3_2_3(self):
| with self.test_session() as sess:
fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs')
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=3)
real = np.arange(120).reshape((5, 2, 6, 2))
np_array = sess.run([outputs], feed_dict={fake: real})[0]
self.assertEqual(tuple(np_array.shape), (5, 2, 2, 6))
self.assertAllEqual(np_array[0, :, :, :], [[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]], [[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]])
|
'Case: dest_a == src, dest_b == src. Transpose within dimension 2.'
| def testTransposingReshape_2_2_3_2_2(self):
| with self.test_session() as sess:
fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs')
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=2)
real = np.arange(120).reshape((5, 2, 6, 2))
np_array = sess.run([outputs], feed_dict={fake: real})[0]
self.assertEqual(tuple(np_array.shape), (5, 2, 6, 2))
self.assertAllEqual(np_array[0, :, :, :], [[[0, 1], [6, 7], [2, 3], [8, 9], [4, 5], [10, 11]], [[12, 13], [18, 19], [14, 15], [20, 21], [16, 17], [22, 23]]])
|
'Case: dest_a < src, dest_b == src. Split with Most sig part going left.'
| def testTransposingReshape_2_2_3_1_2(self):
| with self.test_session() as sess:
fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs')
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=1, dest_dim_b=2)
real = np.arange(120).reshape((5, 2, 6, 2))
np_array = sess.run([outputs], feed_dict={fake: real})[0]
self.assertEqual(tuple(np_array.shape), (5, 4, 3, 2))
self.assertAllEqual(np_array[0, :, :, :], [[[0, 1], [2, 3], [4, 5]], [[12, 13], [14, 15], [16, 17]], [[6, 7], [8, 9], [10, 11]], [[18, 19], [20, 21], [22, 23]]])
|
'Case: dest_a < src, dest_b == src. Split with Most sig part going right.'
| def testTransposingReshape_2_2_3_3_2(self):
| with self.test_session() as sess:
fake = tf.placeholder(tf.float32, shape=(None, None, None, 2), name='inputs')
outputs = shapes.transposing_reshape(fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=3, dest_dim_b=2)
real = np.arange(120).reshape((5, 2, 6, 2))
np_array = sess.run([outputs], feed_dict={fake: real})[0]
self.assertEqual(tuple(np_array.shape), (5, 2, 3, 4))
self.assertAllEqual(np_array[0, :, :, :], [[[0, 1, 6, 7], [2, 3, 8, 9], [4, 5, 10, 11]], [[12, 13, 18, 19], [14, 15, 20, 21], [16, 17, 22, 23]]])
|
'Tests that the output of the graph of the given spec has target_shape.'
| def ExpectScaledSize(self, spec, target_shape, factor=1):
| with tf.Graph().as_default():
with self.test_session() as sess:
self.SetupInputs()
vgsl = vgslspecs.VGSLSpecs(self.ph_widths, self.ph_heights, True)
outputs = vgsl.Build(self.ph_image, spec)
target_widths = tf.div(self.in_widths, factor).eval()
target_heights = tf.div(self.in_heights, factor).eval()
tf.global_variables_initializer().run()
(res_image, res_widths, res_heights) = sess.run([outputs, vgsl.GetLengths(2), vgsl.GetLengths(1)], feed_dict={self.ph_image: self.in_image, self.ph_widths: self.in_widths, self.ph_heights: self.in_heights})
self.assertEqual(tuple(res_image.shape), target_shape)
if (target_shape[1] > 1):
self.assertEqual(tuple(res_heights), tuple(target_heights))
if (target_shape[2] > 1):
self.assertEqual(tuple(res_widths), tuple(target_widths))
|
'Test all types of Conv. There is no scaling.'
| def testSameSizeConv(self):
| self.ExpectScaledSize('[Cs{MyConv}5,5,16 Ct3,3,12 Cr4,4,24 Cl5,5,64]', (self.batch_size, self.max_height, self.max_width, 64))
|
'Test all non-reducing LSTMs. Output depth is doubled with BiDi.'
| def testSameSizeLSTM(self):
| self.ExpectScaledSize('[Lfx16 Lrx8 Do Lbx24 Lfy12 Do{MyDo} Lry7 Lby32]', (self.batch_size, self.max_height, self.max_width, 64))
|
'Parallel affects depth, but not scale.'
| def testSameSizeParallel(self):
| self.ExpectScaledSize('[Cs5,5,16 (Lfx{MyLSTM}32 Lrx32 Lbx16)]', (self.batch_size, self.max_height, self.max_width, 96))
|
'Test a heterogeneous series with scaling.'
| def testScalingOps(self):
| self.ExpectScaledSize('[Cs5,5,16 Mp{MyPool}2,2 Ct3,3,32 Mp3,3 Lfx32 Lry64]', (self.batch_size, (self.max_height / 6), (self.max_width / 6), 64), 6)
|
'Test a heterogeneous series with reduction of x-dimension.'
| def testXReduction(self):
| self.ExpectScaledSize('[Cr5,5,16 Mp2,2 Ct3,3,32 Mp3,3 Lfxs32 Lry64]', (self.batch_size, (self.max_height / 6), 1, 64), 6)
|
'Test a heterogeneous series with reduction of y-dimension.'
| def testYReduction(self):
| self.ExpectScaledSize('[Cl5,5,16 Mp2,2 Ct3,3,32 Mp3,3 Lfys32 Lfx64]', (self.batch_size, 1, (self.max_width / 6), 64), 6)
|
'Test a heterogeneous series with reduction to 0-d.'
| def testXYReduction(self):
| self.ExpectScaledSize('[Cr5,5,16 Lfys32 Lfxs64 Fr{MyFC}16 Ft20 Fl12 Fs32 Fm40]', (self.batch_size, 1, 1, 40))
|
'Tests that a tiled input can be reshaped to the batch dimension.'
| def testReshapeTile(self):
| self.ExpectScaledSize('[S2(3x0)0,2 Cr5,5,16 Lfys16]', ((self.batch_size * 3), 1, (self.max_width / 3), 16), 3)
|
'Tests that depth can be reshaped to the x dimension.'
| def testReshapeDepth(self):
| self.ExpectScaledSize('[Cl5,5,16 Mp3,3 (Lrys32 Lbys16 Lfys32) S3(3x0)2,3]', (self.batch_size, 1, self.max_width, 32))
|
'Constructs a Decoder.
Reads the text file describing the encoding and build the encoder.
The text file contains lines of the form:
<code>[,<code>]*\t<string>
Each line defines a mapping from a sequence of one or more integer codes to
a corresponding utf-8 string.
Args:
filename: Name of file defining the decoding sequences.'
| def __init__(self, filename):
| self.decoder = []
if filename:
self._InitializeDecoder(filename)
|
'Evaluate a model in softmax mode.
Adds char, word recall and sequence error rate events to the sw summary
writer, and returns them as well
TODO(rays) Add LogisticEval.
Args:
sess: A tensor flow Session.
model: The model to run in the session. Requires a VGSLImageModel or any
other class that has a using_ctc attribute and a RunAStep(sess) method
that reurns a softmax result with corresponding labels.
num_steps: Number of steps to evaluate for.
Returns:
ErrorRates named tuple.
Raises:
ValueError: If an unsupported number of dimensions is used.'
| def SoftmaxEval(self, sess, model, num_steps):
| coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
total_label_counts = ec.ErrorCounts(0, 0, 0, 0)
total_word_counts = ec.ErrorCounts(0, 0, 0, 0)
sequence_errors = 0
for _ in xrange(num_steps):
(softmax_result, labels) = model.RunAStep(sess)
predictions = softmax_result.argmax(axis=(-1))
num_dims = (len(predictions.shape) - 1)
batch_size = predictions.shape[0]
null_label = (softmax_result.shape[(-1)] - 1)
for b in xrange(batch_size):
if (num_dims == 2):
raise ValueError('2-d label data not supported yet!')
else:
if (num_dims == 1):
pred_batch = predictions[b, :]
labels_batch = labels[b, :]
else:
pred_batch = [predictions[b]]
labels_batch = [labels[b]]
text = self.StringFromCTC(pred_batch, model.using_ctc, null_label)
truth = self.StringFromCTC(labels_batch, False, null_label)
total_word_counts = ec.AddErrors(total_word_counts, ec.CountWordErrors(text, truth))
total_label_counts = ec.AddErrors(total_label_counts, ec.CountErrors(text, truth))
if (text != truth):
sequence_errors += 1
coord.request_stop()
coord.join(threads)
return ec.ComputeErrorRates(total_label_counts, total_word_counts, sequence_errors, (num_steps * batch_size))
|
'Decodes CTC output to a string.
Extracts only sequences of codes that are allowed by self.decoder.
Labels that make illegal code sequences are dropped.
Note that, by its nature of taking only top choices, this is much weaker
than a full-blown beam search that considers all the softmax outputs.
For languages without many multi-code sequences, this doesn\'t make much
difference, but for complex scripts the accuracy will be much lower.
Args:
ctc_labels: List of class labels including null characters to remove.
merge_dups: If True, Duplicate labels will be merged
null_label: Label value to ignore.
Returns:
Labels decoded to a string.'
| def StringFromCTC(self, ctc_labels, merge_dups, null_label):
| codes = self._CodesFromCTC(ctc_labels, merge_dups, null_label)
length = len(codes)
if (length == 0):
return ''
strings = []
partials = []
for pos in xrange(length):
code = codes[pos]
parts = self.decoder[code]
partials.append([])
strings.append('')
for (utf8, index, num_codes) in parts:
if (index > pos):
continue
if ((index == 0) or (partials[(pos - 1)].count(Part(utf8, (index - 1), num_codes)) > 0)):
if (index < (num_codes - 1)):
partials[(-1)].append(Part(utf8, index, num_codes))
elif (not strings[(-1)]):
if (pos >= num_codes):
strings[(-1)] = (strings[(pos - num_codes)] + utf8)
else:
strings[(-1)] = utf8
if ((not strings[(-1)]) and (pos > 0)):
strings[(-1)] = strings[(-2)]
return strings[(-1)]
|
'Reads the decoder file and initializes self.decoder from it.
Args:
filename: Name of text file mapping codes to utf8 strings.
Raises:
ValueError: if the input file is not parsed correctly.'
| def _InitializeDecoder(self, filename):
| line_re = re.compile('(?P<codes>\\d+(,\\d+)*)\\t(?P<utf8>.+)')
with tf.gfile.GFile(filename) as f:
for line in f:
m = line_re.match(line)
if (m is None):
raise ValueError('Unmatched line:', line)
str_codes = m.groupdict()['codes'].split(',')
codes = []
for code in str_codes:
codes.append(int(code))
utf8 = m.groupdict()['utf8']
num_codes = len(codes)
for (index, code) in enumerate(codes):
while (code >= len(self.decoder)):
self.decoder.append([])
self.decoder[code].append(Part(utf8, index, num_codes))
|
'Collapses CTC output to regular output.
Args:
ctc_labels: List of class labels including null characters to remove.
merge_dups: If True, Duplicate labels will be merged.
null_label: Label value to ignore.
All trailing zeros are removed!!
TODO(rays) This may become a problem with non-CTC models.
If using charset, this should not be a problem as zero is always space.
tf.pad can only append zero, so we have to be able to drop them, as a
non-ctc will have learned to output trailing zeros instead of trailing
nulls. This is awkward, as the stock ctc loss function requires that the
null character be num_classes-1.
Returns:
(List of) Labels with null characters removed.'
| def _CodesFromCTC(self, ctc_labels, merge_dups, null_label):
| out_labels = []
prev_label = (-1)
zeros_needed = 0
for label in ctc_labels:
if (label == null_label):
prev_label = (-1)
elif ((label != prev_label) or (not merge_dups)):
if (label == 0):
zeros_needed += 1
else:
if (merge_dups and (zeros_needed > 0)):
out_labels.append(0)
else:
out_labels += ([0] * zeros_needed)
zeros_needed = 0
out_labels.append(label)
prev_label = label
return out_labels
|
'Initialization. Currently only support amortized tracking.
Args:
total_examples: total number of examples.'
| def __init__(self, total_examples):
| assert (total_examples > 0)
self._total_examples = total_examples
self._eps_squared_sum = tf.Variable(tf.zeros([1]), trainable=False, name='eps_squared_sum')
self._delta_sum = tf.Variable(tf.zeros([1]), trainable=False, name='delta_sum')
|
'Accumulate the privacy spending.
Currently only support approximate privacy. Here we assume we use Gaussian
noise on randomly sampled batch so we get better composition: 1. the per
batch privacy is computed using privacy amplication via sampling bound;
2. the composition is done using the composition with Gaussian noise.
TODO(liqzhang) Add a link to a document that describes the bounds used.
Args:
eps_delta: EpsDelta pair which can be tensors.
unused_sigma: the noise sigma. Unused for this accountant.
num_examples: the number of examples involved.
Returns:
a TensorFlow operation for updating the privacy spending.'
| def accumulate_privacy_spending(self, eps_delta, unused_sigma, num_examples):
| (eps, delta) = eps_delta
with tf.control_dependencies([tf.Assert(tf.greater(delta, 0), ['delta needs to be greater than 0'])]):
amortize_ratio = ((tf.cast(num_examples, tf.float32) * 1.0) / self._total_examples)
amortize_eps = tf.reshape(tf.log((1.0 + (amortize_ratio * (tf.exp(eps) - 1.0)))), [1])
amortize_delta = tf.reshape((amortize_ratio * delta), [1])
return tf.group(*[tf.assign_add(self._eps_squared_sum, tf.square(amortize_eps)), tf.assign_add(self._delta_sum, amortize_delta)])
|
'Report the spending so far.
Args:
sess: the session to run the tensor.
target_eps: the target epsilon. Unused.
Returns:
the list containing a single EpsDelta, with values as Python floats (as
opposed to numpy.float64). This is to be consistent with
MomentAccountant which can return a list of (eps, delta) pair.'
| def get_privacy_spent(self, sess, target_eps=None):
| unused_target_eps = target_eps
(eps_squared_sum, delta_sum) = sess.run([self._eps_squared_sum, self._delta_sum])
return [EpsDelta(math.sqrt(eps_squared_sum), float(delta_sum))]
|
'Initialize a MomentsAccountant.
Args:
total_examples: total number of examples.
moment_orders: the order of moments to keep.'
| def __init__(self, total_examples, moment_orders=32):
| 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]
|
'Compute high moment of privacy loss.
Args:
sigma: the noise sigma, in the multiples of the sensitivity.
q: the sampling ratio.
moment_order: the order of moment.
Returns:
log E[exp(moment_order * X)]'
| @abc.abstractmethod
def _compute_log_moment(self, sigma, q, moment_order):
| pass
|
'Accumulate privacy spending.
In particular, accounts for privacy spending when we assume there
are num_examples, and we are releasing the vector
(sum_{i=1}^{num_examples} x_i) + Normal(0, stddev=l2norm_bound*sigma)
where l2norm_bound is the maximum l2_norm of each example x_i, and
the num_examples have been randomly selected out of a pool of
self.total_examples.
Args:
unused_eps_delta: EpsDelta pair which can be tensors. Unused
in this accountant.
sigma: the noise sigma, in the multiples of the sensitivity (that is,
if the l2norm sensitivity is k, then the caller must have added
Gaussian noise with stddev=k*sigma to the result of the query).
num_examples: the number of examples involved.
Returns:
a TensorFlow operation for updating the privacy spending.'
| def accumulate_privacy_spending(self, unused_eps_delta, sigma, num_examples):
| q = ((tf.cast(num_examples, tf.float64) * 1.0) / self._total_examples)
moments_accum_ops = []
for i in range(len(self._log_moments)):
moment = self._compute_log_moment(sigma, q, self._moment_orders[i])
moments_accum_ops.append(tf.assign_add(self._log_moments[i], moment))
return tf.group(*moments_accum_ops)
|
'Compute delta for given log_moments and eps.
Args:
log_moments: the log moments of privacy loss, in the form of pairs
of (moment_order, log_moment)
eps: the target epsilon.
Returns:
delta'
| def _compute_delta(self, log_moments, eps):
| min_delta = 1.0
for (moment_order, log_moment) in log_moments:
if (math.isinf(log_moment) or math.isnan(log_moment)):
sys.stderr.write(('The %d-th order is inf or Nan\n' % moment_order))
continue
if (log_moment < (moment_order * eps)):
min_delta = min(min_delta, math.exp((log_moment - (moment_order * eps))))
return min_delta
|
'Compute privacy spending in (e, d)-DP form for a single or list of eps.
Args:
sess: the session to run the tensor.
target_eps: a list of target epsilon\'s for which we would like to
compute corresponding delta value.
target_deltas: a list of target deltas for which we would like to
compute the corresponding eps value. Caller must specify
either target_eps or target_delta.
Returns:
A list of EpsDelta pairs.'
| def get_privacy_spent(self, sess, target_eps=None, target_deltas=None):
| assert ((target_eps is None) ^ (target_deltas is None))
eps_deltas = []
log_moments = sess.run(self._log_moments)
log_moments_with_order = zip(self._moment_orders, log_moments)
if (target_eps is not None):
for eps in target_eps:
eps_deltas.append(EpsDelta(eps, self._compute_delta(log_moments_with_order, eps)))
else:
assert target_deltas
for delta in target_deltas:
eps_deltas.append(EpsDelta(self._compute_eps(log_moments_with_order, delta), delta))
return eps_deltas
|
'Initialization.
Args:
total_examples: total number of examples.
moment_orders: the order of moments to keep.'
| def __init__(self, total_examples, moment_orders=32):
| super(self.__class__, self).__init__(total_examples, moment_orders)
self._binomial_table = utils.GenerateBinomialTable(self._max_moment_order)
|
'Compute 0 to t-th differential moments for Gaussian variable.
E[(P(x+s)/P(x+s-1)-1)^t]
= sum_{i=0}^t (t choose i) (-1)^{t-i} E[(P(x+s)/P(x+s-1))^i]
= sum_{i=0}^t (t choose i) (-1)^{t-i} E[exp(-i*(2*x+2*s-1)/(2*sigma^2))]
= sum_{i=0}^t (t choose i) (-1)^{t-i} exp(i(i+1-2*s)/(2 sigma^2))
Args:
sigma: the noise sigma, in the multiples of the sensitivity.
s: the shift.
t: 0 to t-th moment.
Returns:
0 to t-th moment as a tensor of shape [t+1].'
| def _differential_moments(self, sigma, s, t):
| assert (t <= self._max_moment_order), ('The order of %d is out of the upper bound %d.' % (t, self._max_moment_order))
binomial = tf.slice(self._binomial_table, [0, 0], [(t + 1), (t + 1)])
signs = numpy.zeros(((t + 1), (t + 1)), dtype=numpy.float64)
for i in range((t + 1)):
for j in range((t + 1)):
signs[(i, j)] = (1.0 - (2 * ((i - j) % 2)))
exponents = tf.constant([((j * ((j + 1.0) - (2.0 * s))) / ((2.0 * sigma) * sigma)) for j in range((t + 1))], dtype=tf.float64)
x = tf.multiply(binomial, signs)
y = tf.multiply(x, tf.exp(exponents))
z = tf.reduce_sum(y, 1)
return z
|
'Compute high moment of privacy loss.
Args:
sigma: the noise sigma, in the multiples of the sensitivity.
q: the sampling ratio.
moment_order: the order of moment.
Returns:
log E[exp(moment_order * X)]'
| def _compute_log_moment(self, sigma, q, moment_order):
| assert (moment_order <= self._max_moment_order), ('The order of %d is out of the upper bound %d.' % (moment_order, self._max_moment_order))
binomial_table = tf.slice(self._binomial_table, [moment_order, 0], [1, (moment_order + 1)])
qs = tf.exp((tf.constant([(i * 1.0) for i in range((moment_order + 1))], dtype=tf.float64) * tf.cast(tf.log(q), dtype=tf.float64)))
moments0 = self._differential_moments(sigma, 0.0, moment_order)
term0 = tf.reduce_sum(((binomial_table * qs) * moments0))
moments1 = self._differential_moments(sigma, 1.0, moment_order)
term1 = tf.reduce_sum(((binomial_table * qs) * moments1))
return tf.squeeze(tf.log(tf.cast(((q * term0) + ((1.0 - q) * term1)), tf.float64)))
|
'Associates `op_name` key with `pxg_class` value.
Registers `pxg_class` as the class that will be called to perform
per-example differentiation through ops with `op_name`.
Args:
op_name: String op name.
pxg_class: An instance of any class with the same signature as MatMulPXG.'
| def Register(self, op_name, pxg_class):
| self.d[op_name] = pxg_class
|
'Construct an instance of the rule for `op`.
Args:
op: The Operation to differentiate through.
colocate_gradients_with_ops: currently unsupported
gate_gradients: currently unsupported'
| def __init__(self, op, colocate_gradients_with_ops=False, gate_gradients=False):
| assert (op.node_def.op == 'MatMul')
self.op = op
self.colocate_gradients_with_ops = colocate_gradients_with_ops
self.gate_gradients = gate_gradients
|
'Build the graph for the per-example gradient through the op.
Assumes that the MatMul was called with a design matrix with examples
in rows as the first argument and parameters as the second argument.
Args:
x: The Tensor to differentiate with respect to. This tensor must
represent the weights.
z_grads: The list of gradients on the output of the op.
Returns:
x_grads: A Tensor containing the gradient with respect to `x` for
each example. This is a 3-D tensor, with the first axis corresponding
to examples and the remaining axes matching the shape of x.'
| def __call__(self, x, z_grads):
| idx = list(self.op.inputs).index(x)
assert (idx != (-1))
assert (len(z_grads) == len(self.op.outputs))
assert (idx == 1)
(x, _) = self.op.inputs
(z_grads,) = z_grads
x_expanded = tf.expand_dims(x, 2)
z_grads_expanded = tf.expand_dims(z_grads, 1)
return tf.multiply(x_expanded, z_grads_expanded)
|
'conv2d run separately per example, to help compute per-example gradients.
Args:
input_: tensor containing a minibatch of images / feature maps.
Shape [batch_size, rows, columns, channels]
w: convolution kernels. Shape
[kernel rows, kernel columns, input channels, output channels]
strides: passed through to regular conv_2d
padding: passed through to regular conv_2d
Returns:
conv: the output of the convolution.
single tensor, same as what regular conv_2d does
w_px: a list of batch_size copies of w. each copy was used
for the corresponding example in the minibatch.
calling tf.gradients on the copy gives the gradient for just
that example.'
| def _PxConv2DBuilder(self, input_, w, strides, padding):
| input_shape = [int(e) for e in input_.get_shape()]
batch_size = input_shape[0]
input_px = [tf.slice(input_, ([example] + ([0] * 3)), ([1] + input_shape[1:])) for example in xrange(batch_size)]
for input_x in input_px:
assert (int(input_x.get_shape()[0]) == 1)
w_px = [tf.identity(w) for example in xrange(batch_size)]
conv_px = [tf.nn.conv2d(input_x, w_x, strides=strides, padding=padding) for (input_x, w_x) in zip(input_px, w_px)]
for conv_x in conv_px:
num_x = int(conv_x.get_shape()[0])
assert (num_x == 1), num_x
assert (len(conv_px) == batch_size)
conv = tf.concat(axis=0, values=conv_px)
assert (int(conv.get_shape()[0]) == batch_size)
return (conv, w_px)
|
'Construct a differentially private gradient descent optimizer.
The optimizer uses fixed privacy budget for each batch of training.
Args:
learning_rate: for GradientDescentOptimizer.
eps_delta: EpsDelta pair for each epoch.
sanitizer: for sanitizing the graident.
sigma: noise sigma. If None, use eps_delta pair to compute sigma;
otherwise use supplied sigma directly.
use_locking: use locking.
name: name for the object.
batches_per_lot: Number of batches in a lot.'
| def __init__(self, learning_rate, eps_delta, sanitizer, sigma=None, use_locking=False, name='DPGradientDescent', batches_per_lot=1):
| super(DPGradientDescentOptimizer, self).__init__(learning_rate, use_locking, name)
self._batches_per_lot = batches_per_lot
self._grad_accum_dict = {}
if (batches_per_lot > 1):
self._batch_count = tf.Variable(1, dtype=tf.int32, trainable=False, name='batch_count')
var_list = tf.trainable_variables()
with tf.variable_scope('grad_acc_for'):
for var in var_list:
v_grad_accum = tf.Variable(tf.zeros_like(var), trainable=False, name=utils.GetTensorOpName(var))
self._grad_accum_dict[var.name] = v_grad_accum
self._eps_delta = eps_delta
self._sanitizer = sanitizer
self._sigma = sigma
|
'Compute the sanitized gradients.
Args:
loss: the loss tensor.
var_list: the optional variables.
add_noise: if true, then add noise. Always clip.
Returns:
a pair of (list of sanitized gradients) and privacy spending accumulation
operations.
Raises:
TypeError: if var_list contains non-variable.'
| def compute_sanitized_gradients(self, loss, var_list=None, add_noise=True):
| self._assert_valid_dtypes([loss])
xs = [tf.convert_to_tensor(x) for x in var_list]
px_grads = per_example_gradients.PerExampleGradients(loss, xs)
sanitized_grads = []
for (px_grad, v) in zip(px_grads, var_list):
tensor_name = utils.GetTensorOpName(v)
sanitized_grad = self._sanitizer.sanitize(px_grad, self._eps_delta, sigma=self._sigma, tensor_name=tensor_name, add_noise=add_noise, num_examples=(self._batches_per_lot * tf.slice(tf.shape(px_grad), [0], [1])))
sanitized_grads.append(sanitized_grad)
return sanitized_grads
|
'Minimize using sanitized gradients.
This gets a var_list which is the list of trainable variables.
For each var in var_list, we defined a grad_accumulator variable
during init. When batches_per_lot > 1, we accumulate the gradient
update in those. At the end of each lot, we apply the update back to
the variable. This has the effect that for each lot we compute
gradients at the point at the beginning of the lot, and then apply one
update at the end of the lot. In other words, semantically, we are doing
SGD with one lot being the equivalent of one usual batch of size
batch_size * batches_per_lot.
This allows us to simulate larger batches than our memory size would permit.
The lr and the num_steps are in the lot world.
Args:
loss: the loss tensor.
global_step: the optional global step.
var_list: the optional variables.
name: the optional name.
Returns:
the operation that runs one step of DP gradient descent.'
| def minimize(self, loss, global_step=None, var_list=None, name=None):
| if (var_list is None):
var_list = tf.trainable_variables()
for var in var_list:
if (not isinstance(var, tf.Variable)):
raise TypeError(('Argument is not a variable.Variable: %s' % var))
if (self._batches_per_lot == 1):
sanitized_grads = self.compute_sanitized_gradients(loss, var_list=var_list)
grads_and_vars = zip(sanitized_grads, var_list)
self._assert_valid_dtypes([v for (g, v) in grads_and_vars if (g is not None)])
apply_grads = self.apply_gradients(grads_and_vars, global_step=global_step, name=name)
return apply_grads
update_cond = tf.equal(tf.constant(0), tf.mod(self._batch_count, tf.constant(self._batches_per_lot)))
def non_last_in_lot_op(loss, var_list):
'Ops to do for a typical batch.\n\n For a batch that is not the last one in the lot, we simply compute the\n sanitized gradients and apply them to the grad_acc variables.\n\n Args:\n loss: loss function tensor\n var_list: list of variables\n Returns:\n A tensorflow op to do the updates to the gradient accumulators\n '
sanitized_grads = self.compute_sanitized_gradients(loss, var_list=var_list, add_noise=False)
update_ops_list = []
for (var, grad) in zip(var_list, sanitized_grads):
grad_acc_v = self._grad_accum_dict[var.name]
update_ops_list.append(grad_acc_v.assign_add(grad))
update_ops_list.append(self._batch_count.assign_add(1))
return tf.group(*update_ops_list)
def last_in_lot_op(loss, var_list, global_step):
'Ops to do for last batch in a lot.\n\n For the last batch in the lot, we first add the sanitized gradients to\n the gradient acc variables, and then apply these\n values over to the original variables (via an apply gradient)\n\n Args:\n loss: loss function tensor\n var_list: list of variables\n global_step: optional global step to be passed to apply_gradients\n Returns:\n A tensorflow op to push updates from shadow vars to real vars.\n '
sanitized_grads = self.compute_sanitized_gradients(loss, var_list=var_list, add_noise=True)
normalized_grads = []
for (var, grad) in zip(var_list, sanitized_grads):
grad_acc_v = self._grad_accum_dict[var.name]
normalized_grad = tf.div(grad_acc_v.assign_add(grad), tf.to_float(self._batches_per_lot))
normalized_grads.append(normalized_grad)
with tf.control_dependencies(normalized_grads):
grads_and_vars = zip(normalized_grads, var_list)
self._assert_valid_dtypes([v for (g, v) in grads_and_vars if (g is not None)])
apply_san_grads = self.apply_gradients(grads_and_vars, global_step=global_step, name='apply_grads')
resets_list = []
with tf.control_dependencies([apply_san_grads]):
for (_, acc) in self._grad_accum_dict.items():
reset = tf.assign(acc, tf.zeros_like(acc))
resets_list.append(reset)
resets_list.append(self._batch_count.assign_add(1))
last_step_update = tf.group(*([apply_san_grads] + resets_list))
return last_step_update
update_op = tf.cond(update_cond, (lambda : last_in_lot_op(loss, var_list, global_step)), (lambda : non_last_in_lot_op(loss, var_list)))
return tf.group(update_op)
|
'Construct an AmortizedGaussianSanitizer.
Args:
accountant: the privacy accountant. Expect an amortized one.
default_option: the default ClipOptoin.'
| def __init__(self, accountant, default_option):
| self._accountant = accountant
self._default_option = default_option
self._options = {}
|
'Set options for an individual tensor.
Args:
tensor_name: the name of the tensor.
option: clip option.'
| def set_option(self, tensor_name, option):
| self._options[tensor_name] = option
|
'Sanitize the given tensor.
This santize a given tensor by first applying l2 norm clipping and then
adding Gaussian noise. It calls the privacy accountant for updating the
privacy spending.
Args:
x: the tensor to sanitize.
eps_delta: a pair of eps, delta for (eps,delta)-DP. Use it to
compute sigma if sigma is None.
sigma: if sigma is not None, use sigma.
option: a ClipOption which, if supplied, used for
clipping and adding noise.
tensor_name: the name of the tensor.
num_examples: if None, use the number of "rows" of x.
add_noise: if True, then add noise, else just clip.
Returns:
a pair of sanitized tensor and the operation to accumulate privacy
spending.'
| def sanitize(self, x, eps_delta, sigma=None, option=ClipOption(None, None), tensor_name=None, num_examples=None, add_noise=True):
| if (sigma is None):
(eps, delta) = eps_delta
with tf.control_dependencies([tf.Assert(tf.greater(eps, 0), ['eps needs to be greater than 0']), tf.Assert(tf.greater(delta, 0), ['delta needs to be greater than 0'])]):
sigma = (tf.sqrt((2.0 * tf.log((1.25 / delta)))) / eps)
(l2norm_bound, clip) = option
if (l2norm_bound is None):
(l2norm_bound, clip) = self._default_option
if ((tensor_name is not None) and (tensor_name in self._options)):
(l2norm_bound, clip) = self._options[tensor_name]
if clip:
x = utils.BatchClipByL2norm(x, l2norm_bound)
if add_noise:
if (num_examples is None):
num_examples = tf.slice(tf.shape(x), [0], [1])
privacy_accum_op = self._accountant.accumulate_privacy_spending(eps_delta, sigma, num_examples)
with tf.control_dependencies([privacy_accum_op]):
saned_x = utils.AddGaussianNoise(tf.reduce_sum(x, 0), (sigma * l2norm_bound))
else:
saned_x = tf.reduce_sum(x, 0)
return saned_x
|
'Initializes the cell.
Args:
num_units: Number of cell units.
w_initializer: Initializer for the "W" (input) parameter matrices.
u_initializer: Initializer for the "U" (recurrent) parameter matrices.
b_initializer: Initializer for the "b" (bias) parameter vectors.
activation: Cell activation function.'
| def __init__(self, num_units, w_initializer, u_initializer, b_initializer, activation=tf.nn.tanh):
| self._num_units = num_units
self._w_initializer = w_initializer
self._u_initializer = u_initializer
self._b_initializer = b_initializer
self._activation = activation
|
'Returns an initializer for the "W_h" parameter matrix.
See equation (23) in the paper. The "W_h" parameter matrix is the
concatenation of two parameter submatrices. The matrix returned is
[U_z, U_r].
Returns:
A Tensor with shape [num_units, 2 * num_units] as described above.'
| def _w_h_initializer(self):
| def _initializer(shape, dtype=tf.float32, partition_info=None):
num_units = self._num_units
assert (shape == [num_units, (2 * num_units)])
u_z = self._u_initializer([num_units, num_units], dtype, partition_info)
u_r = self._u_initializer([num_units, num_units], dtype, partition_info)
return tf.concat([u_z, u_r], 1)
return _initializer
|
'Returns an initializer for the "W_x" parameter matrix.
See equation (23) in the paper. The "W_x" parameter matrix is the
concatenation of two parameter submatrices. The matrix returned is
[W_z, W_r].
Args:
input_dim: The dimension of the cell inputs.
Returns:
A Tensor with shape [input_dim, 2 * num_units] as described above.'
| def _w_x_initializer(self, input_dim):
| def _initializer(shape, dtype=tf.float32, partition_info=None):
num_units = self._num_units
assert (shape == [input_dim, (2 * num_units)])
w_z = self._w_initializer([input_dim, num_units], dtype, partition_info)
w_r = self._w_initializer([input_dim, num_units], dtype, partition_info)
return tf.concat([w_z, w_r], 1)
return _initializer
|
'GRU cell with layer normalization.'
| def __call__(self, inputs, state, scope=None):
| input_dim = inputs.get_shape().as_list()[1]
num_units = self._num_units
with tf.variable_scope((scope or 'gru_cell')):
with tf.variable_scope('gates'):
w_h = tf.get_variable('w_h', [num_units, (2 * num_units)], initializer=self._w_h_initializer())
w_x = tf.get_variable('w_x', [input_dim, (2 * num_units)], initializer=self._w_x_initializer(input_dim))
z_and_r = (_layer_norm(tf.matmul(state, w_h), scope='layer_norm/w_h') + _layer_norm(tf.matmul(inputs, w_x), scope='layer_norm/w_x'))
(z, r) = tf.split(tf.sigmoid(z_and_r), 2, 1)
with tf.variable_scope('candidate'):
w = tf.get_variable('w', [input_dim, num_units], initializer=self._w_initializer)
u = tf.get_variable('u', [num_units, num_units], initializer=self._u_initializer)
h_hat = ((r * _layer_norm(tf.matmul(state, u), scope='layer_norm/u')) + _layer_norm(tf.matmul(inputs, w), scope='layer_norm/w'))
new_h = (((1 - z) * state) + (z * self._activation(h_hat)))
return (new_h, new_h)
|
'Basic setup. The actual TensorFlow graph is constructed in build().
Args:
config: Object containing configuration parameters.
mode: "train", "eval" or "encode".
input_reader: Subclass of tf.ReaderBase for reading the input serialized
tf.Example protocol buffers. Defaults to TFRecordReader.
Raises:
ValueError: If mode is invalid.'
| def __init__(self, config, mode='train', input_reader=None):
| if (mode not in ['train', 'eval', 'encode']):
raise ValueError(('Unrecognized mode: %s' % mode))
self.config = config
self.mode = mode
self.reader = (input_reader if input_reader else tf.TFRecordReader())
self.uniform_initializer = tf.random_uniform_initializer(minval=(- self.config.uniform_init_scale), maxval=self.config.uniform_init_scale)
self.encode_ids = None
self.decode_pre_ids = None
self.decode_post_ids = None
self.encode_mask = None
self.decode_pre_mask = None
self.decode_post_mask = None
self.encode_emb = None
self.decode_pre_emb = None
self.decode_post_emb = None
self.thought_vectors = None
self.target_cross_entropy_losses = []
self.target_cross_entropy_loss_weights = []
self.total_loss = None
|
'Builds the ops for reading input data.
Outputs:
self.encode_ids
self.decode_pre_ids
self.decode_post_ids
self.encode_mask
self.decode_pre_mask
self.decode_post_mask'
| def build_inputs(self):
| if (self.mode == 'encode'):
encode_ids = None
decode_pre_ids = None
decode_post_ids = None
encode_mask = tf.placeholder(tf.int8, (None, None), name='encode_mask')
decode_pre_mask = None
decode_post_mask = None
else:
input_queue = input_ops.prefetch_input_data(self.reader, self.config.input_file_pattern, shuffle=self.config.shuffle_input_data, capacity=self.config.input_queue_capacity, num_reader_threads=self.config.num_input_reader_threads)
serialized = input_queue.dequeue_many(self.config.batch_size)
(encode, decode_pre, decode_post) = input_ops.parse_example_batch(serialized)
encode_ids = encode.ids
decode_pre_ids = decode_pre.ids
decode_post_ids = decode_post.ids
encode_mask = encode.mask
decode_pre_mask = decode_pre.mask
decode_post_mask = decode_post.mask
self.encode_ids = encode_ids
self.decode_pre_ids = decode_pre_ids
self.decode_post_ids = decode_post_ids
self.encode_mask = encode_mask
self.decode_pre_mask = decode_pre_mask
self.decode_post_mask = decode_post_mask
|
'Builds the word embeddings.
Inputs:
self.encode_ids
self.decode_pre_ids
self.decode_post_ids
Outputs:
self.encode_emb
self.decode_pre_emb
self.decode_post_emb'
| def build_word_embeddings(self):
| if (self.mode == 'encode'):
encode_emb = tf.placeholder(tf.float32, (None, None, self.config.word_embedding_dim), 'encode_emb')
decode_pre_emb = None
decode_post_emb = None
else:
word_emb = tf.get_variable(name='word_embedding', shape=[self.config.vocab_size, self.config.word_embedding_dim], initializer=self.uniform_initializer)
encode_emb = tf.nn.embedding_lookup(word_emb, self.encode_ids)
decode_pre_emb = tf.nn.embedding_lookup(word_emb, self.decode_pre_ids)
decode_post_emb = tf.nn.embedding_lookup(word_emb, self.decode_post_ids)
self.encode_emb = encode_emb
self.decode_pre_emb = decode_pre_emb
self.decode_post_emb = decode_post_emb
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.