我正在使用 TensorFlow Dataset API 来解析 CSV 文件并运行逻辑回归。我正在按照此处的 TF 文档中的示例进行操作。
以下代码片段显示了我如何设置模型:
def input_fn(path, num_epochs, batch_size):
dataset = tf.data.TextLineDataset(path)
dataset = dataset.map(parse_table, num_parallel_calls=12)
dataset = dataset.repeat(num_epochs)
dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, labels
def parse_table(value):
cols = tf.decode_csv(value, record_defaults=TAB_COLUMN_DEFAULTS)
indep_vars = dict(zip(CSV_COLS, cols))
y = indep_vars.pop('y')
return indep_vars, y
def build_indep_vars():
continuous_vars = [
tf.feature_column.numeric_column(x, shape=1) for x in CONT_COLS]
categorical_vars = [
tf.feature_column.categorical_column_with_hash_bucket(
x, hash_bucket_size=100) for x in CAT_COLS]
return categorical_vars + continuous_vars
调用时lr.train(input_fn = lambda: input_fn(data_path, 1, 100))
(注意:批量大小为 100)我收到错误
ValueError: Feature (key: V1) cannot have rank 0. Give: Tensor("IteratorGetNext:0", shape=(), dtype=float32, device=/device:CPU:0)
所以我假设这意味着其中一个tf.feature_column.numeric_column
调用得到了一个它不喜欢的标量值。但是,我无法弄清楚为什么会这样。我已经设置batch_size
为一个正整数,并且根据文档,默认情况下,产生的 NDarray 的形状tf.feature_column.numeric_column
应该是1Xbatch_size
。谁能解释为什么 TensorFlow 返回此错误?
我敢肯定这个问题有一个简单的答案,这会让我因为没有弄清楚而感到愚蠢,但是在花了一些时间之后,我仍然感到困惑。