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.
88 lines
2.8 KiB
88 lines
2.8 KiB
#include "softmaxLayer.h"
|
|
|
|
softmaxLayer::softmaxLayer(bool useGPU) :cortexLayer( SOFTMAX, useGPU ){
|
|
m_pSoftmaxTree = NULL;
|
|
};
|
|
|
|
softmaxLayer::~softmaxLayer(){
|
|
if (m_pSoftmaxTree != NULL){
|
|
//ToDo: Cleanup Tree
|
|
};
|
|
};
|
|
|
|
bool softmaxLayer::create(uint32_t batch, uint32_t inputs, uint32_t groups){
|
|
assert(inputs%groups == 0);
|
|
fprintf(stderr, "softmax %4d\n", inputs);
|
|
m_nBatch = batch;
|
|
m_nGroups = groups;
|
|
m_nInput = inputs;
|
|
m_nOutput = inputs*batch;
|
|
m_pOutput = (float*)calloc(m_nOutput, sizeof(float));
|
|
m_nDelta = inputs*batch;
|
|
m_pDelta = (float*)calloc(m_nDelta, sizeof(float));
|
|
#ifdef GPU
|
|
m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput);// cuda_make_array(l.output, inputs*batch);
|
|
m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta);// cuda_make_array(l.delta, inputs*batch);
|
|
#endif
|
|
return true;
|
|
};
|
|
|
|
void softmaxLayer::tree(float *input, uint32_t batch, uint32_t inputs, float temp, cortexTREE *hierarchy, float *output){
|
|
for(uint32_t b = 0; b < batch; ++b){
|
|
int count = 0;
|
|
for(uint32_t i = 0; i < hierarchy->groups; ++i){
|
|
uint32_t group_size = hierarchy->group_size[i];
|
|
m_pPU->makeSoftMax(input+b*inputs + count, group_size, temp, output+b*inputs + count);
|
|
count += group_size;
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
void softmaxLayer::forward(cortexSTATE state){
|
|
uint32_t inputs = m_nInput / m_nGroups;
|
|
uint32_t batch = m_nBatch * m_nGroups;
|
|
if( m_pSoftmaxTree != NULL ){
|
|
tree(state.input, batch, inputs, m_fTemperature, m_pSoftmaxTree, m_pOutput);
|
|
} else {
|
|
for(uint32_t b = 0; b < batch; ++b){
|
|
m_pPU->makeSoftMax(state.input + b*inputs, inputs, m_fTemperature, m_pOutput + b*inputs);
|
|
};
|
|
};
|
|
};
|
|
|
|
void softmaxLayer::backward(cortexSTATE state){
|
|
for(uint32_t i = 0; i < m_nInput*m_nBatch; ++i){
|
|
state.delta[i] += m_pDelta[i];
|
|
};
|
|
};
|
|
|
|
#ifdef GPU
|
|
|
|
#include "../cuda/cuda.h"
|
|
|
|
void gpuCUDA::pull_layer(softmaxLayer *layer){
|
|
//cuda_pull_array(layer.output_gpu, layer.output, layer.m_nOutput);
|
|
pullFloatArray(layer->m_pGpuOutput, layer->m_pOutput, layer->m_nOutput);
|
|
};
|
|
|
|
void gpuCUDA::forward_layer(softmaxLayer *l, cortexSTATE state){
|
|
uint32_t inputs = l->m_nInput / l->m_nGroups;
|
|
uint32_t batch = l->m_nBatch * l->m_nGroups;
|
|
if( l->m_pSoftmaxTree != NULL ){
|
|
uint32_t count = 0;
|
|
for (uint32_t i = 0; i < l->m_pSoftmaxTree->groups; ++i) {
|
|
uint32_t group_size = l->m_pSoftmaxTree->group_size[i];
|
|
makeSoftMax(state.input + count, group_size, inputs, batch, l->m_fTemperature, l->m_pGpuOutput + count);
|
|
count += group_size;
|
|
};
|
|
} else {
|
|
makeSoftMax(state.input, inputs, inputs, batch, l->m_fTemperature, l->m_pGpuOutput);
|
|
};
|
|
};
|
|
|
|
void gpuCUDA::backward_layer(softmaxLayer *layer, cortexSTATE state){
|
|
calcAxpy(layer->m_nBatch*layer->m_nInput, 1, layer->m_pGpuDelta, 1, state.delta, 1);
|
|
};
|
|
|
|
#endif
|