Trying to build simple model just to figure out how to deal with tf.data.Dataset.from_generator
. I can not understand how to set output_shapes
argument. I tried several combinations including not specifying it but still receive some errors due to shape mismatch of the tensors. The idea is just to yield two numpy arrays with SIZE = 10
and run linear regression with them. Here is the code:
SIZE = 10
def _generator():
feats = np.random.normal(0, 1, SIZE)
labels = np.random.normal(0, 1, SIZE)
yield feats, labels
def input_func_gen():
shapes = (SIZE, SIZE)
dataset = tf.data.Dataset.from_generator(generator=_generator,
output_types=(tf.float32, tf.float32),
output_shapes=shapes)
dataset = dataset.batch(10)
dataset = dataset.repeat(20)
iterator = dataset.make_one_shot_iterator()
features_tensors, labels = iterator.get_next()
features = {'x': features_tensors}
return features, labels
def train():
x_col = tf.feature_column.numeric_column(key='x', )
es = tf.estimator.LinearRegressor(feature_columns=[x_col])
es = es.train(input_fn=input_func_gen)
Another question is if it is possible to use this functionality to provide data for feature columns which are tf.feature_column.crossed_column
? The overall goal is to use Dataset.from_generator
functionality in batch training where data is loaded on chunks from a database in cases when data does not fit in memory. All opinions and examples are highly appreciated.
Thanks!