Skip to content

Commit

Permalink
# ReLu included
Browse files Browse the repository at this point in the history
  • Loading branch information
mvbraga@gmail.com committed Mar 29, 2019
1 parent 40cccb7 commit f30bc7a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 30 deletions.
2 changes: 1 addition & 1 deletion PoC/Marvin.PoC.IA.StartUI.pas
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ procedure TFormStart.ExecutePredict(const AFileName: string);
{ faz o split dos dados para treino e teste }
TTestSplitter.New(LIrisInputData, LIrisOutputData, 0.3).ExecuteSplit(LTreinInputData, LTreinOutputData, LTestInputData, LTestOutputData);
{ cria o classificardor }
FMlp := TMLPClassifier.New(TSigmoidActivation.New, [8, 8], 0.9, 0.9, 2500);
FMlp := TMLPClassifier.New(TSigmoid.New, [8, 8], 0.9, 0.9, 2500);
ProgressBar.Max := FMlp.Epochs;
LFitCost := FMlp.Fit(LTreinInputData, LTreinOutputData).Cost;
LPredictCost := FMlp.Predict(LTestInputData, LPredictedOutputData).Cost;
Expand Down
2 changes: 1 addition & 1 deletion PoC/Marvin.PoC.Perceptron.Clss.pas
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TPocPerceptron.Execute: IPocPerceptron;

LInputTest.Add([0.46, 0.80]);

