Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 带前置或后置填充选项的填充_批次_Tensorflow_Dataset_Padding - Fatal编程技术网

Tensorflow 带前置或后置填充选项的填充_批次

Tensorflow 带前置或后置填充选项的填充_批次,tensorflow,dataset,padding,Tensorflow,Dataset,Padding,我有一个可变长度序列的数据集(一个tensorflow TFRecord数据集)来为LSTM网络提供数据,我想尝试比较批处理中的前填充和后填充,但当前的padded_批处理函数只在序列的末尾填充。我知道API中有tf.keras.preprocessing.sequence.pad_sequences函数,但我不知道如何将此函数应用于数据集批处理程序。tensorflow中的padded_batch函数同时执行填充和批处理,它将动态查找每个批所需的填充大小。我自己如何实现这一点?我现在的代码是这

我有一个可变长度序列的数据集(一个tensorflow TFRecord数据集)来为LSTM网络提供数据,我想尝试比较批处理中的前填充和后填充,但当前的padded_批处理函数只在序列的末尾填充。我知道API中有
tf.keras.preprocessing.sequence.pad_sequences
函数,但我不知道如何将此函数应用于数据集批处理程序。tensorflow中的padded_batch函数同时执行填充和批处理,它将动态查找每个批所需的填充大小。我自己如何实现这一点?我现在的代码是这样的,我正在读取多个TFRecord文件并将它们交错以生成混合数据集:

featuresDict = {'data': tf.FixedLenFeature([], dtype=tf.string),
                'rows': tf.FixedLenFeature([], dtype=tf.int64),
                'label': tf.FixedLenFeature([], dtype=tf.int64)
               }

def parse_tfrecord(example):
    features = tf.parse_single_example(example, featuresDict)
    label = tf.one_hot(features['label'],N)
    rows = features['rows']
    data = tf.decode_raw(features['data'], tf.int64)
    data = tf.reshape(data, (rows,num_features)
    return data, label

def read_datasets(pattern, numFiles, numEpochs=None, batchSize=None):
    files = tf.data.Dataset.list_files(pattern)

    def _parse(x):
        x = tf.data.TFRecordDataset(x, compression_type='GZIP')
        return x

    dataset = files.interleave(_parse, cycle_length=numFiles, block_length=1).map(parse_tfrecord)
    padded_shapes = (tf.TensorShape([None, num_features]), tf.TensorShape([N,])))
    dataset = dataset.padded_batch(batchSize, padded_shapes)
    dataset = dataset.prefetch(buffer_size=batchSize)
    dataset = dataset.repeat(numEpochs)
    return dataset

我和你有同样的问题,我也注意到你也提出了一个关于张量流的问题。我用
pad\u序列解决了这个问题,我想我解决得很好

将numpy导入为np
导入tensorflow作为tf
#源代码段https://www.tensorflow.org/guide/data
#生成数据的生成器
def gen_系列()
i=0
np.random.seed(0)
尽管如此:
size=np.random.randint(0,10)
产量np.random.normal(大小=(大小,))
i+=1
#将生成器转换为数据集
ds_series=tf.data.Dataset.from_生成器(gen_series,
输出类型=(tf.float32),
输出形状=((无,))
#输出_形状为(无),因为向量大小未知
#取前5个样本
打印(“填充前”)
对于ds_系列中的向量,取(5):
打印(矢量)
打印(“*”*10)
#开始转变
def pad_会话(会话):
"""
我们用maxlen为5的预订单填充顺序。
如果任何向量大于5,我们将截断预序列
"""
返回tf.keras.preprocessing.sequence.pad_序列(
[session.numpy()],
maxlen=5,
truncating=“pre”,
padding='pre',
值=0.0,
dtype=np.float).挤压()
def pad_map_fn(会话):
返回tf.py_函数(pad_会话,inp=[session],Tout=(tf.float32))
padded_dataset=ds_series.map(pad_map_fn)
打印(“填充后”)
对于填充数据集中的预填充向量,取(5):
打印(预填充向量)
并将生成以下输出

Before padding
tf.Tensor([ 0.11849646  0.1139678   0.37025538  1.0405308  -1.5169828 ], shape=(5,), dtype=float32)
tf.Tensor(
[-0.8662762  -0.10321885  0.41059852  0.14404356  1.4542735   0.7610377
  0.12167501  0.44386324], shape=(8,), dtype=float32)
tf.Tensor([0.33367434 1.4143772 ], shape=(2,), dtype=float32)
tf.Tensor([-0.12405066  1.1682731   0.94718593], shape=(3,), dtype=float32)
tf.Tensor([-2.5529897], shape=(1,), dtype=float32)
**********
After padding
tf.Tensor([ 0.11849646  0.1139678   0.37025538  1.0405308  -1.5169828 ], shape=(5,), dtype=float32)
tf.Tensor([0.14404356 1.4542735  0.7610377  0.12167501 0.44386324], shape=(5,), dtype=float32)
tf.Tensor([0.         0.         0.         0.33367434 1.4143772 ], shape=(5,), dtype=float32)
tf.Tensor([ 0.          0.         -0.12405066  1.1682731   0.94718593], shape=(5,), dtype=float32)
tf.Tensor([ 0.         0.         0.         0.        -2.5529897], shape=(5,), dtype=float32)
我想让您注意到
pad\u会话
,我们将序列
[session.numpy]
传递到
pad\u序列
,因为我们需要将二维数组传递到其中

也许有更好的办法解决这个问题,但我得到的答案是


希望它能帮助你

谢谢你的评论。在我的例子中,我不知道max_len,因为当为下一批获取数据集时,它在每批中都在变化。Padded_批处理函数有权访问此数据,但我没有。我不想填充整个数据集,我想一批一批地填充。为什么需要tf.py_函数?