How to fix 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

I am complete beginner to AI as well as tensorflow.js. Currently following the Machine Learning course of Stephen Grider. I should have got a output after the following code but instead i got error. Please help:

code:
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) //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)
    );

Error:

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)

41 thoughts on “How to fix 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”

Leave a Comment