def __init__(self, layer_type, input_size, target_size, num_hidden_units, activation_type,
**kwargs):
self.input_size = input_size
self.target_size = target_size
self.num_hidden_units = num_hidden_units
self.square_initializer = tf.random_normal_initializer(0.0, np.sqrt(1.0 / num_hidden_units))
self.non_square_initializer = tf.random_normal_initializer(0.0, np.sqrt(1.0 / num_hidden_units))
self.bias_initializer = tf.constant_initializer(0.0)
Layer = getattr(layers, layer_type)
activation = getattr(tf.nn, activation_type)
self.inputs = tf.placeholder(tf.float32, shape=[None, None, input_size], name='inputs')
self.targets = tf.placeholder(tf.float32, shape=[None, None, target_size], name='targets')
self.batch_size = tf.shape(self.inputs)[0]
self.length = tf.shape(self.inputs)[1]
valid_mask_incl_invalid_seqs = tf.logical_not(tf.is_nan(self.targets[0:, 0:, 0]))
target_step_counts = tf.reduce_sum(tf.to_int32(valid_mask_incl_invalid_seqs), axis=[1],
name='target_step_counts')
valid_seq_mask = tf.greater(target_step_counts, 0, name='valid_seq_mask')
self.valid_split_ind = tf.identity(tf.cumsum(target_step_counts)[:-1], name='valid_split_ind')
valid_seq_ids_incl_invalid_seqs = tf.tile(tf.expand_dims(tf.range(0, self.batch_size), 1), [1, self.length])
valid_seq_ids = tf.boolean_mask(valid_seq_ids_incl_invalid_seqs, valid_mask_incl_invalid_seqs,
name='valid_seq_ids')
self.valid_targets = tf.boolean_mask(self.targets, valid_mask_incl_invalid_seqs, name='valid_targets')
with tf.variable_scope('rnn') as rnn_scope:
inputs = self.inputs
self._rnn_layer = Layer(inputs, self.num_hidden_units, activation, self.square_initializer,
self.non_square_initializer, self.bias_initializer, **kwargs)
self.initial_rnn_states = self._rnn_layer.initial_states
self.final_rnn_states = self._rnn_layer.final_states
with tf.variable_scope('predictions') as predictions_scope:
W = tf.get_variable('W', shape=[self.num_hidden_units, self.target_size], initializer=self.non_square_initializer)
b = tf.get_variable('b', shape=[self.target_size], initializer=self.bias_initializer)
valid_rnn_outputs = tf.boolean_mask(self._rnn_layer.outputs, valid_mask_incl_invalid_seqs)
self.valid_predictions = tf.nn.xw_plus_b(valid_rnn_outputs, W, b, name = 'valid_predictions')
with tf.variable_scope('loss'):
num_valid_seqs = tf.reduce_sum(tf.to_float(valid_seq_mask))
stepwise_losses = self._compute_stepwise_losses()
self.valid_stepwise_loss = tf.reduce_mean(stepwise_losses, name='stepwise_loss')
self.valid_stepwise_loss_for_opt = tf.identity(num_valid_seqs * self.valid_stepwise_loss,
name='valid_stepwise_loss_for_opt')
time_counts = tf.to_float(tf.expand_dims(target_step_counts, 1)) * tf.to_float(valid_mask_incl_invalid_seqs)
valid_time_counts = tf.boolean_mask(time_counts, valid_mask_incl_invalid_seqs)
seq_losses = tf.unsorted_segment_sum(stepwise_losses / valid_time_counts, valid_seq_ids, self.batch_size)
self.valid_seq_losses = tf.boolean_mask(seq_losses, valid_seq_mask, name='valid_seq_losses')
self.valid_seqwise_loss = tf.reduce_mean(self.valid_seq_losses, name='valid_seqwise_loss')
self.valid_seqwise_loss_for_opt = tf.identity(num_valid_seqs * self.valid_seqwise_loss,
name='valid_seqwise_loss_for_opt')
python类logical_not()的实例源码
def __init__(self, img_size, num_channels, num_classes, dropout_prob=0.0):
# Based on https://github.com/fchollet/keras/blob/master/keras/applications/vgg16.py
self.x = tf.placeholder(tf.float32, [None,img_size,img_size,num_channels], 'x')
self.y = tf.placeholder(tf.float32, [None,num_classes], 'y')
self.deterministic = tf.placeholder(tf.bool, name='d')
d = self.deterministic
phase = tf.logical_not(d)
def conv_bn(h, num_filters, phase):
h = Conv2D(num_filters, (3,3), padding='same')(h) # Linear
h = tf.contrib.layers.batch_norm(h, center=True, scale=False, is_training=phase)
return tf.nn.relu(h)
# Block 1
h = conv_bn(self.x,64,phase)
h = conv_bn(h,64,phase)
h = MaxPooling2D((2, 2), strides=(2,2))(h)
# Block 2
h = conv_bn(h,128,phase)
h = conv_bn(h,128,phase)
h = MaxPooling2D((2, 2), strides=(2,2))(h)
# Block 3
h = conv_bn(h,256,phase)
h = conv_bn(h,256,phase)
h = conv_bn(h,256,phase)
h = MaxPooling2D((2,2), strides=(2,2))(h)
# Block 4
h = conv_bn(h,512,phase)
h = conv_bn(h,512,phase)
h = conv_bn(h,512,phase)
h = MaxPooling2D((2,2), strides=(2,2))(h)
# Block 5
h = conv_bn(h,512,phase)
h = conv_bn(h,512,phase)
h = conv_bn(h,512,phase)
h = MaxPooling2D((2,2), strides=(2,2))(h)
h = Flatten()(h)
self.pred = Dense(num_classes, activation='softmax')(h)
pred = tf.clip_by_value(self.pred,eps,1-eps)
loss = -tf.reduce_sum(tf.log(pred)*self.y)
correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.pred, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy')
optimizer = tf.train.AdamOptimizer(0.001)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
# Ensures that we execute the update_ops before performing the train_step
self.train_step = optimizer.minimize(loss)
def __init__(self, img_size, num_channels, num_classes):
# Based on https://github.com/fchollet/keras/blob/master/keras/applications/vgg16.py
self.x = tf.placeholder(tf.float32, [None,img_size,img_size,num_channels], 'x')
self.y = tf.placeholder(tf.float32, [None,num_classes], 'y')
self.deterministic = tf.placeholder(tf.bool, name='d')
d = self.deterministic
phase = tf.logical_not(d)
def conv_bn(h, filters_in, filters_out, d, phase):
h = Conv2DVarDropout(filters_in, filters_out, (3,3), padding='SAME', nonlinearity=tf.identity)(h,d) # Linear
h = tf.contrib.layers.batch_norm(h, center=True, scale=False, is_training=phase)
return tf.nn.relu(h)
# Block 1
h = conv_bn(self.x, num_channels, 64, d, phase)
h = conv_bn(h, 64, 64, d, phase)
h = MaxPooling2D((2, 2), strides=(2,2))(h)
# Block 2
h = conv_bn(h, 64, 128, d, phase)
h = conv_bn(h, 128, 128, d, phase)
h = MaxPooling2D((2, 2), strides=(2,2))(h)
# Block 3
h = conv_bn(h, 128, 256, d, phase)
h = conv_bn(h, 256, 256, d, phase)
h = conv_bn(h, 256, 256, d, phase)
h = MaxPooling2D((2,2), strides=(2,2))(h)
# Block 4
h = conv_bn(h, 256, 512, d, phase)
h = conv_bn(h, 512, 512, d, phase)
h = conv_bn(h, 512, 512, d, phase)
h = MaxPooling2D((2, 2), strides=(2, 2))(h)
# Block 5
h = conv_bn(h, 512, 512, d, phase)
h = conv_bn(h, 512, 512, d, phase)
h = conv_bn(h, 512, 512, d, phase)
h = MaxPooling2D((2, 2), strides=(2, 2))(h)
h = Flatten()(h)
self.pred = FCVarDropout(512, num_classes, tf.nn.softmax)(h,d)
pred = tf.clip_by_value(self.pred,eps,1-eps)
W = tf.get_collection('W')
log_sigma2 = tf.get_collection('log_sigma2')
loss = sgvlb(pred, self.y, W, log_sigma2, batch_size)
correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.pred, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy')
optimizer = tf.train.AdamOptimizer(0.0001)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
# Ensures that we execute the update_ops before performing the train_step
self.train_step = optimizer.minimize(loss)
balanced_positive_negative_sampler.py 文件源码
项目:tensorflow
作者: luyishisi
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def subsample(self, indicator, batch_size, labels):
"""Returns subsampled minibatch.
Args:
indicator: boolean tensor of shape [N] whose True entries can be sampled.
batch_size: desired batch size.
labels: boolean tensor of shape [N] denoting positive(=True) and negative
(=False) examples.
Returns:
is_sampled: boolean tensor of shape [N], True for entries which are
sampled.
Raises:
ValueError: if labels and indicator are not 1D boolean tensors.
"""
if len(indicator.get_shape().as_list()) != 1:
raise ValueError('indicator must be 1 dimensional, got a tensor of '
'shape %s' % indicator.get_shape())
if len(labels.get_shape().as_list()) != 1:
raise ValueError('labels must be 1 dimensional, got a tensor of '
'shape %s' % labels.get_shape())
if labels.dtype != tf.bool:
raise ValueError('labels should be of type bool. Received: %s' %
labels.dtype)
if indicator.dtype != tf.bool:
raise ValueError('indicator should be of type bool. Received: %s' %
indicator.dtype)
# Only sample from indicated samples
negative_idx = tf.logical_not(labels)
positive_idx = tf.logical_and(labels, indicator)
negative_idx = tf.logical_and(negative_idx, indicator)
# Sample positive and negative samples separately
max_num_pos = int(self._positive_fraction * batch_size)
sampled_pos_idx = self.subsample_indicator(positive_idx, max_num_pos)
max_num_neg = batch_size - tf.reduce_sum(tf.cast(sampled_pos_idx, tf.int32))
sampled_neg_idx = self.subsample_indicator(negative_idx, max_num_neg)
sampled_idx = tf.logical_or(sampled_pos_idx, sampled_neg_idx)
return sampled_idx