【问题标题】:concatenating two tf.data.Dataset gives "this.lastRead.then is not a function" error in tensorflow.js连接两个 tf.data.Dataset 在 tensorflow.js 中给出“this.lastRead.then 不是函数”错误
【发布时间】:2021-10-11 14:03:44
【问题描述】:

我正在尝试在 tensorflow.js 中的 tf.data.Dataset 中追加一个新行,经过搜索后我发现这样做的方法是将最初是 json 对象的新行转换为数据集对象将它与前一个连接起来,但我最终遇到了这个错误

 "this.lastRead.then is not a function" 

我尝试对其进行调试,因此我尝试将相同的数据集与其自身连接并遇到同样的问题:

csvUrl = 'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';
const a = tf.data.csv(
  csvUrl, {
    columnConfigs: {
      medv: {
        isLabel: true
      }
    }
  });

const b = a.concatenate(a);
await b.forEachAsync(e => console.log(e));

得到同样的错误信息,你能帮帮我吗?

【问题讨论】:

    标签: javascript concatenation tensorflow-datasets tensorflow.js


    【解决方案1】:

    显然这是 tensorflow.js 库中的一个错误。此拉取请求中的错误已修复:https://github.com/tensorflow/tfjs/pull/5444

    谢谢。

    编辑

    pull request 已经合并,现在是 3.9.0 版本

    【讨论】:

      【解决方案2】:

      目前有一个bug 可以防止连接数据集。在将其部署到新版本之前,可以使用数据集迭代器进行连接。这是一个例子:

      const csvUrl =
      'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';
      
      async function run() {
      
        const csvDataset = tf.data.csv(
          csvUrl, {
            columnConfigs: {
              medv: {
                isLabel: true
              }
            }
          });
      
        const numOfFeatures = (await csvDataset.columnNames()).length - 1;
      
        // Prepare the Dataset for training.
        const flattenedDataset =
          csvDataset
          .map(({xs, ys}) =>
            {
              // Convert xs(features) and ys(labels) from object form (keyed by
              // column name) to array form.
              return {xs:Object.values(xs), ys:Object.values(ys)};
            })
          //.batch(10);
      
      const it = await flattenedDataset.iterator()
      const it2 = await flattenedDataset.iterator()
         const xs = []
         const ys = []
         // read only the data for the first 5 rows
         // all the data need not to be read once 
         // since it will consume a lot of memory
         for (let i = 0; i < 5; i++) {
              let e = await it.next()
              let f = await it2.next()
            xs.push(e.value.xs.concat(f.value.xs))
            ys.push(e.value.ys.concat(f.value.ys))
         }
        const features = tf.tensor(xs)
        const labels = tf.tensor(ys)
      
        console.log(features.shape)
        console.log(labels.shape)
      
      }
      
      await run();
      

      唯一要记住的是,上面会加载内存中的所有张量,这取决于数据集的大小,这并不理想。数据生成器可用于减少内存占用。 here 是一个非常详细的答案来展示如何做到这一点

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 2017-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        相关资源
        最近更新 更多