with TPerceptron.New(TSignalActivation.New, -1) do
with TPerceptron.New(TSignal.New, -1) do
begin
Fit(LInputTrein, LOutputTrein);
Predict(LInputTest, LOutputTest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,85 @@ interface
Marvin.Core.IA.Connectionist.Activation;

type
TDegreeActivation = class(TInterfacedObject, IActivation)
public
TDegree = class(TInterfacedObject, IActivation)
protected
function Execute(const AValue: Double): Double;
public
class function New: IActivation;
end;

TSignalActivation = class(TInterfacedObject, IActivation)
public
TSignal = class(TInterfacedObject, IActivation)
protected
function Execute(const AValue: Double): Double;
public
class function New: IActivation;
end;

TSigmoidActivation = class(TInterfacedObject, IActivation)
public
TSigmoid = class(TInterfacedObject, IActivation)
protected
function Execute(const AValue: Double): Double;
public
class function New: IActivation;
end;

THyperbolicTangentActivation = class(TInterfacedObject, IActivation)
public
THyperbolicTangent = class(TInterfacedObject, IActivation)
protected
function Execute(const AValue: Double): Double;
public
class function New: IActivation;
end;

TGaussianActivation = class(TInterfacedObject, IActivation)
TGaussian = class(TInterfacedObject, IActivation)
private
FCenter: Double;
FSoftness: Double;
FCenterMaximumDistance: Double;
protected
function Execute(const AValue: Double): Double;
public
constructor Create(const ACenter: Double; const ACenterMaximumDistance: Double; const ASoftness: Double);
class function New(const ACenter: Double = 0; const ACenterMaximumDistance: Double = 2; const ASoftness: Double = 1): IActivation;
end;

TReLu = class(TInterfacedObject, IActivation)
protected
function Execute(const AValue: Double): Double;
public
class function New: IActivation;
end;

TLeakyReLu = class(TInterfacedObject, IActivation)
private
FLeaky: Double;
protected
function Execute(const AValue: Double): Double;
public
constructor Create; overload;
constructor Create(const ALeaky: Double); overload;
class function New: IActivation; overload;
class function New(const ALeaky: Double): IActivation; overload;
end;

implementation

uses
System.Math;

{ TSigmoideActivation }

function TSigmoidActivation.Execute(const AValue: Double): Double;
function TSigmoid.Execute(const AValue: Double): Double;
begin
Result := 1 / (1 + Exp(-AValue));
end;

class function TSigmoidActivation.New: IActivation;
class function TSigmoid.New: IActivation;
begin
Result := TSigmoidActivation.Create;
Result := TSigmoid.Create;
end;

{ TDegreeActivation }

function TDegreeActivation.Execute(const AValue: Double): Double;
function TDegree.Execute(const AValue: Double): Double;
begin
Result := 0;
if AValue >= 0 then
Expand All @@ -90,14 +117,14 @@ function TDegreeActivation.Execute(const AValue: Double): Double;
end;
end;

class function TDegreeActivation.New: IActivation;
class function TDegree.New: IActivation;
begin
Result := TDegreeActivation.Create;
Result := TDegree.Create;
end;

{ TSignalActivation }

function TSignalActivation.Execute(const AValue: Double): Double;
function TSignal.Execute(const AValue: Double): Double;
begin
Result := -1;
if AValue >= 0 then
Expand All @@ -106,42 +133,91 @@ function TSignalActivation.Execute(const AValue: Double): Double;
end;
end;

class function TSignalActivation.New: IActivation;
class function TSignal.New: IActivation;
begin
Result := TSignalActivation.Create;
Result := TSignal.Create;
end;

{ THyperbolicTangentActivation }

function THyperbolicTangentActivation.Execute(
const AValue: Double): Double;
function THyperbolicTangent.Execute(const AValue: Double): Double;
begin
Result := (1 - Exp(-AValue)) / (1 + Exp(-AValue));
end;

class function THyperbolicTangentActivation.New: IActivation;
class function THyperbolicTangent.New: IActivation;
begin
Result := THyperbolicTangentActivation.Create;
Result := THyperbolicTangent.Create;
end;

{ TGaussianActivation }

constructor TGaussianActivation.Create(const ACenter: Double; const ACenterMaximumDistance: Double; const ASoftness: Double);
constructor TGaussian.Create(const ACenter: Double; const ACenterMaximumDistance: Double; const ASoftness: Double);
begin
inherited Create;
FCenter := ACenter;
FSoftness := ASoftness;
FCenterMaximumDistance := ACenterMaximumDistance;
end;

function TGaussianActivation.Execute(const AValue: Double): Double;
function TGaussian.Execute(const AValue: Double): Double;
begin
Result := Exp(-(Sqr(AValue - FCenter) / Sqr(FCenterMaximumDistance * FSoftness)));
Result := Exp(-(Sqr(AValue - FCenter) / Sqr(FCenterMaximumDistance * FSoftness)));
end;

class function TGaussianActivation.New(const ACenter: Double = 0; const ACenterMaximumDistance: Double = 2; const ASoftness: Double = 1): IActivation;
class function TGaussian.New(const ACenter: Double = 0; const ACenterMaximumDistance: Double = 2; const ASoftness: Double = 1): IActivation;
begin
Result := TGaussianActivation.Create(ACenter, ACenterMaximumDistance, ASoftness);
Result := TGaussian.Create(ACenter, ACenterMaximumDistance, ASoftness);
end;

{ TReLu }

function TReLu.Execute(const AValue: Double): Double;
begin
Result := 0;
if (AValue >= 0) then
begin
Result := AValue;
end;
end;

class function TReLu.New: IActivation;
begin
Result := TReLu.Create;
end;

{ TLeakyReLu }

constructor TLeakyReLu.Create;
begin
inherited;
FLeaky := 0.2;
end;

constructor TLeakyReLu.Create(const ALeaky: Double);
begin
Self.Create;
FLeaky := ALeaky;
end;

function TLeakyReLu.Execute(const AValue: Double): Double;
begin
Result := FLeaky;
if (AValue >= 0) then
begin
Result := AValue;
end;
end;

class function TLeakyReLu.New: IActivation;
begin
Result := TLeakyReLu.Create;
end;

class function TLeakyReLu.New(const ALeaky: Double): IActivation;
begin
Result := TLeakyReLu.Create(ALeaky);
end;

end.

Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ function TMultiLayerPerceptron.FeedForward: TMultiLayerPerceptron;
if not(Assigned(FActivation)) then
begin
{ executa a ativação com função sigmóide }
FActivation := TSigmoidActivation.New;
FActivation := TSigmoid.New;
end;
SetValue(Self.Activate(Value / LLayer.NeuronsCount, FActivation));
end;
Expand Down

0 comments on commit f30bc7a

Please sign in to comment.