001 // Copyright 2012, 2013 Brad Block, Pawjaw, LLC. (an Ohio Limited Liability Company) 002 // 003 // This file is part of JBTCRF. 004 // 005 // JBTCRF is free software: you can redistribute it and/or modify 006 // it under the terms of the GNU General Public License as published by 007 // the Free Software Foundation, either version 3 of the License, or 008 // (at your option) any later version. 009 // 010 // JBTCRF is distributed in the hope that it will be useful, 011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 // GNU General Public License for more details. 014 // 015 // You should have received a copy of the GNU General Public License 016 // along with JBTCRF. If not, see <http://www.gnu.org/licenses/>. 017 018 package com.pawjaw.classification.crf.lmcbt.sequences; 019 020 import com.pawjaw.classification.crf.lmcbt.configurations.Configuration; 021 import com.pawjaw.classification.crf.lmcbt.points.Point; 022 import java.util.ArrayList; 023 import java.util.List; 024 025 public class TrainingSequences { 026 private Configuration c; 027 private TrainingSequence[] trainingsequence_s; 028 private int[][] s_offset_i; 029 private int[][] true_features_i; 030 031 public void allocate(Configuration c, int training_sequences) { 032 this.c = c; 033 trainingsequence_s = new TrainingSequence[training_sequences]; 034 } 035 036 public int length() { 037 return trainingsequence_s.length; 038 } 039 040 public void set(int sequence_index, Point[] points, int[] labels) { 041 trainingsequence_s[sequence_index] = new TrainingSequence(); 042 trainingsequence_s[sequence_index].set(points); 043 trainingsequence_s[sequence_index].allocate(c); 044 trainingsequence_s[sequence_index].setLabels(labels); 045 } 046 047 public void indexExamples() { 048 int i = 0, s = 0, I = 0, x, X, offset = 0; 049 for(TrainingSequence ts : trainingsequence_s) 050 I += ts.length() * c.label_count_including_start_label; 051 s_offset_i = new int[I][2]; 052 for(TrainingSequence ts : trainingsequence_s) { 053 X = ts.length() * c.label_count_including_start_label; 054 for(x = 0;x < X;x++) { 055 s_offset_i[i][0] = s; 056 s_offset_i[i][1] = offset; 057 i++; 058 } 059 s++; 060 offset += X; 061 } 062 } 063 064 public void cacheTrueFeatures() { 065 int example_index, examples = examples(), i; 066 List<Integer> feature_indexes = new ArrayList(); 067 true_features_i = new int[examples][]; 068 for(example_index = 0;example_index < examples;example_index++) { 069 i = 0; 070 getTrueFeatures(example_index, feature_indexes); 071 true_features_i[example_index] = new int[feature_indexes.size()]; 072 for(int feature_index : feature_indexes) 073 true_features_i[example_index][i++] = feature_index; 074 } 075 } 076 077 public int examples() { 078 return s_offset_i.length; 079 } 080 081 public int features() { 082 return c.expanded_features; 083 } 084 085 public boolean hasTrueFeature(int example_index, int feature_index) { 086 return trainingsequence_s[s_offset_i[example_index][0]].hasTrueFeature(c, 087 example_index - s_offset_i[example_index][1], 088 feature_index); 089 } 090 091 public List<Integer> getTrueFeatures(int example_index, List<Integer> feature_indexes) { 092 return trainingsequence_s[s_offset_i[example_index][0]].getTrueFeatures(c, 093 example_index - s_offset_i[example_index][1], 094 feature_indexes); 095 } 096 097 public int[] getCachedTrueFeatures(int example_index) { 098 return true_features_i[example_index]; 099 } 100 101 public int compare(int example_indexa, int example_indexb, int feature_index) { 102 boolean a = hasTrueFeature(example_indexa, feature_index); 103 boolean b = hasTrueFeature(example_indexb, feature_index); 104 return ((a && b) || (!a && !b)) ? 0 : (!a && b) ? -1 : 1; 105 } 106 107 public double getTarget(int label, int example_index) { 108 return trainingsequence_s[s_offset_i[example_index][0]].getTarget(label, example_index - s_offset_i[example_index][1]); 109 } 110 111 public void incrementPotential(int label, int example_index, double potential) { 112 trainingsequence_s[s_offset_i[example_index][0]].incrementPotential(label, example_index - s_offset_i[example_index][1], potential); 113 } 114 115 public void update() { 116 for(TrainingSequence ts : trainingsequence_s) { 117 ts.updateForwardBackward(); 118 ts.updateTargets(); 119 } 120 } 121 }