Tensorflow.js 不一致的预测,returns 0 或按预期工作
Tensorflow.js inconsistent prediction, returns 0 or works as intended
我正在尝试做一个简单的 Tensorflow.js 线性模型,但得到的结果不一致。对于输入的任何输入值,它将 return 0,或者它将按预期工作(例如,如果我输入 11 作为输入,它 return 接近 110)。
当页面加载时,它要么工作要么不工作。如果我刷新页面 3 或 4 次,我就能让它工作。一旦它工作,它似乎继续工作。
我做错了什么?
import {Component, OnInit} from '@angular/core';
import * as tf from '@tensorflow/tfjs';
@Component({
selector: 'app-linear-model',
templateUrl: './linear-model.component.html',
styleUrls: ['./linear-model.component.css']
})
export class LinearModelComponent implements OnInit {
title = 'Linear Model example';
linearModel: tf.Sequential;
prediction: any;
xData: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
yData: number[] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
constructor() {
}
ngOnInit() {
this.trainNewModel();
}
async trainNewModel() {
// this is based on the following tutorial:
// https://angularfirebase.com/lessons/tensorflow-js-quick-start/#Step-2-Install-Tensorflow-js
const learningRate = 0.01;
const optimizerVar = tf.train.sgd(learningRate);
// Define a model for linear regression.
this.linearModel = tf.sequential();
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'relu'}));
// Prepare the model for training: Specify the loss and the optimizer.
this.linearModel.compile({loss: 'meanSquaredError', optimizer: optimizerVar});
// Training data defined at top
const x = tf.tensor1d(this.xData);
const y = tf.tensor1d(this.yData);
// Train
await this.linearModel.fit(x, y, {epochs: 10});
console.log('model trained!');
}
predict(val) {
val = parseFloat(val);
const output = this.linearModel.predict(tf.tensor2d([val], [1, 1])) as any;
this.prediction = Array.from(output.dataSync())[0];
console.log(output.toString());
}
}
您的问题与密集层内核的随机初始化有关。
给定权重和偏差的值,学习率可能会导致损失不减少。可以跟踪损失值,如果发生这种情况会降低学习率。
另一种解决问题的方法是为密集层设置一个初始化矩阵。
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'relu', kernelInitializer:'ones'}
现场代码here
我正在尝试做一个简单的 Tensorflow.js 线性模型,但得到的结果不一致。对于输入的任何输入值,它将 return 0,或者它将按预期工作(例如,如果我输入 11 作为输入,它 return 接近 110)。
当页面加载时,它要么工作要么不工作。如果我刷新页面 3 或 4 次,我就能让它工作。一旦它工作,它似乎继续工作。
我做错了什么?
import {Component, OnInit} from '@angular/core';
import * as tf from '@tensorflow/tfjs';
@Component({
selector: 'app-linear-model',
templateUrl: './linear-model.component.html',
styleUrls: ['./linear-model.component.css']
})
export class LinearModelComponent implements OnInit {
title = 'Linear Model example';
linearModel: tf.Sequential;
prediction: any;
xData: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
yData: number[] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
constructor() {
}
ngOnInit() {
this.trainNewModel();
}
async trainNewModel() {
// this is based on the following tutorial:
// https://angularfirebase.com/lessons/tensorflow-js-quick-start/#Step-2-Install-Tensorflow-js
const learningRate = 0.01;
const optimizerVar = tf.train.sgd(learningRate);
// Define a model for linear regression.
this.linearModel = tf.sequential();
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'relu'}));
// Prepare the model for training: Specify the loss and the optimizer.
this.linearModel.compile({loss: 'meanSquaredError', optimizer: optimizerVar});
// Training data defined at top
const x = tf.tensor1d(this.xData);
const y = tf.tensor1d(this.yData);
// Train
await this.linearModel.fit(x, y, {epochs: 10});
console.log('model trained!');
}
predict(val) {
val = parseFloat(val);
const output = this.linearModel.predict(tf.tensor2d([val], [1, 1])) as any;
this.prediction = Array.from(output.dataSync())[0];
console.log(output.toString());
}
}
您的问题与密集层内核的随机初始化有关。 给定权重和偏差的值,学习率可能会导致损失不减少。可以跟踪损失值,如果发生这种情况会降低学习率。
另一种解决问题的方法是为密集层设置一个初始化矩阵。
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'relu', kernelInitializer:'ones'}
现场代码here