Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sigmoid in python that can take scalar, vector or matrix

The following code is written in Octave Programming language

g =1./(1+e.^-(z)

It computes a sigmoid function and can take scalar, vector or Matrix. For example if I put the above into a function sigmoid(z), where z=0, the result will be:

result=sigmoid(0)

The result will be scalar ( 0.5) if the pass a vector say z= [ 0.2, 0.4, 0.1], it would output a vector for result as:-

 result=sigmoid(z)

result is a vector:

 0.54983   0.59869   0.52498

if z is a matrix like

 z=[ 0.2 0.4; 0.5 0.7; 0.9 .004]

result = sigmoid(z)

the result is =

  0.54983   0.59869
  0.62246   0.66819
  0.71095   0.50100

Now how do I implement a similar method in Python?. I tried the below code,

g=1./ (1 + math.exp(-z))

But it works only for scalar. Not for vector and Matrix. what am I doing wrong. sorry my question before was not very clear. I am re-edited it.

like image 835
sunny Avatar asked Mar 02 '17 04:03

sunny


People also ask

Can sigmoid take in a vector?

2022-06-18. The sigmoid() function returns the sigmoid value of the input(s), by default this is done using the standard logistic function. Inputs can also be tensors, such as vectors, matrices, or arrays.

Is there a sigmoid function in Python?

Plotting Sigmoid Activation using PythonThe sigmoid function is commonly used for predicting probabilities since the probability is always between 0 and 1. One of the disadvantages of the sigmoid function is that towards the end regions the Y values respond very less to the change in X values.

Does Numpy have sigmoid function?

With the help of Sigmoid activation function, we are able to reduce the loss during the time of training because it eliminates the gradient problem in machine learning model while training.


1 Answers

The numpy module, included in many Python distributions and easy to add to others, has array capabilities. Here is how to do what you want in Python with numpy. Note that defining an array in numpy is a bit different than in Octave, but the sigmoid expression is almost exactly the same.

from numpy import array, exp

z = array([ 0.2, 0.4, 0.1])
print('z = \n', z)
g = 1 / (1 + exp(-z))
print('g =\n', g)

print()

z = array([[0.2, 0.4], [0.5, 0.7], [0.9, .004]])
print('z = \n', z)
g = 1 / (1 + exp(-z))
print('g =\n', g)

The results of that code (running in IPython) are:

z = 
 [ 0.2  0.4  0.1]
g =
 [ 0.549834    0.59868766  0.52497919]

z = 
 [[ 0.2    0.4  ]
 [ 0.5    0.7  ]
 [ 0.9    0.004]]
g =
 [[ 0.549834    0.59868766]
 [ 0.62245933  0.66818777]
 [ 0.7109495   0.501     ]]
like image 193
Rory Daulton Avatar answered Oct 12 '22 11:10

Rory Daulton