### TensorFlow 1.x RMSNorm Function Usage Source: https://context7.com/bzhanggo/rmsnorm/llms.txt Shows basic RMSNorm application, partial RMSNorm (pRMSNorm), and RMSNorm with a bias term in TensorFlow 1.x. Also includes an example of its use within an RNN cell and a full session run. ```python import tensorflow as tf from rmsnorm_tensorflow import rms_norm # ----- Basic usage: normalize last dimension ----- x = tf.placeholder(tf.float32, shape=[None, 128, 512], name="input") normed = rms_norm(x, eps=1e-8, scope="rms_norm_1") # normed shape: [batch, 128, 512] # ----- Partial RMSNorm: use 25% of dims for statistics ----- normed_partial = rms_norm(x, p=0.25, scope="rms_norm_partial") # ----- With bias term enabled ----- normed_bias = rms_norm(x, bias=True, scope="rms_norm_biased") # ----- Inside an RNN cell (Theano-style TF port) ----- hidden_size = 256 cell_input = tf.placeholder(tf.float32, shape=[None, hidden_size]) # Replace LayerNorm in a GRU gate pre-activation gate_preact = tf.matmul(cell_input, tf.get_variable("W_gate", [hidden_size, hidden_size])) gate_normed = rms_norm(gate_preact, scope="gate_rms") gate = tf.sigmoid(gate_normed) # ----- Full session run example ----- import numpy as np with tf.Session() as sess: sess.run(tf.global_variables_initializer()) feed = {x: np.random.randn(8, 128, 512).astype(np.float32)} result = sess.run(normed, feed_dict=feed) print(result.shape) # (8, 128, 512) print(result.mean(axis=-1)[:2, :3]) # values near 0 not guaranteed (no re-centering) ``` -------------------------------- ### Download Order-Embedding Models Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Download pre-trained models for the order-embedding model used in image-caption retrieval experiments. ```bash wget http://data.statmt.org/bzhang/neurips19_rmsnorm/oe/order.npz wget http://data.statmt.org/bzhang/neurips19_rmsnorm/oe/order.pkl ``` -------------------------------- ### Download Dataset for Attentive Reader Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Download the dataset required for the CNN/Daily Mail Reading Comprehension experiments. ```bash wget http://www.cs.toronto.edu/~rkiros/top4.zip ``` -------------------------------- ### Initializing and Using RMS-LN GRU Layer in Theano Source: https://context7.com/bzhanggo/rmsnorm/llms.txt Shows how to initialize parameters for the RMS-LN GRU layer using `param_init_rlngru` and then perform a forward pass with `rlngru_layer`. The output `rval[0]` represents the Theano expression for hidden states. ```python options = {'dim_proj': 64} params = {} params = param_init_rlngru(options, params, prefix='rlngru') print(list(params.keys())) # ['rlngru_W', 'rlngru_b', 'rlngru_U', 'rlngru_Wx', 'rlngru_Ux', # 'rlngru_bx', 'rlngru_s1', 'rlngru_s2', 'rlngru_s3', 'rlngru_s4'] # Convert params to Theano shared variables and run a forward pass import theano tparams = {k: theano.shared(v) for k, v in params.items()} state_below = tensor.tensor3('state_below') # [seq_len, batch, input_dim] init_state = tensor.matrix('init_state') # [batch, dim] mask = tensor.matrix('mask') # [seq_len, batch] rval = rlngru_layer(tparams, state_below, init_state, options, prefix='rlngru', mask=mask) # rval[0] is the Theano expression for hidden states [seq_len, batch, dim] ``` -------------------------------- ### Symbolic Usage of Theano's rln and ln Functions Source: https://context7.com/bzhanggo/rmsnorm/llms.txt Demonstrates the symbolic definition and compilation of Theano functions for RMS normalization (rln) and full layer normalization (ln). Shows how to create Theano functions and execute them with sample NumPy data. ```python import theano import theano.tensor as tensor from layers import rln, ln, get_layer, param_init_rlngru, rlngru_layer # ----- Symbolic usage of rln ----- x_sym = tensor.matrix('x') # [batch, dim] s_sym = tensor.vector('s') # [dim] normed_rln = rln(x_sym, s_sym) # normed_rln = x / sqrt(mean(x^2) + 1e-5) * s (per row) f_rln = theano.function([x_sym, s_sym], normed_rln) import numpy as np x_val = np.random.randn(4, 64).astype('float32') s_val = np.ones(64, dtype='float32') print(f_rln(x_val, s_val).shape) # (4, 64) # ----- Symbolic usage of ln (full LayerNorm for comparison) ----- b_sym = tensor.vector('b') normed_ln = ln(x_sym, b_sym, s_sym) f_ln = theano.function([x_sym, b_sym, s_sym], normed_ln) b_val = np.zeros(64, dtype='float32') print(f_ln(x_val, b_val, s_val).shape) # (4, 64) ``` -------------------------------- ### Download Attentive Reader Stats Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Download the log files containing statistics from the model trained using RMSNorm for the attentive reader. ```bash wget http://data.statmt.org/bzhang/neurips19_rmsnorm/attentive_reader/stats_lstm_s1.npz.pkl ``` -------------------------------- ### PyTorch RMSNorm Module Usage Source: https://context7.com/bzhanggo/rmsnorm/llms.txt Demonstrates standard RMSNorm, partial RMSNorm (pRMSNorm), and RMSNorm with a bias term. Also shows integration as a drop-in replacement for nn.LayerNorm within a Transformer block and includes a gradient check for training. ```python import torch import torch.nn as nn from rmsnorm_torch import RMSNorm # ----- Standard RMSNorm (full dimensions) ----- model_dim = 512 rms_norm = RMSNorm(d=model_dim, eps=1e-8) x = torch.randn(32, 128, model_dim) # [batch=32, seq_len=128, d_model=512] out = rms_norm(x) print(out.shape) # torch.Size([32, 128, 512]) # ----- Partial RMSNorm (pRMSNorm): use 50% of dims for RMS estimate ----- p_rms_norm = RMSNorm(d=model_dim, p=0.5, eps=1e-8) out_partial = p_rms_norm(x) print(out_partial.shape) # torch.Size([32, 128, 512]) # ----- RMSNorm with optional bias term ----- rms_norm_biased = RMSNorm(d=model_dim, bias=True) out_biased = rms_norm_biased(x) # ----- Drop-in replacement inside a Transformer encoder layer ----- class TransformerBlock(nn.Module): def __init__(self, d_model, n_heads, ffn_dim): super().__init__() self.attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True) self.ffn = nn.Sequential( nn.Linear(d_model, ffn_dim), nn.ReLU(), nn.Linear(ffn_dim, d_model), ) self.norm1 = RMSNorm(d=d_model) # replaces nn.LayerNorm(d_model) self.norm2 = RMSNorm(d=d_model) def forward(self, x): attn_out, _ = self.attn(x, x, x) x = self.norm1(x + attn_out) x = self.norm2(x + self.ffn(x)) return x block = TransformerBlock(d_model=512, n_heads=8, ffn_dim=2048) tokens = torch.randn(4, 64, 512) # [batch=4, seq=64, d=512] print(block(tokens).shape) # torch.Size([4, 64, 512]) # ----- Training with gradient check ----- optimizer = torch.optim.Adam(rms_norm.parameters(), lr=1e-3) loss = out.mean() loss.backward() optimizer.step() print("scale grad:", rms_norm.scale.grad) # non-None gradient tensor ``` -------------------------------- ### Clone Nematus Repository Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Clone the Nematus repository and checkout the specific version v0.3 for machine translation experiments. ```bash git clone https://github.com/EdinburghNLP/nematus.git cd nematus git checkout tags/v0.3 ``` -------------------------------- ### Train Attentive Reader Model Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Command to train the attentive reader model using RMSNorm. Ensure to set the correct device and model directory. ```bash GPUARRAY_FORCE_CUDA_DRIVER_LOAD=True THEANO_FLAGS=mode=FAST_RUN,floatX=float32,device=$device,gpuarray.preallocate=0.8 python -u train_attentive_reader.py \ --use_dq_sims 1 --use_desc_skip_c_g 0 --dim 240 --learn_h0 1 --lr 8e-5 --truncate -1 --model "lstm_s1.npz" --batch_size 64 --optimizer "adam" --validFreq 1000 --model_dir $MDIR --use_desc_skip_c_g 1 --unit_type rlnlstm --use_bidir 1 ``` -------------------------------- ### Implement RMSNorm Layer in Lasagne Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Add this RMSNormLayer class to `nn.py`. It implements the Root Mean Square Layer Normalization. ```python class RMSNormLayer(lasagne.layers.Layer): def __init__(self, incoming, b=lasagne.init.Constant(0.), g=lasagne.init.Constant(1.), W=lasagne.init.Normal(0.05), nonlinearity=relu, **kwargs): super(RMSNormLayer, self).__init__(incoming, **kwargs) self.nonlinearity = nonlinearity k = self.input_shape[1] if b is not None: self.b = self.add_param(b, (k,), name="b", regularizable=False) if g is not None: self.g = self.add_param(g, (k,), name="g") if len(self.input_shape)==4: self.axes_to_sum = (2,3) self.dimshuffle_args = ['x',0,'x','x'] else: self.axes_to_sum = 1 self.dimshuffle_args = ['x',0] def get_output_for(self, input, **kwargs): meanS = T.mean(input ** 2,axis=self.axes_to_sum,keepdims=True) norm_input = input / T.sqrt(meanS + 1e-6) if hasattr(self, 'g'): activation = norm_input*self.g.dimshuffle(*self.dimshuffle_args) else: activation = norm_input if hasattr(self, 'b'): activation += self.b.dimshuffle(*self.dimshuffle_args) return self.nonlinearity(activation) def rms_norm(layer, b=lasagne.init.Constant(0.), g=lasagne.init.Constant(1.), **kwargs): nonlinearity = getattr(layer, 'nonlinearity', None) if nonlinearity is not None: layer.nonlinearity = lasagne.nonlinearities.identity if hasattr(layer, 'b'): del layer.params[layer.b] layer.b = None return RMSNormLayer(layer, b, g, nonlinearity=nonlinearity, **kwargs) ``` -------------------------------- ### Theano RMS-LN LSTM Layer Implementation Source: https://context7.com/bzhanggo/rmsnorm/llms.txt Details the Theano implementation of an LSTM cell with RMS Layer Normalization. It shows parameter initialization using `param_init_rlnlstm`, defining symbolic variables for input, state, memory, and mask, and executing the `rlnlstm_layer` for a forward pass. ```python from layers import param_init_rlnlstm, rlnlstm_layer import theano import theano.tensor as tensor import numpy as np options = {'dim_proj': 128, 'learn_h0': False} params = {} params = param_init_rlnlstm(options, params, prefix='rlnlstm', nin=128, dim=128) # Parameters added: W, U, b, s1, s2, s3 (scale-only, no bias — RMSNorm) tparams = {k: theano.shared(v) for k, v in params.items()} state_below = tensor.tensor3('x') # [seq_len, batch, nin] init_state = tensor.matrix('h0') # [batch, dim] init_memory = tensor.matrix('c0') # [batch, dim] mask = tensor.matrix('mask') # [seq_len, batch] rval = rlnlstm_layer( tparams, state_below, options, prefix='rlnlstm', mask=mask, init_state=init_state, init_memory=init_memory ) h_states = rval[0] # hidden states [seq_len, batch, dim] c_states = rval[1] # cell states [seq_len, batch, dim] # Compile and run f = theano.function( [state_below, mask, init_state, init_memory], [h_states, c_states] ) seq_len, batch, dim = 20, 8, 128 x_val = np.random.randn(seq_len, batch, dim).astype('float32') m_val = np.ones((seq_len, batch), dtype='float32') h0_val = np.zeros((batch, dim), dtype='float32') c0_val = np.zeros((batch, dim), dtype='float32') h, c = f(x_val, m_val, h0_val, c0_val) print(h.shape, c.shape) # (20, 8, 128) (20, 8, 128) ``` -------------------------------- ### Add RMSNorm Option to Training Script Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Integrate the RMSNorm option into `train.py`. This allows specifying 'rms_norm' as the normalization type. ```python elif args.norm_type=='rms_norm': normalizer = lambda l: nn.rms_norm(l) ``` -------------------------------- ### Modify LayerNormLayer for RMSNorm (TensorFlow) Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Replace the existing LayerNormLayer implementation in `layers.py` with this RMSNorm version for TensorFlow. Note the handling of the epsilon value. ```python class LayerNormLayer(object): def __init__(self, layer_size, eps=1e-5): # TODO: If nematus_compat is true, then eps must be 1e-5! self.new_std = tf.get_variable('new_std', [layer_size], initializer=tf.constant_initializer(1)) self.eps = eps self.layer_size = layer_size def forward(self, x): # m, v = tf.nn.moments(x, axes=[-1], keep_dims=True) # std = tf.sqrt(v + self.eps) # norm_x = (x-m)/std # new_x = norm_x*self.new_std + self.new_mean # return new_x ms = tf.reduce_sum(tf.square(x), axis=-1, keep_dims=True) * 1./self.layer_size norm_inputs = x * tf.rsqrt(ms + self.eps) return norm_inputs * self.new_std ``` -------------------------------- ### Modify layer_norm for RMSNorm (Theano) Source: https://github.com/bzhanggo/rmsnorm/blob/master/README.md Update the `layer_norm` function in `layers.py` for Theano compatibility. The bias term `b` can be removed. ```python def layer_norm(x, b, s): _eps = numpy_floatX(1e-5) norm_x = tensor.mean(x * x, axis=-1, keepdims=True) output = x / tensor.sqrt(norm_x + _eps) if x.ndim == 3: output = s[None, None, :] * output + b[None, None,:] else: output = s[None, :] * output + b[None,:] return output ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.