⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wma.c

📁 使用具有增量学习的监控式学习方法。包括几个不同的分类算法。
💻 C
字号:
/* Copyright (C) 2002  Mikael Ylikoski * See the accompanying file "README" for the full copyright notice *//** * @file * Weighted Majority Algorithm (WMA) for linear-max learning algorithm. * * @author  Mikael Ylikoski * @date    2002 */#include <stdlib.h>#include <string.h>#include "multi.h"#include "utility.h"#include "vector.h"#include "wma.h"/** * WMA global data */typedef struct {    //vector *v;		/**< Vector for new classes */    float a;		/**< Learning rate */} wma_db;/** * WMA class */typedef struct {    vector *w;		/**< Weight vector */} wma_class;void *wma_new_db (const char *opts) {    float f;    wma_db *db;    db = my_malloc (sizeof(wma_class));    db->a = 2;    if (opts) {	f = get_opt_flt (opts, "a=");	if (f > 0)	    db->a = f;    }    return db;}void *wma_new_class (void) {    wma_class *lc;    lc = my_malloc (sizeof(wma_class));    lc->w = vector_new (10);    if (!lc->w) {	free (lc);	return NULL;    }    return lc;}void *wma_copy (void *class) {    wma_class *lc;    wma_class *nc;    lc = (wma_class *)class;    nc = my_malloc (sizeof(wma_class));    nc->w = vector_copy (lc->w);    if (!nc->w) {	free (nc);	return NULL;    }    return nc;}voidwma_free (void *class) {    wma_class *lc;    lc = (wma_class *)class;    vector_free (lc->w);    free (lc);}/** * WMA learning function. */intwma_learn (void *db, void *data, vector *v, int class) {    wma_class *lc;    wma_db *ldb;    lc = (wma_class *)data;    ldb = (wma_db *)db;    if (class > 0)	vector_exp_mul (lc->w, v, ldb->a);    else	vector_exp_mul (lc->w, v, 1 / ldb->a);    return 0;}/** * WMA classifier function. */doublewma_classify (void *db, void *data, vector *v) {    wma_class *lc;    lc = (wma_class *)data;    return vector_dot_product_nonzero (lc->w, v, 1);}void *wma_load_db (FILE *file) {    int i;    wma_db *ndb;    ndb = my_malloc (sizeof(wma_db));    i = fscanf (file, "rate %f\n", &ndb->a);    /*fscanf (file, "new_vec ");    ndb->v = vector_load (file);    if (!ndb->v) {	free (ndb);	return NULL;    }    fscanf (file, "\n");*/    return ndb;}void *wma_load_class (FILE *file) {    wma_class *ncl;    ncl = my_malloc (sizeof(wma_class));    fscanf (file, "weights ");    ncl->w = vector_load (file);    if (!ncl->w) {	free (ncl);	return NULL;    }    fscanf (file, "\n");    return ncl;}intwma_save_db (FILE *file, void *db) {    wma_db *ndb;    ndb = (wma_db *)db;    fprintf (file, "rate %f\n", ndb->a);    /*fprintf (file, "new_vec ");    vector_save (ndb->v, file);    fprintf (file, "\n");*/    return 0;}intwma_save_class (FILE *file, void *data) {    wma_class *ncl;    ncl = (wma_class *)data;    fprintf (file, "weights ");    vector_save (ncl->w, file);    fprintf (file, "\n");    return 0;}/** * Keep cygwin happy. */intmain (void) {    return 0;}/** * WMA classifier name. */const char *my_classifier_name = "WMA";/** * WMA classifier functions. */const multi_functions my_functions = {    .new_db = wma_new_db,    .new = wma_new_class,    .copy = wma_copy,    .free = wma_free,    .learn = wma_learn,    .classify = wma_classify,    .load_db = wma_load_db,    .load_class = wma_load_class,    .save_db = wma_save_db,    .save_class = wma_save_class,    .option = OPTION_BINARY | OPTION_BOOLEAN};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -