本文介绍了如何直接在第一个时期使用map函数生成的转换,而不是在每个时期执行map函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用map函数对数据集进行一些转换.但是,我发现map函数在每个时代都执行了.映射功能是否可能仅在第一个时期执行,而随后的时期直接使用在第一个时期生成的转换?

I want to apply some transformation to dataset by using map function. However, I found the map function was executed in every epoch.Is it possible that the map function is only executed in first epoch and the following epochs directly use the transformation generated in first epoch?

推荐答案

如果使用Tensorflow设置种子并使用tf.image应用转换,则各个时期之间的随机转换将保持一致.

If you set the seed with Tensorflow and apply transformations with tf.image, the random transformations will be consistent between epochs.

import tensorflow as tf
from skimage import data
tf.random.set_seed(42)
import matplotlib.pyplot as plt

def transform(image):
    image = tf.image.random_hue(image, 0.5, 1.)
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_flip_up_down(image)
    return image

X = tf.stack([data.chelsea() for i in range(4)])
ds = tf.data.Dataset.from_tensor_slices(X).map(transform)

inputs = [[], []]

for epoch in range(2):
    for sample in ds:
        inputs[epoch].append(sample)

inputs_paired = [i for s in inputs for i in s]

fig = plt.figure(figsize=(16, 8))
plt.subplots_adjust(wspace=.1, hspace=.1)
for i in range(8):
    ax = plt.subplot(2, 4, i + 1)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.imshow(inputs_paired[i])
plt.show()

顶部是第一个纪元,底部是第二个纪元.

Top is the first epoch, bottom is the second epoch.

这篇关于如何直接在第一个时期使用map函数生成的转换,而不是在每个时期执行map函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:20