00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
#include "GeneralizedOneHotVMatrix.h"
00042
00043
namespace PLearn {
00044
using namespace std;
00045
00046
00049
PLEARN_IMPLEMENT_OBJECT(GeneralizedOneHotVMatrix,
"ONE LINE DESC",
"ONE LINE HELP");
00050
00051 GeneralizedOneHotVMatrix::GeneralizedOneHotVMatrix()
00052 {
00053 }
00054
00055 GeneralizedOneHotVMatrix::GeneralizedOneHotVMatrix(
VMat the_distr,
Vec the_index,
00056
Vec the_nclasses,
Vec the_cold_value,
00057
Vec the_host_value)
00058 :
inherited(the_distr->length(), the_distr->width()+(
int)
sum(the_nclasses)-the_nclasses.length()),
00059 distr(the_distr), index(the_index), nclasses(the_nclasses),
00060 cold_value(the_cold_value), hot_value(the_host_value)
00061 {
00062
if (
min(
index)<0 ||
max(
index)>
distr->
length()-1)
00063
PLERROR(
"In GeneralizedOneHotVMatrix: all values of index must be in range [0,%d]",
00064
distr->
length()-1);
00065
if (
index.
length()!=
nclasses.
length() ||
cold_value.
length()!=
hot_value.
length() ||
index.
length()!=
hot_value.
length())
00066
PLERROR(
"In GeneralizedOneHotVMatrix: index, nclasses, cold_value and hot_value must have same length");
00067 }
00068
00069
void
00070 GeneralizedOneHotVMatrix::build()
00071 {
00072 inherited::build();
00073
build_();
00074 }
00075
00076
void
00077 GeneralizedOneHotVMatrix::build_()
00078 {
00079 }
00080
00081
void
00082 GeneralizedOneHotVMatrix::declareOptions(
OptionList &ol)
00083 {
00084
declareOption(ol,
"distr", &GeneralizedOneHotVMatrix::distr, OptionBase::buildoption,
"");
00085
declareOption(ol,
"index", &GeneralizedOneHotVMatrix::index, OptionBase::buildoption,
"");
00086
declareOption(ol,
"nclasses", &GeneralizedOneHotVMatrix::nclasses, OptionBase::buildoption,
"");
00087
declareOption(ol,
"cold_value", &GeneralizedOneHotVMatrix::cold_value, OptionBase::buildoption,
"");
00088
declareOption(ol,
"hot_value", &GeneralizedOneHotVMatrix::hot_value, OptionBase::buildoption,
"");
00089 inherited::declareOptions(ol);
00090 }
00091
00092 void GeneralizedOneHotVMatrix::getNewRow(
int i,
const Vec& v)
const
00093
{
00094
#ifdef BOUNDCHECK
00095
if(i<0 || i>=
length())
00096
PLERROR(
"In OneHotVMatrix::getNewRow OUT OF BOUNDS");
00097
if(v.
length()!=
width())
00098
PLERROR(
"In GeneralizedOneHotVMatrix::getNewRow v.length() must be equal to the VMat's width");
00099
#endif
00100
00101
Vec input(
distr->
width());
00102
distr->getRow(i, input);
00103
int v_pos = 0;
00104
for (
int j=0; j<input.
length(); j++) {
00105
const int index_pos =
vec_find(
index, (
real)j);
00106
if (index_pos == -1)
00107 v[v_pos++] = input[j];
00108
else {
00109
const int nb_class = (
int)
nclasses[index_pos];
00110
Vec target = v.
subVec(v_pos, nb_class);
00111
const real cold =
cold_value[index_pos];
00112
const real hot =
hot_value[index_pos];
00113
const int classnum =
int(
distr->get(i,j));
00114
fill_one_hot(target, classnum, cold, hot);
00115 v_pos += nb_class;
00116 }
00117 }
00118 }
00119
00120
00121 }