You could convert non-rectangular Python sequence to RaggedTensor as:

x_train = tf.ragged.constant(XTrain)
Answer from Rahul Vishwakarma on Stack Overflow
๐ŸŒ
ProjectPro
projectpro.io โ€บ recipes โ€บ convert-numpy-array-tensor
Convert numpy array to tensor - ProjectPro
January 19, 2023 - import tensorflow as tf import numpy as np ยท array = np.array([[1,2,3],[3,4,5],[5,6,7]]) print("This is a numpy array:") print(array, array.shape, type(array)) This is a numpy array: [[1 2 3] [3 4 5] [5 6 7]] (3, 3) <class 'numpy.ndarray'> tensor = tf.convert_to_tensor(array) print("Converted numpy array into tensor:") print(tensor) Converted numpy array into tensor: tf.Tensor( [[1 2 3] [3 4 5] [5 6 7]], shape=(3, 3), dtype=int64) def con_ten(convert_func): convert_func = tf.convert_to_tensor(convert_func, dtype=tf.int32) return convert_func first_value = con_ten(tf.constant([[1,2,3,4],[5,6,7,8]])) print(first_value) tf.Tensor( [[1 2 3 4] [5 6 7 8]], shape=(2, 4), dtype=int32) In the second method, we can directly make a function and call that function whenever we want to perform the task of conversion of array into tensor for several times.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-convert-a-numpy-ndarray-to-a-pytorch-tensor-and-vice-versa
How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?
September 12, 2023 - The difference between these two is that a tensor utilizes the GPUs to accelerate numeric computation. We convert a numpy.ndarray to a PyTorch tensor using the function torch.from_numpy().
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72874580 โ€บ how-to-convert-numpy-array-to-a-tensor-in-this-case
How to convert numpy array to a tensor in this case? - Stack Overflow
You can convert numpy array into tensor using tf.convert_to_tensor(). For example: import numpy as np a=np.array([1,2,3,4,5,5,]) type(a) Output: numpy.ndarray ยท Converting numpy array to tensor: import tensorflow as tf b=tf.convert_to_tensor(a) ...
Find elsewhere
๐ŸŒ
Data Science Weekly
datascienceweekly.org โ€บ tutorials โ€บ pytorch-numpy-to-tensor-convert-a-numpy-array-to-a-pytorch-tensor
PyTorch NumPy to tensor: Convert A NumPy Array To A PyTorch Tensor
To do that, we're going to define a variable torch_ex_float_tensor and use the PyTorch from NumPy functionality and pass in our variable numpy_ex_array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-a-numpy-array-to-tensor
How to Convert a Numpy Array to Tensor?
August 3, 2023 - In this example, we first import the necessary libraries ? PyTorch and Numpy. Then we create a simple 1D Numpy array. We then use the torch.tensor() method to convert the Numpy array to a PyTorch tensor, and store the resulting tensor in the variable tensor.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ user โ€บ basics.interoperability.html
Interoperability with NumPy โ€” NumPy v2.1 Manual
PyTorch implements __array_wrap__ to be able to get tensors back from NumPy functions, and we can modify it directly to control which type of objects are returned from these functions. CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python. CuPy implements a subset of the NumPy interface by implementing cupy.ndarray, a counterpart to NumPy ndarrays.
๐ŸŒ
Stack Exchange
datascience.stackexchange.com โ€บ questions โ€บ 118465 โ€บ how-can-i-convert-a-numpy-array-of-tensors-to-tensor-of-tensors
tensorflow - How can I convert a numpy array of tensors to tensor of tensors? - Data Science Stack Exchange
February 12, 2023 - The type of an element of 'dafr["Data"]' is "numpy.ndarray" type of each element of this element is "torch.Tensor" shape of each tensor is "200 {torch. Size([200])}" and dtype of tensor is float. Type of element of 'dafr["Label"]' is 'numpy.int64'. While fitting I am getting error "ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).".
๐ŸŒ
Medium
medium.com โ€บ @heyamit10 โ€บ converting-a-numpy-array-to-a-tensor-step-by-step-df329c44b035
Converting a NumPy Array to a Tensor (Step-by-Step) | by Hey Amit | Medium
February 8, 2025 - Finally, you made the tensor GPU-ready with .to('cuda'). โ€œNow that youโ€™ve mastered the basics of conversion, letโ€™s answer some common questions you might have while working with NumPy and tensors.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.interoperability.html
Interoperability with NumPy โ€” NumPy v2.4 Manual
PyTorch implements __array_wrap__ to be able to get tensors back from NumPy functions, and we can modify it directly to control which type of objects are returned from these functions. CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python. CuPy implements a subset of the NumPy interface by implementing cupy.ndarray, a counterpart to NumPy ndarrays.
๐ŸŒ
PyMC Discourse
discourse.pymc.io โ€บ questions โ€บ v5
Is there any function in Pytensor can convert np arrays to tensors - v5 - PyMC Discourse
December 10, 2023 - Is there any function in Pytensor can convert np arrays to tensors?Like np_array = np.array(data) x_np = torch.from_numpy(np_array) in PyTorch ยท Thanks.I have another question: When I use normal likelihood,I need to determine mu by another function, and this function will be called in pymc ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ numpy-array-to-tensor-and-tensor-to-numpy-array-with-pytorch
Convert Numpy Array to Tensor and Tensor to Numpy Array with PyTorch
May 22, 2023 - In this short guide, learn how to convert a Numpy array to a PyTorch tensor, and how to convert a PyTorch tensor to a Numpy array. Deal with both CPU and GPU tensors and avoid conversion exceptions!