10 #include <shogun/lib/config.h> 59 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
79 int32_t *sv=SG_MALLOC(int32_t,
x_n), size_sv=0, iter=0;
89 SG_PRINT(
"Maximum number of Newton steps reached. Try larger lambda")
93 obj_fun_linear(weights, out, &obj, sv, &size_sv, grad);
100 for (int32_t i=0; i<
x_d+1; i++)
101 SG_PRINT(
"grad[%d]=%.16g\n", i, grad[i])
104 for (int32_t i=0; i<size_sv; i++)
110 for (int32_t k=0; k<size_sv; k++)
113 for (int32_t j=0; j<
x_d; j++)
114 Xsv[k*x_d+j]=sgv.
vector[j];
127 for (int32_t i=0; i<
x_d; i++)
134 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, x_d, x_d, size_sv,
135 1.0, Xsv, size_sv, Xsv, size_sv, 0.0, Xsv2, x_d);
138 for (int32_t j=0; j<
x_d; j++)
140 for (int32_t i=0; i<size_sv; i++)
141 sum[j]+=Xsv[i+j*size_sv];
146 for (int32_t i=0; i<
x_d; i++)
148 for (int32_t j=0; j<
x_d; j++)
149 Xsv2sum[j*(x_d+1)+i]=Xsv2[j*x_d+i];
151 Xsv2sum[x_d*(x_d+1)+i]=sum[i];
154 for (int32_t j=0; j<
x_d; j++)
155 Xsv2sum[j*(x_d+1)+
x_d]=sum[j];
157 Xsv2sum[x_d*(x_d+1)+x_d]=size_sv;
163 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, x_d+1, x_d+1,
164 x_d+1, 1.0, lcrossdiag, x_d+1, identity_matrix, x_d+1, 1.0,
173 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, r, 1, r, 1.0,
174 inverse, r, grad, r, 0.0, s2, r);
176 for (int32_t i=0; i<r; i++)
179 line_search_linear(weights, step, out, &t);
184 for (int32_t i=0; i<
x_n; i++)
185 SG_PRINT(
"out[%d]=%.16g\n", i, out[i])
187 for (int32_t i=0; i<x_d+1; i++)
188 SG_PRINT(
"weights[%d]=%.16g\n", i, weights[i])
193 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1, r, -0.5,
194 step, r, grad, r, 0.0, &newton_decrement, 1);
196 SG_PRINT(
"Itr=%d, Obj=%f, No of sv=%d, Newton dec=%0.3f, line search=%0.3f\n\n",
197 iter, obj, size_sv, newton_decrement, t);
205 SG_FREE(identity_matrix);
210 if (newton_decrement*2<
prec*obj)
215 SG_PRINT(
"FINAL W AND BIAS Vector=\n\n")
216 CMath::display_matrix(weights,
x_d+1, 1);
245 for (int32_t i=0; i<
x_n; i++)
251 CMath::display_vector(d, x_d+1,
"Weight vector");
253 for (int32_t i=0; i<x_d+1; i++)
256 CMath::display_vector(Xd, x_n,
"XD vector=");
260 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1, x_d,
lambda,
261 weights, x_d, d, x_d, 0.0, &wd, 1);
263 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1, x_d,
lambda, d,
264 x_d, d, x_d, 0.0, &dd, 1);
267 int32_t sv_len=0, *sv=SG_MALLOC(int32_t, x_n);
272 sg_memcpy(temp1forout, temp1,
sizeof(
float64_t)*x_n);
279 for (int32_t i=0; i<
x_n; i++)
286 for (int32_t i=0; i<sv_len; i++)
288 outzsv[i]=outz[sv[i]];
293 memset(temp1, 0,
sizeof(
float64_t)*sv_len);
300 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1, sv_len, 1.0,
301 Xsv, sv_len, Xsv, sv_len, 0.0, &h, 1);
312 for (int32_t i=0; i<
x_n; i++)
319 SG_FREE(temp1forout);
332 for (int32_t i=0; i<
x_n; i++)
339 for (int32_t i=0; i<
x_n; i++)
358 cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, 1, 1,
x_d+1,
lambda,
359 w0,
x_d+1, w0copy,
x_d+1, 0.0, &C1, 1);
367 for (int32_t i=0; i<
x_n; i++)
372 CMath::display_vector(vec.
vector,
x_d,
"vector");
373 CMath::display_vector(temp1,
x_d,
"debuging");
378 for (int32_t i=0; i<
x_d; i++)
385 for (int32_t i=0; i<
x_n; i++)
virtual ELabelType get_label_type() const =0
virtual bool train_machine(CFeatures *data=NULL)
virtual void set_w(const SGVector< float64_t > src_w)
The class Labels models labels, i.e. class assignments of objects.
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)=0
bool has_property(EFeatureProperty p) const
static T sum(T *vec, int32_t len)
Return sum(vec)
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
virtual int32_t get_num_vectors() const =0
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)=0
void display_matrix(const char *name="matrix") const
Features that support dot products among other operations.
void add(const SGVector< T > x)
virtual int32_t get_dim_feature_space() const =0
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
static void clear_cancel()
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
static void fill_vector(T *vec, int32_t len, T value)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
static bool cancel_computations()
all of classes and functions are contained in the shogun namespace
T sum(const Container< T > &a, bool no_diag=false)
The class Features is the base class of all feature objects.
SGVector< float64_t > get_computed_dot_feature_vector(int32_t num)
Binary Labels for binary classification.
virtual void set_bias(float64_t b)
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
virtual void set_labels(CLabels *lab)
static void vec1_plus_scalar_times_vec2(T *vec1, const T scalar, const T *vec2, int32_t n)
x=x+alpha*y