构建一个基本的感知器。我在训练后的结果非常不一致,即使经过数千个周期。权重似乎调整得当,但模型无法准确预测。非常希望有第二双眼睛能审视一下结构,我在寻找自己犯错的地方时遇到了困难。准确率始终停留在60%左右。
// Perceptronclass Perceptron { constructor (x_train, y_train, learn_rate= 0.1, epochs=10) { this.epochs = epochs this.x_train = x_train this.y_train = y_train this.learn_rate = learn_rate this.weights = new Array(x_train[0].length) // 初始化随机权重 for ( let n = 0; n < x_train[0].length; n++ ) { this.weights[n] = this.random() } } // 生成-1到1之间的随机浮点数(用于生成权重) random () { return Math.random() * 2 - 1 } // 激活函数 activation (n) { return n < 0 ? 0 : 1 } // 给定输入张量的y-hat输出 predict (input) { let total = 0 this.weights.forEach((w, index) => { total += input[index] * w }) // 将每个权重乘以每个输入向量值 return this.activation(total) } // 在数据上训练感知器 fit () { for ( let e = 0; e < this.epochs; e++) { // 周期循环 for ( let i = 0; i < this.x_train.length; i++ ) { // 遍历每个训练样本 let prediction = this.predict(this.x_train[i]) // 预测样本输出 console.log('Expected: ' + this.y_train[i] + ' Model Output: ' + prediction) // 记录预期与预测 let loss = this.y_train[i] - prediction // 计算损失 for ( let w = 0; w < this.weights.length; w++ ) { // 循环更新权重 this.weights[w] += loss * this.x_train[i][w] * this.learn_rate // 更新所有权重以减少损失 } } } }}x = [[1, 1, 1], [0, 0, 0], [0, 0, 1], [1, 1, 0], [0, 0, 1]]y = [1, 0, 0, 1, 0]p = new Perceptron(x, y, epochs=5000, learn_rate=.1)
更新:
// Perceptronmodule.exports = class Perceptron {constructor (x_train, y_train, epochs=1000, learn_rate= 0.1) { // 用于生成百分比准确率 this.accuracy = 0 this.samples = 0 this.x_train = x_train this.y_train = y_train this.epochs = epochs this.learn_rate = learn_rate this.weights = new Array(x_train[0].length) this.bias = 0 // 初始化随机权重 for ( let n = 0; n < x_train[0].length; n++ ) { this.weights[n] = this.random() }}// 返回当前准确率百分比 current_accuracy () { return this.accuracy/this.samples}// 生成-1到1之间的随机浮点数(用于生成权重)random () { return Math.random() * 2 - 1}// 激活函数activation (n) { return n < 0 ? 0 : 1}// 给定输入张量的y-hat输出 predict (input) { let total = this.bias this.weights.forEach((w, index) => { total += input[index] * w }) // 将每个权重乘以每个输入向量值 return this.activation(total)}// 在数据上训练感知器fit () { // 周期循环 for ( let e = 0; e < this.epochs; e++) { // 对于每个训练样本 for ( let i = 0; i < this.x_train.length; i++ ) { // 获取预测 let prediction = this.predict(this.x_train[i]) console.log('Expected: ' + this.y_train[i] + ' Model Output: ' + prediction) // 更新准确率测量 this.y_train[i] === prediction ? this.accuracy += 1 : this.accuracy -= 1 this.samples++ // 计算损失 let loss = this.y_train[i] - prediction // 更新所有权重 for ( let w = 0; w < this.weights.length; w++ ) { this.weights[w] += loss * this.x_train[i][w] * this.learn_rate } this.bias += loss * this.learn_rate } // 周期后的准确率 console.log(this.current_accuracy()) } }}
回答:
这只是一个语法错误 🙂
将最后两个参数的顺序调换,如下所示:
p = new Perceptron(x, y, learn_rate=.1, epochs=5000)
现在一切应该都能正常工作了。
然而,你的实现中存在一个更严重的问题:
你忘记了偏置
在使用感知器时,你试图学习一个线性函数,形式如
y = wx + b
但你当前计算的只是
y = wx
如果你试图学习的只是单个输入的恒等函数,就像你的情况那样,这是可以的。但是一旦你开始尝试做一些稍微复杂的事情,比如学习AND函数,它可以表示为:
y = x1 + x2 – 1.5
这就会失效了。
如何修复?
非常简单,只需在构造函数中初始化 this.bias = 0
。然后,在 predict()
中,初始化 let total = this.bias
,在 fit()
中,在最内层循环之后添加 this.bias += loss * this.learn_rate
即可。