Can anyone explain why the following code produces input_t with a shape of (32,) instead of (,32), given the fact that inputs has a shape (100, 32)? Shouldn't input_t produce a vector with 32 attributes/columns?

import numpy as np

timesteps = 100
input_features = 32
output_features = 64

inputs = np.random.random((timesteps, input_features))

state_t = np.zeros((output_features,))

W = np.random.random((output_features, input_features))
U = np.random.random((output_features, output_features))
b = np.random.random((output_features,))

successive_outputs = [ ]

for input_t in inputs:
    output_t = np.tanh(np.dot(W, input_t) + np.dot(U, state_t) + b)
    state_t = output_t 
  • $\begingroup$ What is U parameters for? Is this supposed to be a neural network? $\endgroup$ – JahKnows May 30 '18 at 16:01
  • $\begingroup$ Yes, it's a simple naive implementation of RNN found here $\endgroup$ – Da-le May 30 '18 at 16:16

Imagine the matrix inputs as a 2D table. You have 100 rows and 32 columns. Then the for loop acts as an iterator which will return values along the first dimensional axis. This dimension thus disappears and returns the remaining dimensions. When there is a single dimension the default in Python is $(n,)$.

A matrix $(100, 32)$ iterates through $(32,)$

A matrix $(100,28,28)$ iterated through $(28,28)$

A matrix $(100,2,2,2)$ iterated through $(2,2,2)$

  • $\begingroup$ Re: "When there is a single dimension the default in Python is (n,)." - Does this mean Python will transpose the columns to rows by default, in this example? $\endgroup$ – Da-le May 30 '18 at 16:20
  • $\begingroup$ Yeah it can be seen as a transpose. But it's just the way the data is passed as a one dimensional array. $\endgroup$ – JahKnows May 30 '18 at 16:23
  • 1
    $\begingroup$ It makes sense. For insight, I was ultimately evaluating how the matrices W and input_t posses the correct dimensions for multiplication in np.dot(W, input_t). Thanks for the insight! $\endgroup$ – Da-le May 30 '18 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.