Python 如何使用带有“num\u epochs”集合的“read\u batch\u examples”创建“input\u fn”?
我有一个基本的Python 如何使用带有“num\u epochs”集合的“read\u batch\u examples”创建“input\u fn”?,python,tensorflow,skflow,Python,Tensorflow,Skflow,我有一个基本的input\u fn,可以与下面的Tensorflow估计器一起使用。它无需设置num_epochs参数即可完美工作;得到的张量具有离散形状。传入num_epochs,因为除None之外的任何内容都会导致形状未知。我的问题在于在使用num_epoch时构造稀疏张量;在不知道输入张量的形状的情况下,我无法理解如何一般地创建所述张量 有人能想出解决这个问题的办法吗?我希望能够通过num_epochs=1对数据集进行一次评估,以及通过predict生成一组与数据集大小相同的预测 def
input\u fn
,可以与下面的Tensorflow估计器一起使用。它无需设置num_epochs
参数即可完美工作;得到的张量具有离散形状。传入num_epochs
,因为除None
之外的任何内容都会导致形状未知。我的问题在于在使用num_epoch
时构造稀疏张量;在不知道输入张量的形状的情况下,我无法理解如何一般地创建所述张量
有人能想出解决这个问题的办法吗?我希望能够通过num_epochs=1
对数据集进行一次评估,以及通过predict
生成一组与数据集大小相同的预测
def input_fn(batch_size):
examples_op = tf.contrib.learn.read_batch_examples(
FILE_NAMES,
batch_size=batch_size,
reader=tf.TextLineReader,
num_epochs=1,
parse_fn=lambda x: tf.decode_csv(x, [tf.constant([''], dtype=tf.string)] * len(HEADERS)))
examples_dict = {}
for i, header in enumerate(HEADERS):
examples_dict[header] = examples_op[:, i]
continuous_cols = {k: tf.string_to_number(examples_dict[k], out_type=tf.float32)
for k in CONTINUOUS_FEATURES}
# Problems lay here while creating sparse categorical tensors
categorical_cols = {
k: tf.SparseTensor(
indices=[[i, 0] for i in range(examples_dict[k].get_shape()[0])],
values=examples_dict[k],
shape=[int(examples_dict[k].get_shape()[0]), 1])
for k in CATEGORICAL_FEATURES}
feature_cols = dict(continuous_cols)
feature_cols.update(categorical_cols)
label = tf.string_to_number(examples_dict[LABEL], out_type=tf.int32)
return feature_cols, label
我通过创建一个特定于
输入的函数来解决上述问题;它在不知道形状的情况下吸收一个密集的柱状物并产生一个稀疏传感器。使用tf.range
和tf.shape
实现该功能。无需进一步说明,以下是独立于设置的num\u epochs
工作的通用input\u fn
代码:
def input_fn(batch_size):
examples_op = tf.contrib.learn.read_batch_examples(
FILE_NAMES,
batch_size=batch_size,
reader=tf.TextLineReader,
num_epochs=1,
parse_fn=lambda x: tf.decode_csv(x, [tf.constant([''], dtype=tf.string)] * len(HEADERS)))
examples_dict = {}
for i, header in enumerate(HEADERS):
examples_dict[header] = examples_op[:, i]
feature_cols = {k: tf.string_to_number(examples_dict[k], out_type=tf.float32)
for k in CONTINUOUS_FEATURES}
feature_cols.update({k: dense_to_sparse(examples_dict[k])
for k in CATEGORICAL_FEATURES})
label = tf.string_to_number(examples_dict[LABEL], out_type=tf.int32)
return feature_cols, label
def dense_to_sparse(dense_tensor):
indices = tf.to_int64(tf.transpose([tf.range(tf.shape(dense_tensor)[0]), tf.zeros_like(dense_tensor, dtype=tf.int32)]))
values = dense_tensor
shape = tf.to_int64([tf.shape(dense_tensor)[0], tf.constant(1)])
return tf.SparseTensor(
indices=indices,
values=values,
shape=shape
)
希望这对别人有帮助