I have a 2d matrix where the features are columns and the samples are rows (178136 features and 416 samples). I transposed this matrix and then converted this matrix to 3d by creating a NumPy array of 3 duplicate copies of this matrix using the following code.
matrix = matrix.transpose()
dataset = np.array([matrix,matrix,matrix])
The dimensions became the following: (3,178136,416) where 3 is the # of time steps for the sequential model, 178136 are the features, and 416 is the number of samples.
I am having trouble specifying the input shape for the Keras function api for the model I am trying to train. Below is the code I have written:
import numpy as np
import tensorflow
from tensorflow import keras
from tensorflow.keras import layers
inputdata = np.load('filepath')
outputdata = np.load('filepath')
# loads data from .npy files
input_size = 178136
# The input size is the number of genes in the dataset.
output_size = 155551
# The output size is the number of protein coding genes in the dataset.
inputs = keras.Input(shape=(3,input_size))
# This is the input layer of the neural network.
x = layers.GRU(input_size, return_sequences=True)(inputs)
# This is the first hidden layer of the GRU neural network.
x = layers.GRU(input_size, return_sequences=True)(x)
# This is the second hidden layer of the GRU neural network.
outputs = layers.Dense(output_size)(x)
# This is the output layer of the GRU neural network.
model = keras.Model(inputs=inputs, outputs=outputs, name="GRU_for_GRE_Modeling")
# This code specifies the input and output layers of the neural network.
model.compile(optimizer=keras.optimizers.Adam(), loss="categorical_crossentropy",metrics = ['accuracy'])
# This code specifies the optimizer, loss, and evaluation metrics for the neural network.
print('model compiled')
csv_logger = tensorflow.keras.callbacks.CSVLogger('evaluation_metrics.csv', separator=",", append=False)
history = model.fit(inputdata, outputdata, batch_size=1, epochs=1000000, callbacks=csv_logger, validation_split=.25)
# This code actually trains the neural network and saves the evaluation metrics.
model.save('filepath')
# This code saves the trained neural network to a specified path.
Here is the error I keep receiving when I try to run this code:
history = model.fit(inputdata, outputdata, batch_size=1, epochs=1000000, callbacks=csv_logger, validation_split=.25)
File "/data/Libs/tensorflow/1.12.2/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
validation_split=validation_split)
File "/data/Libs/tensorflow/1.12.2/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
class_weight, batch_size)
File "/data/Libs/tensorflow/1.12.2/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1117, in _standardize_weights
exception_prefix='input')
File "/data/Libs/tensorflow/1.12.2/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 332, in standardize_input_data
' but got array with shape ' + str(data_shape))
ValueError: Error when checking input: expected input_1 to have shape (3, 178136) but got array with shape (178136, 416)
Can someone help me resolve this issue? I have gone through all the documentation and I can't seem to find how to specify the number of samples to make this error go away
I tried to replicate your issue but its working as expected
import numpy as np
import tensorflow
from tensorflow import keras
from tensorflow.keras import layers
#inputdata = np.load('filepath')
#outputdata = np.load('filepath')
# loads data from .npy files
input_size = 100
# The input size is the number of genes in the dataset.
output_size = 80
# The output size is the number of protein coding genes in the dataset.
inputs = keras.Input(shape=(3,input_size))
# This is the input layer of the neural network.
x = layers.GRU(input_size, return_sequences=True)(inputs)
x = layers.GRU(input_size, return_sequences=True)(x)
outputs = layers.Dense(output_size)(x)
# This is the output layer of the GRU neural network.
model = keras.Model(inputs=inputs, outputs=outputs, name="GRU_for_GRE_Modeling")
# This code specifies the input and output layers of the neural network.
model.compile(optimizer=keras.optimizers.Adam(), loss="categorical_crossentropy",metrics = ['accuracy'])
# This code specifies the optimizer, loss, and evaluation metrics for the neural network.
print('model compiled')
model.summary()
#csv_logger = tensorflow.keras.callbacks.CSVLogger('evaluation_metrics.csv', separator=",", append=False)
#history = model.fit(inputdata, outputdata, batch_size=1, epochs=1000000, callbacks=csv_logger, validation_split=.25)
Output: model.summary shows correct input shape
model compiled
Model: "GRU_for_GRE_Modeling"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_7 (InputLayer) [(None, 3, 100)] 0
_________________________________________________________________
gru_10 (GRU) (None, 3, 100) 60600
_________________________________________________________________
gru_11 (GRU) (None, 3, 100) 60600
_________________________________________________________________
dense_2 (Dense) (None, 3, 80) 8080
=================================================================
Total params: 129,280
Trainable params: 129,280
Non-trainable params: 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.