model.predict 上的 Tensorflow.js print() 和 dataSync 方法不起作用

Hap*_*ine 3 tensorflow.js

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

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

export const top_words = 10000; 
export const max_review_length = 1
export const embedding_vector_length = 32
Run Code Online (Sandbox Code Playgroud)

这是代码,我现在用模拟标记或一个字长替换了张量。我收到打字稿 linting 错误,显示 .print() 或 .dataSync()[0] 将因它们不存在而失败。有问题的代码行(.predict)返回一个没有 print 或 datasync 方法的张量

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]))
Run Code Online (Sandbox Code Playgroud)

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

小智 7

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

(model.predict(...) as tf.Tensor).print()
Run Code Online (Sandbox Code Playgroud)

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