我正在用 tfjs 和一些(假)医疗数据(乳腺癌)做一个 POC 脚本。数据如下所示:
[206, 293, 140, 126, 117, 27, 35, 152, 239, 79],结果 (ys) 为 [1],其中 1 为恶性,0 为良性。
该脚本似乎可以训练,但准确性/损失永远不会改变,无论数据如何,我都会得到相同的结果。我已经验证了数据/格式。下面的脚本:
const transformedData = _.shuffle(data).map(util.transformRow);
// Define the model.
const model = tf.sequential();
// Set up the network layers
model.add(tf.layers.dense({units: 10, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 150, activation: 'relu'}));
model.add(tf.layers.dense({units: 250, activation: 'relu'}));
model.add(tf.layers.dense({units: 250, activation: 'relu'}));
model.add(tf.layers.dense({units: 250, activation: 'relu'}));
model.add(tf.layers.dense({units: 1, activation: 'softmax', outputShape: [1]}));
// Define the optimizer
const optimizer = tf.train.adam(LEARNING_RATE);
// Init the model
model.compile({
optimizer: optimizer,
loss: 'meanSquaredError',
metrics: ['accuracy'],
});
const ys = transformedData.map(d => [d.ys]);
const xs = transformedData.map(d => d.xs);
let xTrain = tf.tensor2d(xs.slice(0,500), [xs.slice(0,500).length, 10]); // [[123,234,345...], [...]...]
let yTrain = tf.tensor2d(ys.slice(0,500), [ys.slice(0,500).length, 1]); // [[1], [0]...]
console.log('ready to start training model');
const history = await model.fit(xTrain, yTrain, {
epochs: EPOCHS,
validationData: [xTrain, yTrain],
})
需要明确的是,我现在不关心超准确的结果或优化,我只希望脚本实际训练模型。