Python 如何通过tf.data.Dataset API向生成的批次添加时间步长维度
我想将时间步长维度添加到批生成中 目前我正在做Python 如何通过tf.data.Dataset API向生成的批次添加时间步长维度,python,tensorflow,keras,tensorflow2.0,tf.data.dataset,Python,Tensorflow,Keras,Tensorflow2.0,Tf.data.dataset,我想将时间步长维度添加到批生成中 目前我正在做 train_ds = tf.data.Dataset.\ from_tensor_slices((x_train, y_train)).\ shuffle(10000).\ batch(32) 获取批量大小(32,特征向量长度) 我想将时间步长尺寸添加到批次中,使其具有(批次大小、时间步长、特征向量长度) 如何使用tf.data?您可以使用tf.data.Dataset()和.zip()的.window()方法进行输入和输
train_ds = tf.data.Dataset.\
from_tensor_slices((x_train, y_train)).\
shuffle(10000).\
batch(32)
获取批量大小(32,特征向量长度)
我想将时间步长尺寸添加到批次中,使其具有(批次大小、时间步长、特征向量长度)
如何使用
tf.data
?您可以使用tf.data.Dataset()
和.zip()
的.window()
方法进行输入和输出
import tensorflow as tf
import numpy as np
x_train = np.random.rand(1000, 5)
y_train = np.sum(x_train, axis=1)
x = tf.data.Dataset.from_tensor_slices(x_train).\
window(size=3, shift=1, stride=1, drop_remainder=True).\
flat_map(lambda l: l.batch(3))
y = tf.data.Dataset.from_tensor_slices(y_train)
ds = tf.data.Dataset.zip((x, y)).batch(2, drop_remainder=True)
for xx, yy in ds:
print(xx, yy)
break
这是一批2个张量,共3个时间步,共5个功能及其各自的目标。我想您要找的是
.window
函数。
tf.Tensor(
[[[0.85339111 0.00937855 0.6432005 0.31875691 0.83835893]
[0.91914805 0.13469408 0.40381527 0.80296816 0.4389627 ]
[0.40326491 0.28575999 0.86602507 0.40515333 0.35390637]]
[[0.91914805 0.13469408 0.40381527 0.80296816 0.4389627 ]
[0.40326491 0.28575999 0.86602507 0.40515333 0.35390637]
[0.00197349 0.46558597 0.66426367 0.00787106 0.07879078]]], shape=(2, 3, 5),
dtype=float64) tf.Tensor([2.663086 2.69958826], shape=(2,), dtype=float64)