下載鏈接:https://github.com/attractivechaos/kann
以什么是KANN:
? ?????KANN是一個獨立的輕量級庫改执,用于構(gòu)建和訓練中小型人工神經(jīng)網(wǎng)絡(luò)腿椎,如多層感知器崎溃,卷積神經(jīng)網(wǎng)絡(luò)和遞歸神經(jīng)網(wǎng)絡(luò)(包括LSTM和GRU)搅荞。它實現(xiàn)了基于圖形的反向模式自動區(qū)分柱彻,并允許構(gòu)建具有遞歸何之,共享權(quán)重和多個輸入/輸出/成本的拓撲復雜神經(jīng)網(wǎng)絡(luò)梢灭。與主流深度學習框架(如TensorFlow)相比夷家,KANN不具備可擴展性,但它的靈活性接近敏释,代碼庫小得多库快,僅依賴于標準C庫。與其他輕量級框架(如tiny-dnn)相比钥顽,KANN仍然更小义屏,更快,更通用蜂大,支持RNN闽铐,VAE和非標準神經(jīng)網(wǎng)絡(luò),可能會使這些輕量級框架失效奶浦。
特征
????靈活兄墅。通過使用運算符構(gòu)建計算圖來進行模型構(gòu)建。支持RNN澳叉,權(quán)重共享和多個輸入/輸出隙咸。
????高效沐悦。合理優(yōu)化的矩陣產(chǎn)品和卷積。支持迷你批處理和有效的多線程五督。有時在主CPU模式下比主流框架更快藏否。
????小巧便攜。截至目前充包,KANN在四個源代碼文件中的代碼少于4000行副签,默認情況下沒有非標準依賴項。與ANSI C編譯器兼容基矮。
限制
????僅限CPU淆储。因此,KANN?不是用于訓練巨大的神經(jīng)網(wǎng)絡(luò)家浇。
????缺少一些常見的運算符和體系結(jié)構(gòu)遏考,如批量標準化。
? ? 缺少用于訓練RNN的詳細API
API
神經(jīng)網(wǎng)絡(luò)中的網(wǎng)絡(luò)層:
? ? kad_node_t *kad_feed(int n_d, ...):設(shè)置輸入層節(jié)點蓝谨,如:t = kad_feed(4, 1, 1, 28, 28);
????kad_node_t *kann_layer_dense(kad_node_t *in, int n1)
????kad_node_t *kann_layer_conv2d(kad_node_t *in, int n_flt, int k_rows, int k_cols, int stride_r, int stride_c, int pad_r, int pad_c):
? ?kad_node_t *kann_layer_gru(kad_node_t *in, int n1, int rnn_flag)
????kad_node_t *kann_layer_rnn(kad_node_t *in, int n1, int rnn_flag)
????kad_node_t *kann_layer_layernorm(kad_node_t *in)? ????//Batch Normalization
????kad_node_t *kann_layer_dropout(kad_node_t *t, float r)
? ??kad_node_t *kad_max2d(kad_node_t *x, int kernel_r, int kernel_c, int stride_r, int stride_c, int top_pad, int left_pad)
激活函數(shù):
????kad_node_t *kad_square(kad_node_t *x); ????/* f(x) = x^2 (element-wise square) */
????kad_node_t *kad_sigm(kad_node_t *x);????? /* f(x) = 1/(1+exp(-x))? ? ? ? ? ? ? (element-wise sigmoid) */
????kad_node_t *kad_tanh(kad_node_t *x);????? /* f(x) = (1-exp(-2x)) / (1+exp(-2x)) (element-wise tanh) */
????kad_node_t *kad_relu(kad_node_t *x);? ???? /* f(x) = max{0,x}? ? ? ? ? ? ? ? ? ? (element-wise rectifier, aka ReLU) */
????kad_node_t *kad_softmax(kad_node_t *x);????/* f_i(x_1,...,x_n) = exp(x_i) / \sum_j exp(x_j) (softmax: tf.nn.softmax(x,dim=-1)) */
????kad_node_t *kad_1minus(kad_node_t *x); ????/* f(x) = 1 - x */
????kad_node_t *kad_exp(kad_node_t *x);? ? ????/* f(x) = exp(x) */
????kad_node_t *kad_log(kad_node_t *x);? ? ????/* f(x) = log(x) */
????kad_node_t *kad_sin(kad_node_t *x);? ?????/* f(x) = sin(x) */
構(gòu)建網(wǎng)絡(luò):
? ??kann_t *kann_new(kad_node_t *cost, int n_rest, ...)
訓練網(wǎng)絡(luò):
? ??int kann_train_fnn1(kann_t *ann, float lr, int mini_size, int max_epoch, int max_drop_streak, float frac_val, int n, float **_x, float **_y)
? ? ? ? 對于RNN多輸入的網(wǎng)絡(luò)青团,?kann_train_fnn1并不適合譬巫,而且kann中也沒有提供類似API
模型保存及加載:
????kann_t *kann_load_fp(FILE *fp)
? ??void kann_save(const char *fn, kann_t *ann)
線程加速:
? ??void kann_mt(kann_t *ann, int n_threads, int max_batch_size)
內(nèi)存釋放:
? ??void kann_data_free(kann_data_t *d)
????void kann_delete(kann_t *a)
數(shù)據(jù)結(jié)構(gòu)
輸入數(shù)據(jù)結(jié)構(gòu)為【1,1督笆,28芦昔,28】
The shape of n-d arrays
????For 2D convolution, cuDNN and Theano take images in shape of (batch-size,in-channel,height,width) and convolution weights in shape of (out-channel,in-channel,kernel-height,kernel-width). KANN follows the cuDNN and Theano convention. Caffe and TensorFlow are different. They take images in shape of (batch-size,height,weight,in-channel) and weights in (kernel-height,kernel-width,in-channel,out-channel).
存儲到鏈表中為:
typedef struct kad_node_t {??
????uint8_t n_d; ???????????????????????????????????????? /* number of dimensions; no larger than KAD_MAX_DIM */ //尺寸數(shù);不大于KADY-Max??
????uint8_t flag; ???????????????????????????????????????????????????? /* type of the node; see KAD_F_* for valid flags */ //節(jié)點的類型??
????uint16_t op; ???????????????????????????????????????????????????? /* operator; kad_op_list[op] is the actual function */??
????int32_t n_child; ???????????????????????????????????????????????? /* number of operands/child nodes */??
????int32_t tmp; ???????????????????????????????????????????????????? /* temporary field; MUST BE zero before calling kad_compile() */??
????int32_t ptr_size; ???????????????????????????????????????????????? /* size of ptr below */??
????int32_t d[KAD_MAX_DIM]; ????????????????????????????????/* dimensions */??
????int32_t ext_label; ???????????????????????????????????????????? /* labels for external uses (not modified by the kad_* APIs) */??
????uint32_t ext_flag; ???????????????????????????????????????????? /* flags for external uses (not modified by the kad_* APIs) */??
????float *x; ???????????????????????????????????????????????????????????? /* value; allocated for internal nodes */??
????float *g;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/* gradient; allocated for internal nodes */??
????void *ptr; ???????????????????????????????????????????????????????? /* for special operators that need additional parameters (e.g. conv2d) */??
????void *gtmp;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* temporary data generated at the forward pass but used at the backward pass */??
????struct kad_node_t **child; ???????????????????????????? /* operands/child nodes */??
????struct kad_node_t *pre; ???????????????????????????????? /* usually NULL; only used for RNN */
} kad_node_t, *kad_node_p;?
圖示結(jié)構(gòu)圖:
? ??