Vanilla RNN Hidden States
Problem statement
Implement the forward pass of a vanilla recurrent neural network over one input sequence. All weights are fixed; compute the hidden state after every input.
Cell and sequence
inputshasTrows ofDfeatures in time order.inputWeightshas shapeH x D;recurrentWeightshas shapeH x H. Matrix rowrproduces hidden coordinater.biasandinitialStateeach containHvalues. Start withprevious = initialState.
For input row t and each hidden coordinate r, compute:
value = bias[r]
+ sum(inputWeights[r][j] * inputs[t][j], j = 0..D-1)
+ sum(recurrentWeights[r][k] * previous[k], k = 0..H-1)
following[r] = tanh(value)
Every coordinate of following must read the same frozen previous vector. Only after the entire new vector is computed does it become previous for the next input.
Returned states
- Return a matrix with
Trows andHcolumns, containing each post-input hidden vector in time order. - Do not include
initialStateas an output row. An empty input sequence returns[]. - Retain the values of every output row and leave all supplied arrays unchanged.
- Use direct loops and standard mathematical functions such as
tanh. Training, backpropagation, LSTM gates, and external tensor or ML libraries are outside this exercise. - Each returned value is accepted within absolute error
10^-6.
What the interview report shared
The interviewer began a technical screen by asking the candidate to handwrite an RNN and asked details about the implementation. The report does not specify the cell, tensor shapes, or whether training was required. This practice version fixes a vanilla tanh cell and its forward-pass contract.
Function
rnnHiddenStates(inputs: double[][], inputWeights: double[][], recurrentWeights: double[][], bias: double[], initialState: double[]) → double[][]Examples
Example 1
inputs = [[0],[1],[-1]]inputWeights = [[1]]recurrentWeights = [[0]]bias = [0]initialState = [0.75]return = [[0.0],[0.7615941559557649],[-0.7615941559557649]]The recurrent weight is 0, so each step depends only on its input. The states are tanh(0), tanh(1), and tanh(-1).
Example 2
inputs = [[0],[0],[0]]inputWeights = [[0]]recurrentWeights = [[1]]bias = [0]initialState = [1]return = [[0.7615941559557649],[0.6420149920119999],[0.5662699759614797]]The input contribution and bias are zero. Starting at 1, repeatedly apply tanh to the previous hidden value; the initial 1 is not an output row.
Example 3
inputs = [[0.5,-1]]inputWeights = [[1,0],[0,1]]recurrentWeights = [[0,0],[0,0]]bias = [0,0]initialState = [0,0]return = [[0.46211715726000974,-0.7615941559557649]]The input-weight matrix is the identity and recurrence is zero. The only returned row is [tanh(0.5), tanh(-1)].
Constraints
0 <= T <= 30,1 <= D <= 8, and1 <= H <= 8.- Every array has the shape stated above. When
T = 0, deriveDfrom the nonempty rows ofinputWeights. - All values are finite. Entries of
inputs,inputWeights, andbiaslie in[-2, 2]; entries ofinitialStatelie in[-1, 1]. - For each row
rofrecurrentWeights,sum(abs(recurrentWeights[r][k]), k = 0..H-1) <= 1. This bound limits amplification of numerical error across the sequence.