Search code examples
javascripttensorflowneural-networkregularized

Cost Function Regularization in TensorFlow.js


I wondering if someone could help me. I'm new to TensorFlow.js (JavaScript version). I've built a neural network and want to add a regularization term to the cost function (loss function).

I can see the regularizers in the JavaScript API documentation, but can't figure out how to use them. The layers can have some sort of regularizer associated with them, but the cost function is not defined in the layers, so I don't think this is what I'm looking for.

I had a look through the source code on GitHub. I found some open tickets that mentioned regularization. I also found a regularization function that applied the L2 or L1 norm to a vector. I can try and write a function that augments the cost function, using the regularization function, but I don't want to go to that much effort when a function already exists. The python version of TensorFlow does contain what I'm looking for. Does anyone know if what I'm looking for already exists in the javascript version and if so, how I implement it? Thanks.


Solution

  • Assuming that TensorFlow operates the same way in Python and Javascript, it looks like you do add regularisation of the weights to the cost function, via the layers. From a mathematical point-of-view, this is not exactly obvious, hence my question.

    If you search the internet for regularisation of the loss function, in TensorFlow.js, there is nothing. However, if you read the python tutorials, they do provide an answer. I particularly found this website useful,

    https://www.analyticsvidhya.com/blog/2018/04/fundamentals-deep-learning-regularization-techniques/