程序在Tensorflow 1.6

时间:2018-04-22 03:49:47

标签: python csv tensorflow tensorflow-datasets tensorflow-estimator

作为一种学习工具,我正在尝试做一些简单的事情。

我有两个培训CSV文件:

一个包含36列(3500条记录)且0和1的文件。我将这个文件想象成一个扁平的6x6矩阵。 我有另一个CSV文件,其中有1列地面实况0或1(3500条记录),表明6x6矩阵对角线中6个元素中至少有4个是1。

我还有两个测试CSV文件,它们与训练文件的结构相同,但每个文件只有500条记录。

当我使用调试器逐步执行程序时,似乎是......

estimator.train(
    input_fn=lambda: get_inputs(x_paths=[x_train_file], y_paths=[y_train_file], batch_size=32), steps=100)

...运行正常。我在checkpoint目录中看到了文件,并在Tensorboard中看到了丢失函数图。

但是当程序到达时......

eval_result = estimator.evaluate(
    input_fn=lambda: get_inputs(x_paths=[x_test_file], y_paths=[y_test_file], batch_size=32))

......它只是挂起。

我检查了测试文件,并且还尝试使用培训文件运行estimator.evaluate。仍然挂起

我正在使用TensorFlow 1.6,Python 3.6

以下是所有代码:

import tensorflow as tf
import os
import numpy as np

x_train_file = os.path.join('D:', 'Diag', '6x6_train.csv')
y_train_file  = os.path.join('D:', 'Diag', 'HasDiag_train.csv')
x_test_file = os.path.join('D:', 'Diag', '6x6_test.csv')
y_test_file  = os.path.join('D:', 'Diag', 'HasDiag_test.csv')
model_chkpt = os.path.join('D:', 'Diag', "checkpoints")

def get_inputs(
        count=None, shuffle=True, buffer_size=1000, batch_size=32,
        num_parallel_calls=8, x_paths=[x_train_file], y_paths=[y_train_file]):
    """
    Get x, y inputs.

    Args:
        count: number of epochs. None indicates infinite epochs.
        shuffle: whether or not to shuffle the dataset
        buffer_size: used in shuffle
        batch_size: size of batch. See outputs below
        num_parallel_calls: used in map. Note if > 1, intra-batch ordering
            will be shuffled
        x_paths: list of paths to x-value files.
        y_paths: list of paths to y-value files.

    Returns:
        x: (batch_size, 6, 6) tensor
        y: (batch_size, 2) tensor of 1-hot labels
    """

    def x_map(line):
        n_dims = 6
        columns = [str(i1) for i1 in range(n_dims**2)]
        # Decode the line into its fields
        fields = tf.decode_csv(line, record_defaults=[[0]] * (n_dims ** 2))

        # Pack the result into a dictionary
        features = dict(zip(columns, fields))
        return features

    def y_map(line):
        y_row = tf.string_to_number(line, out_type=tf.int32)
        return y_row

    def xy_map(x, y):
        return x_map(x), y_map(y)

    x_ds = tf.data.TextLineDataset(x_train_file)
    y_ds = tf.data.TextLineDataset(y_train_file)

    combined = tf.data.Dataset.zip((x_ds, y_ds))
    combined = combined.repeat(count=count)
    if shuffle:
        combined = combined.shuffle(buffer_size)
    combined = combined.map(xy_map, num_parallel_calls=num_parallel_calls)
    combined = combined.batch(batch_size)
    x, y = combined.make_one_shot_iterator().get_next()
    return x, y

columns = [str(i1) for i1 in range(6 ** 2)]

feature_columns = [
    tf.feature_column.numeric_column(name)
    for name in columns]

estimator = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                   hidden_units=[18, 9],
                                   activation_fn=tf.nn.relu,
                                   n_classes=2,
                                   model_dir=model_chkpt)

estimator.train(
    input_fn=lambda: get_inputs(x_paths=[x_train_file], y_paths=[y_train_file], batch_size=32), steps=100)

eval_result = estimator.evaluate(
    input_fn=lambda: get_inputs(x_paths=[x_test_file], y_paths=[y_test_file], batch_size=32))

print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

1 个答案:

答案 0 :(得分:3)

导致此问题的原因有两个:

  • tf.data.Dataset.repeat有一个count参数:

      

    count :(可选。)tf.int64标量tf.Tensor,代表   数据集应重复的次数。默认行为   (如果countNone-1),则无限期重复数据集。

    在您的情况下,count始终为None,因此数据集会无限期重复。<​​/ p>

  • tf.estimator.Estimator.evaluatesteps参数:

      

    steps:评估模型的步骤数。如果None,则评估直到input_fn引发输入结束异常。

    为训练设置了步骤,但没有为评估设置步骤,因此估算器一直运行,直到input_fn引发输入结束异常,如上所述,这种异常永远不会发生。

你应该设置其中任何一个,我认为count=1是最合理的评估。