Skip to content

Commit

Permalink
Added NeuralNetworkInterface and updated learning rate config logic i…
Browse files Browse the repository at this point in the history
…n gomind.go
  • Loading branch information
surenderthakran committed Jul 31, 2018
1 parent 3fbec64 commit cda4b10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions gomind.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ import (
"time"

"github.com/surenderthakran/gomind/activation"
"github.com/surenderthakran/gomind/layer"
"github.com/surenderthakran/gomind/network"
)

const (
defaultActivationFunction = "LINEAR"
)

type NeuralNetworkInterface interface {
CalculateOutput(input []float64) []float64
LastOutput() []float64
HiddenLayer() *layer.Layer
OutputLayer() *layer.Layer
CalculateNewOutputLayerWeights(outputs, targetOutputs []float64) error
CalculateNewHiddenLayerWeights() error
UpdateWeights()
CalculateError(targetOutput []float64) (float64, error)
}

// Model type defines the neural network's architecture and metadata.
type Model struct {
numberOfInputs int
Expand All @@ -23,7 +35,7 @@ type Model struct {
numberOfOutputs int
outputLayerActivationFunctionName string
learningRate float64
network *network.NeuralNetwork
network NeuralNetworkInterface
}

// ModelConfiguration type defines the network configuration template filled by external code while creating a new model.
Expand Down Expand Up @@ -108,11 +120,14 @@ func New(configuration *ModelConfiguration) (*Model, error) {
}
model.numberOfOutputs = configuration.NumberOfOutputs

model.learningRate = 0.5
if configuration.LearningRate <= 0 || configuration.LearningRate > 1 {
return nil, errors.New("LearningRate cannot be less than or equals to 0 or greater than 1.")
if configuration.LearningRate < 0 || configuration.LearningRate > 1 {
return nil, errors.New("LearningRate cannot be less than 0 or greater than 1.")
} else if configuration.LearningRate == 0 {
model.learningRate = 0.5
fmt.Println("Using default learning rate 0.5")
} else {
model.learningRate = configuration.LearningRate
}
model.learningRate = configuration.LearningRate

model.numberOfHiddenNeurons = configuration.NumberOfHiddenLayerNeurons
if model.numberOfHiddenNeurons == 0 {
Expand All @@ -127,7 +142,7 @@ func New(configuration *ModelConfiguration) (*Model, error) {
}

model.outputLayerActivationFunctionName = activation.ValidFunction(configuration.OutputLayerActivationFunctionName)
if model.hiddenLayerActivationFunctionName == "" {
if model.outputLayerActivationFunctionName == "" {
model.outputLayerActivationFunctionName = defaultActivationFunction
fmt.Println("Estimated Ideal Activation Function for Output Layer Neurons: ", model.outputLayerActivationFunctionName)
}
Expand Down
4 changes: 2 additions & 2 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (network *NeuralNetwork) CalculateError(targetOutput []float64) (float64, e
func New(numberOfInputs, numberOfHiddenNeurons, numberOfOutputs int, learningRate float64, hiddenLayerActivationFunctionName, outputLayerActivationFunctionName string) (*NeuralNetwork, error) {
hiddenLayerActivationService, err := activation.New(hiddenLayerActivationFunctionName)
if err != nil {
return nil, fmt.Errorf("invalid activation function: %v", err)
return nil, fmt.Errorf("unable to create hidden layer activation service for %s: %v", hiddenLayerActivationFunctionName, err)
}
hiddenLayer, err := layer.New(numberOfHiddenNeurons, numberOfInputs, hiddenLayerActivationService)
if err != nil {
Expand All @@ -221,7 +221,7 @@ func New(numberOfInputs, numberOfHiddenNeurons, numberOfOutputs int, learningRat

outputLayerActivationService, err := activation.New(outputLayerActivationFunctionName)
if err != nil {
return nil, fmt.Errorf("invalid activation function: %v", err)
return nil, fmt.Errorf("unable to create output layer activation service for %s: %v", outputLayerActivationFunctionName, err)
}
outputLayer, err := layer.New(numberOfOutputs, numberOfHiddenNeurons, outputLayerActivationService)
if err != nil {
Expand Down

0 comments on commit cda4b10

Please sign in to comment.