matrix weights1, weights2, weights3; // matrici dei pesi
matrix output1, output2, result; // output dello strato neurale delle matrici
input int layer1 = 200; // la dimensione del primo livello nascosto
input int layer2 = 200; // la dimensione del secondo livello nascosto
input int Epochs = 20000; //il numero dei periodi di addestramento
input double lr = 3e-6; // tasso di apprendimento
input ENUM_ACTIVATION_FUNCTION ac_func = AF_SWISH; // funzione di attivazione
//+------------------------------------------------------------------+
//| Script start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
int train = 1000; // dimensione del campione di addestramento
int test = 10; // dimensione del campione di prova
matrix m_data, m_target;
//-- generazione di un campione di addestramento
if(!CreateData(m_data, m_target, train))
return;
//-- addestrare il modello
if(!Train(m_data, m_target, Epochs))
return;
//-- genera un campione di prova
if(!CreateData(m_data, m_target, test))
return;
//-- testa il modello
Test(m_data, m_target);
}
//+------------------------------------------------------------------+
// Metodo di generazione del campione |
//+------------------------------------------------------------------+
bool CreateData(matrix &data, matrix &target, const int count)
{
//--- inizializza i dati iniziali e le matrici risultanti
if(!data.Init(count, 3) || !target.Init(count, 1))
return false;
//-- riempire la matrice dei dati iniziali con valori casuali
data.Random(-10, 10);
//-- calcola i valori target per il campione di allenamento
vector X1 = MathPow(data.Col(0) + data.Col(1) + data.Col(1), 2);
vector X2 = MathPow(data.Col(0), 2) + MathPow(data.Col(1), 2) + MathPow(data.Col(2), 2);
if(!target.Col(X1 / X2, 0))
return false;
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
//| Metodo del modello di addestramento |
//+------------------------------------------------------------------+
bool Train(matrix &data, matrix &target, const int epochs = 10000)
{
//-- creare il modello
if(!CreateNet())
return false;
//-- addestrare il modello
for(int ep = 0; ep < epochs; ep++)
{
//--- feedforward pass
if(!FeedForward(data))
return false;
PrintFormat("Epoch %d, loss %.5f", ep, result.Loss(target, LOSS_MSE));
//--- backpropagation e aggiornamento della matrice del peso
if(!Backprop(data, target))
return false;
}
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
//| Metodo del modello della creazione |
//+------------------------------------------------------------------+
bool CreateNet()
{
//--- inizializza le matrici del peso
if(!weights1.Init(4, layer1) || !weights2.Init(layer1 + 1, layer2) || !weights3.Init(layer2 + 1, 1))
return false;
//-- riempire le matrici del peso con valori casuali
weights1.Random(-0.1, 0.1);
weights2.Random(-0.1, 0.1);
weights3.Random(-0.1, 0.1);
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
//| Metodo Feedforward |
//+------------------------------------------------------------------+
bool FeedForward(matrix &data)
{
//-- verifica la dimensione dei dati iniziali
if(data.Cols() != weights1.Rows() - 1)
return false;
//-- calcola il primo strato neurale
matrix temp = data;
if(!temp.Resize(temp.Rows(), weights1.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights1.Rows() - 1))
return false;
output1 = temp.MatMul(weights1);
//-- calcola la funzione di attivazione
if(!output1.Activation(temp, ac_func))
return false;
//-- calcola il secondo strato neurale
if(!temp.Resize(temp.Rows(), weights2.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights2.Rows() - 1))
return false;
output2 = temp.MatMul(weights2);
//-- calcola la funzione di attivazione
if(!output2.Activation(temp, ac_func))
return false;
//-- calcola il terzo strato neurale
if(!temp.Resize(temp.Rows(), weights3.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights3.Rows() - 1))
return false;
result = temp.MatMul(weights3);
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
//| Metodo Backpropagation |
//+------------------------------------------------------------------+
bool Backprop(matrix &data, matrix &target)
{
//-- controllare la dimensione della matrice dei valori target
if(target.Rows() != result.Rows() ||
target.Cols() != result.Cols())
return false;
//-- determinare la deviazione dei valori calcolati dall'obiettivo
matrix loss = (target - result) * 2;
//-- propagare il gradiente al livello precedente
matrix gradient = loss.MatMul(weights3.Transpose());
//-- aggiornare la matrice del peso dell'ultimo livello
matrix temp;
if(!output2.Activation(temp, ac_func))
return false;
if(!temp.Resize(temp.Rows(), weights3.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights3.Rows() - 1))
return false;
weights3 = weights3 + temp.Transpose().MatMul(loss) * lr;
//--- regola il gradiente di errore in base alla derivata della funzione di attivazione
if(!output2.Derivative(temp, ac_func))
return false;
if(!gradient.Resize(gradient.Rows(), gradient.Cols() - 1))
return false;
loss = gradient * temp;
//-- propagare il gradiente ad uno strato inferiore
gradient = loss.MatMul(weights2.Transpose());
//-- aggiornare la matrice di peso del secondo livello nascosto
if(!output1.Activation(temp, ac_func))
return false;
if(!temp.Resize(temp.Rows(), weights2.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights2.Rows() - 1))
return false;
weights2 = weights2 + temp.Transpose().MatMul(loss) * lr;
//--- regola il gradiente di errore in base alla derivata della funzione di attivazione
if(!output1.Derivative(temp, ac_func))
return false;
if(!gradient.Resize(gradient.Rows(), gradient.Cols() - 1))
return false;
loss = gradient * temp;
//-- aggiornare la matrice di peso del primo livello nascosto
temp = data;
if(!temp.Resize(temp.Rows(), weights1.Rows()) ||
!temp.Col(vector::Ones(temp.Rows()), weights1.Rows() - 1))
return false;
weights1 = weights1 + temp.Transpose().MatMul(loss) * lr;
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
//| Metodo di prova del modello |
//+------------------------------------------------------------------+
bool Test(matrix &data, matrix &target)
{
//-- feedforward sui dati di prova
if(!FeedForward(data))
return false;
//-- registra i risultati del calcolo del modello e i valori reali
PrintFormat("Test loss %.5f", result.Loss(target, LOSS_MSE));
ulong total = data.Rows();
for(ulong i = 0; i < total; i++)
PrintFormat("(%.2f + %.2f + %.2f)^2 / (%.2f^2 + %.2f^2 + %.2f^2) = Net %.2f, Target %.2f", data[i, 0], data[i, 1], data[i, 2],
data[i, 0], data[i, 1], data[i, 2], result[i, 0], target[i, 0]);
//-- risultato restituito
return true;
}
//+------------------------------------------------------------------+
|