why am i getting this error with activation function in my code

9 views (last 30 days)
layers = [fullyConnectedLayer(num_neurons, 'ActivationFunction', activation_function) fullyConnectedLayer(1, 'ActivationFunction', 'purelin')];
i am getting this error
Error using fullyConnectedLayer
layers = [fullyConnectedLayer(num_neurons, 'ActivationFunction', activation_function) fullyConnectedLayer(1, 'ActivationFunction', 'purelin')];
Invalid argument name 'ActivationFunction'. Name must be one of these:
Error in ann (line 14)
layers = [fullyConnectedLayer(num_neurons, 'ActivationFunction', activation_function) fullyConnectedLayer(1, 'ActivationFunction', 'purelin')];
>> ann

Answers (1)

Jack on 29 Mar 2023
The error message suggests that the argument 'ActivationFunction' is not a valid argument for the fullyConnectedLayer function.
In recent versions of MATLAB (R2020b and later), the ActivationFunction argument has been replaced with Activation. Therefore, the correct syntax to specify the activation function for a fully connected layer in these versions would be:
layers = [fullyConnectedLayer(num_neurons, 'Activation', activation_function)fullyConnectedLayer(1, 'Activation', 'linear')];
Note that the 'purelin' activation function has been replaced with 'linear'.
If you are using an older version of MATLAB, you can try removing the 'ActivationFunction' argument and use the default activation function ('relu'):
layers = [fullyConnectedLayer(num_neurons)fullyConnectedLayer(1, 'ActivationFunction', 'purelin')];
Alternatively, you can specify the activation function using the 'Name' argument, like this:
layers = [fullyConnectedLayer(num_neurons, 'Name', 'fc1', 'ActivationFunction', activation_function) fullyConnectedLayer(1, 'Name', 'output', 'ActivationFunction', 'purelin')];
Here, 'fc1' and 'output' are the layer names, and activation_function is the name of the desired activation function (e.g., 'relu', 'sigmoid', etc.).





Community Treasure Hunt

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

Start Hunting!