【问题标题】:Tensorflow.js print() and dataSync methods on model.predict not workingmodel.predict 上的 Tensorflow.js print() 和 dataSync 方法不起作用
【发布时间】:2020-04-22 23:54:43
【问题描述】:

我已将其精简为尽可能少的代码行,以便深入了解此问题。

目前这些是下面的配置常量(我使用长度为 1 的数组来表示我正在对其进行语义分析的标记词。

export const top_words = 10000; 
export const max_review_length = 1
export const embedding_vector_length = 32

这是代码,我暂时用模拟标记或一个字长替换了张量。我收到 typescript linting 错误,显示 .print() 或 .dataSync()[0] 会因为它们不存在而失败。有问题的代码行(.predict)正在返回一个没有打印或数据同步方法的张量

const x_train = tf.tensor([[80], [86], [10], [1], [2]]);
const y_train = tf.tensor([[1],[1],[1],[0],[0]])
const x_val = tf.tensor([[1], [3], [102], [100], [104]]);
const y_val = tf.tensor([[0],[0],[1],[1],[1]])
const model = tf.sequential();


model.add(tf.layers.embedding({ inputDim: dictionary.size, inputLength: max_review_length, outputDim: 1 }))
model.add(tf.layers.lstm({units: 200, dropout: 0.2, recurrentDropout: 0.2}))
model.add(tf.layers.dense({units: 1, activation:'sigmoid'}))
model.compile({ loss:'binaryCrossentropy', optimizer:'rmsprop', metrics:['accuracy'] }) 
const history=model.fit(x_train, y_train,{epochs: 12, batchSize: 5}) 
history.then(hist => console.log(hist.history.loss)) // Show error loss vs epoch

const predictOut =  model.predict(tf.tensor2d([10]))

predictOut.print() 或 predictOut.dataSync()[0] 返回

【问题讨论】:

  • 它们只是检查错误还是代码真的抛出异常?
  • “张量”类型上不存在属性“dataSync” |张量[]'。或属性“打印”...
  • 这是运行时异常还是 linting 错误?
  • 它既是 linting 也是运行时
  • 尝试console.log(predictOut)时会记录什么?

标签: tensorflow.js


【解决方案1】:

如果您使用的是 TypeScript,您需要指定 predict() 以这种方式返回的内容:

(model.predict(...) as tf.Tensor).print()

因为 predict() 可以返回 Tensor 或 Tensor[]

【讨论】:

    【解决方案2】:

    好的,如果您不习惯使用 Python,那么很容易忘记一件事。 Python 是同步的!

    模型是异步的,所以要在这段代码中解决这个问题。 历史(结果)

    history.then(result => {
      model.predict(tftensor2d([10)).print()
      console.log('loss ', result.history.loss)
    }
    

    否则模型还没有预测方法,因为它仍在计算中。

    一定喜欢异步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      相关资源
      最近更新 更多