【发布时间】:2021-01-06 14:11:39
【问题描述】:
这是我在 tensorflow 中的第一步。
想法
有一些数字模式(数字数组:Pattern = number[])。以及与此模式对应的类别(从 0 到 2 的数字:Category = 0 | 1 | 2)。我已经按照结构数据:xs = Pattern[],ys = Category[]。
例如:
xs = [[1, 2, 3, 4], [5, 6, 7, 8], ..., [9, 10, 11, 12]];
ys = [1, 0, ..., 2];
我希望神经网络在 xs[0] 和 xy[0] 之间找到匹配项,依此类推。我想传递像[1, 2, 3, 4] 这样的神经网络数据并得到接近1 的结果。
model.predict(tf.tensor([1, 2, 3, 4])) // ≈1
我的代码
import * as tf from '@tensorflow/tfjs';
require('@tensorflow/tfjs-node');
const xs = tf.tensor2d([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]);
const ys = tf.tensor1d([0, 1, 2]);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 4, inputShape: xs.shape, activation: 'relu' }));
^ - Pattern length, it is constant
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy', metrics: ['accuracy'] });
model.fit(xs, ys, { epochs: 500 });
我收到跟随错误:
检查输入时出错:预期 dense_Dense1_input 具有 3 个维度。但得到了形状为 3,4 的数组
我不明白如何解释我的神经网络数据结构。
【问题讨论】:
标签: tensorflow tensorflow2.0 tensorflow.js