GenSVM
gensvm_init.c
Go to the documentation of this file.
1 
34 #include "gensvm_init.h"
35 #include "gensvm_print.h"
36 
57 void gensvm_init_V(struct GenModel *from_model,
58  struct GenModel *to_model, struct GenData *data)
59 {
60  long i, j, k, jj_start, jj_end, jj;
61  double cmin, cmax, value, rnd;
62  double *col_min = NULL,
63  *col_max = NULL;
64 
65  long n = data->n;
66  long m = data->m;
67  long K = data->K;
68 
69  if (from_model == NULL) {
70  col_min = Calloc(double, m+1);
71  col_max = Calloc(double, m+1);
72  for (j=0; j<m+1; j++) {
73  col_min[j] = 1.0e100;
74  col_max[j] = -1.0e100;
75  }
76 
77  if (data->Z == NULL) {
78  // sparse matrix
79  long *visit_count = Calloc(long, m+1);
80  for (i=0; i<n; i++) {
81  jj_start = data->spZ->ia[i];
82  jj_end = data->spZ->ia[i+1];
83  for (jj=jj_start; jj<jj_end; jj++) {
84  j = data->spZ->ja[jj];
85  value = data->spZ->values[jj];
86 
87  col_min[j] = minimum(col_min[j], value);
88  col_max[j] = maximum(col_max[j], value);
89  visit_count[j]++;
90  }
91  }
92  // correction in case the minimum or maximum is 0
93  for (j=0; j<m+1; j++) {
94  if (visit_count[j] < n) {
95  col_min[j] = minimum(col_min[j], 0.0);
96  col_max[j] = maximum(col_max[j], 0.0);
97  }
98  }
99  free(visit_count);
100  } else {
101  // dense matrix
102  for (i=0; i<n; i++) {
103  for (j=0; j<m+1; j++) {
104  value = matrix_get(data->Z, m+1, i, j);
105  col_min[j] = minimum(col_min[j], value);
106  col_max[j] = maximum(col_max[j], value);
107  }
108  }
109  }
110  for (j=0; j<m+1; j++) {
111  cmin = (fabs(col_min[j]) < 1e-10) ? -1 : col_min[j];
112  cmax = (fabs(col_max[j]) < 1e-10) ? 1 : col_max[j];
113  for (k=0; k<K-1; k++) {
114  rnd = ((double) rand()) / ((double) RAND_MAX);
115  value = 1.0/cmin + (1.0/cmax - 1.0/cmin)*rnd;
116  matrix_set(to_model->V, K-1, j, k, value);
117  }
118  }
119  free(col_min);
120  free(col_max);
121  } else {
122  for (i=0; i<m+1; i++) {
123  for (j=0; j<K-1; j++) {
124  value = matrix_get(from_model->V, K-1, i, j);
125  matrix_set(to_model->V, K-1, i, j, value);
126  }
127  }
128  }
129 }
130 
152 void gensvm_initialize_weights(struct GenData *data, struct GenModel *model)
153 {
154  long *groups = NULL;
155  long i;
156 
157  long n = model->n;
158  long K = model->K;
159 
160  if (model->weight_idx == 1) {
161  for (i=0; i<n; i++)
162  model->rho[i] = 1.0;
163  }
164  else if (model->weight_idx == 2) {
165  groups = Calloc(long, K);
166  for (i=0; i<n; i++)
167  groups[data->y[i]-1]++;
168  for (i=0; i<n; i++)
169  model->rho[i] = ((double) n)/((double) (
170  groups[data->y[i]-1]*K));
171  } else {
172  // LCOV_EXCL_START
173  err("[GenSVM Error]: Unknown weight specification.\n");
174  exit(EXIT_FAILURE);
175  // LCOV_EXCL_STOP
176  }
177  free(groups);
178 }
#define Calloc(type, size)
Definition: gensvm_memory.h:40
long * ja
column indices, should be of length nnz
Definition: gensvm_sparse.h:67
void err(const char *fmt,...)
Parse a formatted string and write it to standard error.
Definition: gensvm_print.c:84
long K
number of classes
Definition: gensvm_base.h:58
#define matrix_get(M, cols, i, j)
void gensvm_initialize_weights(struct GenData *data, struct GenModel *model)
Initialize instance weights.
Definition: gensvm_init.c:152
double * Z
Definition: gensvm_base.h:68
int weight_idx
which weights to use (1 = unit, 2 = group)
Definition: gensvm_base.h:93
double * V
augmented weight matrix
Definition: gensvm_base.h:115
long * y
array of class labels, 1..K
Definition: gensvm_base.h:66
A structure to represent the data.
Definition: gensvm_base.h:57
double * values
actual nonzero values, should be of length nnz
Definition: gensvm_sparse.h:63
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
long n
number of instances in the dataset
Definition: gensvm_base.h:97
#define maximum(a, b)
double * rho
vector of instance weights
Definition: gensvm_base.h:128
long K
number of classes in the dataset
Definition: gensvm_base.h:95
long m
number of predictors (width of RAW)
Definition: gensvm_base.h:62
#define matrix_set(M, cols, i, j, val)
#define minimum(a, b)
Header file for gensvm_init.c.
long n
number of instances
Definition: gensvm_base.h:60
long * ia
cumulative row lengths, should be of length n_row+1
Definition: gensvm_sparse.h:65
struct GenSparse * spZ
sparse representation of the augmented data matrix
Definition: gensvm_base.h:71
Header file for gensvm_print.c.
void gensvm_init_V(struct GenModel *from_model, struct GenModel *to_model, struct GenData *data)
Seed the matrix V from an existing model or using rand.
Definition: gensvm_init.c:57