def inference(self):
"""main computation graph here: 1. embeddding layer, 2.Bi-LSTM layer, 3.concat, 4.FC layer 5.softmax """
#1.get emebedding of words in the sentence
self.embedded_words = tf.nn.embedding_lookup(self.Embedding,self.input_x) #shape:[None,sentence_length,embed_size]
#2. Bi-lstm layer
# define lstm cess:get lstm cell output
lstm_fw_cell=rnn.BasicLSTMCell(self.hidden_size) #forward direction cell
lstm_bw_cell=rnn.BasicLSTMCell(self.hidden_size) #backward direction cell
if self.dropout_keep_prob is not None:
lstm_fw_cell=rnn.DropoutWrapper(lstm_fw_cell,output_keep_prob=self.dropout_keep_prob)
lstm_bw_cell=rnn.DropoutWrapper(lstm_bw_cell,output_keep_prob=self.dropout_keep_prob)
# bidirectional_dynamic_rnn: input: [batch_size, max_time, input_size]
# output: A tuple (outputs, output_states)
# where:outputs: A tuple (output_fw, output_bw) containing the forward and the backward rnn output `Tensor`.
outputs,_=tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell,lstm_bw_cell,self.embedded_words,dtype=tf.float32) #[batch_size,sequence_length,hidden_size] #creates a dynamic bidirectional recurrent neural network
print("outputs:===>",outputs) #outputs:(<tf.Tensor 'bidirectional_rnn/fw/fw/transpose:0' shape=(?, 5, 100) dtype=float32>, <tf.Tensor 'ReverseV2:0' shape=(?, 5, 100) dtype=float32>))
#3. concat output
output_rnn=tf.concat(outputs,axis=2) #[batch_size,sequence_length,hidden_size*2]
self.output_rnn_last=tf.reduce_mean(output_rnn,axis=1) #[batch_size,hidden_size*2] #output_rnn_last=output_rnn[:,-1,:] ##[batch_size,hidden_size*2] #TODO
print("output_rnn_last:", self.output_rnn_last) # <tf.Tensor 'strided_slice:0' shape=(?, 200) dtype=float32>
#4. logits(use linear layer)
with tf.name_scope("output"): #inputs: A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network.
logits = tf.matmul(self.output_rnn_last, self.W_projection) + self.b_projection # [batch_size,num_classes]
return logits
python类Tensor()的实例源码
def masked_apply(tensor, op, mask):
"""Applies `op` to tensor only at locations indicated by `mask` and sets the rest to zero.
Similar to doing `tensor = tf.where(mask, op(tensor), tf.zeros_like(tensor))` but it behaves correctly
when `op(tensor)` is NaN or inf while tf.where does not.
:param tensor: tf.Tensor
:param op: tf.Op
:param mask: tf.Tensor with dtype == bool
:return: tf.Tensor
"""
chosen = tf.boolean_mask(tensor, mask)
applied = op(chosen)
idx = tf.to_int32(tf.where(mask))
result = tf.scatter_nd(idx, applied, tf.shape(tensor))
return result
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
def repeat(tensor: tf.Tensor, repeats: int, axis: int) -> tf.Tensor:
"""
Repeat elements of the input tensor in the specified axis ``repeats``-times.
.. note::
Chaining of this op may produce TF warnings although the performance seems to be unaffected.
:param tensor: TF tensor to be repeated
:param repeats: number of repeats
:param axis: axis to repeat
:return: tensor with repeated elements
"""
shape = tensor.get_shape().as_list()
dims = np.arange(len(tensor.shape))
prepare_perm = np.hstack(([axis], np.delete(dims, axis)))
restore_perm = np.hstack((dims[1:axis+1], [0], dims[axis+1:]))
indices = tf.cast(tf.floor(tf.range(0, shape[axis]*repeats)/tf.constant(repeats)), 'int32')
shuffled = tf.transpose(tensor, prepare_perm)
repeated = tf.gather(shuffled, indices)
return tf.transpose(repeated, restore_perm)
def bin_stats(predictions: tf.Tensor, labels: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
"""
Calculate f1, precision and recall from binary classification expected and predicted values.
:param predictions: 2-d tensor (batch, predictions) of predicted 0/1 classes
:param labels: 2-d tensor (batch, labels) of expected 0/1 classes
:return: a tuple of batched (f1, precision and recall) values
"""
predictions = tf.cast(predictions, tf.int32)
labels = tf.cast(labels, tf.int32)
true_positives = tf.reduce_sum((predictions * labels), axis=1)
false_positives = tf.reduce_sum(tf.cast(tf.greater(predictions, labels), tf.int32), axis=1)
false_negatives = tf.reduce_sum(tf.cast(tf.greater(labels, predictions), tf.int32), axis=1)
recall = true_positives / (true_positives + false_negatives)
precision = true_positives / (true_positives + false_positives)
f1_score = 2 / (1 / precision + 1 / recall)
return f1_score, precision, recall
def bin_dice(predictions: tf.Tensor, labels: tf.Tensor) -> tf.Tensor:
"""
Calculate Sorensen–Dice coefficient from the given binary classification expected and predicted values.
The coefficient is defined as :math:`2*|X \cup Y| / (|X| + |Y|)`.
:param predictions: 2-d tensor (batch, predictions) of predicted 0/1 classes
:param labels: 2-d tensor (batch, labels) of expected 0/1 classes
:return: batched Sørensen–Dice coefficients
"""
predictions = tf.cast(predictions, tf.int32)
labels = tf.cast(labels, tf.int32)
true_positives = tf.reduce_sum((predictions * labels), axis=1)
pred_positives = tf.reduce_sum(predictions, axis=1)
label_positives = tf.reduce_sum(labels, axis=1)
return 2 * true_positives / (pred_positives + label_positives)
def loss_nce(self,l2_lambda=0.0001): #0.0001-->0.001
"""calculate loss using (NCE)cross entropy here"""
# Compute the average NCE loss for the batch.
# tf.nce_loss automatically draws a new sample of the negative labels each
# time we evaluate the loss.
if self.is_training: #training
#labels=tf.reshape(self.input_y,[-1]) #[batch_size,1]------>[batch_size,]
labels=tf.expand_dims(self.input_y,1) #[batch_size,]----->[batch_size,1]
loss = tf.reduce_mean( #inputs: A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network.
tf.nn.nce_loss(weights=tf.transpose(self.W_projection),#[hidden_size*2, num_classes]--->[num_classes,hidden_size*2]. nce_weights:A `Tensor` of shape `[num_classes, dim].O.K.
biases=self.b_projection, #[label_size]. nce_biases:A `Tensor` of shape `[num_classes]`.
labels=labels, #[batch_size,1]. train_labels, # A `Tensor` of type `int64` and shape `[batch_size,num_true]`. The target classes.
inputs=self.output_rnn_last,# [batch_size,hidden_size*2] #A `Tensor` of shape `[batch_size, dim]`. The forward activations of the input network.
num_sampled=self.num_sampled, #scalar. 100
num_classes=self.num_classes,partition_strategy="div")) #scalar. 1999
l2_losses = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'bias' not in v.name]) * l2_lambda
loss = loss + l2_losses
return loss
def compute_exponential_averages(variables, decay):
"""Given a list of tensorflow scalar variables
create ops corresponding to their exponential
averages
Parameters
----------
variables: [tf.Tensor]
List of scalar tensors.
Returns
-------
averages: [tf.Tensor]
List of scalar tensors corresponding to averages
of al the `variables` (in order)
apply_op: tf.runnable
Op to be run to update the averages with current value
of variables.
"""
averager = tf.train.ExponentialMovingAverage(decay=decay)
apply_op = averager.apply(variables)
return [averager.average(v) for v in variables], apply_op
def make_moving_average(name, value, init, decay, log=True):
"""Creates an exp-moving average of `value` and an update op, which is added to UPDATE_OPS collection.
:param name: string, name of the created moving average tf.Variable
:param value: tf.Tensor, the value to be averaged
:param init: float, an initial value for the moving average
:param decay: float between 0 and 1, exponential decay of the moving average
:param log: bool, add a summary op if True
:return: tf.Tensor, the moving average
"""
var = tf.get_variable(name, shape=value.get_shape(),
initializer=tf.constant_initializer(init), trainable=False)
update = moving_averages.assign_moving_average(var, value, decay, zero_debias=False)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update)
if log:
tf.summary.scalar(name, var)
return var
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 image_reading(path: str, resized_size: Tuple[int, int]=None, data_augmentation: bool=False,
padding: bool=False) -> Tuple[tf.Tensor, tf.Tensor]:
# Read image
image_content = tf.read_file(path, name='image_reader')
image = tf.cond(tf.equal(tf.string_split([path], '.').values[1], tf.constant('jpg', dtype=tf.string)),
true_fn=lambda: tf.image.decode_jpeg(image_content, channels=1, try_recover_truncated=True), # TODO channels = 3 ?
false_fn=lambda: tf.image.decode_png(image_content, channels=1), name='image_decoding')
# Data augmentation
if data_augmentation:
image = augment_data(image)
# Padding
if padding:
with tf.name_scope('padding'):
image, img_width = padding_inputs_width(image, resized_size, increment=CONST.DIMENSION_REDUCTION_W_POOLING)
# Resize
else:
image = tf.image.resize_images(image, size=resized_size)
img_width = tf.shape(image)[1]
with tf.control_dependencies([tf.assert_equal(image.shape[:2], resized_size)]):
return image, img_width
def random_rotation(img: tf.Tensor, max_rotation: float=0.1, crop: bool=True) -> tf.Tensor: # from SeguinBe
with tf.name_scope('RandomRotation'):
rotation = tf.random_uniform([], -max_rotation, max_rotation)
rotated_image = tf.contrib.image.rotate(img, rotation, interpolation='BILINEAR')
if crop:
rotation = tf.abs(rotation)
original_shape = tf.shape(rotated_image)[:2]
h, w = original_shape[0], original_shape[1]
# see https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders for formulae
old_l, old_s = tf.cond(h > w, lambda: [h, w], lambda: [w, h])
old_l, old_s = tf.cast(old_l, tf.float32), tf.cast(old_s, tf.float32)
new_l = (old_l * tf.cos(rotation) - old_s * tf.sin(rotation)) / tf.cos(2*rotation)
new_s = (old_s - tf.sin(rotation) * new_l) / tf.cos(rotation)
new_h, new_w = tf.cond(h > w, lambda: [new_l, new_s], lambda: [new_s, new_l])
new_h, new_w = tf.cast(new_h, tf.int32), tf.cast(new_w, tf.int32)
bb_begin = tf.cast(tf.ceil((h-new_h)/2), tf.int32), tf.cast(tf.ceil((w-new_w)/2), tf.int32)
rotated_image_crop = rotated_image[bb_begin[0]:h - bb_begin[0], bb_begin[1]:w - bb_begin[1], :]
# If crop removes the entire image, keep the original image
rotated_image = tf.cond(tf.equal(tf.size(rotated_image_crop), 0),
true_fn=lambda: img,
false_fn=lambda: rotated_image_crop)
return rotated_image
def create_training_output(self, shared_resources: SharedResources,
training_input_tensors: Mapping[TensorPort, tf.Tensor]) \
-> Mapping[TensorPort, tf.Tensor]:
"""
This function needs to be implemented in order to define how the module produces tensors only used
during training given tensors corresponding to the ones defined by `training_input_ports`, which might include
tensors corresponding to ports defined by `output_ports`. This sub-graph should only be created during training.
Args:
shared_resources: contains resources shared by modules, such as hyper-parameters or vocabularies.
training_input_tensors: a mapping from training input tensorports to tensors.
Returns:
mapping from defined training output ports to their tensors.
"""
raise NotImplementedError
def __init__(self, incoming, shape, **kwargs):
super(ReshapeLayer, self).__init__(incoming, **kwargs)
shape = tuple(shape)
for s in shape:
if isinstance(s, int):
if s == 0 or s < - 1:
raise ValueError("`shape` integers must be positive or -1")
elif isinstance(s, list):
if len(s) != 1 or not isinstance(s[0], int) or s[0] < 0:
raise ValueError("`shape` input references must be "
"single-element lists of int >= 0")
elif isinstance(s, (tf.Tensor, tf.Variable)): # T.TensorVariable):
raise NotImplementedError
# if s.ndim != 0:
# raise ValueError(
# "A symbolic variable in a shape specification must be "
# "a scalar, but had %i dimensions" % s.ndim)
else:
raise ValueError("`shape` must be a tuple of int and/or [int]")
if sum(s == -1 for s in shape) > 1:
raise ValueError("`shape` cannot contain multiple -1")
self.shape = shape
# try computing the output shape once as a sanity check
self.get_output_shape_for(self.input_shape)
def apply_gradients(self, grads_and_vars, global_step=None, name=None):
with tf.name_scope(name, self._name) as name:
update_op = self._opt.apply_gradients(
grads_and_vars, global_step=global_step)
add_noise_ops = []
with tf.control_dependencies([update_op]):
for grad, var in grads_and_vars:
if grad is None:
continue
with tf.name_scope("sgld_noise_" + var.op.name):
if isinstance(grad, tf.Tensor):
add_noise_ops.append(self._noise_dense(var))
else:
add_noise_ops.append(self._noise_sparse(grad, var))
## running combined op
return tf.group(*([update_op] + add_noise_ops), name=name)
def apply_gradients(self, grads_and_vars, global_step=None, name=None):
with tf.name_scope(name, self._name) as name:
update_op = self._opt.apply_gradients(
grads_and_vars, global_step=global_step)
add_noise_ops = []
with tf.control_dependencies([update_op]):
for grad, var in grads_and_vars:
if grad is None:
continue
with tf.name_scope("psgld_noise_" + var.op.name):
if isinstance(grad, tf.Tensor):
add_noise_ops.append(self._noise_dense(var))
else:
add_noise_ops.append(self._noise_sparse(grad, var))
## running combined op
return tf.group(*([update_op] + add_noise_ops), name=name)
def compute_exponential_averages(variables, decay):
"""Given a list of tensorflow scalar variables
create ops corresponding to their exponential
averages
Parameters
----------
variables: [tf.Tensor]
List of scalar tensors.
Returns
-------
averages: [tf.Tensor]
List of scalar tensors corresponding to averages
of al the `variables` (in order)
apply_op: tf.runnable
Op to be run to update the averages with current value
of variables.
"""
averager = tf.train.ExponentialMovingAverage(decay=decay)
apply_op = averager.apply(variables)
return [averager.average(v) for v in variables], apply_op
def is_same_dynamic_shape(x, y):
"""
Whether `x` and `y` has the same dynamic shape.
:param x: A Tensor.
:param y: A Tensor.
:return: A scalar Tensor of `bool`.
"""
# There is a BUG of Tensorflow for not doing static shape inference
# right in nested tf.cond()'s, so we are not comparing x and y's
# shape directly but working with their concatenations.
return tf.cond(
tf.equal(tf.rank(x), tf.rank(y)),
lambda: tf.reduce_all(tf.equal(
tf.concat([tf.shape(x), tf.shape(y)], 0),
tf.concat([tf.shape(y), tf.shape(x)], 0))),
lambda: tf.convert_to_tensor(False, tf.bool))
tf_dataset_generator.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def __init__(self, name, generator, output_types, output_shapes=None, min_queue_examples=0,
shuffle_size=None, padded_shapes=None, padded_values=None):
"""
:param name: name of the dataset.
:param generator generator: generator of elements in of the dataset
:param output_types: list of output types of the generator
:param output_shapes: output shapes of the generator
:param int min_queue_examples: minimum number of examples to queue, this value should be
proportional to the ram of the computer. By default is 0
:param int shuffle_size: size of the buffer for shuffling, this value should be
proportional to the ram of the computer
:param List[tf.Tensor] padded_shapes: shape for padding the batch
:param tf.Tensor padded_values: values for the padding
"""
if not callable(generator):
raise TypeError("`generator` must be callable.")
self.name = name
self.generator = generator
self.output_types = output_types
self.output_shapes = output_shapes
self.min_queue_examples = min_queue_examples
self.shuffle_size = shuffle_size
self.padded_shapes = padded_shapes
self.padded_values = padded_values
def __init__(self, name, data_files_pattern, dataset_class=TextLineDataset,
min_queue_examples=0, shuffle_size=None, padded_shapes=None, padded_values=None):
"""
:param name: name of the dataset.
:param str data_files_pattern: pattern of the data files
:param Dataset dataset_class: class to create the dataset with the files. By default is
TextLineDataset
:param int min_queue_examples: minimum number of examples to queue, this value should be
proportional to the ram of the computer. By default is 0
:param int shuffle_size: size of the buffer for shuffling, this value should be
proportional to the ram of the computer
:param List[tf.Tensor] padded_shapes: shape for padding the batch
:param tf.Tensor padded_values: values for the padding
"""
self.name = name
self.data_files_pattern = data_files_pattern
self.dataset_class = dataset_class
self.min_queue_examples = min_queue_examples
self.shuffle_size = shuffle_size
self.padded_shapes = padded_shapes
self.padded_values = padded_values
self._size = None
text_classification_model_simple.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def optimize(self, loss, global_step,
learning_rate_initial=TC_LEARNING_RATE_INITIAL,
learning_rate_decay=TC_LEARNING_RATE_DECAY,
learning_rate_decay_steps=TC_LEARNING_RATE_DECAY_STEPS):
"""
Creates a learning rate and an optimizer for the loss
:param tf.Tensor loss: the tensor with the loss of the model
:param tf.Tensor global_step: the global step for training
:param int learning_rate_initial: the initial learning rate
:param int learning_rate_decay: the decay of the learning rate
:param int learning_rate_decay_steps: the number of steps to decay the learning rate
:return (tf.Tensor, tf.Tensor): a tuple with the optimizer and the learning rate
"""
learning_rate = tf.train.exponential_decay(learning_rate_initial, global_step,
learning_rate_decay_steps,
learning_rate_decay,
staircase=True, name='learning_rate')
# optimizer
optimizer = tf.train.RMSPropOptimizer(learning_rate)
# optimizer = tf.train.GradientDescentOptimizer(learning_rate)
# optimizer = tf.train.AdamOptimizer(learning_rate)
optimizer = optimizer.minimize(loss, global_step=global_step)
return optimizer, learning_rate
def multilabel_image_to_class(label_image: tf.Tensor, classes_file: str) -> tf.Tensor:
classes_color_values, colors_labels = get_classes_color_from_file_multilabel(classes_file)
# Convert label_image [H,W,3] to the classes [H,W,C],int32 according to the classes [C,3]
with tf.name_scope('LabelAssign'):
if len(label_image.get_shape()) == 3:
diff = tf.cast(label_image[:, :, None, :], tf.float32) - tf.constant(classes_color_values[None, None, :, :]) # [H,W,C,3]
elif len(label_image.get_shape()) == 4:
diff = tf.cast(label_image[:, :, :, None, :], tf.float32) - tf.constant(
classes_color_values[None, None, None, :, :]) # [B,H,W,C,3]
else:
raise NotImplementedError('Length is : {}'.format(len(label_image.get_shape())))
pixel_class_diff = tf.reduce_sum(tf.square(diff), axis=-1) # [H,W,C] or [B,H,W,C]
class_label = tf.argmin(pixel_class_diff, axis=-1) # [H,W] or [B,H,W]
return tf.gather(colors_labels, class_label) > 0
def data_augmentation_fn(input_image: tf.Tensor, label_image: tf.Tensor) -> (tf.Tensor, tf.Tensor):
with tf.name_scope('DataAugmentation'):
with tf.name_scope('random_flip_lr'):
sample = tf.random_uniform([], 0, 1)
label_image = tf.cond(sample > 0.5, lambda: tf.image.flip_left_right(label_image), lambda: label_image)
input_image = tf.cond(sample > 0.5, lambda: tf.image.flip_left_right(input_image), lambda: input_image)
with tf.name_scope('random_flip_ud'):
sample = tf.random_uniform([], 0, 1)
label_image = tf.cond(sample > 0.5, lambda: tf.image.flip_up_down(label_image), lambda: label_image)
input_image = tf.cond(sample > 0.5, lambda: tf.image.flip_up_down(input_image), lambda: input_image)
chanels = input_image.get_shape()[-1]
input_image = tf.image.random_contrast(input_image, lower=0.8, upper=1.0)
if chanels == 3:
input_image = tf.image.random_hue(input_image, max_delta=0.1)
input_image = tf.image.random_saturation(input_image, lower=0.8, upper=1.2)
return input_image, label_image
def extract_patches_fn(image: tf.Tensor, patch_shape: list, offsets) -> tf.Tensor:
"""
:param image: tf.Tensor
:param patch_shape: [h, w]
:param offsets: tuple between 0 and 1
:return: patches [batch_patches, h, w, c]
"""
with tf.name_scope('patch_extraction'):
h, w = patch_shape
c = image.get_shape()[-1]
offset_h = tf.cast(tf.round(offsets[0] * h // 2), dtype=tf.int32)
offset_w = tf.cast(tf.round(offsets[1] * w // 2), dtype=tf.int32)
offset_img = image[offset_h:, offset_w:, :]
offset_img = offset_img[None, :, :, :]
patches = tf.extract_image_patches(offset_img, ksizes=[1, h, w, 1], strides=[1, h // 2, w // 2, 1],
rates=[1, 1, 1, 1], padding='VALID')
patches_shape = tf.shape(patches)
return tf.reshape(patches, [tf.reduce_prod(patches_shape[0:3]), h, w, int(c)]) # returns [batch_patches, h, w, c]
def tf_obj_shape(input):
"""
Convert tf objects to shape tuple.
Arguments:
input: tf.TensorShape, tf.Tensor, tf.AttrValue or tf.NodeDef
the corresponding tensorflow object
Returns:
tuple: shape of the tensorflow object
"""
if isinstance(input, tf.TensorShape):
return tuple([int(i.value) for i in input])
elif isinstance(input, tf.Tensor):
return tf_obj_shape(input.get_shape())
elif isinstance(input, tf.AttrValue):
return tuple([int(d.size) for d in input.shape.dim])
elif isinstance(input, tf.NodeDef):
return tf_obj_shape(input.attr['shape'])
else:
raise TypeError("Input to `tf_obj_shape` has the wrong type.")
def get_weights(self, weight_tensor):
""" Get Weights.
Get a variable weights.
Examples:
dnn = DNNTrainer(...)
w = dnn.get_weights(denselayer.W) # get a dense layer weights
w = dnn.get_weights(convlayer.b) # get a conv layer biases
```
Arguments:
weight_tensor: `Tensor`. A Variable.
Returns:
`np.array`. The provided variable weights.
"""
return weight_tensor.eval(self.trainer.session)
```
def __init__(self, network, dictionary=None, seq_maxlen=25,
clip_gradients=0.0, tensorboard_verbose=0,
tensorboard_dir="/tmp/tflearn_logs/",
checkpoint_path=None, max_checkpoints=None,
session=None):
assert isinstance(network, tf.Tensor), "'network' arg is not a Tensor!"
self.net = network
self.train_ops = tf.get_collection(tf.GraphKeys.TRAIN_OPS)
self.trainer = Trainer(self.train_ops,
clip_gradients=clip_gradients,
tensorboard_dir=tensorboard_dir,
tensorboard_verbose=tensorboard_verbose,
checkpoint_path=checkpoint_path,
max_checkpoints=max_checkpoints,
session=session)
self.session = self.trainer.session
self.inputs = tf.get_collection(tf.GraphKeys.INPUTS)
self.targets = tf.get_collection(tf.GraphKeys.TARGETS)
self.predictor = Evaluator([self.net],
session=self.session)
self.dic = dictionary
self.rev_dic = reverse_dictionary(dictionary)
self.seq_maxlen = seq_maxlen
def get_weights(self, weight_tensor):
""" Get weights.
Get a variable weights.
Examples:
sgen = SequenceGenerator(...)
w = sgen.get_weights(denselayer.W) -- get a dense layer weights
Arguments:
weight_tensor: `tf.Tensor`. A Variable.
Returns:
`np.array`. The provided variable weights.
"""
return weight_tensor.eval(self.trainer.session)
def softmax(x):
""" Softmax.
Computes softmax activations.
For each batch `i` and class `j` we have
softmax[i, j] = exp(logits[i, j]) / sum(exp(logits[i]))
Arguments:
x: A `Tensor`. Must be one of the following types: `float32`,
`float64`. 2-D with shape `[batch_size, num_classes]`.
Returns:
A `Tensor`. Has the same type as `x`. Same shape as `x`.
"""
return tf.nn.softmax(x)
def elu(x):
""" ELU.
Exponential Linear Unit.
Arguments:
x : A `Tensor` with type `float`, `double`, `int32`, `int64`, `uint8`,
`int16`, or `int8`
Returns:
A `tuple` of `tf.Tensor`. This layer inference, i.e. output Tensors
at training and testing time.
References:
Fast and Accurate Deep Network Learning by Exponential Linear Units,
Djork-Arné Clevert, Thomas Unterthiner, Sepp Hochreiter. 2015.
Links:
[http://arxiv.org/abs/1511.07289](http://arxiv.org/abs/1511.07289)
"""
return tf.nn.elu(x)