📄 romma.c
字号:
/* Copyright (C) 2001-2002 Mikael Ylikoski * See the accompanying file "README" for the full copyright notice *//** * @file * Romma learning algorithm. * * @author Mikael Ylikoski * @date 2001-2002 */#include <stdio.h>#include <stdlib.h>#include "multi.h"#include "romma.h"#include "utility.h"#include "vector.h"/** * Romma class. */typedef struct { vector *w; /**< Weight vector */} romma_class;/** * Create a new romma class. * * @return The class. */void *romma_new (void) { romma_class *rc; rc = my_malloc (sizeof(romma_class)); rc->w = vector_new (10); return rc;}/** * Copy a romma class. * * @param data class to copy * @return The copy. */void *romma_copy (void *data) { romma_class *nc; romma_class *rc; nc = my_malloc (sizeof(romma_class)); rc = (romma_class *)data; nc->w = vector_copy (rc->w); return nc;}/** * Free memory used by romma class. * * @param data class to free */voidromma_free (void *data) { romma_class *rc; rc = (romma_class *)data; vector_free (rc->w); free (rc);}/** * Learn from a positive example. * * @param db classifier database * @param data class * @param v example vector * @param class example class */intromma_learn (void *db, void *data, vector *v, int class) { double c, d, e, x2, w2, wx; romma_class *rc; rc = (romma_class *)data; d = vector_dot_product (rc->w, v); if (d * class > 0) return 0; w2 = vector_length (rc->w); if (w2 == 0) { vector_add (rc->w, v); // FIXME incorrect //vector_cosine_normalize (rc->w); return 0; } w2 *= w2; x2 = vector_length (v); x2 *= x2; wx = vector_dot_product (rc->w, v); e = (x2 * w2 - wx * wx); //assert (e != 0) c = (x2 * w2 - class * wx) / e; d = w2 * (class - wx) / e; vector_scale (rc->w, d); vector_add_w (rc->w, v, d); return 0;}/** * Classify a vector. * * @param db classifier database * @param data class * @param v vector to classify * @return The most probable class. */doubleromma_classify (void *db, void *data, vector *v) { romma_class *rc; rc = (romma_class *)data; return vector_dot_product (rc->w, v);}void *romma_load_class (FILE *file) { romma_class *rcl; rcl = my_malloc (sizeof(romma_class)); fscanf (file, "weights "); rcl->w = vector_load (file); if (!rcl->w) { free (rcl); return NULL; } fscanf (file, "\n"); return rcl;}intromma_save_class (FILE *file, void *data) { romma_class *rcl; rcl = (romma_class *)data; fprintf (file, "weights "); vector_save (rcl->w, file); fprintf (file, "\n"); return 0;}/** * Keep cygwin happy. */intmain (void) { return 0;}/** * Romma classifier name. */const char *my_classifier_name = "Romma";/** * Romma classifier functions. */const multi_functions my_functions = { .new = romma_new, .copy = romma_copy, .free = romma_free, .learn = romma_learn, .classify = romma_classify, .load_class = romma_load_class, .save_class = romma_save_class, .option = OPTION_BINARY};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -