'Reshape tensorflow tensors from feature columns into training samples

Currently my dataset looks like:

feat_1 = tf.random.uniform(
    shape=[8000,1],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

feat_2 = tf.random.uniform(
    shape=[8000,24],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

feat_3 = tf.random.uniform(
    shape=[8000,26],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

# Current_state
dataset = (feat_1, feat_2, feat_3)

How can I reshape it in tensorflow so that the dataset is instead shaped like: (8000,3) where the 3 is a record from each of the three feat_3 tensors?

so rather than have: ((8000,), (8000,24), (8000,26)) I want an 8000 long tensor with each item looking like ((1,), (24,), (26,))



Solution 1:[1]

IIUC, you can try using tf.data.Dataset.from_tensor_slices:

import tensorflow as tf

feat_1 = tf.random.uniform(
    shape=[8000,1],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

feat_2 = tf.random.uniform(
    shape=[8000,24],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

feat_3 = tf.random.uniform(
    shape=[8000,26],
    minval=0,
    maxval=1,
    dtype=tf.dtypes.float32,
    seed=1123,
    name=None
)

dataset = tf.data.Dataset.from_tensor_slices((feat_1, feat_2, feat_3))

for x, y, z in dataset.take(1):
  print(x.shape, y.shape, z.shape)

# (1,) (24,) (26,)

Otherwise, you could also consider using a ragged tensor or tf.tuple if you want a single tensor.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1