Python tensorflow.ones_initializer() Examples
The following are 30
code examples of tensorflow.ones_initializer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: unet-TF-withBatchNormal.py From U-net with MIT License | 6 votes |
def batch_norm(x, is_training, eps=EPS, decay=0.9, affine=True, name='BatchNorm2d'): from tensorflow.python.training.moving_averages import assign_moving_average with tf.variable_scope(name): params_shape = x.shape[-1:] moving_mean = tf.get_variable(name='mean', shape=params_shape, initializer=tf.zeros_initializer, trainable=False) moving_var = tf.get_variable(name='variance', shape=params_shape, initializer=tf.ones_initializer, trainable=False) def mean_var_with_update(): mean_this_batch, variance_this_batch = tf.nn.moments(x, list(range(len(x.shape) - 1)), name='moments') with tf.control_dependencies([ assign_moving_average(moving_mean, mean_this_batch, decay), assign_moving_average(moving_var, variance_this_batch, decay) ]): return tf.identity(mean_this_batch), tf.identity(variance_this_batch) mean, variance = tf.cond(is_training, mean_var_with_update, lambda: (moving_mean, moving_var)) if affine: # 如果要用beta和gamma进行放缩 beta = tf.get_variable('beta', params_shape, initializer=tf.zeros_initializer) gamma = tf.get_variable('gamma', params_shape, initializer=tf.ones_initializer) normed = tf.nn.batch_normalization(x, mean=mean, variance=variance, offset=beta, scale=gamma, variance_epsilon=eps) else: normed = tf.nn.batch_normalization(x, mean=mean, variance=variance, offset=None, scale=None, variance_epsilon=eps) return normed
Example #2
Source File: models.py From DRL_DeliveryDuel with MIT License | 6 votes |
def create_vector_input(self, s_size): if self.brain.vector_observation_space_type == "continuous": self.vector_in = tf.placeholder(shape=[None, s_size], dtype=tf.float32, name='vector_observation') if self.normalize: self.running_mean = tf.get_variable("running_mean", [s_size], trainable=False, dtype=tf.float32, initializer=tf.zeros_initializer()) self.running_variance = tf.get_variable("running_variance", [s_size], trainable=False, dtype=tf.float32, initializer=tf.ones_initializer()) self.new_mean = tf.placeholder(shape=[s_size], dtype=tf.float32, name='new_mean') self.new_variance = tf.placeholder(shape=[s_size], dtype=tf.float32, name='new_variance') self.update_mean = tf.assign(self.running_mean, self.new_mean) self.update_variance = tf.assign(self.running_variance, self.new_variance) self.normalized_state = tf.clip_by_value((self.vector_in - self.running_mean) / tf.sqrt( self.running_variance / (tf.cast(self.global_step, tf.float32) + 1)), -5, 5, name="normalized_state") else: self.normalized_state = self.vector_in else: self.vector_in = tf.placeholder(shape=[None, 1], dtype=tf.int32, name='vector_observation')
Example #3
Source File: layer.py From UNMT-SPR with MIT License | 6 votes |
def layer_norm(input_data, epsilon=1e-6, dtype=None, scope=None): with tf.variable_scope(scope, default_name="layer_norm"): input_size = infer_shape(input_data)[-1] scale = tf.get_variable("scale", shape=[input_size], initializer=tf.ones_initializer()) bias = tf.get_variable("bias", shape=[input_size], initializer=tf.zeros_initializer) mean = tf.reduce_mean(input_data, -1, True) variance = tf.reduce_mean(tf.square(input_data - mean), -1, True) input_norm = (input_data - mean) * tf.rsqrt(variance + epsilon) output = input_norm * scale + bias return output
Example #4
Source File: network_units.py From yolo_v2 with Apache License 2.0 | 6 votes |
def __init__(self, component, name, shape, dtype): """Construct variables to normalize an input of given shape. Arguments: component: ComponentBuilder handle. name: Human readable name to organize the variables. shape: Shape of the layer to be normalized. dtype: Type of the layer to be normalized. """ self._name = name self._shape = shape self._component = component beta = tf.get_variable( 'beta_%s' % name, shape=shape, dtype=dtype, initializer=tf.zeros_initializer()) gamma = tf.get_variable( 'gamma_%s' % name, shape=shape, dtype=dtype, initializer=tf.ones_initializer()) self._params = [beta, gamma]
Example #5
Source File: common_layers.py From fine-lm with MIT License | 6 votes |
def group_norm(x, filters=None, num_groups=8, epsilon=1e-5): """Group normalization as in https://arxiv.org/abs/1803.08494.""" x_shape = shape_list(x) if filters is None: filters = x_shape[-1] assert len(x_shape) == 4 assert filters % num_groups == 0 # Prepare variables. scale = tf.get_variable( "group_norm_scale", [filters], initializer=tf.ones_initializer()) bias = tf.get_variable( "group_norm_bias", [filters], initializer=tf.zeros_initializer()) epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]] # Reshape and compute group norm. x = tf.reshape(x, x_shape[:-1] + [num_groups, filters // num_groups]) # Calculate mean and variance on heights, width, channels (not groups). mean, variance = tf.nn.moments(x, [1, 2, 4], keep_dims=True) norm_x = (x - mean) * tf.rsqrt(variance + epsilon) return tf.reshape(norm_x, x_shape) * scale + bias
Example #6
Source File: common_layers.py From fine-lm with MIT License | 6 votes |
def layer_norm(x, filters=None, epsilon=1e-6, name=None, reuse=None): """Layer normalize the tensor x, averaging over the last dimension.""" if filters is None: filters = shape_list(x)[-1] with tf.variable_scope( name, default_name="layer_norm", values=[x], reuse=reuse): scale = tf.get_variable( "layer_norm_scale", [filters], initializer=tf.ones_initializer()) bias = tf.get_variable( "layer_norm_bias", [filters], initializer=tf.zeros_initializer()) if allow_defun: result = layer_norm_compute(x, tf.constant(epsilon), scale, bias) result.set_shape(x.get_shape()) else: result = layer_norm_compute_python(x, epsilon, scale, bias) return result
Example #7
Source File: glow_ops.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def scale_gaussian_prior(name, z, logscale_factor=3.0, trainable=True): """Returns N(s^i * z^i, std^i) where s^i and std^i are pre-component. s^i is a learnable parameter with identity initialization. std^i is optionally learnable with identity initialization. Args: name: variable scope. z: input_tensor logscale_factor: equivalent to scaling up the learning_rate by a factor of logscale_factor. trainable: Whether or not std^i is learnt. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): z_shape = common_layers.shape_list(z) latent_multiplier = tf.get_variable( "latent_multiplier", shape=z_shape, dtype=tf.float32, initializer=tf.ones_initializer()) log_scale = tf.get_variable( "log_scale_latent", shape=z_shape, dtype=tf.float32, initializer=tf.zeros_initializer(), trainable=trainable) log_scale = log_scale * logscale_factor return tfp.distributions.Normal( loc=latent_multiplier * z, scale=tf.exp(log_scale))
Example #8
Source File: modules.py From transformer with Apache License 2.0 | 6 votes |
def ln(inputs, epsilon = 1e-8, scope="ln"): '''Applies layer normalization. See https://arxiv.org/abs/1607.06450. inputs: A tensor with 2 or more dimensions, where the first dimension has `batch_size`. epsilon: A floating number. A very small number for preventing ZeroDivision Error. scope: Optional scope for `variable_scope`. Returns: A tensor with the same shape and data dtype as `inputs`. ''' with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): inputs_shape = inputs.get_shape() params_shape = inputs_shape[-1:] mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True) beta= tf.get_variable("beta", params_shape, initializer=tf.zeros_initializer()) gamma = tf.get_variable("gamma", params_shape, initializer=tf.ones_initializer()) normalized = (inputs - mean) / ( (variance + epsilon) ** (.5) ) outputs = gamma * normalized + beta return outputs
Example #9
Source File: common_layers.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def group_norm(x, filters=None, num_groups=8, epsilon=1e-5): """Group normalization as in https://arxiv.org/abs/1803.08494.""" x_shape = shape_list(x) if filters is None: filters = x_shape[-1] assert len(x_shape) == 4 assert filters % num_groups == 0 # Prepare variables. scale = tf.get_variable( "group_norm_scale", [filters], initializer=tf.ones_initializer()) bias = tf.get_variable( "group_norm_bias", [filters], initializer=tf.zeros_initializer()) epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]] # Reshape and compute group norm. x = tf.reshape(x, x_shape[:-1] + [num_groups, filters // num_groups]) # Calculate mean and variance on heights, width, channels (not groups). mean, variance = tf.nn.moments(x, [1, 2, 4], keep_dims=True) norm_x = (x - mean) * tf.rsqrt(variance + epsilon) return tf.reshape(norm_x, x_shape) * scale + bias
Example #10
Source File: network_units.py From DOTA_models with Apache License 2.0 | 6 votes |
def __init__(self, component, name, shape, dtype): """Construct variables to normalize an input of given shape. Arguments: component: ComponentBuilder handle. name: Human readable name to organize the variables. shape: Shape of the layer to be normalized. dtype: Type of the layer to be normalized. """ self._name = name self._shape = shape self._component = component beta = tf.get_variable( 'beta_%s' % name, shape=shape, dtype=dtype, initializer=tf.zeros_initializer()) gamma = tf.get_variable( 'gamma_%s' % name, shape=shape, dtype=dtype, initializer=tf.ones_initializer()) self._params = [beta, gamma]
Example #11
Source File: TestSomeFunction.py From U-net with MIT License | 6 votes |
def batch_norm(x, is_training, eps=1e-05, decay=0.9, affine=True, name='BatchNorm2d'): from tensorflow.python.training.moving_averages import assign_moving_average with tf.variable_scope(name): print(get_scope()) print(tf.get_variable_scope()) params_shape = x.shape[-1:] moving_mean = tf.get_variable(name='mean', shape=params_shape, initializer=tf.zeros_initializer, trainable=False) moving_var = tf.get_variable(name='variance', shape=params_shape, initializer=tf.ones_initializer, trainable=False) def mean_var_with_update(): mean, variance = tf.nn.moments(x, list(range(len(x.shape) - 1)), name='moments') with tf.control_dependencies([ assign_moving_average(moving_mean, mean, decay), assign_moving_average(moving_var, variance, decay) ]): return tf.identity(mean), tf.identity(variance) mean, variance = tf.cond(is_training, mean_var_with_update, lambda: (moving_mean, moving_var)) if affine: beta = tf.get_variable('beta', params_shape, initializer=tf.zeros_initializer) gamma = tf.get_variable('gamma', params_shape, initializer=tf.ones_initializer) normed = tf.nn.batch_normalization(x, mean, variance, beta, gamma, eps) else: normed = tf.nn.batch_normalization(x, mean, variance, None, None, eps) return normed
Example #12
Source File: nn.py From THUMT with BSD 3-Clause "New" or "Revised" License | 6 votes |
def layer_norm(inputs, epsilon=1e-6, dtype=None, scope=None): """ Layer Normalization :param inputs: A Tensor of shape [..., channel_size] :param epsilon: A floating number :param dtype: An optional instance of tf.DType :param scope: An optional string :returns: A Tensor with the same shape as inputs """ with tf.variable_scope(scope, default_name="layer_norm", values=[inputs], dtype=dtype): channel_size = inputs.get_shape().as_list()[-1] scale = tf.get_variable("scale", shape=[channel_size], initializer=tf.ones_initializer()) offset = tf.get_variable("offset", shape=[channel_size], initializer=tf.zeros_initializer()) mean = tf.reduce_mean(inputs, -1, True) variance = tf.reduce_mean(tf.square(inputs - mean), -1, True) norm_inputs = (inputs - mean) * tf.rsqrt(variance + epsilon) return norm_inputs * scale + offset
Example #13
Source File: test_mlp_model.py From garage with MIT License | 6 votes |
def test_is_pickleable(self, output_dim, hidden_sizes): model = MLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) outputs = model.build(self.input_var).outputs # assign bias to all one with tf.compat.v1.variable_scope('MLPModel/mlp', reuse=True): bias = tf.compat.v1.get_variable('hidden_0/bias') bias.load(tf.ones_like(bias).eval()) output1 = self.sess.run(outputs, feed_dict={self.input_var: self.obs}) h = pickle.dumps(model) with tf.compat.v1.Session(graph=tf.Graph()) as sess: input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, 5)) model_pickled = pickle.loads(h) outputs = model_pickled.build(input_var).outputs output2 = sess.run(outputs, feed_dict={input_var: self.obs}) assert np.array_equal(output1, output2)
Example #14
Source File: test_mlp_model.py From garage with MIT License | 6 votes |
def test_output_values_merging(self, output_dim, hidden_sizes): model = MLPMergeModel(output_dim=output_dim, hidden_sizes=hidden_sizes, concat_layer=0, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) input_var2 = tf.compat.v1.placeholder(tf.float32, shape=(None, 5)) obs2 = np.ones((1, 5)) outputs = model.build(self.input_var, input_var2).outputs output = self.sess.run(outputs, feed_dict={ self.input_var: self.obs, input_var2: obs2 }) expected_output = np.full([1, output_dim], 10 * np.prod(hidden_sizes)) assert np.array_equal(output, expected_output) # yapf: disable
Example #15
Source File: test_categorical_mlp_model.py From garage with MIT License | 6 votes |
def test_is_pickleable(self, output_dim, hidden_sizes): model = CategoricalMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist # assign bias to all one with tf.compat.v1.variable_scope('CategoricalMLPModel/mlp', reuse=True): bias = tf.compat.v1.get_variable('hidden_0/bias') bias.load(tf.ones_like(bias).eval()) output1 = self.sess.run(dist.probs, feed_dict={self.input_var: self.obs}) h = pickle.dumps(model) with tf.compat.v1.Session(graph=tf.Graph()) as sess: input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, 5)) model_pickled = pickle.loads(h) dist2 = model_pickled.build(input_var).dist output2 = sess.run(dist2.probs, feed_dict={input_var: self.obs}) assert np.array_equal(output1, output2)
Example #16
Source File: test_gaussian_mlp_model.py From garage with MIT License | 6 votes |
def test_softplus_output_values(self, output_dim, hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, hidden_nonlinearity=None, std_share_network=False, adaptive_std=False, init_std=2, std_parameterization='softplus', hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist mean, log_std = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) expected_mean = np.full([1, 1, output_dim], 5 * np.prod(hidden_sizes)) expected_std_param = np.full([1, 1, output_dim], np.log(np.exp(2) - 1)) expected_log_std = np.log(np.log(1. + np.exp(expected_std_param))) assert np.array_equal(mean, expected_mean) assert np.allclose(log_std, expected_log_std)
Example #17
Source File: glow_ops.py From BERT with Apache License 2.0 | 6 votes |
def scale_gaussian_prior(name, z, logscale_factor=3.0, trainable=True): """Returns N(s^i * z^i, std^i) where s^i and std^i are pre-component. s^i is a learnable parameter with identity initialization. std^i is optionally learnable with identity initialization. Args: name: variable scope. z: input_tensor logscale_factor: equivalent to scaling up the learning_rate by a factor of logscale_factor. trainable: Whether or not std^i is learnt. """ with tf.variable_scope(name, reuse=tf.AUTO_REUSE): z_shape = common_layers.shape_list(z) latent_multiplier = tf.get_variable( "latent_multiplier", shape=z_shape, dtype=tf.float32, initializer=tf.ones_initializer()) log_scale = tf.get_variable( "log_scale_latent", shape=z_shape, dtype=tf.float32, initializer=tf.zeros_initializer(), trainable=trainable) log_scale = log_scale * logscale_factor return tfp.distributions.Normal( loc=latent_multiplier * z, scale=tf.exp(log_scale))
Example #18
Source File: common_layers.py From BERT with Apache License 2.0 | 6 votes |
def group_norm(x, filters=None, num_groups=8, epsilon=1e-5): """Group normalization as in https://arxiv.org/abs/1803.08494.""" x_shape = shape_list(x) if filters is None: filters = x_shape[-1] assert len(x_shape) == 4 assert filters % num_groups == 0 # Prepare variables. scale = tf.get_variable( "group_norm_scale", [filters], initializer=tf.ones_initializer()) bias = tf.get_variable( "group_norm_bias", [filters], initializer=tf.zeros_initializer()) epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]] # Reshape and compute group norm. x = tf.reshape(x, x_shape[:-1] + [num_groups, filters // num_groups]) # Calculate mean and variance on heights, width, channels (not groups). mean, variance = tf.nn.moments(x, [1, 2, 4], keep_dims=True) norm_x = (x - mean) * tf.rsqrt(variance + epsilon) return tf.reshape(norm_x, x_shape) * scale + bias
Example #19
Source File: test_gaussian_mlp_model.py From garage with MIT License | 6 votes |
def test_adaptive_std_network_output_values(self, output_dim, hidden_sizes, std_hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, std_share_network=False, hidden_sizes=hidden_sizes, std_hidden_sizes=std_hidden_sizes, adaptive_std=True, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer(), std_hidden_nonlinearity=None, std_hidden_w_init=tf.ones_initializer(), std_output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist mean, log_std = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) expected_mean = np.full([1, 1, output_dim], 5 * np.prod(hidden_sizes)) expected_log_std = np.full([1, 1, output_dim], 5 * np.prod(std_hidden_sizes)) assert np.array_equal(mean, expected_mean) assert np.array_equal(log_std, expected_log_std)
Example #20
Source File: test_gaussian_mlp_model.py From garage with MIT License | 6 votes |
def test_without_std_share_network_output_values(self, output_dim, hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, init_std=2, std_share_network=False, adaptive_std=False, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist mean, log_std = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) expected_mean = np.full([1, 1, output_dim], 5 * np.prod(hidden_sizes)) expected_log_std = np.full([1, 1, output_dim], np.log(2.)) assert np.array_equal(mean, expected_mean) assert np.allclose(log_std, expected_log_std)
Example #21
Source File: layer_norm_residual_conn.py From BERT with Apache License 2.0 | 6 votes |
def layer_normalization(self,x): """ x should be:[batch_size,sequence_length,d_model] :return: """ filter=x.get_shape()[-1] #last dimension of x. e.g. 512 #print("layer_normalization:==================>variable_scope:","layer_normalization"+str(self.layer_index)) with tf.variable_scope("layer_normalization"+str(self.layer_index)): # 1. normalize input by using mean and variance according to last dimension mean=tf.reduce_mean(x,axis=-1,keepdims=True) #[batch_size,sequence_length,1] variance=tf.reduce_mean(tf.square(x-mean),axis=-1,keepdims=True) #[batch_size,sequence_length,1] norm_x=(x-mean)*tf.rsqrt(variance+1e-6) #[batch_size,sequence_length,d_model] # 2. re-scale normalized input back scale=tf.get_variable("layer_norm_scale",[filter],initializer=tf.ones_initializer) #[filter] bias=tf.get_variable("layer_norm_bias",[filter],initializer=tf.ones_initializer) #[filter] output=norm_x*scale+bias #[batch_size,sequence_length,d_model] return output #[batch_size,sequence_length,d_model]
Example #22
Source File: test_gaussian_mlp_model.py From garage with MIT License | 6 votes |
def test_std_share_network_output_values(self, output_dim, hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, std_share_network=True, hidden_nonlinearity=None, std_parameterization='exp', hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist mean, log_std = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) expected_mean = np.full([1, 1, output_dim], 5 * np.prod(hidden_sizes)) expected_log_std = np.full([1, 1, output_dim], 5 * np.prod(hidden_sizes)) assert np.array_equal(mean, expected_mean) assert np.array_equal(log_std, expected_log_std)
Example #23
Source File: func.py From zero with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rms_norm(x, eps=None, scope=None): """RMS-based Layer normalization layer""" if eps is None: eps = dtype.epsilon() with tf.variable_scope(scope or "rms_norm", dtype=tf.as_dtype(dtype.floatx())): layer_size = util.shape_list(x)[-1] scale = tf.get_variable("scale", [layer_size], initializer=tf.ones_initializer()) ms = tf.reduce_mean(x ** 2, -1, keep_dims=True) return scale * x * tf.rsqrt(ms + eps)
Example #24
Source File: func.py From zero with BSD 3-Clause "New" or "Revised" License | 5 votes |
def layer_norm(x, eps=None, scope=None): """Layer normalization layer""" if eps is None: eps = dtype.epsilon() with tf.variable_scope(scope or "layer_norm", dtype=tf.as_dtype(dtype.floatx())): layer_size = util.shape_list(x)[-1] scale = tf.get_variable("scale", [layer_size], initializer=tf.ones_initializer()) offset = tf.get_variable("offset", [layer_size], initializer=tf.zeros_initializer()) mean = tf.reduce_mean(x, -1, keep_dims=True) var = tf.reduce_mean((x - mean) ** 2, -1, keep_dims=True) return scale * (x - mean) * tf.rsqrt(var + eps) + offset
Example #25
Source File: attention_wrapper.py From addons with Apache License 2.0 | 5 votes |
def build(self, input_shape): super().build(input_shape) if self.scale and self.scale_weight is None: self.scale_weight = self.add_weight( "attention_g", initializer=tf.ones_initializer, shape=() ) self.built = True
Example #26
Source File: test_gaussian_mlp_model.py From garage with MIT License | 5 votes |
def test_adaptive_std_is_pickleable(self, output_dim, hidden_sizes, std_hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, std_hidden_sizes=std_hidden_sizes, std_share_network=False, adaptive_std=True, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer(), std_hidden_nonlinearity=None, std_hidden_w_init=tf.ones_initializer(), std_output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist # get output bias with tf.compat.v1.variable_scope('GaussianMLPModel', reuse=True): bias = tf.compat.v1.get_variable( 'dist_params/mean_network/output/bias') # assign it to all ones bias.load(tf.ones_like(bias).eval()) h = pickle.dumps(model) output1 = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) with tf.compat.v1.Session(graph=tf.Graph()) as sess: input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, None, 5)) model_pickled = pickle.loads(h) dist2 = model_pickled.build(input_var).dist output2 = sess.run( [dist2.loc, tf.math.log(dist2.stddev())], feed_dict={input_var: self.obs}) assert np.array_equal(output1, output2) # pylint: disable=assignment-from-no-return
Example #27
Source File: tf_util_dgcnn.py From AlignNet-3D with BSD 3-Clause "New" or "Revised" License | 5 votes |
def batch_norm_dist_template(inputs, is_training, scope, moments_dims, bn_decay): """ The batch normalization for distributed training. Args: inputs: Tensor, k-D input ... x C could be BC or BHWC or BDHWC is_training: boolean tf.Varialbe, true indicates training phase scope: string, variable scope moments_dims: a list of ints, indicating dimensions for moments calculation bn_decay: float or float tensor variable, controling moving average weight Return: normed: batch-normalized maps """ with tf.variable_scope(scope) as sc: num_channels = inputs.get_shape()[-1].value beta = _variable_on_cpu('beta', [num_channels], initializer=tf.zeros_initializer()) gamma = _variable_on_cpu('gamma', [num_channels], initializer=tf.ones_initializer()) pop_mean = _variable_on_cpu('pop_mean', [num_channels], initializer=tf.zeros_initializer(), trainable=False) pop_var = _variable_on_cpu('pop_var', [num_channels], initializer=tf.ones_initializer(), trainable=False) def train_bn_op(): batch_mean, batch_var = tf.nn.moments(inputs, moments_dims, name='moments') decay = bn_decay if bn_decay is not None else 0.9 train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean * (1 - decay)) train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1 - decay)) with tf.control_dependencies([train_mean, train_var]): return tf.nn.batch_normalization(inputs, batch_mean, batch_var, beta, gamma, 1e-3) def test_bn_op(): return tf.nn.batch_normalization(inputs, pop_mean, pop_var, beta, gamma, 1e-3) normed = tf.cond(is_training, train_bn_op, test_bn_op) return normed
Example #28
Source File: test_gaussian_mlp_model.py From garage with MIT License | 5 votes |
def test_without_std_share_network_is_pickleable(self, output_dim, hidden_sizes): model = GaussianMLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, std_share_network=False, adaptive_std=False, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) dist = model.build(self.input_var).dist # get output bias with tf.compat.v1.variable_scope('GaussianMLPModel', reuse=True): bias = tf.compat.v1.get_variable( 'dist_params/mean_network/output/bias') # assign it to all ones bias.load(tf.ones_like(bias).eval()) output1 = self.sess.run( [dist.loc, tf.math.log(dist.stddev())], feed_dict={self.input_var: self.obs}) h = pickle.dumps(model) with tf.compat.v1.Session(graph=tf.Graph()) as sess: input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, None, 5)) model_pickled = pickle.loads(h) dist2 = model_pickled.build(input_var).dist output2 = sess.run( [dist2.loc, tf.math.log(dist2.stddev())], feed_dict={input_var: self.obs}) assert np.array_equal(output1, output2)
Example #29
Source File: common_layers.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def l2_norm(x, filters=None, epsilon=1e-6, name=None, reuse=None): """Layer normalization with l2 norm.""" if filters is None: filters = shape_list(x)[-1] with tf.variable_scope(name, default_name="l2_norm", values=[x], reuse=reuse): scale = tf.get_variable( "l2_norm_scale", [filters], initializer=tf.ones_initializer()) bias = tf.get_variable( "l2_norm_bias", [filters], initializer=tf.zeros_initializer()) epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]] mean = tf.reduce_mean(x, axis=[-1], keepdims=True) l2norm = tf.reduce_sum(tf.square(x - mean), axis=[-1], keepdims=True) norm_x = (x - mean) * tf.rsqrt(l2norm + epsilon) return norm_x * scale + bias
Example #30
Source File: test_mlp_model.py From garage with MIT License | 5 votes |
def test_output_values(self, output_dim, hidden_sizes): model = MLPModel(output_dim=output_dim, hidden_sizes=hidden_sizes, hidden_nonlinearity=None, hidden_w_init=tf.ones_initializer(), output_w_init=tf.ones_initializer()) outputs = model.build(self.input_var).outputs output = self.sess.run(outputs, feed_dict={self.input_var: self.obs}) expected_output = np.full([1, output_dim], 5 * np.prod(hidden_sizes)) assert np.array_equal(output, expected_output) # yapf: disable