将 tensorflow 张量从特征列重塑为训练样本

Reshape tensorflow tensors from feature columns into training samples

目前我的数据集看起来像:

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)

如何在 tensorflow 中重塑它,使数据集的形状变为: (8000,3) 其中 3 是来自三个 feat_3 张量中每一个的记录?

所以而不是拥有: ((8000,), (8000,24), (8000,26)) 我想要一个 8000 长的张量,每个项目看起来像 ((1,), (24,), (26,))

IIUC,你可以试试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,)

否则,您也可以考虑使用参差不齐的张量,或者 tf.tuple 如果您想要单个张量。