我是 AI 和 tensorflow.js 的完全初学者。目前正在学习 Stephen Grider 的机器学习课程。我应该在下面的代码之后得到一个输出,但我得到了错误。请帮忙:
代码: 线性回归.js:
const tf = require('@tensorflow/tfjs');
class LinearRegression {
constructor(features, labels, options) {
this.features = tf.tensor(features);
this.labels = tf.tensor(labels);
this.features = tf.ones([this.features.shape[0], 1]).concat(this.features) //generates the column of one for the horse power
this.options = Object.assign(
{ learningRate: 0.1, iterations: 1000 },
options
); //default value is 0.1, if the learning rate is provided, the value is overrided... iteration no. of times gradient decent runs
this.weights = tf.zeros([2, 1]); // intial tensor of both m and b are zeros
}
gradientDescent() {
const currentGuesses = this.features.matMul(this.weights); //matMul is matrix multiplication which is features * weights
const differences = currentGuesses.sub(this.labels); //(features * weights) - labels
const slopes = this.features
.transpose()
.matMul(differences)
.div(features.shape[0]); // slope of MSE with respect to both m and b. features * ((features * weights) - labels) / total no. of features.
this.weights = this.weights.sub(slopes.mul(this.options.learningRate));
}
train() {
for (let i=0; i < this.options.iterations; i++) {
this.gradientDescent();
}
/*test(testFeatures, testLabels) {
testFeatures = tf.tensor(testFeatures);
testLabels = tf.tensor(testLabels);
} */
}
}
module.exports = LinearRegression;
index.js:
require('@tensorflow/tfjs-node');
const tf = require('@tensorflow/tfjs');
const loadCSV = require('./load-csv');
const LinearRegression = require('./linear-regression');
let { features, labels, testFeatures, testLabels } =loadCSV('./cars.csv', {
shuffle: true,
splitTest: 50,
dataColumns: ['horsepower'],
labelColumns: ['mpg']
});
const regression = new LinearRegression(features, labels, {
learningRate: 0.002,
iterations: 100
});
regression.train();
console.log(
'Updated M is:',
regression.weights.get(1, 0),
'Updated B is:',
regression.weights.get(0, 0)
);
错误:
D:\Application Development\MLKits-master\MLKits-master\regressions\node_modules\@tensorflow\tfjs-core\dist\ops\operation.js:32
throw ex;
^
Error: Error in matMul: inner shapes (1) and (2) of Tensors with shapes 684,1 and 2,1 and transposeA=false and transposeB=false must match.
at Object.assert (D:\Application Development\MLKits-master\MLKits-master\regressions\node_modules\@tensorflow\tfjs-core\dist\util.js:36:15)
at matMul_ (D:\Application Development\MLKits-master\MLKits-master\regressions\node_modules\@tensorflow\tfjs-core\dist\ops\matmul.js:25:10)
at Object.matMul (D:\Application Development\MLKits-master\MLKits-master\regressions\node_modules\@tensorflow\tfjs-core\dist\ops\operation.js:23:29)
at Tensor.matMul (D:\Application Development\MLKits-master\MLKits-master\regressions\node_modules\@tensorflow\tfjs-core\dist\tensor.js:315:26)
at LinearRegression.gradientDescent (D:\Application Development\MLKits-master\MLKits-master\regressions\linear-regression.js:19:46)
at LinearRegression.train (D:\Application Development\MLKits-master\MLKits-master\regressions\linear-regression.js:34:18)
at Object.<anonymous> (D:\Application Development\MLKits-master\MLKits-master\regressions\index.js:18:12)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
答案 0 :(得分:0)
错误是由
抛出的 <块引用>this.features.matMul(this.weights)
形状this.features
的[684, 1]
和形状this.weights
的[2, 1]
之间存在矩阵乘法。为了能够将矩阵 A(形状 [a, b]
)与 B(形状 [c, d]
)相乘,b
和 c
应该匹配,但此处并非如此。
要解决这里的问题,this.weights
应该转置
this.features.matMul(this.weights, false, true)