如何扩展tf.data.TextLineDataset类的功能?

时间:2019-02-05 09:57:43

标签: python python-3.x oop tensorflow tensorflow-datasets

我正在加载CSV文件,并且我想自定义tf.data.TextLineDataset以根据类本身的属性实现地图功能。例如,我要删除先前绘制的项目的最小值。我发现的唯一方法是类似

  deleteTodo = (id) => {
const { todos } = this.state;
this.setState({
  todos: delete todos[id],
});

即使我不那么优雅,此代码也可以工作,因为我在myDataSet中有一个finaldataset。但是,我无法从主代码访问tmin的值。实际上,

Warning: Failed prop type: Invalid prop `todos` of type `boolean` supplied to `TodoList`, expected an object.

引发以下错误:

class myDataSet(tf.data.TextLineDataset):
__slots__ = ['dim', 'batch_size', 'shuffle', 'tmin', 'finaldataset']

def __init__(self, batch_size=5, dim=14,
             shuffle=True, filepath="test.csv"):
    super(myDataSet, self).__init__([filepath])

    self.dim = dim
    self.batch_size = batch_size
    self.shuffle = shuffle

    self.tmin = tf.Variable(initial_value=[10.])

    self.finaldataset = self.skip(1)

    self.finaldataset = self.finaldataset.repeat()
    self.finaldataset = self.finaldataset.shuffle(10 * self.batch_size)
    self.finaldataset = self.finaldataset.prefetch(20 * self.batch_size)

    self.finaldataset = self.finaldataset.batch(self.batch_size) 
    self.finaldataset = self.finaldataset.map(self.parsefn, num_parallel_calls=8)

def parsefn(self, tf_string):
    data = tf.decode_csv(tf_string, record_defaults=[[0.]] * self.dim)

    input_tens = data[:-1]
    target_tens = data[-1]

    input_tens = tf.stack(input_tens , axis=-1)

    self.tmin = 0.8 * self.tmin + 0.2 * tf.reduce_min(target_tens, axis=0)

    target_tens = target_tens - self.tmin

    return input_tens, target_tens

def getminmax(self):
    return self.tmin

如何读取此属性?非常感谢

-编辑-

这是在调用datasetG.getminmax()之前运行的代码

sess.run(datasetG.getminmax())

0 个答案:

没有答案