[Q&A] Do tf.keras.model convert numpy.ndarray to tensor for every batch?

I currently use tensorflow 2.5 with GPU Quadro P1000. The tensorflow was built with cudatoolkit and cudnn to activate my GPU

In current, I have a large numpy array (4Gb) with np.uint8 dtype. The model was built using tf.keras.model but at every layer, the argument dtype, i use np.float32 but not tf.float32.

Although when calling model.fit(ndarray), I don’t convert to tf.Tensor and GPU still worked woth 40 - 50%.

My question is
(1): Is it possible to set the layers’ dtype by tf.dtype such as tf.float32 and use numpy array to directly model.fit() or model.predict() with active GPU without error, and vice versa?
(2) Does tensorflow, for every batch fed to tf.keras.Model automatically convert to the Tensor before training it (with the setting mentioned above), and if so, do I should convert to tf.Tensor (tf.constant) beforehand to boost performance and input pipeline, in that case, do I need to disable eager execution or do some trick to boost performance?

At current, the model is trained with 240 - 300 seconds per epoch with ~ 2300 batchs (256 batch size)

Thank you. I love to hear some response

Windows 10 Pro, TF 2.5, Numpy 1.19+, Python 3.8.11

Hi @IchiruTake, Yes, you can pass dtype argument to layers, but some layers by default will accept only float. For example, A Dense layer can only be built with a floating-point . If you pass dtype=tf.int64 it will produce the error

TypeError: A Dense layer can only be built with a floating-point dtype. Received: dtype=<dtype: 'int64'>

you can pass numpy array directly to model.fit( ). For example, if i have a model

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,),dtype=tf.float32))
model.add(tf.keras.layers.Dense(32, activation='relu',dtype=tf.float32))
model.add(tf.keras.layers.Dense(32))

now i will create a random integer data to train the model using numpy

import numpy as np
X_train = np.random.randint(10,size=(100,16))
y_train = np.random.randint(10,size=(100,32))

now i can train the model using model.fit( )

model.fit(X_train, y_train,batch_size=32,epochs=1)

As per my knowledge, TensorFlow will automatically convert input to tensor during the training process.

Thank You!