SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SegmentLoss.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 
5 #include <shogun/lib/config.h>
6 #include <shogun/io/SGIO.h>
8 #include <shogun/lib/Array.h>
9 #include <shogun/lib/Array2.h>
10 #include <shogun/lib/Array3.h>
11 #include <shogun/base/SGObject.h>
12 //# define DEBUG
13 
14 using namespace shogun;
15 
17  :CSGObject(),
18  m_segment_loss_matrix(1,1),
19  m_segment_loss(1,1,2),
20  m_segment_ids(NULL),
21  m_segment_mask(NULL),
22  m_num_segment_types(0)
23 {
24 }
26 {
27 }
28 
29 void CSegmentLoss::set_segment_loss(float64_t* segment_loss, int32_t m, int32_t n)
30 {
31  // here we need two matrices. Store it in one: 2N x N
32  if (2*m!=n)
33  SG_ERROR( "segment_loss should be 2 x quadratic matrix: %i!=%i\n", 2*m, n) ;
34 
36 
37  m_segment_loss.set_array(segment_loss, m, n/2, 2, true, true) ;
38 }
39 
41 {
42  m_segment_ids = segment_ids;
43 }
44 
46 {
47  m_segment_mask = segment_mask;
48 }
49 
50 void CSegmentLoss::compute_loss(int32_t* all_pos, int32_t len)
51 {
52 #ifdef DEBUG
53  SG_PRINT("compute loss: len: %i, m_num_segment_types: %i\n", len, m_num_segment_types);
54  SG_PRINT("m_segment_mask->element(0):%f \n", m_segment_mask->element(0));
55  SG_PRINT("m_segment_ids->element(0):%i \n", m_segment_ids->element(0));
56 #endif
57  ASSERT(m_segment_ids->get_dim1()==len);
59 
61 
62  for (int seg_type=0; seg_type<m_num_segment_types; seg_type++)
63  {
64  float32_t value = 0;
65  int32_t last_id = -1;
66  int32_t last_pos = all_pos[len-1];
67  for (int pos=len-1;pos>=0; pos--)
68  {
69  int32_t cur_id = m_segment_ids->element(pos);
70  if (cur_id!=last_id)
71  {
72  // segment contribution
73  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 0);
74  last_id = cur_id;
75  }
76  //length contribution (nucleotide loss)
77  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 1)*(last_pos-all_pos[pos]);
78  last_pos = all_pos[pos];
79  m_segment_loss_matrix.element(seg_type, pos)=value;
80  }
81  }
82 #ifdef DEBUG
84 #endif
85 }
86 

SHOGUN Machine Learning Toolbox - Documentation