Aideen
Aideen

Reputation: 61

Tensorflow.js Please make sure the operations that use variables are inside the function f passed to minimize()

I have started to use javascript and TensorFlow.js to work on some machine learning projects, I am working on creating a linear regression model, however, I cannot figure out what is causing this error

Can not find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."

I have created two Tensors

globalTensorXs = tf.tensor2d(globaArrayXs); //input data
globalTensorYs =  tf.tensor1d(globaArrayYs); //y output

I have created the coefficients/weights as below as an array of tf scalars.

function createWeights(_numWeights)
{       
  for ( var x = 0; x < _numWeights; x++)
  {
  globalWeightsTensorArr.push(tf.variable(tf.scalar(Math.random())));  
 }      
} 

There is A training function which I pass the x and y Tensors into, it is the call to the optimise.minimize that causes the issue. it does not detect the variable for training, which are stored in globalWeightsTensorArr

async function train(xsTensor, ysTensor, numIterations)
{

    /*//////OPTIMISER.MINIMISE/////////////
    Minimize takes a function that does two things:
    It predicts y values for all the x values using the predict 
    model function. 
    It returns the mean squared error loss for those predictions 
    using the loss function. Minimize then automatically adjusts any 
    Variables used by thi predict/loss function in order to minimize 
    the return value (our loss), in this case the variables are in 
    "globalWeightsTensorArr" which contains the coefficient values 
     to be altered by the modeld during "numIterations" iterations of 
     SGD.
*/

  for (let iter = 0; iter < numIterations; iter++) 
  {     
      optimiser.minimize(function ()
      {
        return loss(predict(xsTensor), ysTensor);
       }, globalWeightsTensorArr);          
  }

}

// the predict and loss function are here...

//The following code constructs a predict function that takes inputs(X's) //and returns prediction Y: it represents our 'model'. Given an input //'xs' it will try and * predict the appropriate output 'y'.

function predict(_Xs) 
{           
    return tf.tidy(() => {  
    for ( var x = 0; x < 8; x++)
        globalWeightsArr[x] = globalWeightsTensorArr[x].dataSync();
        const weightTensor =  tf.tensor1d(globalWeightsArr); 
        const prediction = tf.dot(_Xs, weightTensor);
        return prediction;
 });        
}

//The loss function takes the predictions from the predict function //and the actual lables and adjusts the weights //the weights are considered to be any tensor variable that impact the //function We can define a MSE loss function in TensorFlow.js as follows:

function loss(_predictedTensor, _labels) 
{
const meanSquareError =_predictedTensor.sub(_labels).square().mean();
return meanSquareError ;
}

can anyone please help explain the problem?

Regards Aideen

Upvotes: 2

Views: 489

Answers (2)

edkeveked
edkeveked

Reputation: 18401

The issue is related to tf.variable. One needs to use tf.variable to create the weights that will be updated by the function created by optimiser.minimize().

A variable created by tf.variable is mutable contrary to tf.tensor that is immutable. As a result if one uses tf.tensor to create the weights they could not be updated during the training

Upvotes: 1

Aideen
Aideen

Reputation: 61

We resolved the issue by changing the way the weights/coefficients were created. Now minimize can detect the variables used by predict, and it adjusts them accordingly. later I will post the entire solution to codepen. still learning!

function createWeights(_numWeights) {
    const randomTensor = tf.randomUniform([_numWeights, 1]);
    globalWeightsTensorVar = tf.variable(randomTensor);
}

here is the predict function used b

function predictLogical(_Xs) {

    return tf.dot(_Xs, globalWeightsTensorVar);
}

Upvotes: 1

Related Questions