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.
87 lines
2.0 KiB
87 lines
2.0 KiB
#include "dropoutLayer.h"
|
|
//#include "utils.h"
|
|
#include "../cuda/cuda.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
dropoutLayer::dropoutLayer(bool AA) : cortexLayer(DROPOUT, AA){
|
|
};
|
|
|
|
dropoutLayer::~dropoutLayer(){
|
|
};
|
|
|
|
bool dropoutLayer::create(uint32_t batch, uint32_t inputs, float probability){
|
|
m_fProbability = probability;
|
|
m_nInput = inputs;
|
|
m_nOutput = inputs;
|
|
m_nBatch = batch;
|
|
m_pRand = (float*)calloc(m_nInput*m_nBatch, sizeof(float));
|
|
if (m_pRand == NULL)return false;
|
|
m_fScale = 1.f / (1.f - m_fProbability);
|
|
// On Creation CUDA memory may be Unbounded from Host mem
|
|
//m_pGpuRand = m_pPU->makeFloatArray(m_nInput*m_nBatch, m_pRand );
|
|
//if (m_pGpuRand == NULL) return false;
|
|
memset(m_pRand, 0, sizeof(float)*m_nInput*m_nBatch);
|
|
m_pGpuRand = NULL;
|
|
fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs);
|
|
return true;
|
|
};
|
|
|
|
|
|
bool dropoutLayer::resize(uint32_t w, uint32_t h){
|
|
m_pRand = (float*)realloc(m_pRand, m_nInput*m_nBatch*sizeof(float));
|
|
#ifdef GPU
|
|
m_pPU->freeMem(m_pGpuRand);
|
|
m_pGpuRand = m_pPU->makeFloatArray(m_nInput * m_nBatch, m_pRand );
|
|
#endif
|
|
return true;
|
|
};
|
|
|
|
/*
|
|
void activationLayer::forward(cortexSTATE state){
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
|
|
}
|
|
else {
|
|
#endif
|
|
|
|
#ifdef GPU
|
|
};
|
|
#endif
|
|
};
|
|
|
|
void activationLayer::backward(cortexSTATE state){
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
|
|
}
|
|
else {
|
|
#endif
|
|
|
|
#ifdef GPU
|
|
};
|
|
#endif
|
|
};
|
|
*/
|
|
|
|
//void dropoutLayer::update( int batch, float learning_rate, float momentum, float decay);
|
|
void dropoutLayer::forward(cortexSTATE state){
|
|
if (!state.train) return;
|
|
for( uint32_t i = 0; i < m_nBatch * m_nInput; ++i){
|
|
float r = rand_uniform(0, 1);
|
|
m_pRand[i] = r;
|
|
if(r < m_fProbability) state.input[i] = 0;
|
|
else state.input[i] *= m_fScale;
|
|
};
|
|
};
|
|
|
|
void dropoutLayer::backward(cortexSTATE state){
|
|
if(!state.delta) return;
|
|
for (uint32_t i = 0; i < m_nBatch * m_nInput; ++i){
|
|
float r = m_pRand[i];
|
|
if(r < m_fProbability) state.delta[i] = 0;
|
|
else state.delta[i] *= m_fScale;
|
|
};
|
|
};
|
|
|