資源簡介
機器學習經典算法的C語言代碼,比如:ID3算法 人臉識別源代碼 K緊鄰算法、人工神經網絡
代碼片段和文件信息
/*
?******************************************************************
?*?HISTORY
?*?15-Oct-94??Jeff?Shufelt?(js)?Carnegie?Mellon?University
?* Prepared?for?15-681?Fall?1994.
?*
?******************************************************************
?*/
#include?
#include?
#include?
#define?ABS(x)??????????(((x)?>?0.0)???(x)?:?(-(x)))
#define?fastcopy(tofromlen)\
{\
??register?char?*_to*_from;\
??register?int?_i_l;\
??_to?=?(char?*)(to);\
??_from?=?(char?*)(from);\
??_l?=?(len);\
??for?(_i?=?0;?_i?}
/***?Return?random?number?between?0.0?and?1.0?***/
double?drnd()
{
??return?((double)?random()?/?(double)?BIGRND);
}
/***?Return?random?number?between?-1.0?and?1.0?***/
double?dpn1()
{
??return?((drnd()?*?2.0)?-?1.0);
}
/***?The?squashing?function.??Currently?it‘s?a?sigmoid.?***/
double?squash(x)
double?x;
{
??return?(1.0?/?(1.0?+?exp(-x)));
}
/***?Allocate?1d?array?of?doubles?***/
double?*alloc_1d_dbl(n)
int?n;
{
??double?*new;
??new?=?(double?*)?malloc?((unsigned)?(n?*?sizeof?(double)));
??if?(new?==?NULL)?{
????printf(“ALLOC_1D_DBL:?Couldn‘t?allocate?array?of?doubles\n“);
????return?(NULL);
??}
??return?(new);
}
/***?Allocate?2d?array?of?doubles?***/
double?**alloc_2d_dbl(m?n)
int?m?n;
{
??int?i;
??double?**new;
??new?=?(double?**)?malloc?((unsigned)?(m?*?sizeof?(double?*)));
??if?(new?==?NULL)?{
????printf(“ALLOC_2D_DBL:?Couldn‘t?allocate?array?of?dbl?ptrs\n“);
????return?(NULL);
??}
??for?(i?=?0;?i?????new[i]?=?alloc_1d_dbl(n);
??}
??return?(new);
}
bpnn_randomize_weights(w?m?n)
double?**w;
int?m?n;
{
??int?i?j;
??for?(i?=?0;?i?<=?m;?i++)?{
????for?(j?=?0;?j?<=?n;?j++)?{
??????w[i][j]?=?dpn1();
????}
??}
}
bpnn_zero_weights(w?m?n)
double?**w;
int?m?n;
{
??int?i?j;
??for?(i?=?0;?i?<=?m;?i++)?{
????for?(j?=?0;?j?<=?n;?j++)?{
??????w[i][j]?=?0.0;
????}
??}
}
void?bpnn_initialize(seed)
{
??printf(“Random?number?generator?seed:?%d\n“?seed);
??srandom(seed);
}
BPNN?*bpnn_internal_create(n_in?n_hidden?n_out)
int?n_in?n_hidden?n_out;
{
??BPNN?*newnet;
??newnet?=?(BPNN?*)?malloc?(sizeof?(BPNN));
??if?(newnet?==?NULL)?{
????printf(“BPNN_CREATE:?Couldn‘t?allocate?neural?network\n“);
????return?(NULL);
??}
??newnet->input_n?=?n_in;
??newnet->hidden_n?=?n_hidden;
??newnet->output_n?=?n_out;
??newnet->input_units?=?alloc_1d_dbl(n_in?+?1);
??newnet->hidden_units?=?alloc_1d_dbl(n_hidden?+?1);
??newnet->output_units?=?alloc_1d_dbl(n_out?+?1);
??newnet->hidden_delta?=?alloc_1d_dbl(n_hidden?+?1);
??newnet->output_delta?=?alloc_1d_dbl(n_out?+?1);
??newnet->target?=?alloc_1d_dbl(n_out?+?1);
??newnet->input_weights?=?alloc_2d_dbl(n_in?+?1?n_hidden?+?1);
??newnet->hidden_weights?=?alloc_2d_dbl(n_hidden?+?1?n_out?+?1);
??newnet->input_prev_weights?=?alloc_2d_dbl(n_in?+?1?n_hidden?+?1);
??newnet->hidden_p
- 上一篇:MFC單文檔繪圖例程
- 下一篇:C++讀取hdf轉
評論
共有 條評論