<commit_before>package neural

import "github.com/gonum/matrix/mat64"

type Layer interface {
	Forward(input []float64) []float64
	UpdateWeights(weights *mat64.Dense, biases *mat64.Dense)
}

type simpleLayer struct {
	weights *mat64.Dense
	biases  *mat64.Dense
	inputs  int
	neurons int
}

func NewSimpleLayer(inputs, neurons int) Layer {
	return &simpleLayer{
		weights: mat64.NewDense(neurons, inputs, nil),
		biases:  mat64.NewDense(neurons, 1, nil),
		inputs:  inputs,
		neurons: neurons,
	}
}

func (s *simpleLayer) Forward(input []float64) []float64 {
	inputMat := mat64.NewDense(s.inputs, 1, input)
	outMat := mat64.NewDense(s.neurons, 1, nil)

	outMat.Mul(s.weights, inputMat)
	outMat.Add(outMat, s.biases)

	return outMat.RawMatrix().Data
}

func (s *simpleLayer) UpdateWeights(weights *mat64.Dense, biases *mat64.Dense) {
	s.weights.Add(s.weights, weights)
	s.biases.Add(s.biases, biases)
}<commit_msg>Let load weights into layers<commit_after>package neural

import (
	"math/rand"

	"github.com/gonum/matrix/mat64"
)

type Layer interface {
	Forward(input []float64) []float64
	Backward(delta []float64) []float64
	SetWeights(weights *mat64.Dense, biases *mat64.Dense)
	UpdateWeights(weights *mat64.Dense, biases *mat64.Dense)
}

type simpleLayer struct {
	weights *mat64.Dense
	biases  *mat64.Dense
	inputs  int
	neurons int
}

func randomMatrix(rows, cols int) *mat64.Dense {
	data := make([]float64, rows*cols)
	for i := range data {
		data[i] = rand.Float64()
	}

	return mat64.NewDense(rows, cols, data)
}

func NewSimpleLayer(inputs, neurons int) Layer {
	return &simpleLayer{
		weights: randomMatrix(neurons, inputs),
		biases:  randomMatrix(neurons, 1),
		inputs:  inputs,
		neurons: neurons,
	}
}

func (s *simpleLayer) Forward(input []float64) []float64 {
	inputMat := mat64.NewDense(s.inputs, 1, input)
	outMat := mat64.NewDense(s.neurons, 1, nil)

	outMat.Mul(s.weights, inputMat)
	outMat.Add(outMat, s.biases)

	return outMat.RawMatrix().Data
}

func (s *simpleLayer) Backward(input []float64) []float64 {
	inputMat := mat64.NewDense(len(input), 1, input)
	outMat := mat64.NewDense(s.inputs, len(input), nil)

	outMat.Mul(s.weights.T(), inputMat)

	return outMat.RawMatrix().Data
}

func (s *simpleLayer) SetWeights(weights *mat64.Dense, biases *mat64.Dense) {
	s.weights.Clone(weights)
	s.biases.Clone(biases)
}

func (s *simpleLayer) UpdateWeights(weights *mat64.Dense, biases *mat64.Dense) {
	weights.Scale(-0.1, weights)
	biases.Scale(-0.1, biases)

	s.weights.Add(s.weights, weights)
	s.biases.Add(s.biases, biases)
}
{'fraction_non_alphanumeric': 0.14139827179890024, 'fraction_numerical': 0.03731343283582089, 'mean_word_length': 4.992941176470588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5947182', 'n_tokens_mistral': 1071, 'n_tokens_neox': 985, 'n_words': 209}