Skip to content

Instantly share code, notes, and snippets.

@orhanerday
Created December 4, 2022 07:49
Show Gist options
  • Save orhanerday/492438897fbaf1874751337e2afb3137 to your computer and use it in GitHub Desktop.
Save orhanerday/492438897fbaf1874751337e2afb3137 to your computer and use it in GitHub Desktop.
very primitive neural network written in go
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
// Neuron represents a single neuron in a neural network.
type Neuron struct {
weights []float64
bias float64
}
// Activation function for a neuron.
func (n *Neuron) activate(inputs []float64) float64 {
// Calculate the weighted sum of inputs and biases.
var sum float64
for i, input := range inputs {
sum += input * n.weights[i]
}
sum += n.bias
// Apply the activation function (sigmoid).
return 1.0 / (1.0 + math.Exp(-sum))
}
// NeuralNetwork represents a simple neural network.
type NeuralNetwork struct {
neurons []*Neuron
}
// NewNeuralNetwork creates a new neural network with the given number of inputs and outputs.
func NewNeuralNetwork(inputs, outputs int) *NeuralNetwork {
n := &NeuralNetwork{}
// Create the neurons for the output layer.
for i := 0; i < outputs; i++ {
n.neurons = append(n.neurons, &Neuron{
weights: randomWeights(inputs),
bias: rand.Float64(),
})
}
return n
}
// randomWeights returns a new slice of random weights.
func randomWeights(n int) []float64 {
//weights := make([]float64, n)
//for i := 0; i < n; i++ {
// weights[i] = rand.Float64()
// }
weights := []float64{0.03, 0.95}
return weights
}
// Predict takes a set of inputs and returns the predicted outputs from the neural network.
func (n *NeuralNetwork) Predict(inputs []float64) []float64 {
// Make sure the input size matches the number of inputs in the neurons.
if len(inputs) != len(n.neurons[0].weights) {
panic("input size does not match number of inputs in the neurons")
}
// Predict the output for each neuron.
outputs := make([]float64, len(n.neurons))
for i, neuron := range n.neurons {
outputs[i] = neuron.activate(inputs)
}
return outputs
}
func main() {
// Create a new neural network with 2 inputs and 1 output.
nn := NewNeuralNetwork(2, 2)
// Predict the output for a set of inputs.
// nail
println("nail")
for i := 0; i < 200; i++ {
time.Sleep(time.Millisecond * 30)
j := float64(i)
print("nail : ")
fmt.Printf("Started for %d : ", i)
outputs := nn.Predict([]float64{j, 0})
fmt.Print(math.Floor(outputs[0]*100) / 100)
fmt.Print(" candle :")
outputs = nn.Predict([]float64{0, j})
fmt.Println(math.Floor(outputs[0]*100) / 100)
}
}
@orhanerday
Copy link
Author

It is possible to create a very simple neural network in the Go programming language. Here is an example of how this could be done:

package main

import (
	"math"
	"math/rand"
)

// Neuron represents a single neuron in a neural network.
type Neuron struct {
	weights []float64
	bias    float64
}

// Activation function for a neuron.
func (n *Neuron) activate(inputs []float64) float64 {
	// Calculate the weighted sum of inputs and biases.
	var sum float64
	for i, input := range inputs {
		sum += input * n.weights[i]
	}
	sum += n.bias

	// Apply the activation function (sigmoid).
	return 1.0 / (1.0 + math.Exp(-sum))
}

// NeuralNetwork represents a simple neural network.
type NeuralNetwork struct {
	neurons []*Neuron
}

// NewNeuralNetwork creates a new neural network with the given number of inputs and outputs.
func NewNeuralNetwork(inputs, outputs int) *NeuralNetwork {
	n := &NeuralNetwork{}

	// Create the neurons for the output layer.
	for i := 0; i < outputs; i++ {
		n.neurons = append(n.neurons, &Neuron{
			weights: randomWeights(inputs),
			bias:    rand.Float64(),
		})
	}

	return n
}

// randomWeights returns a new slice of random weights.
func randomWeights(n int) []float64 {
	weights := make([]float64, n)
	for i := 0; i < n; i++ {
		weights[i] = rand.Float64()
	}
	return weights
}

// Predict takes a set of inputs and returns the predicted outputs from the neural network.
func (n *NeuralNetwork) Predict(inputs []float64) []float64 {
	// Make sure the input size matches the number of inputs in the neurons.
	if len(inputs) != len(n.neurons[0].weights) {
		panic("input size does not match number of inputs in the neurons")
	}

	// Predict the output for each neuron.
	outputs := make([]float64, len(n.neurons))
	for i, neuron := range n.neurons {
		outputs[i] = neuron.activate(inputs)
	}

	return outputs
}

func main() {
	// Create a new neural network with 2 inputs and 1 output.
	nn := NewNeuralNetwork(2, 1)

	// Predict the output for a set of inputs.
	outputs := nn.Predict([]float64{1.0, 0.5})
	fmt.Println(outputs)
}

This code creates a very simple neural network with a single layer of output neurons. The neurons use the sigmoid activation function, and the weights and biases are initialized to random values. The Predict method takes a set of inputs and returns the predicted outputs from the neural network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment