code: def checkSequentialMatchesBatch():
      """check LSTM I/O forward/backward interactions"""
  
      n, b, d = (5, 3, 4)  # sequence length, batch size, hidden size
      input_size = 10
      WLSTM = LSTM.init(input_size, d)  # input size, hidden size
      X = np.random.randn(n, b, input_size)
      h0 = np.random.randn(b, d)
      c0 = np.random.randn(b, d)
  
      # sequential forward
      cprev = c0
      hprev = h0
      caches = [{} for t in range(n)]
      Hcat = np.zeros((n, b, d))
      for t in range(n):
          xt = X[t : t + 1]
          _, cprev, hprev, cache = LSTM.forward(xt, WLSTM, cprev, hprev)
          caches[t] = cache
          Hcat[t] = hprev
  
      # sanity check: perform batch forward to check that we get the same thing
      H, _, _, batch_cache = LSTM.forward(X, WLSTM, c0, h0)
      assert allclose_with_out(H, Hcat), (
          "Sequential and Batch forward don" "t match!"
      )
  
      # eval loss
      wrand = np.random.randn(*Hcat.shape)
      # loss = np.sum(Hcat * wrand)
      dH = wrand
  
      # get the batched version gradients
      BdX, BdWLSTM, Bdc0, Bdh0 = LSTM.backward(dH, batch_cache)
  
      # now perform sequential backward
      dX = np.zeros_like(X)
      dWLSTM = np.zeros_like(WLSTM)
      dc0 = np.zeros_like(c0)
      dh0 = np.zeros_like(h0)
      dcnext = None
      dhnext = None
      for t in reversed(range(n)):
          dht = dH[t].reshape(1, b, d)
          dx, dWLSTMt, dcprev, dhprev = LSTM.backward(
              dht, caches[t], dcnext, dhnext
          )
          dhnext = dhprev
          dcnext = dcprev
  
          dWLSTM += dWLSTMt  # accumulate LSTM gradient
          dX[t] = dx[0]
          if t == 0:
              dc0 = dcprev
              dh0 = dhprev
  
      # and make sure the gradients match
      neon_logger.display(
          "Making sure batched version agrees with sequential version: "
          "(should all be True)"
      )
      neon_logger.display(np.allclose(BdX, dX))
      neon_logger.display(np.allclose(BdWLSTM, dWLSTM))
      neon_logger.display(np.allclose(Bdc0, dc0))
      neon_logger.display(np.allclose(Bdh0, dh0))
  
file_path: neon/tests/lstm_ref.py
imports: from builtins import input
  import numpy
  from neon import logger
  from utils import allclose_with_out
label_desc: Use image node_id 1 for calling a global function with example usage: checkSequentialMatchesBatch() without return types