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
00042
00043
00044
00045
00046
#include "SampleVariable.h"
00047
#include <plearn/math/random.h>
00048
00049
namespace PLearn {
00050
using namespace std;
00051
00052
00053
00054 string SourceSampleVariable::classname()
const
00055
{
return "SourceSampleVariable"; }
00056
00057 VarArray SourceSampleVariable::random_sources()
00058 {
00059
if (marked)
00060
return VarArray(0,0);
00061 marked =
true;
00062
return Var(
this);
00063 }
00064
00065
00066
00067 string UnarySampleVariable::classname()
const
00068
{
return "UnarySampleVariable"; }
00069
00070 VarArray UnarySampleVariable::random_sources()
00071 {
00072
if (marked)
00073
return VarArray(0,0);
00074 marked =
true;
00075
return input->random_sources() &
Var(
this);
00076 }
00077
00078
00079
00080 string BinarySampleVariable::classname()
const
00081
{
return "BinarySampleVariable"; }
00082
00083 VarArray BinarySampleVariable::random_sources()
00084 {
00085
if (marked)
00086
return VarArray(0,0);
00087 marked =
true;
00088
return input1->random_sources() & input2->random_sources() &
Var(
this);
00089 }
00090
00091
00092
00093 string UniformSampleVariable::classname()
const
00094
{
return "UniformSampleVariable"; }
00095
00096 UniformSampleVariable::UniformSampleVariable(
int length,
int width,
00097
real minvalue,
00098
real maxvalue)
00099 :
SourceSampleVariable(length,width),
00100 min_value(minvalue),max_value(maxvalue)
00101 {
00102 sprintf(
name,
"U[%f,%f]",
min_value,
max_value);
00103 }
00104
00105 UniformSampleVariable* UniformSampleVariable::deepCopy(map<const void*, void*>& copies)
const
00106
{
00107 map<const void*, void*>::iterator it = copies.find(
this);
00108
if (it!=copies.end())
00109
return (
UniformSampleVariable*)it->second;
00110
00111
00112
UniformSampleVariable* deep_copy =
new UniformSampleVariable(*
this);
00113
00114 copies[
this] = deep_copy;
00115
00116 deep_copy->
makeDeepCopyFromShallowCopy(copies);
00117
00118
return deep_copy;
00119 }
00120
00121 void UniformSampleVariable::fprop()
00122 {
00123
for (
int k=0;
k<
nelems();
k++)
00124 valuedata[
k] =
bounded_uniform(
min_value,
max_value);
00125
00126 }
00127
00128
00129
00130 string MultinomialSampleVariable::classname()
const
00131
{
return "MultinomialSampleVariable"; }
00132
00133 MultinomialSampleVariable::MultinomialSampleVariable(
Variable* probabilities,
00134
int length,
int width)
00135 :
UnarySampleVariable(probabilities, length, width)
00136 {
00137 sprintf(
name,
"Multinomial[%dx%d]",length,width);
00138 }
00139
00140 MultinomialSampleVariable* MultinomialSampleVariable::deepCopy(map<const void*, void*>& copies)
const
00141
{
00142 map<const void*, void*>::iterator it = copies.find(
this);
00143
if (it!=copies.end())
00144
return (
MultinomialSampleVariable*)it->second;
00145
00146
00147
MultinomialSampleVariable* deep_copy =
new MultinomialSampleVariable(*
this);
00148
00149 copies[
this] = deep_copy;
00150
00151 deep_copy->
makeDeepCopyFromShallowCopy(copies);
00152
00153
return deep_copy;
00154 }
00155
00156 void MultinomialSampleVariable::fprop()
00157 {
00158
for (
int k=0;
k<
nelems();
k++)
00159 valuedata[
k] =
multinomial_sample(input->value);
00160
00161 }
00162
00163
00164
00165 string DiagonalNormalSampleVariable::classname()
const
00166
{
return "DiagonalNormalSampleVariable"; }
00167
00168 DiagonalNormalSampleVariable::DiagonalNormalSampleVariable
00169 (
Variable* mu,
Variable* sigma)
00170 :
BinarySampleVariable(mu, sigma, mu->length(), mu->width())
00171 {
00172
if (!sigma->isScalar() && (mu->length()!=sigma->length() || mu->width()!=sigma->width()) )
00173
PLERROR(
"DiagonalNormalSampleVariable: mu(%d,%d) incompatible with sigma(%d,%d)",
00174 mu->length(),mu->width(),sigma->length(),sigma->width());
00175 }
00176
00177 DiagonalNormalSampleVariable* DiagonalNormalSampleVariable::deepCopy(map<const void*, void*>& copies)
const
00178
{
00179 map<const void*, void*>::iterator it = copies.find(
this);
00180
if (it!=copies.end())
00181
return (
DiagonalNormalSampleVariable*)it->second;
00182
00183
00184
DiagonalNormalSampleVariable* deep_copy =
new DiagonalNormalSampleVariable(*
this);
00185
00186 copies[
this] = deep_copy;
00187
00188 deep_copy->
makeDeepCopyFromShallowCopy(copies);
00189
00190
return deep_copy;
00191 }
00192
00193 void DiagonalNormalSampleVariable::fprop()
00194 {
00195
if (input2->isScalar())
00196 {
00197
real sigma = input2->valuedata[0];
00198
for (
int k=0;
k<
length();
k++)
00199 valuedata[
k] =
gaussian_mu_sigma(input1->valuedata[
k],
00200 sigma);
00201 }
00202
else
00203
for (
int k=0;
k<
length();
k++)
00204 valuedata[
k] =
gaussian_mu_sigma(input1->valuedata[
k],
00205 input2->valuedata[
k]);
00206 }
00207
00208
00209 }
00210