- Deep learning network: https://www.mathworks.com/help/deeplearning/ref/dlnetwork.html
- Feedforward neutral network: https://www.mathworks.com/help/thingspeak/create-and-train-a-feedforward-neural-network.html
How to import weights in a neural network used in regression
2 views (last 30 days)
Show older comments
I have weights and biases and I just want to import them to a neural network in matlab , so how can I do that ?
0 Comments
Answers (1)
Purvaja
on 29 Aug 2025
I understand that you have pre-trained weights and biases that you'd like to import into a neural network — here I'll be discussing both the Neural Network Toolbox (feedforwardnet) and Deep Learning Toolbox (dlnetwork) approaches.
1. Neural Network Toolbox (feedforwardnet)
Here’s how you can define a network, inject your weights and biases, and verify that the forward pass matches your manual calculation:
% Define layer sizes (nInput, nHidden, nOutput)
% Example weights & biases with dummy data, ensure the sizes are similar to
% layer dimensions.
W1 = randn(nHidden, nInput);
b1 = randn(nHidden, 1);
W2 = randn(nOutput, nHidden);
b2 = randn(nOutput, 1);
% test input x
% Create custom network
net = network;
net.numInputs = 1;
net.numLayers = 2;
net.biasConnect = [1;1];
net.inputConnect(1,1) = 1;
net.layerConnect(2,1) = 1;
net.outputConnect(2) = 1;
% Layer sizes
net.inputs{1}.size = nInput;
net.layers{1}.size = nHidden;
net.layers{2}.size = nOutput;
% Assign weights/biases
net.IW{1,1} = W1;
net.b{1} = b1;
net.LW{2,1} = W2;
net.b{2} = b2;
% Test forward pass
y = net(x);
2. Deep Learning Toolbox (dlnetwork)
Here’s how to use dlnetwork to load your custom weights and biases:
% Define layer sizes (nInput, nHidden, nOutput)
% Pre-trained weights & biases (filled with dummy data for this example)
W1 = randn(nHidden, nInput);
b1 = randn(nHidden, 1);
W2 = randn(nOutput, nHidden);
b2 = randn(nOutput, 1);
% Define network architecture
layers = [
featureInputLayer(nInput,"Name","input")
fullyConnectedLayer(nHidden,"Name","fc1")
reluLayer("Name","relu1")
fullyConnectedLayer(nOutput,"Name","fc2")
];
% Create dlnetwork
dlnet = dlnetwork(layers);
% Inject custom weights/biases
learnables = dlnet.Learnables;
% Wrap weights/biases as dlarray and assign
learnables.Value(learnables.Layer=="fc1" & learnables.Parameter=="Weights") = {dlarray(W1)};
learnables.Value(learnables.Layer=="fc1" & learnables.Parameter=="Bias") = {dlarray(b1)};
learnables.Value(learnables.Layer=="fc2" & learnables.Parameter=="Weights") = {dlarray(W2)};
learnables.Value(learnables.Layer=="fc2" & learnables.Parameter=="Bias") = {dlarray(b2)};
% Assign back to network
dlnet.Learnables = learnables;
% Forward pass with your imported weights
X = randn(nInput,1); % dummy input column vector
dlX = dlarray(X,"CB");
dlY = predict(dlnet, dlX);
disp("Network output (dlnetwork):");
disp(extractdata(dlY));
For additional details on these APIs, refer to the MATLAB documentation:
I hope this solves your doubt, I'll recommend to cross-check it after implementing.
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!