【发布时间】:2020-06-01 20:57:50
【问题描述】:
我正在准备一个数据集,然后在存储输出之前训练一个模型(出于知识蒸馏的目的)
为了以 tfrecords 格式存储它们,我需要使用 .zip() 函数。
我使用以下代码重现了错误/错误。 我的实际训练文件有数百行,所以我没有在这里包含它们。
我使用 TensorFlow 2.1。和 ubuntu 18.04 上的 python 3.7
我不能解决的问题是:
数据被洗牌(没关系)。但是在压缩后,元组的顺序不同(这是不行的)。
import tensorflow as tf
ds = tf.data.Dataset.from_tensor_slices([1,2,3,4, 5])
#prepare dataset for training
batch_size=2
ds = ds.cache().repeat().shuffle(buffer_size=5, reshuffle_each_iteration=True).batch(batch_size)
#create model. here: map identity function
model = tf.keras.models.Sequential([tf.keras.layers.Lambda(lambda x: x , input_shape=(1,))])
#train with model.fit()
#make predictions.
pred = model.predict(ds, steps=5//batch_size)
#prepare for saving to tfrecords
ds = ds.unbatch()
ds = ds.take(5)
pred = tf.data.Dataset.from_tensor_slices(pred)
combined = tf.data.Dataset.zip((ds, pred))
#show unwanted behaviour
for (a),c in combined:
print(a,c)
代码 sn-p 的输出显示每行的元素不匹配。 (例如第 1 行:3 应该映射到 3)
tf.Tensor(3, shape=(), dtype=int32) tf.Tensor([4.], shape=(1,), dtype=float32)
tf.Tensor(1, shape=(), dtype=int32) tf.Tensor([1.], shape=(1,), dtype=float32)
tf.Tensor(4, shape=(), dtype=int32) tf.Tensor([1.], shape=(1,), dtype=float32)
tf.Tensor(3, shape=(), dtype=int32) tf.Tensor([2.], shape=(1,), dtype=float32)
【问题讨论】:
-
这里有什么更新吗?我看到类似的东西。 Zip 似乎也很慢。
标签: tensorflow-datasets tensorflow2.x