def testVariableWithVariableDeviceChooser(self): with tf.Graph().as_default(): device_fn = variables.VariableDeviceChooser( num_parameter_servers=2) with scopes.arg_scope([variables.variable], device=device_fn): a = variables.variable('a', []) b = variables.variable('b', []) c = variables.variable('c', [], device='cpu:12') d = variables.variable('d', []) with tf.device('cpu:99'): e_init = tf.constant(12) e = variables.variable('e', initializer=e_init) # The values below highlight how the VariableDeviceChooser puts initial # values on the same device as the variable job. self.assertDeviceEqual(a.device, '/job:ps/task:0/cpu:0') self.assertDeviceEqual(a.initial_value.device, a.device) self.assertDeviceEqual(b.device, '/job:ps/task:1/cpu:0') self.assertDeviceEqual(b.initial_value.device, b.device) self.assertDeviceEqual(c.device, '/cpu:12') self.assertDeviceEqual(c.initial_value.device, c.device) self.assertDeviceEqual(d.device, '/job:ps/task:0/cpu:0') self.assertDeviceEqual(d.initial_value.device, d.device) self.assertDeviceEqual(e.device, '/job:ps/task:1/cpu:0') self.assertDeviceEqual(e.initial_value.device, '/cpu:99')
def testGetVariablesToRestore(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', [5]) with tf.variable_scope('B'): b = variables.variable('a', [5]) self.assertEquals([a, b], variables.get_variables_to_restore())
def testVariableWithDeviceFunction(self): class DevFn(object): def __init__(self): self.counter = -1 def __call__(self, op): self.counter += 1 return 'cpu:%d' % self.counter with self.test_session(): with scopes.arg_scope([variables.variable], device=DevFn()): a = variables.variable('a', []) b = variables.variable('b', []) c = variables.variable('c', [], device='cpu:12') d = variables.variable('d', []) with tf.device('cpu:99'): e_init = tf.constant(12) e = variables.variable('e', initializer=e_init) self.assertDeviceEqual(a.device, 'cpu:0') self.assertDeviceEqual(a.initial_value.device, 'cpu:0') self.assertDeviceEqual(b.device, 'cpu:1') self.assertDeviceEqual(b.initial_value.device, 'cpu:1') self.assertDeviceEqual(c.device, 'cpu:12') self.assertDeviceEqual(c.initial_value.device, 'cpu:12') self.assertDeviceEqual(d.device, 'cpu:2') self.assertDeviceEqual(d.initial_value.device, 'cpu:2') self.assertDeviceEqual(e.device, 'cpu:3') self.assertDeviceEqual(e.initial_value.device, 'cpu:99')
def testVariableWithDeviceFromScope(self): with self.test_session(): with tf.device('/cpu:0'): a = variables.variable('a', []) b = variables.variable('b', [], device='cpu:1') self.assertDeviceEqual(a.device, 'cpu:0') self.assertDeviceEqual(b.device, 'cpu:1')
def testGetVariableGivenNameScoped(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', [5]) b = variables.variable('b', [5]) self.assertEquals([a], variables.get_variables_by_name('a')) self.assertEquals([b], variables.get_variables_by_name('b'))
def testReuseVariable(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', []) with tf.variable_scope('A', reuse=True): b = variables.variable('a', []) self.assertEquals(a, b) self.assertListEqual([a], variables.get_variables())
def testGetVariableWithDistractors(self): with self.test_session(): with tf.variable_scope('parent'): a = variables.variable('child', [5]) with tf.variable_scope('child'): variables.variable('grandchild1', [7]) variables.variable('grandchild2', [9]) self.assertEquals(a, variables.get_unique_variable('parent/child'))
def testVariableCollectionsWithArgScopeNested(self): with self.test_session(): with scopes.arg_scope([variables.variable], collections='A'): a = variables.variable('a', []) with scopes.arg_scope([variables.variable], collections='B'): b = variables.variable('b', []) self.assertEquals(a, tf.get_collection('A')[0]) self.assertEquals(b, tf.get_collection('B')[0])
def testGetThrowsExceptionWithChildrenButNoMatch(self): var_name = 'parent/child' with self.test_session(): with tf.variable_scope(var_name): variables.variable('grandchild1', [7]) variables.variable('grandchild2', [9]) with self.assertRaises(ValueError): variables.get_unique_variable(var_name)
def testGetVariablesSuffix(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', [5]) with tf.variable_scope('A'): b = variables.variable('b', [5]) self.assertEquals([a], variables.get_variables(suffix='a')) self.assertEquals([b], variables.get_variables(suffix='b'))
def testVariableCollectionsWithArgScopeNonNested(self): with self.test_session(): with scopes.arg_scope([variables.variable], collections='A'): a = variables.variable('a', []) with scopes.arg_scope([variables.variable], collections='B'): b = variables.variable('b', []) variables.variable('c', []) self.assertListEqual([a], tf.get_collection('A')) self.assertListEqual([b], tf.get_collection('B'))
def testGetMixedVariablesToRestore(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', [5]) b = variables.variable('b', [5], restore=False) with tf.variable_scope('B'): c = variables.variable('c', [5]) d = variables.variable('d', [5], restore=False) self.assertEquals([a, b, c, d], variables.get_variables()) self.assertEquals([a, c], variables.get_variables_to_restore())
def testVariableRestoreWithArgScopeNested(self): with self.test_session(): with scopes.arg_scope([variables.variable], restore=True): a = variables.variable('a', []) with scopes.arg_scope([variables.variable], trainable=False, collections=['A', 'B']): b = variables.variable('b', []) c = variables.variable('c', []) self.assertListEqual([a, b, c], variables.get_variables_to_restore()) self.assertListEqual([a, c], tf.trainable_variables()) self.assertListEqual([b], tf.get_collection('A')) self.assertListEqual([b], tf.get_collection('B'))
def testGetVariablesByNameReturnsByValueWithoutScope(self): with self.test_session(): a = variables.variable('a', [5]) matched_variables = variables.get_variables_by_name('a') # If variables.get_variables_by_name returns the list by reference, the # following append should persist, and be returned, in subsequent calls # to variables.get_variables_by_name('a'). matched_variables.append(4) matched_variables = variables.get_variables_by_name('a') self.assertEquals([a], matched_variables)
def testVariableWithReplicaDeviceSetter(self): with self.test_session(): with tf.device(tf.train.replica_device_setter(ps_tasks=2)): a = variables.variable('a', []) b = variables.variable('b', []) c = variables.variable('c', [], device='cpu:12') d = variables.variable('d', []) with tf.device('cpu:99'): e_init = tf.constant(12) e = variables.variable('e', initializer=e_init) # The values below highlight how the replica_device_setter puts initial # values on the worker job, and how it merges explicit devices. self.assertDeviceEqual(a.device, '/job:ps/task:0/cpu:0') self.assertDeviceEqual(a.initial_value.device, '/job:worker/cpu:0') self.assertDeviceEqual(b.device, '/job:ps/task:1/cpu:0') self.assertDeviceEqual(b.initial_value.device, '/job:worker/cpu:0') self.assertDeviceEqual(c.device, '/job:ps/task:0/cpu:12') self.assertDeviceEqual(c.initial_value.device, '/job:worker/cpu:12') self.assertDeviceEqual(d.device, '/job:ps/task:1/cpu:0') self.assertDeviceEqual(d.initial_value.device, '/job:worker/cpu:0') self.assertDeviceEqual(e.device, '/job:ps/task:0/cpu:0') self.assertDeviceEqual(e.initial_value.device, '/job:worker/cpu:99')
def create_variable(self, parent, cur): """ Create left-hand-side variable (Expression) | Structure: | Cget|Cvar|Fget|Fvar|Get|Nget|Var|Sget|Svar | | <list of expression>? Args: parent (Node): Reference to parent node cur (int): position where variable is identified Returns: int: position where variable ends See also: :py:func:`matlab2cpp.tree.variables.variable` """ return variables.variable(self, parent, cur)
def conv2d(inputs, num_filters_out, kernel_size, stride=1, padding='SAME', activation=tf.nn.relu, stddev=0.01, bias=None, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a 2D convolution followed by an optional batch_norm layer. conv2d creates a variable called 'weights', representing the convolutional kernel, that is convolved with the input. If `batch_norm_params` is None, a second variable called 'biases' is added to the result of the convolution operation. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_filters_out: the number of output filters. kernel_size: a 2-D list comprising of the height and width of the filters. stride: the stride in height and width of the convolution. padding: one of 'VALID' or 'SAME'. activation: activation function. stddev: standard deviation of the truncated guassian weight distribution. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: a tensor representing the output of the operation. Raises: ValueError: if 'kernel_size' is not a 2-D list. """ if len(kernel_size) != 2: raise ValueError('kernel_size must be a 2-D list.') with tf.variable_op_scope([inputs], scope, 'Conv'): num_filters_in = inputs.get_shape()[-1] #num_filters_in = inputs.get_shape()[-3] weights_shape = [kernel_size[0], kernel_size[1], num_filters_in, num_filters_out] #weights_initializer = tf.truncated_normal_initializer(stddev=stddev) stddev = math.sqrt(2./(kernel_size[0]*kernel_size[1]*num_filters_out)) weights_initializer = tf.random_normal_initializer(stddev=stddev) l2_regularizer = lambda t: losses.l2_loss(t, weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) conv = tf.nn.conv2d(inputs, weights, [1, stride, stride, 1], padding=padding) #conv = tf.nn.conv2d(inputs, weights, [1, 1, stride, stride], # padding=padding, data_format='NCHW') if batch_norm_params is not None: with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(conv, **batch_norm_params) elif bias is None: outputs = conv else: bias_shape = [num_filters_out,] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.bias_add(conv, biases) #outputs = tf.nn.bias_add(conv, biases, data_format='NCHW') #outputs = tf.reshape(outputs, conv.get_shape()) if activation: outputs = activation(outputs) return outputs
def conv2d(inputs, num_filters_out, kernel_size, stride=1, padding='SAME', activation=tf.nn.relu, stddev=0.01, bias=0.0, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None, reuse=None): """Adds a 2D convolution followed by an optional batch_norm layer. conv2d creates a variable called 'weights', representing the convolutional kernel, that is convolved with the input. If `batch_norm_params` is None, a second variable called 'biases' is added to the result of the convolution operation. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_filters_out: the number of output filters. kernel_size: a list of length 2: [kernel_height, kernel_width] of of the filters. Can be an int if both values are the same. stride: a list of length 2: [stride_height, stride_width]. Can be an int if both strides are the same. Note that presently both strides must have the same value. padding: one of 'VALID' or 'SAME'. activation: activation function. stddev: standard deviation of the truncated guassian weight distribution. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. reuse: whether or not the layer and its variables should be reused. To be able to reuse the layer scope must be given. Returns: a tensor representing the output of the operation. """ with tf.variable_op_scope([inputs], scope, 'Conv', reuse=reuse): kernel_h, kernel_w = _two_element_tuple(kernel_size) stride_h, stride_w = _two_element_tuple(stride) num_filters_in = inputs.get_shape()[-1] weights_shape = [kernel_h, kernel_w, num_filters_in, num_filters_out] weights_initializer = tf.truncated_normal_initializer(stddev=stddev) l2_regularizer = None if weight_decay and weight_decay > 0: l2_regularizer = losses.l2_regularizer(weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) conv = tf.nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding=padding) if batch_norm_params is not None: with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(conv, **batch_norm_params) else: bias_shape = [num_filters_out,] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.bias_add(conv, biases) if activation: outputs = activation(outputs) return outputs
def batch_norm(inputs, decay=0.999, scale=False, epsilon=0.001, moving_vars='moving_vars', activation=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a Batch Normalization layer. Args: inputs: a tensor of size [batch_size, height, width, channels] or [batch_size, channels]. decay: decay for the moving average. scale: If True, multiply by gamma. If False, gamma is not used. When the next layer is linear (also e.g. ReLU), this can be disabled since the scaling can be done by the next layer. epsilon: small float added to variance to avoid dividing by zero. moving_vars: collection to store the moving_mean and moving_variance. activation: activation function. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: a tensor representing the output of the operation. """ inputs_shape = inputs.get_shape() with tf.variable_op_scope([inputs], scope, 'BatchNorm'): axis = range(len(inputs_shape) - 1) params_shape = inputs_shape[-1:] #axis = [0] if len(inputs_shape) == 2 else [0,2,3] #params_shape = [tf.Dimension(1),inputs_shape[1]] if len(inputs_shape) == 2 else [tf.Dimension(1),inputs_shape[1],tf.Dimension(1),tf.Dimension(1)] with scopes.arg_scope([variables.variable], restore=restore): # Allocate parameters for the beta and gamma of the normalization. beta = variables.variable('beta', params_shape, initializer=tf.zeros_initializer, trainable=trainable) if scale: gamma = variables.variable('gamma', params_shape, initializer=tf.ones, trainable=trainable) else: gamma = None # Create moving_mean and moving_variance add them to moving_vars and # GraphKeys.MOVING_AVERAGE_VARIABLES collections. with scopes.arg_scope([variables.variable], trainable=False, collections=[ moving_vars, tf.GraphKeys.MOVING_AVERAGE_VARIABLES ]): moving_mean = variables.variable( 'moving_mean', params_shape, initializer=tf.zeros_initializer) moving_variance = variables.variable('moving_variance', params_shape, initializer=tf.ones) #moving_mean = tf.Print(moving_mean,[moving_mean],"moving_mean: ") if is_training: # Calculate the moments based on the individual batch. mean, variance = tf.nn.moments(inputs, axis) #mean, variance = tf.nn.moments(inputs, axis, keep_dims=True) update_moving_mean = moving_averages.assign_moving_average( moving_mean, mean, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean) update_moving_variance = moving_averages.assign_moving_average( moving_variance, variance, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance) else: # Just use the moving_mean and moving_variance. mean = moving_mean variance = moving_variance # Normalize the activations. outputs = tf.nn.batch_normalization(inputs, mean, variance, beta, gamma, epsilon) outputs.set_shape(inputs.get_shape()) if activation: outputs = activation(outputs) return outputs
def fc(inputs, num_units_out, activation=tf.nn.relu, stddev=0.01, bias=None, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a fully connected layer followed by an optional batch_norm layer. FC creates a variable called 'weights', representing the fully connected weight matrix, that is multiplied by the input. If `batch_norm` is None, a second variable called 'biases' is added to the result of the initial vector-matrix multiplication. Args: inputs: a [B x N] tensor where B is the batch size and N is the number of input units in the layer. num_units_out: the number of output units in the layer. activation: activation function. stddev: the standard deviation for the weights. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: the tensor variable representing the result of the series of operations. """ with tf.variable_op_scope([inputs], scope, 'FC'): num_units_in = inputs.get_shape()[1] weights_shape = [num_units_in, num_units_out] #weights_initializer = tf.truncated_normal_initializer(stddev=stddev) weights_initializer = tf.random_normal_initializer(stddev=stddev) l2_regularizer = lambda t: losses.l2_loss(t, weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) if batch_norm_params is not None: outputs = tf.matmul(inputs, weights) with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(outputs, **batch_norm_params) elif bias is None: outputs = tf.matmul(inputs, weights) else: bias_shape = [ num_units_out, ] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.xw_plus_b(inputs, weights, biases) if activation: outputs = activation(outputs) return outputs
def conv2d(inputs, num_filters_out, kernel_size, stride=1, padding='SAME', activation=tf.nn.relu, stddev=0.01, bias=None, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a 2D convolution followed by an optional batch_norm layer. conv2d creates a variable called 'weights', representing the convolutional kernel, that is convolved with the input. If `batch_norm_params` is None, a second variable called 'biases' is added to the result of the convolution operation. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_filters_out: the number of output filters. kernel_size: a 2-D list comprising of the height and width of the filters. stride: the stride in height and width of the convolution. padding: one of 'VALID' or 'SAME'. activation: activation function. stddev: standard deviation of the truncated guassian weight distribution. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: a tensor representing the output of the operation. Raises: ValueError: if 'kernel_size' is not a 2-D list. """ if len(kernel_size) != 2: raise ValueError('kernel_size must be a 2-D list.') with tf.variable_op_scope([inputs], scope, 'Conv'): num_filters_in = inputs.get_shape()[-1] #num_filters_in = inputs.get_shape()[-3] weights_shape = [ kernel_size[0], kernel_size[1], num_filters_in, num_filters_out ] #weights_initializer = tf.truncated_normal_initializer(stddev=stddev) stddev = math.sqrt(2. / (kernel_size[0] * kernel_size[1] * num_filters_out)) weights_initializer = tf.random_normal_initializer(stddev=stddev) l2_regularizer = lambda t: losses.l2_loss(t, weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) conv = tf.nn.conv2d(inputs, weights, [1, stride, stride, 1], padding=padding) #conv = tf.nn.conv2d(inputs, weights, [1, 1, stride, stride], # padding=padding, data_format='NCHW') if batch_norm_params is not None: with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(conv, **batch_norm_params) elif bias is None: outputs = conv else: bias_shape = [ num_filters_out, ] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.bias_add(conv, biases) #outputs = tf.nn.bias_add(conv, biases, data_format='NCHW') #outputs = tf.reshape(outputs, conv.get_shape()) if activation: outputs = activation(outputs) return outputs
def batch_norm(inputs, decay=0.999, center=True, scale=False, epsilon=0.001, moving_vars='moving_vars', activation=None, is_training=True, trainable=True, restore=True, scope=None, reuse=None): """Adds a Batch Normalization layer. Args: inputs: a tensor of size [batch_size, height, width, channels] or [batch_size, channels]. decay: decay for the moving average. center: If True, subtract beta. If False, beta is not created and ignored. scale: If True, multiply by gamma. If False, gamma is not used. When the next layer is linear (also e.g. ReLU), this can be disabled since the scaling can be done by the next layer. epsilon: small float added to variance to avoid dividing by zero. moving_vars: collection to store the moving_mean and moving_variance. activation: activation function. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_scope. reuse: whether or not the layer and its variables should be reused. To be able to reuse the layer scope must be given. Returns: a tensor representing the output of the operation. """ inputs_shape = inputs.get_shape() with tf.variable_scope(scope, 'BatchNorm', [inputs], reuse=reuse): axis = list(range(len(inputs_shape) - 1)) params_shape = inputs_shape[-1:] # Allocate parameters for the beta and gamma of the normalization. beta, gamma = None, None if center: beta = variables.variable('beta', params_shape, initializer=tf.zeros_initializer(), trainable=trainable, restore=restore) if scale: gamma = variables.variable('gamma', params_shape, initializer=tf.ones_initializer(), trainable=trainable, restore=restore) # Create moving_mean and moving_variance add them to # GraphKeys.MOVING_AVERAGE_VARIABLES collections. moving_collections = [moving_vars, tf.GraphKeys.MOVING_AVERAGE_VARIABLES] moving_mean = variables.variable('moving_mean', params_shape, initializer=tf.zeros_initializer(), trainable=False, restore=restore, collections=moving_collections) moving_variance = variables.variable('moving_variance', params_shape, initializer=tf.ones_initializer(), trainable=False, restore=restore, collections=moving_collections) if is_training: # Calculate the moments based on the individual batch. mean, variance = tf.nn.moments(inputs, axis) update_moving_mean = moving_averages.assign_moving_average( moving_mean, mean, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean) update_moving_variance = moving_averages.assign_moving_average( moving_variance, variance, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance) else: # Just use the moving_mean and moving_variance. mean = moving_mean variance = moving_variance # Normalize the activations. outputs = tf.nn.batch_normalization( inputs, mean, variance, beta, gamma, epsilon) outputs.set_shape(inputs.get_shape()) if activation: outputs = activation(outputs) return outputs
def conv2d(inputs, num_filters_out, kernel_size, stride=1, padding='SAME', activation=tf.nn.relu, stddev=0.015, bias=0.0, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None, reuse=None): """Adds a 2D convolution followed by an optional batch_norm layer. conv2d creates a variable called 'weights', representing the convolutional kernel, that is convolved with the input. If `batch_norm_params` is None, a second variable called 'biases' is added to the result of the convolution operation. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_filters_out: the number of output filters. kernel_size: a list of length 2: [kernel_height, kernel_width] of of the filters. Can be an int if both values are the same. stride: a list of length 2: [stride_height, stride_width]. Can be an int if both strides are the same. Note that presently both strides must have the same value. padding: one of 'VALID' or 'SAME'. activation: activation function. stddev: standard deviation of the truncated guassian weight distribution. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_scope. reuse: whether or not the layer and its variables should be reused. To be able to reuse the layer scope must be given. Returns: a tensor representing the output of the operation. """ with tf.variable_scope(scope, 'Conv', [inputs], reuse=reuse): kernel_h, kernel_w = _two_element_tuple(kernel_size) stride_h, stride_w = _two_element_tuple(stride) num_filters_in = inputs.get_shape()[-1] weights_shape = [kernel_h, kernel_w, num_filters_in, num_filters_out] weights_initializer = tf.truncated_normal_initializer(stddev=stddev) l2_regularizer = None if weight_decay and weight_decay > 0: l2_regularizer = losses.l2_regularizer(weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) conv = tf.nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding=padding) if batch_norm_params is not None: with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(conv, **batch_norm_params) else: bias_shape = [num_filters_out,] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.bias_add(conv, biases) if activation: outputs = activation(outputs) return outputs
def testVariableCollections(self): with self.test_session(): a = variables.variable('a', [], collections=['A', 'C']) b = variables.variable('b', [], collections=['B', 'C']) self.assertEquals(a, tf.get_collection('A')[0]) self.assertEquals(b, tf.get_collection('B')[0])
def testCreateVariable(self): with self.test_session(): with tf.variable_scope('A'): a = variables.variable('a', [5]) self.assertEquals(a.op.name, 'A/a') self.assertListEqual(a.get_shape().as_list(), [5])
def fc(inputs, num_units_out, activation=tf.nn.relu, stddev=0.01, bias=None, weight_decay=0, batch_norm_params=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a fully connected layer followed by an optional batch_norm layer. FC creates a variable called 'weights', representing the fully connected weight matrix, that is multiplied by the input. If `batch_norm` is None, a second variable called 'biases' is added to the result of the initial vector-matrix multiplication. Args: inputs: a [B x N] tensor where B is the batch size and N is the number of input units in the layer. num_units_out: the number of output units in the layer. activation: activation function. stddev: the standard deviation for the weights. bias: the initial value of the biases. weight_decay: the weight decay. batch_norm_params: parameters for the batch_norm. If is None don't use it. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: the tensor variable representing the result of the series of operations. """ with tf.variable_op_scope([inputs], scope, 'FC'): num_units_in = inputs.get_shape()[1] weights_shape = [num_units_in, num_units_out] #weights_initializer = tf.truncated_normal_initializer(stddev=stddev) weights_initializer = tf.random_normal_initializer(stddev=stddev) l2_regularizer = lambda t: losses.l2_loss(t, weight_decay) weights = variables.variable('weights', shape=weights_shape, initializer=weights_initializer, regularizer=l2_regularizer, trainable=trainable, restore=restore) if batch_norm_params is not None: outputs = tf.matmul(inputs, weights) with scopes.arg_scope([batch_norm], is_training=is_training, trainable=trainable, restore=restore): outputs = batch_norm(outputs, **batch_norm_params) elif bias is None: outputs = tf.matmul(inputs, weights) else: bias_shape = [num_units_out,] bias_initializer = tf.constant_initializer(bias) biases = variables.variable('biases', shape=bias_shape, initializer=bias_initializer, trainable=trainable, restore=restore) outputs = tf.nn.xw_plus_b(inputs, weights, biases) if activation: outputs = activation(outputs) return outputs
def batch_norm(inputs, decay=0.999, scale=False, epsilon=0.001, moving_vars='moving_vars', activation=None, is_training=True, trainable=True, restore=True, scope=None): """Adds a Batch Normalization layer. Args: inputs: a tensor of size [batch_size, height, width, channels] or [batch_size, channels]. decay: decay for the moving average. scale: If True, multiply by gamma. If False, gamma is not used. When the next layer is linear (also e.g. ReLU), this can be disabled since the scaling can be done by the next layer. epsilon: small float added to variance to avoid dividing by zero. moving_vars: collection to store the moving_mean and moving_variance. activation: activation function. is_training: whether or not the model is in training mode. trainable: whether or not the variables should be trainable or not. restore: whether or not the variables should be marked for restore. scope: Optional scope for variable_op_scope. Returns: a tensor representing the output of the operation. """ inputs_shape = inputs.get_shape() with tf.variable_op_scope([inputs], scope, 'BatchNorm'): axis = range(len(inputs_shape) - 1) params_shape = inputs_shape[-1:] #axis = [0] if len(inputs_shape) == 2 else [0,2,3] #params_shape = [tf.Dimension(1),inputs_shape[1]] if len(inputs_shape) == 2 else [tf.Dimension(1),inputs_shape[1],tf.Dimension(1),tf.Dimension(1)] with scopes.arg_scope([variables.variable], restore=restore): # Allocate parameters for the beta and gamma of the normalization. beta = variables.variable('beta', params_shape, initializer=tf.zeros_initializer, trainable=trainable) if scale: gamma = variables.variable('gamma', params_shape, initializer=tf.ones, trainable=trainable) else: gamma = None # Create moving_mean and moving_variance add them to moving_vars and # GraphKeys.MOVING_AVERAGE_VARIABLES collections. with scopes.arg_scope([variables.variable], trainable=False, collections=[ moving_vars, tf.GraphKeys.MOVING_AVERAGE_VARIABLES]): moving_mean = variables.variable('moving_mean', params_shape, initializer=tf.zeros_initializer) moving_variance = variables.variable('moving_variance', params_shape, initializer=tf.ones) #moving_mean = tf.Print(moving_mean,[moving_mean],"moving_mean: ") if is_training: # Calculate the moments based on the individual batch. mean, variance = tf.nn.moments(inputs, axis) #mean, variance = tf.nn.moments(inputs, axis, keep_dims=True) update_moving_mean = moving_averages.assign_moving_average( moving_mean, mean, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean) update_moving_variance = moving_averages.assign_moving_average( moving_variance, variance, decay) tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance) else: # Just use the moving_mean and moving_variance. mean = moving_mean variance = moving_variance # Normalize the activations. outputs = tf.nn.batch_normalization( inputs, mean, variance, beta, gamma, epsilon) outputs.set_shape(inputs.get_shape()) if activation: outputs = activation(outputs) return outputs
def testGetVariableWithSingleVar(self): with self.test_session(): with tf.variable_scope('parent'): a = variables.variable('child', [5]) self.assertEquals(a, variables.get_unique_variable('parent/child'))