FastPrepVanilla RNN Hidden States

Vanilla RNN Hidden States

NVIDIA logoNVIDIA● MediumFULLTIMEPHONE SCREEN
Learn

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

  • inputs has T rows of D features in time order.
  • inputWeights has shape H x D; recurrentWeights has shape H x H. Matrix row r produces hidden coordinate r.
  • bias and initialState each contain H values. Start with previous = 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 T rows and H columns, containing each post-input hidden vector in time order.
  • Do not include initialState as 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, and 1 <= H <= 8.
  • Every array has the shape stated above. When T = 0, derive D from the nonempty rows of inputWeights.
  • All values are finite. Entries of inputs, inputWeights, and bias lie in [-2, 2]; entries of initialState lie in [-1, 1].
  • For each row r of recurrentWeights, sum(abs(recurrentWeights[r][k]), k = 0..H-1) <= 1. This bound limits amplification of numerical error across the sequence.

More NVIDIA problems

See NVIDIA hiring insights
public double[][] rnnHiddenStates(double[][] inputs, double[][] inputWeights, double[][] recurrentWeights, double[] bias, double[] initialState) {
    // Return the hidden state after each input row.
}
inputs[[0],[1],[-1]]
inputWeights[[1]]
recurrentWeights[[0]]
bias[0]
initialState[0.75]
expected[[0.0],[0.7615941559557649],[-0.7615941559557649]]
Checking account…