Скажем, у нас есть ввод x
и метка y
:
iterator = tf.data.Iterator.from_structure((x_type, y_type), (x_shape, y_shape))
tf_x, tf_y = iterator.get_next()
Теперь я использую функцию генерации для создания набора данных:
def gen():
for ....: yield (x, y)
ds = tf.data.Dataset.from_generator(gen, (x_type, y_type), (x_shape, y_shape))
На моем графике я использую tf_x
и tf_y
для обучения, это нормально. Но теперь я хочу сделать ссылку, где у меня нет метки y
. Один обходной путь, который я сделал, - это подделать y (например, tf.zeros(y_shape)), затем я использую заполнитель для инициализации итератора.
x_placeholder = tf.placeholder(...)
y_placeholder = tf.placeholder(...)
ds = tf.data.Dataset.from_tensors((x_placeholder, y_placeholder))
ds_init_op = iterator.make_initializer(ds)
sess.run(ds_init_op, feed_dict={x_placeholder=x, y_placeholder=fake(y))})
Мой вопрос в том, есть ли более чистый способ сделать это? без подделки y
во время вывода?
ОБНОВИТЬ:
Я немного поэкспериментировал, похоже, отсутствует одна операция с набором данных unzip
:
import numpy as np
import tensorflow as tf
x_type = tf.float32
y_type = tf.float32
x_shape = tf.TensorShape([None, 128])
y_shape = tf.TensorShape([None, 10])
x_shape_nobatch = tf.TensorShape([128])
y_shape_nobatch = tf.TensorShape([10])
iterator_x = tf.data.Iterator.from_structure((x_type,), (x_shape,))
iterator_y = tf.data.Iterator.from_structure((y_type,), (y_shape,))
def gen1():
for i in range(100):
yield np.random.randn(128)
ds1 = tf.data.Dataset.from_generator(gen1, (x_type,), (x_shape_nobatch,))
ds1 = ds1.batch(5)
ds1_init_op = iterator_x.make_initializer(ds1)
def gen2():
for i in range(80):
yield np.random.randn(128), np.random.randn(10)
ds2 = tf.data.Dataset.from_generator(gen2, (x_type, y_type), (x_shape_nobatch, y_shape_nobatch))
ds2 = ds2.batch(10)
# my ds2 has two tensors in one element, now the problem is
# how can I unzip this dataset so that I can apply them to iterator_x and iterator_y?
# such as:
ds2_x, ds2_y = tf.data.Dataset.unzip(ds2) #?? missing this unzip operation!
ds2_x_init_op = iterator_x.make_initializer(ds2_x)
ds2_y_init_op = iterator_y.make_initializer(ds2_y)
tf_x = iterator_x.get_next()
tf_y = iterator_y.get_next()