Skip to content

Instantly share code, notes, and snippets.

@ahmetaa
Created November 9, 2015 22:02
Show Gist options
  • Save ahmetaa/614ca9ab7eb5e0a9436b to your computer and use it in GitHub Desktop.
Save ahmetaa/614ca9ab7eb5e0a9436b to your computer and use it in GitHub Desktop.
package suskun.nn;
public class SigmoidLookup {
public static final float SCALE_FACTOR = 50.0f;
public static final int LOOKUP_SIZE = 1024;
int[] lookup;
static final float SIGMOID_QUANTIZATION_MULTIPLIER = 255.0f;
static final int SIGMOID_QUANTIZATION_MULTIPLIER_UCHAR = 255;
static final int SIGMOID_HALF_LOOKUP_SIZE = LOOKUP_SIZE/2;
public SigmoidLookup() {
int size = LOOKUP_SIZE; // table lookup size 1088=64*20 is arbitrary but can be
// divided to 64
lookup = new int[LOOKUP_SIZE];
// when a sigmoid is quantized with 255, numbers below around -5.4 becomes 0,
// numbers over around +5.4 becomes 254
for (int i = -SIGMOID_HALF_LOOKUP_SIZE; i < SIGMOID_HALF_LOOKUP_SIZE; ++i) {
float k = i / SCALE_FACTOR;
float sigmoid = (float) (1.0f / (1 + Math.exp(-k)));
int q = Math.round(sigmoid * SIGMOID_QUANTIZATION_MULTIPLIER);
lookup[i + size / 2] = q;
}
}
int get(float input) {
int k = Math.round(input * SCALE_FACTOR);
if (k <= -SIGMOID_HALF_LOOKUP_SIZE) return 0;
if (k >= SIGMOID_HALF_LOOKUP_SIZE)
return SIGMOID_QUANTIZATION_MULTIPLIER_UCHAR;
return lookup[k + SIGMOID_HALF_LOOKUP_SIZE];
}
static int sigmoid(float input) {
return (int) Math.round(1f/(1+Math.exp(-input))*255);
}
public static void main(String[] args) {
SigmoidLookup sl = new SigmoidLookup();
for(int i =-10; i<10;i++) {
for(int j =0;j<100;j++) {
float k = i + j/ 100f;
System.out.println(k+" - "+ sl.get(k) + " - " + sigmoid(k));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment