Open CharlesWHarrison opened 3 years ago
Hi, thanks for filing.
You are correct that R arrays use a different ordering from default numpy arrays (Fortran ordering vs C ordering). However, array ordering is, at least for keras/tensorflow, independent from how array dimensions are interpreted.
When you call fit(X)
, where dim(X) == c(8, 5, 3)
, then keras understands that you've provided a batch with 8 cases, 5 timesteps, and 3 features per timestep. This is regardless of if X
is Fortran ordered or C ordered under the hood.
Hello, thank you very much for your reply. I understand now, so thank you. Also, perhaps this should be documented as it differs from keras' behavior in Python.
Hi, thanks.
What do you mean it differ's from the behavior in Python? As far as I know, if you construct a Fortran-ordered numpy array and pass it to fit, it is interpreted the same way as a C-ordered array, both in R and python.
Sorry for the confusion. I mean that the R array passed to keras looks different from the equivalent Python array, and this may be confusing to some R users.
Describe the problem
Python's 3D array differs from R's 3D array. The first dimension of a Python 3D array is the number of arrays, the second is the number of rows in each array, and the third is the number of columns in each array. In R, a 3D array's first dimension is the number of rows, the second dimension is the number of columns, and the third is the number of matrices (arrays). Python's 3D array is consistent with keras, but R's 3D array does not seem to be consistent with keras and I think this is causing a problem. Reshaping the R 3D array allows keras to run without error, but the structure of the data is changed and is no longer consistent with the underlying problem.
Describe the current behavior.
The code above produces the following error:
A 3D array in R has 3 dimensions i,j,k where i is the number of rows in each matrix, j is the number of columns in each matrix, and k is the number of matrices. Keras expects the X input to have three dimensions as well, but there seems to be a conflict between R and keras. The keras library expects i to be the number of matrices (R uses number of rows in each matrix), j to be the number of rows (R uses number of columns), and k to be the number of columns (R uses number of matrices).
In the example above, I have 8 samples, 5 time steps, and 3 features, so my X has dimension 5 x 3 x 8 in R. My response Y has dimension 8 (rows) by 2 (columns). However, keras expects X to have dimension 8 x 5 x 3.
Attempted Solution To fix the problem, I thought I could just reshape the data using the code below. This code reshapes the array to be 8 x 5 x 3, but 8 x 5 x 3 in R has a different meaning from that of keras. In this case, 8 x 5 x 3 in R means that there are only 3 samples where each sample has 8 rows and 5 columns.