我是一个完全初学者的人工智能以及tensorflow.js。目前正在学习Stephen的机器学习课程。我应该在下面的代码之后得到一个输出,但是却得到了错误。请帮助:
代码:line-regression.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)发布于 2021-01-29 21:04:18
此错误由
this.features.matMul(this.weights)
形状this.features的[684, 1]与形状[2, 1]的this.weights之间有一种基本的乘法。为了能够将矩阵A (shape [a, b])与B (shape [c, d])相乘,b和c应该匹配--这里的情况并非如此。
为了在这里解决这个问题,应该将this.weights转置
this.features.matMul(this.weights, false, true)https://stackoverflow.com/questions/65961477
复制相似问题