在矩阵乘法中出现错误:内层形状(1)和(2)的张量,其形状分别为684,1和2,1,且transposeA=false和transposeB=false时必须匹配

我是人工智能和TensorFlow.js的初学者,目前正在跟随Stephen Grider的机器学习课程。运行以下代码后,本应得到输出结果,但却遇到了错误。请帮助我:

代码:linear-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) //生成马力的一列        this.options = Object.assign(            { learningRate: 0.1, iterations: 1000 },             options        ); //默认学习率为0.1,如果提供了学习率,则使用提供的值...迭代次数为梯度下降运行的次数        this.weights = tf.zeros([2, 1]); //初始权重m和b的张量为零    }    gradientDescent() {        const currentGuesses = this.features.matMul(this.weights); //matMul是矩阵乘法,即特征乘以权重        const differences = currentGuesses.sub(this.labels); //(特征 * 权重) - 标签        const slopes = this.features            .transpose()            .matMul(differences)            .div(features.shape[0]); //相对于m和b的MSE斜率。特征 * ((特征 * 权重) - 标签) / 特征总数                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(    '更新后的M值为:',     regression.weights.get(1, 0),     '更新后的B值为:',     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: 在矩阵乘法中出现错误:内层形状(1)和(2)的张量,其形状分别为684,1和2,1,且transposeA=false和transposeB=false时必须匹配。    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)

回答:

错误是由以下代码抛出的:

this.features.matMul(this.weights)

this.features的形状为[684, 1],而this.weights的形状为[2, 1]。要使矩阵A(形状为[a, b])能够与矩阵B(形状为[c, d])相乘,bc必须匹配,但这里并不符合这一条件。

要解决这个问题,this.weights应该进行转置:

this.features.matMul(this.weights, false, true)

Related Posts

如何对SVC进行超参数调优?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

如何在初始训练后向模型添加训练数据?

我想在我的scikit-learn模型已经训练完成后再…

使用Google Cloud Function并行运行带有不同用户参数的相同训练作业

我正在寻找一种方法来并行运行带有不同用户参数的相同训练…

加载Keras模型,TypeError: ‘module’ object is not callable

我已经在StackOverflow上搜索并阅读了文档,…

在计算KNN填补方法中特定列中NaN值的”距离平均值”时

当我从头开始实现KNN填补方法来处理缺失数据时,我遇到…

使用巨大的S3 CSV文件或直接从预处理的关系型或NoSQL数据库获取数据的机器学习训练/测试工作

已关闭。此问题需要更多细节或更清晰的说明。目前不接受回…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注