2

I have trained a neural network in Matlab (Using the neural network toolbox). Now I would like to export the calculated weights and biases to another platform (PHP) in order to make calculations with them. Is there a way to create a function or equation to do this?

I found this related question: Equation that compute a Neural Network in Matlab.

Is there a way to do what I want and port the results of my NN (29 inputs, 10 hidden layers, 1 output) to PHP?

Community
  • 1
  • 1
Ox3
  • 451
  • 1
  • 6
  • 22

3 Answers3

2

Yes, the net properties also referenced in the other question are simple matrices:

W1=net.IW{1,1};
W2=net.LW{2,1};
b1=net.b{1,1};
b2=net.b{2,1};

So you can write them to a file, say, as comma-separated-values.

csvwrite('W1.csv',W1)

Then, in PHP read this data and convert or use it as you like.

<?php

if (($handle = fopen("test.csv", "r")) !== FALSE) {
    $data = fgetcsv($handle, 1000, ",");
}
?>

Than, to process the weights, you can use the formula from the other question by replacing the tansig function, which is calculated according to:

n = 2/(1+exp(-2*n))-1

This is mathematically equivalent to tanh(N)

Which exists in php as well.

source: http://dali.feld.cvut.cz/ucebna/matlab/toolbox/nnet/tansig.html

Jasper van den Bosch
  • 3,169
  • 4
  • 32
  • 55
  • 1
    Thanks for your reply, but what I'm trying to figure out is how to handle the weights once they are in php arrays in order to perform calculations, basically the equivalent of the "uscita=tansig(W2*(tansig(W1*in+b1))+b2);" line in the other question. I think there is not a function like "tansig" in php. – Ox3 Dec 11 '12 at 18:21
  • Ah, Carlosdc was quicker with the tansig explanation – Jasper van den Bosch Dec 11 '12 at 22:00
  • 1
    In my case n = 2/(1+exp(-2*n))-1 was not good enough to give similar results. When I used tanh(), not the approximation (http://php.net/manual/en/function.tanh.php) the problem got solved. – fermat4214 Mar 09 '17 at 15:23
2

Transferring all of these is pretty trivial. You will need:

  1. Write the code for matrix multiplication, which are a pretty simple couple of for loops.
  2. Second, observe that according to the Matlab documentation tansig(n) = 2/(1+exp(-2*n))-1. I'm pretty sure that PHP has exp (and if not, it is has a pretty simple polynomial expansion which you can write yourself)
  3. Read, understand and apply Jasper van den Bosch's excellent answer to your question.
carlosdc
  • 12,022
  • 4
  • 45
  • 62
0

Hence the solution becomes (after correcting all wrong parts)

Here I am giving a solution in Matlab, but if you have tanh() function, you may easily convert it to any programming language. For PHP, tanh() function exists: php tanh(). It is for just showing the fields from network object and the operations you need.

  • Assume you have a trained ann (network object) that you want to export
  • Assume that the name of the trained ann is trained_ann

Here is the script for exporting and testing. Testing script compares original network result with my_ann_evaluation() result

% Export IT
exported_ann_structure = my_ann_exporter(trained_ann);

% Run and Compare 
% Works only for single INPUT vector
% Please extend it to MATRIX version by yourself
input = [12 3 5 100];
res1 = trained_ann(input')';
res2 = my_ann_evaluation(exported_ann_structure, input')';

where you need the following two functions

First my_ann_exporter:

function [ my_ann_structure ] = my_ann_exporter(trained_netw)
% Just for extracting as Structure object
my_ann_structure.input_ymax = trained_netw.inputs{1}.processSettings{1}.ymax;
my_ann_structure.input_ymin = trained_netw.inputs{1}.processSettings{1}.ymin;
my_ann_structure.input_xmax = trained_netw.inputs{1}.processSettings{1}.xmax;
my_ann_structure.input_xmin = trained_netw.inputs{1}.processSettings{1}.xmin;

my_ann_structure.IW = trained_netw.IW{1};
my_ann_structure.b1 = trained_netw.b{1};
my_ann_structure.LW = trained_netw.LW{2};
my_ann_structure.b2 = trained_netw.b{2};

my_ann_structure.output_ymax = trained_netw.outputs{2}.processSettings{1}.ymax;
my_ann_structure.output_ymin = trained_netw.outputs{2}.processSettings{1}.ymin;
my_ann_structure.output_xmax = trained_netw.outputs{2}.processSettings{1}.xmax;
my_ann_structure.output_xmin = trained_netw.outputs{2}.processSettings{1}.xmin;
end

Second my_ann_evaluation:

function [ res ] = my_ann_evaluation(my_ann_structure, input)
% Works with only single INPUT vector
% Matrix version can be implemented

ymax = my_ann_structure.input_ymax;
ymin = my_ann_structure.input_ymin;
xmax = my_ann_structure.input_xmax;
xmin = my_ann_structure.input_xmin;
input_preprocessed = (ymax-ymin) * (input-xmin) ./ (xmax-xmin) + ymin;

% Pass it through the ANN matrix multiplication
y1 = tanh(my_ann_structure.IW * input_preprocessed + my_ann_structure.b1);

y2 = my_ann_structure.LW * y1 + my_ann_structure.b2;

ymax = my_ann_structure.output_ymax;
ymin = my_ann_structure.output_ymin;
xmax = my_ann_structure.output_xmax;
xmin = my_ann_structure.output_xmin;
res = (y2-ymin) .* (xmax-xmin) /(ymax-ymin) + xmin;
end
fermat4214
  • 101
  • 2