首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matMul中的错误:形状为684,1和2的张量的内部形状(1)和(2),transposeA=false和transposeB=false必须匹配

matMul中的错误:形状为684,1和2的张量的内部形状(1)和(2),transposeA=false和transposeB=false必须匹配
EN

Stack Overflow用户
提问于 2021-01-29 20:47:29
回答 1查看 140关注 0票数 1

我是一个完全初学者的人工智能以及tensorflow.js。目前正在学习Stephen的机器学习课程。我应该在下面的代码之后得到一个输出,但是却得到了错误。请帮助:

代码:line-regression.js:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
    );

错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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])相乘,bc应该匹配--这里的情况并非如此。

为了在这里解决这个问题,应该将this.weights转置

代码语言:javascript
复制
this.features.matMul(this.weights, false, true)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65961477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档