1

I am trying to replicate the MATLAB 'sim' function as described in this post: Export a neural network trained with MATLAB in other programming languages however I am struggling to work out a way of implementing the MATLAB tansig function in C#. It is defined as: a = (2 ./ (1 + exp(-2*n)) - 1) and the way I read it is that I need to perform an exponential on a matrix. Research on the net indicates this is a significant maths problem, particularly when the matrix is not symmetric. Any assistance appreciated.

Community
  • 1
  • 1
hamish
  • 447
  • 1
  • 9
  • 19
  • @jorgenkg you were correct - when I apply the activation function individually to each value in the output vector I was able to calculate the same outcome as MATLAB does with its "sim" function (which uses the "tansig" function). – hamish Aug 29 '13 at 04:14

2 Answers2

3

Though somewhat unclear, I am guessing you are refering to applying the squashing function to the output signals of a neuron.

You have to apply the function to each of the elements in the output vector of the neuron.

Thereby you are effectively applying the function to floats and not the matrix itself.

output = [ 1, 0, 1, 0, 1 ] # output vector from a neuron

def sqash( n ):
   return (2 ./ (1 + exp(-2*n)) - 1)

squashed_output = [ sqash(1), sqash(0), sqash(1), sqash(0), sqash(1) ]

Matlab probably support the same syntax sqash( output ) as NumPy in Python. Thus applying the function to each element individually when calling the function with a vector argument.

output = [ 1, 0, 1, 0, 1 ] # output vector from a neuron

def sqash( vector ):
   return (2 ./ (1 + exp(-2 * vector)) - 1)

squashed_output = sqash( output )
jorgenkg
  • 4,140
  • 1
  • 34
  • 48
1

tansig in MATLAB is just an approximation of tanh() function, so you can use the standard tanh() function on each element, while working in MATLAB or any other programming language.