Clear Filters
Clear Filters

Deep Learning Train Bayesian Neural Network

18 views (last 30 days)
Joy Wang
Joy Wang on 12 Mar 2024
Answered: Yash on 18 Mar 2024
when I use the MATLAB TrainBayesianNeuralNetworkUsingBayesByBackpropExample
I can work with good results as it was offered
However, when I copied this code to m.file , named demo_BNN
I have found lots of problems
in Train model section
mbq = minibatchqueue(dsTrain, ...
MiniBatchSize= miniBatchSize, ...
MiniBatchFcn= @preprocessMiniBatch, ...
MiniBatchFormat=["SSCB" "CB"]);
Error using minibatchqueue>iValidateMiniBatchFcn
Invalid value for 'MiniBatchFcn'. Unable to find function 'preprocessMiniBatch'.
Error in minibatchqueue>iParseInputs (line 786)
iValidateMiniBatchFcn(options.MiniBatchFcn, numVariables, options.NumOutputs);
Error in minibatchqueue (line 288)
options = iParseInputs(numVariables, varargin{:});
Error in demo_BNN (line 82)
mbq = minibatchqueue(dsTrain, ...
However, there are similar operations from document of minibatchqueue
SImilaraly, In monitor section
[elboLoss,rmsError,gradientsNet,gradientsNoise] = dlfeval(@modelLoss, ...
net,X,T,samplingNoise,miniBatchIdx,numIterationsPerEpoch);
Undefined function 'modelLoss' for input arguments of type 'dlnetwork'.
Error in deep.internal.dlfeval (line 17)
[varargout{1:nargout}] = fun(x{:});
Error in deep.internal.dlfevalWithNestingCheck (line 19)
[varargout{1:nargout}] = deep.internal.dlfeval(fun,varargin{:});
Error in dlfeval (line 31)
[varargout{1:nargout}] = deep.internal.dlfevalWithNestingCheck(fun,varargin{:});
Error in demo_BNN (line 123)
[elboLoss,rmsError,gradientsNet,gradientsNoise] = dlfeval(@modelLoss, ...

Answers (1)

Yash
Yash on 18 Mar 2024
Hi Joy,
I tried reproducing the errors on my end. However, "demo_BNN.mlx" is working fine for me. I created a copy of "TrainBayesianNeuralNetworkUsingBayesByBackpropExample.mlx" and renamed it to "demo_BNN.mlx".
Ensure the following to try to resolve the errors:
  • Check that all the supporting functions ("preprocessMiniBatch", "modelPosteriorSample", "logLikelihood", "modelLoss", "negativeELBO", "averageNegativeELBO" and "KLWeight") are defined at the end of "demo_BNN.mlx". These functions are defined in "TrainBayesianNeuralNetworkUsingBayesByBackpropExample.mlx".
  • Check that all the helper .M files are present in the same directory as "demo_BNN.mlx". These files are "bayesFullyConnectedLayer.m", "initializeGlorot.m" and "logProbabilityNormal.m". These files are present in the same directory as "TrainBayesianNeuralNetworkUsingBayesByBackpropExample.mlx".
Attaching "demo_BNN.mlx" and other helper functions for your reference.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!