Skip to content

Instantly share code, notes, and snippets.

@odds-get-evened
Last active January 21, 2022 02:07
Show Gist options
  • Save odds-get-evened/11e23e465d495d597dfbb83fb74946ec to your computer and use it in GitHub Desktop.
Save odds-get-evened/11e23e465d495d597dfbb83fb74946ec to your computer and use it in GitHub Desktop.
a simple neuron model
package org.white5moke;
import org.apache.commons.lang3.math.NumberUtils;
import org.hipparchus.analysis.function.Sigmoid;
import org.hipparchus.analysis.function.Tanh;
import org.openscience.cdk.math.RandomNumbersTool;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.ZoneId;
import java.util.*;
class Neuron {
public List<NeuralInput> inputs = new ArrayList<NeuralInput>();
public Neuron() {}
}
class Listener5 {
public Listener5() {}
}
class Sender5 {
public Sender5() {}
}
class Activation {
public enum activationType {
TANH, SIGMOID
};
private String type;
private double weight = 0.0;
public Activation() {
setType(getRandomType());
setWeight(new Random().doubles(-1.0, 1.0).findFirst().getAsDouble());
trigger();
}
public void trigger() {
switch (activationType.valueOf(getType())) {
case TANH -> {
Tanh t = new Tanh();
setWeight((float) t.value(getWeight()));
}
case SIGMOID -> {
Sigmoid s = new Sigmoid(-1, 1);
setWeight((float) s.value(getWeight()));
}
default -> {}
}
}
private String getRandomType() {
List<activationType> v = List.of(activationType.values());
int size = v.size();
Random r = new Random();
return v.get(r.nextInt(size)).toString();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
class NeuralInput {
public String id;
public double weight;
public List<Listener5> listener;
public Sender5 sender;
public NeuralInput() throws InterruptedException {
while(true) {
// use a random string token for identifier
setId(generateToken());
Activation activation = new Activation();
setWeight(activation.getWeight());
int t = (int) Math.floor(getWeight() * 10_000);
System.out.println(String.format("%16s|%28s|%8s|%32s",
getId(),
getWeight(),
activation.getType(),
Instant.now().atZone(ZoneId.of("UTC"))
));
Thread.sleep(Math.abs(t));
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public static String generateToken() {
SecureRandom r = new SecureRandom();
byte[] b = new byte[20];
r.nextBytes(b);
Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding();
return encoder.encodeToString(b);
}
}
public class App {
public App(int p) throws InterruptedException {
new NeuralInput();
}
public static void main(String[] args) throws InterruptedException {
new App(Integer.parseInt(args[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment