how to convert numpy array to keras tensor how to convert numpy array to keras tensor python python

how to convert numpy array to keras tensor


In Tensorflow it can be done the following way:

import tensorflow.keras.backend as Kimport numpy as npa = np.array([1,2,3])b = K.constant(a)print(b)# <tf.Tensor 'Const_1:0' shape=(3,) dtype=float32>print(K.eval(b))# array([1., 2., 3.], dtype=float32)

In raw keras it should be done replacing import tensorflow.keras.backend as K with from keras import backend as K.


To convert numpy array to tensor,

import tensor as tf#Considering y variable holds numpy arrayy_tensor = tf.convert_to_tensor(y, dtype=tf.int64) 

#You can use any of the available datatypes that suits best - https://www.tensorflow.org/api_docs/python/tf/dtypes/DType