forked from animatedread/Warrior_EA
83 lines
3.7 KiB
MQL5
83 lines
3.7 KiB
MQL5
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| NeuronPrimitives.mqh |
|
||
|
|
//| |
|
||
|
|
//| CConnection/CArrayCon bodies - the per-synapse weight (+ Adam |
|
||
|
|
//| moment) storage and its owning array, used by the CPU-only |
|
||
|
|
//| CNeuronBase/CNeuron neuron family (AI\Network.mqh) as their |
|
||
|
|
//| fallback last-resort weight representation. |
|
||
|
|
//| |
|
||
|
|
//| Included from AI\Network.mqh AFTER every class declaration - |
|
||
|
|
//| bodies only, no declarations. Relocation is behaviour-neutral by |
|
||
|
|
//| construction: nothing here is reachable until Network.mqh ends. |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#ifndef WARRIOR_AI_IMPL_NEURONPRIMITIVES_MQH
|
||
|
|
#define WARRIOR_AI_IMPL_NEURONPRIMITIVES_MQH
|
||
|
|
#include "..\..\System\Random.mqh"
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool CConnection::Save(int file_handle)
|
||
|
|
{
|
||
|
|
if(file_handle == INVALID_HANDLE)
|
||
|
|
return false;
|
||
|
|
//---
|
||
|
|
if(FileWriteDouble(file_handle, weight) <= 0)
|
||
|
|
return false;
|
||
|
|
if(FileWriteDouble(file_handle, deltaWeight) <= 0)
|
||
|
|
return false;
|
||
|
|
if(FileWriteDouble(file_handle, mt) <= 0)
|
||
|
|
return false;
|
||
|
|
if(FileWriteDouble(file_handle, vt) <= 0)
|
||
|
|
return false;
|
||
|
|
//---
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool CConnection::Load(int file_handle)
|
||
|
|
{
|
||
|
|
if(file_handle == INVALID_HANDLE)
|
||
|
|
return false;
|
||
|
|
//---
|
||
|
|
weight = FileReadDouble(file_handle);
|
||
|
|
deltaWeight = FileReadDouble(file_handle);
|
||
|
|
mt = FileReadDouble(file_handle);
|
||
|
|
vt = FileReadDouble(file_handle);
|
||
|
|
//---
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool CArrayCon::CreateElementScaled(int index, double weighScale)
|
||
|
|
{
|
||
|
|
if(index < 0 || index >= m_data_max)
|
||
|
|
return false;
|
||
|
|
//---
|
||
|
|
// Fan-in-scaled to match the CNeuronBaseOCL/CNeuronConvOCL/CNeuronLSTMOCL weight-init sites -
|
||
|
|
// callers now compute their own He/LeCun-uniform weighScale (same rationale as those) and pass it
|
||
|
|
// down through CNeuronBase::Init(), since this per-connection constructor has no visibility into
|
||
|
|
// its owning neuron's fan-in on its own. weighScale < 0 (no caller opinion) keeps the old flat
|
||
|
|
// draw as a safe default.
|
||
|
|
double weigh;
|
||
|
|
if(weighScale > 0.0)
|
||
|
|
weigh = WarriorRandSymmetric() * weighScale;
|
||
|
|
else
|
||
|
|
weigh = WarriorRandUniform() - 0.5;
|
||
|
|
if(weigh == 0)
|
||
|
|
weigh = 0.001;
|
||
|
|
m_data[index] = new CConnection(weigh);
|
||
|
|
if(CheckPointer(m_data[index]) == POINTER_INVALID)
|
||
|
|
return false;
|
||
|
|
//---
|
||
|
|
return (true);
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#endif // WARRIOR_AI_IMPL_NEURONPRIMITIVES_MQH
|
||
|
|
//+------------------------------------------------------------------+
|