You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.5 KiB
84 lines
3.5 KiB
#include "cpu.h"
|
|
#include "../layers/crnnLayer.h"
|
|
|
|
void cpuHOST::forward_layer(crnnLayer *layer, cortexSTATE state){
|
|
cortexSTATE s = { 0 };
|
|
s.train = state.train;
|
|
cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
|
|
cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
|
|
cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
|
|
uint32_t CC = layer->m_nHidden * layer->m_nBatch;
|
|
makeFill(layer->m_nOutput * layer->m_nBatch * layer->m_nSteps, 0, output_layer->m_pDelta, 1);
|
|
makeFill(CC * layer->m_nSteps, 0, self_layer->m_pDelta, 1);
|
|
makeFill(CC * layer->m_nSteps, 0, input_layer->m_pDelta, 1);
|
|
if (state.train) makeFill(CC, 0, layer->m_pState, 1);
|
|
for (uint32_t i = 0; i < layer->m_nSteps; ++i) {
|
|
s.input = state.input;
|
|
forward_layer((convolutionalLayer*)input_layer, s);
|
|
s.input = layer->m_pState;
|
|
forward_layer((convolutionalLayer*)self_layer, s);
|
|
float *old_state = layer->m_pState;
|
|
if (state.train) layer->m_pState += CC;
|
|
if (layer->m_bShortcut){
|
|
makeCopy(CC, old_state, 1, layer->m_pState, 1);
|
|
}else{
|
|
makeFill(CC, 0, layer->m_pState, 1);
|
|
};
|
|
calcAxpy(CC, 1, input_layer->m_pOutput, 1, layer->m_pState, 1);
|
|
calcAxpy(CC, 1, self_layer->m_pOutput, 1, layer->m_pState, 1);
|
|
s.input = layer->m_pState;
|
|
forward_layer((convolutionalLayer*)output_layer, s);
|
|
state.input += layer->m_nInput*layer->m_nBatch;
|
|
incrementLayer(input_layer, 1);
|
|
incrementLayer(self_layer, 1);
|
|
incrementLayer(output_layer, 1);
|
|
};
|
|
};
|
|
|
|
void cpuHOST::backward_layer(crnnLayer *layer, cortexSTATE state){
|
|
cortexSTATE s = { 0 };
|
|
s.train = state.train;
|
|
cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
|
|
cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
|
|
cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
|
|
incrementLayer(input_layer, layer->m_nSteps - 1);
|
|
incrementLayer(self_layer, layer->m_nSteps - 1);
|
|
incrementLayer(output_layer, layer->m_nSteps - 1);
|
|
uint32_t CC = layer->m_nHidden * layer->m_nBatch;
|
|
layer->m_pState += CC*layer->m_nSteps;
|
|
for (uint32_t i = layer->m_nSteps - 1; i >= 0; --i) {
|
|
makeCopy(CC, input_layer->m_pOutput, 1, layer->m_pState, 1);
|
|
calcAxpy(CC, 1, self_layer->m_pOutput, 1, layer->m_pState, 1);
|
|
s.input = layer->m_pState;
|
|
s.delta = self_layer->m_pDelta;
|
|
backward_layer((convolutionalLayer*)output_layer, s);
|
|
layer->m_pState -= CC;
|
|
/*
|
|
if(i > 0){
|
|
makeCopy(CC, input_layer.output - CC, 1, l.state, 1);
|
|
calcAxpy(CC, 1, self_layer.output - CC, 1, l.state, 1);
|
|
}else{
|
|
makeFill(CC, 0, l.state, 1);
|
|
}
|
|
*/
|
|
s.input = layer->m_pState;
|
|
s.delta = self_layer->m_pDelta - CC;
|
|
if (i == 0) s.delta = 0;
|
|
backward_layer((convolutionalLayer*)self_layer, s);
|
|
makeCopy(CC, self_layer->m_pDelta, 1, input_layer->m_pDelta, 1);
|
|
if (i > 0 && layer->m_bShortcut) calcAxpy(CC, 1, self_layer->m_pDelta, 1, self_layer->m_pDelta - CC, 1);
|
|
s.input = state.input + i*layer->m_nInput*layer->m_nBatch;
|
|
if (state.delta) s.delta = state.delta + i*layer->m_nInput*layer->m_nBatch;
|
|
else s.delta = 0;
|
|
backward_layer((convolutionalLayer*)input_layer, s);
|
|
incrementLayer(input_layer, -1);
|
|
incrementLayer(self_layer, -1);
|
|
incrementLayer(output_layer, -1);
|
|
};
|
|
};
|
|
|
|
void cpuHOST::update_layer(crnnLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
|
|
update_layer(layer->m_pInputLayer, batch, learning_rate, momentum, decay);
|
|
update_layer(layer->m_pSelfLayer, batch, learning_rate, momentum, decay);
|
|
update_layer(layer->m_pOutputLayer, batch, learning_rate, momentum, decay);
|
|
};
|