首页 > 解决方案 > 如何使用 P5 createVector 创建的向量作为 tensorflow.js 中的张量

问题描述

我正在使用 p5 返回绘制线的矢量路径。该行中的所有向量都被推入一个包含所有向量的数组中。我正在尝试将其用作张量,但我不断收到错误消息

检查模型输入时出错:您传递给模型的张量数组不是模型预期的大小。预计会看到 1 个张量,但得到了以下张量列表:当我在开发工具上打开数组时,每个向量都打印如下:

0:向量{p5:p5,x:0.5150300601202404,y:-0.25450901803607207,z:0}

可能是向量数组中的 p5 文本给了我错误吗?这是我的模型和拟合代码:

let vectorpath = []; //vector path array

// model, setting layers till next '-----'

const model = tf.sequential();
model.add(tf.layers.dense({units: 4, inputShape: [2, 2], activation: 'sigmoid'}));
model.add(tf.layers.dense({units: 2, activation: 'sigmoid'}));

console.log(JSON.stringify(model.outputs[0].shape));
model.weights.forEach(w => {
    console.log(w.name, w.shape);
   });

// -----

//this is under the draw function so it is continually updated
const labels = tf.randomUniform([0, 1]);
    function onBatchEnd(batch, logs) {
        console.log('Accuracy', logs.acc);
      }

    model.fit(vectorpath, labels, {
        epochs: 5,
        batchSize: 32,
        callbacks: {onBatchEnd}
    }).then(info => {
        console.log('Final accuracy', info.history.acc);
    });

什么可能导致错误?我该如何解决?这个问题很模糊,但我真的不确定。

标签: tensorflowp5.js

解决方案


推荐阅读