void CNN::loss_function_gradient(const double* y, const double* t, double* dst, int len)
{
for (int i = 0; i < len; i++) {
dst[i] = loss_function_mse_derivative(y[i], t[i]);
}
}
double CNN::dot_product(const double* s1, const double* s2, int len)
{
double result = 0.0;
for (int i = 0; i < len; i++) {
result += s1[i] * s2[i];
}
return result;
}
bool CNN::muladd(const double* src, double c, int len, double* dst)
{
for (int i = 0; i < len; i++) {
dst[i] += (src[i] * c);
}
return true;
}
int CNN::get_index(int x, int y, int channel, int width, int height, int depth)
{
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
assert(channel >= 0 && channel < depth);
return (height * channel + y) * width + x;
}
void CNN::calc_out2wi(int width_in, int height_in, int width_out, int height_out, int depth_out, std::vector& out2wi)
{
for (int i = 0; i < depth_out; i++) {
int block = width_in * height_in * i;
for (int y = 0; y < height_out; y++) {
for (int x = 0; x < width_out; x++) {
int rows = y * width_kernel_pooling_CNN;
int cols = x * height_kernel_pooling_CNN;
wi_connections wi_connections_;
std::pair pair_;
for (int m = 0; m < width_kernel_pooling_CNN; m++) {
for (int n = 0; n < height_kernel_pooling_CNN; n++) {
pair_.first = i;
pair_.second = (rows + m) * width_in + cols + n + block;
wi_connections_.push_back(pair_);
}
}
out2wi.push_back(wi_connections_);
}
}
}
}
void CNN::calc_out2bias(int width, int height, int depth, std::vector& out2bias)
{
for (int i = 0; i < depth; i++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
out2bias.push_back(i);
}
}
}
}
void CNN::calc_in2wo(int width_in, int height_in, int width_out, int height_out, int depth_in, int depth_out, std::vector& in2wo)
{
int len = width_in * height_in * depth_in;
in2wo.resize(len);
for (int c = 0; c < depth_in; c++) {
for (int y = 0; y < height_in; y += height_kernel_pooling_CNN) {
for (int x = 0; x < width_in; x += width_kernel_pooling_CNN) {
int dymax = min(size_pooling_CNN, height_in - y);
int dxmax = min(size_pooling_CNN, width_in - x);
int dstx = x / width_kernel_pooling_CNN;
int dsty = y / height_kernel_pooling_CNN;
for (int dy = 0; dy < dymax; dy++) {
for (int dx = 0; dx < dxmax; dx++) {
int index_in = get_index(x + dx, y + dy, c, width_in, height_in, depth_in);
int index_out = get_index(dstx, dsty, c, width_out, height_out, depth_out);
wo_connections wo_connections_;
std::pair pair_;
pair_.first = c;
pair_.second = index_out;
wo_connections_.push_back(pair_);
in2wo[index_in] = wo_connections_;
}
}
}
}
}
}
void CNN::calc_weight2io(int width_in, int height_in, int width_out, int height_out, int depth_in, int depth_out, std::vector& weight2io)
{
int len = depth_in;
weight2io.resize(len);
for (int c = 0; c < depth_in; c++) {
for (int y = 0; y < height_in; y += height_kernel_pooling_CNN) {
for (int x = 0; x < width_in; x += width_kernel_pooling_CNN) {
int dymax = min(size_pooling_CNN, height_in - y);
int dxmax = min(size_pooling_CNN, width_in - x);
int dstx = x / width_kernel_pooling_CNN;
int dsty = y / height_kernel_pooling_CNN;
for (int dy = 0; dy < dymax; dy++) {
for (int dx = 0; dx < dxmax; dx++) {
int index_in = get_index(x + dx, y + dy, c, width_in, height_in, depth_in);
int index_out = get_index(dstx, dsty, c, width_out, height_out, depth_out);
std::pair pair_;
pair_.first = index_in;
pair_.second = index_out;
weight2io[c].push_back(pair_);
}
}
}
}
}
}
void CNN::calc_bias2out(int width_in, int height_in, int width_out, int height_out, int depth_in, int depth_out, std::vector& bias2out)
{
int len = depth_in;
bias2out.resize(len); max_value) {
max_value = neuron_output[i];
pos = i;
}
}
return pos;
}
bool CNN::readModelFile(const char* name)
{
FILE* fp = fopen(name, "rb");
if (fp == NULL) {
return false;
}
评论
查看更多