SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/base/SGObject.h>
9 //# define DEBUG
10 
11 using namespace shogun;
12 
14  :CSGObject(),
15  m_segment_loss_matrix(1,1),
16  m_segment_loss(1,1,2),
17  m_segment_ids(NULL),
18  m_segment_mask(NULL),
19  m_num_segment_types(0)
20 {
21 }
23 {
24 }
25 
26 void CSegmentLoss::set_segment_loss(float64_t* segment_loss, int32_t m, int32_t n)
27 {
28  // here we need two matrices. Store it in one: 2N x N
29  if (2*m!=n)
30  SG_ERROR( "segment_loss should be 2 x quadratic matrix: %i!=%i\n", 2*m, n) ;
31 
33 
34  m_segment_loss.set_array(segment_loss, m, n/2, 2, true, true) ;
35 }
36 
38 {
39  m_segment_ids = segment_ids;
40 }
41 
43 {
44  m_segment_mask = segment_mask;
45 }
46 
47 void CSegmentLoss::compute_loss(int32_t* all_pos, int32_t len)
48 {
49 #ifdef DEBUG
50  SG_PRINT("compute loss: len: %i, m_num_segment_types: %i\n", len, m_num_segment_types);
51  SG_PRINT("m_segment_mask->element(0):%f \n", m_segment_mask->element(0));
52  SG_PRINT("m_segment_ids->element(0):%i \n", m_segment_ids->element(0));
53 #endif
54  ASSERT(m_segment_ids->get_dim1()==len);
56 
58 
59  for (int seg_type=0; seg_type<m_num_segment_types; seg_type++)
60  {
61  float32_t value = 0;
62  int32_t last_id = -1;
63  int32_t last_pos = all_pos[len-1];
64  for (int pos=len-1;pos>=0; pos--)
65  {
66  int32_t cur_id = m_segment_ids->element(pos);
67  if (cur_id!=last_id)
68  {
69  // segment contribution
70  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 0);
71  last_id = cur_id;
72  }
73  //length contribution (nucleotide loss)
74  value += m_segment_mask->element(pos)*m_segment_loss.element(cur_id, seg_type, 1)*(last_pos-all_pos[pos]);
75  last_pos = all_pos[pos];
76  m_segment_loss_matrix.element(seg_type, pos)=value;
77  }
78  }
79 #ifdef DEBUG
81 #endif
82 }
83 

SHOGUN Machine Learning Toolbox - Documentation