I am trying to code functions that will put a variable or number through the sigmoid function and another one for the step function.
Yes
Can you please show your entire code and tell us what the problem is?
Usually we don’t post solutions
I don’t know how to code it, so I am just looking for a general idea to start.
My first sketch here Mathematic function
Sigmoid is 1/(1 + e ^ x)
You need to use return lest you are saving the values directly. E should be exp() and power should be pow( input, power).
One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.
This is a very short list:
Resources < Click here to expand !
I encourage you to review the resources available here:
- The Processing website has references, examples, tutorials, etc.
https://processing.org/ - The Processing PDE (IDE) has lots to offer!
An exploration of the tabs will reveal what is available; libraries, tools and local examples there. - The Coding Train
https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw
https://thecodingtrain.com/ - Happy Coding
Processing Tutorials - Happy Coding - The source code can give insight if you are adventurous:
GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)
Many of the Processing functions are here:
processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub - The Processing Foundation
https://processingfoundation.org/
Check out the tabs.
In the education tab, there is a reference to the Coding Train. - And the Internet.
:)
:)
Thanks for your replies, I did search the internet, but I never really found a way to put variables in it, I probably didn’t look hard enough. @paulgoux should I do this?:
float e = exp(1.0)
1/(1+pow(e, x);
I don’t know what x should be, is it the input?
I want the sigmoid to go from 0 to 1, because it would find the probability, so I thought that the 1 in (1+e^x) would be 0, but I don’t know if I am right.
X is the variable you are testing the sigmoid should return a value based on that calculation
Ok so x is the input. One more request @paulgoux, I would like the same function just the other way around, in your equation negative numbers get closer to 1, and positive numbers get closer to 0. Is there a way to make negative numbers closer to 0, and positive numbers closer to 1.
Just do 1 - your sigmoid function
Never mind, I got it, Thanks for your help everyone .
Hello,
can you please post your solution/ entire sketch?
Hi, sorry for the late reply, here it is.
1/(1+pow(exp(1), x);//Logistic Sig. X is the input.
((pow(exp(1), x))-(pow(exp(1),(x*-1))))-((pow(exp(1), x))+(pow(exp(1),(x*-1))))// Tanh X is the input.
There is probably a better way for the Tanh.
glad you figured it out.
might want to take a look at this example to compare your implementation. In this example sigmoid is precalculated at setup and stored in an array to be read later, saving compute time later. You don’t need to read all of it, theres a link to his github for downloads, you’ll want to download part4
Great! I also heard that pow is really slow
You can write a function where the multiplying is done in a for loop x times without pow
You can write a function for integer exponents and where execution time is important to speed things up.
Discussed and tested here:
In the example provided he did not require pow():
float x;
float y;
void setup()
{
size(500, 500);
strokeWeight(2);
translate(width/2, height/2 - 10);
//Using integer steps in formula used
for (int i = -10; i<10; i+= 1)
{
y = 200/(1+pow(exp(1), i));
point(10*i, 1-y);
}
translate(0, height/2);
// Simplified and using float steps of 0.5
for (float i = -10; i<10; i+= .5)
{
y = 200/(1+exp(i));
point(10*i, 1-y);
}
}
References:
Processing source code for exp() and pow()
:)
Thanks, this looks like a really interesting article, just what I was looking for!