Skip to content

Commit 6c08a5b

Browse files
author
plearner
committed
experimental softsoftmax
1 parent 2444860 commit 6c08a5b

File tree

6 files changed

+571
-18
lines changed

6 files changed

+571
-18
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// -*- C++ -*-
2+
3+
// AdditiveGaussianNoiseVariable.cc
4+
//
5+
// Copyright (C) 2010 Pascal Vincent
6+
//
7+
// Redistribution and use in source and binary forms, with or without
8+
// modification, are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright
14+
// notice, this list of conditions and the following disclaimer in the
15+
// documentation and/or other materials provided with the distribution.
16+
//
17+
// 3. The name of the authors may not be used to endorse or promote
18+
// products derived from this software without specific prior written
19+
// permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23+
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24+
// NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
//
32+
// This file is part of the PLearn library. For more information on the PLearn
33+
// library, go to the PLearn Web site at www.plearn.org
34+
35+
// Authors: Pascal Vincent
36+
37+
/*! \file AdditiveGaussianNoiseVariable.cc */
38+
39+
40+
#include "AdditiveGaussianNoiseVariable.h"
41+
42+
namespace PLearn {
43+
using namespace std;
44+
45+
/** AdditiveGaussianNoiseVariable **/
46+
47+
PLEARN_IMPLEMENT_OBJECT(
48+
AdditiveGaussianNoiseVariable,
49+
"Adds an isotropic Gaussian noise of given standard deviation",
50+
""
51+
);
52+
53+
AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable()
54+
:sigma(1)
55+
{
56+
// ### You may (or not) want to call build_() to finish building the object
57+
// ### (doing so assumes the parent classes' build_() have been called too
58+
// ### in the parent classes' constructors, something that you must ensure)
59+
}
60+
61+
// constructor from input variable and parameters
62+
// AdditiveGaussianNoiseVariable::AdditiveGaussianNoiseVariable(Variable* input, param_type the_parameter,...)
63+
// ### replace with actual parameters
64+
// : inherited(input, this_variable_length, this_variable_width),
65+
// parameter(the_parameter),
66+
// ...
67+
//{
68+
// // ### You may (or not) want to call build_() to finish building the
69+
// // ### object
70+
//}
71+
72+
void AdditiveGaussianNoiseVariable::recomputeSize(int& l, int& w) const
73+
{
74+
if (input)
75+
{
76+
l = input.length();
77+
w = input.width(); // the computed width
78+
}
79+
else
80+
l = w = 0;
81+
}
82+
83+
// ### computes value from input's value
84+
void AdditiveGaussianNoiseVariable::fprop()
85+
{
86+
checkContiguity();
87+
88+
if(random_gen.isNull())
89+
random_gen = PRandom::common(false);
90+
91+
int n = value.length();
92+
for(int i=0; i<n; i++)
93+
{
94+
valuedata[i] = random_gen->gaussian_mu_sigma(input->valuedata[i], sigma);
95+
}
96+
}
97+
98+
// ### computes input's gradient from gradient
99+
void AdditiveGaussianNoiseVariable::bprop()
100+
{
101+
}
102+
103+
// ### You can implement these methods:
104+
// void AdditiveGaussianNoiseVariable::bbprop() {}
105+
// void AdditiveGaussianNoiseVariable::symbolicBprop() {}
106+
// void AdditiveGaussianNoiseVariable::rfprop() {}
107+
108+
109+
// ### Nothing to add here, simply calls build_
110+
void AdditiveGaussianNoiseVariable::build()
111+
{
112+
inherited::build();
113+
build_();
114+
}
115+
116+
void AdditiveGaussianNoiseVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
117+
{
118+
inherited::makeDeepCopyFromShallowCopy(copies);
119+
120+
// ### Call deepCopyField on all "pointer-like" fields
121+
// ### that you wish to be deepCopied rather than
122+
// ### shallow-copied.
123+
// ### ex:
124+
deepCopyField(random_gen, copies);
125+
126+
// ### If you want to deepCopy a Var field:
127+
// varDeepCopyField(somevariable, copies);
128+
}
129+
130+
void AdditiveGaussianNoiseVariable::declareOptions(OptionList& ol)
131+
{
132+
// ### Declare all of this object's options here.
133+
// ### For the "flags" of each option, you should typically specify
134+
// ### one of OptionBase::buildoption, OptionBase::learntoption or
135+
// ### OptionBase::tuningoption. If you don't provide one of these three,
136+
// ### this option will be ignored when loading values from a script.
137+
// ### You can also combine flags, for example with OptionBase::nosave:
138+
// ### (OptionBase::buildoption | OptionBase::nosave)
139+
140+
declareOption(ol, "sigma", &AdditiveGaussianNoiseVariable::sigma,
141+
OptionBase::buildoption,
142+
"The standard deviation of the isotropic Gaussian noise to be added to the input");
143+
declareOption(ol, "random_gen", &AdditiveGaussianNoiseVariable::random_gen,
144+
OptionBase::buildoption,
145+
"Random number generator. If null, the PRandom::common(false) generator will be used.");
146+
147+
// Now call the parent class' declareOptions
148+
inherited::declareOptions(ol);
149+
}
150+
151+
void AdditiveGaussianNoiseVariable::build_()
152+
{
153+
// ### This method should do the real building of the object,
154+
// ### according to set 'options', in *any* situation.
155+
// ### Typical situations include:
156+
// ### - Initial building of an object from a few user-specified options
157+
// ### - Building of a "reloaded" object: i.e. from the complete set of
158+
// ### all serialised options.
159+
// ### - Updating or "re-building" of an object after a few "tuning"
160+
// ### options have been modified.
161+
// ### You should assume that the parent class' build_() has already been
162+
// ### called.
163+
}
164+
165+
166+
} // end of namespace PLearn
167+
168+
169+
/*
170+
Local Variables:
171+
mode:c++
172+
c-basic-offset:4
173+
c-file-style:"stroustrup"
174+
c-file-offsets:((innamespace . 0)(inline-open . 0))
175+
indent-tabs-mode:nil
176+
fill-column:79
177+
End:
178+
*/
179+
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// -*- C++ -*-
2+
3+
// AdditiveGaussianNoiseVariable.h
4+
//
5+
// Copyright (C) 2010 Pascal Vincent
6+
//
7+
// Redistribution and use in source and binary forms, with or without
8+
// modification, are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright
14+
// notice, this list of conditions and the following disclaimer in the
15+
// documentation and/or other materials provided with the distribution.
16+
//
17+
// 3. The name of the authors may not be used to endorse or promote
18+
// products derived from this software without specific prior written
19+
// permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23+
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24+
// NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
//
32+
// This file is part of the PLearn library. For more information on the PLearn
33+
// library, go to the PLearn Web site at www.plearn.org
34+
35+
// Authors: Pascal Vincent
36+
37+
/*! \file AdditiveGaussianNoiseVariable.h */
38+
39+
40+
#ifndef AdditiveGaussianNoiseVariable_INC
41+
#define AdditiveGaussianNoiseVariable_INC
42+
43+
#include <plearn/var/UnaryVariable.h>
44+
#include <plearn/math/PRandom.h>
45+
46+
namespace PLearn {
47+
using namespace std;
48+
49+
/*! * AdditiveGaussianNoiseVariable * */
50+
51+
/**
52+
* The first sentence should be a BRIEF DESCRIPTION of what the class does.
53+
* Place the rest of the class programmer documentation here. Doxygen supports
54+
* Javadoc-style comments. See http://www.doxygen.org/manual.html
55+
*
56+
* @todo Write class to-do's here if there are any.
57+
*
58+
* @deprecated Write deprecated stuff here if there is any. Indicate what else
59+
* should be used instead.
60+
*/
61+
class AdditiveGaussianNoiseVariable : public UnaryVariable
62+
{
63+
typedef UnaryVariable inherited;
64+
65+
public:
66+
//##### Public Build Options ############################################
67+
68+
//! ### declare public option fields (such as build options) here
69+
//! Start your comments with Doxygen-compatible comments such as //!
70+
PP<PRandom> random_gen;
71+
real sigma;
72+
73+
public:
74+
//##### Public Member Functions #########################################
75+
76+
//! Default constructor, usually does nothing
77+
AdditiveGaussianNoiseVariable();
78+
79+
// ### If your class has parameters, you probably want a constructor that
80+
// ### initializes them
81+
// AdditiveGaussianNoiseVariable(Variable* input, param_type the_parameter, ...);
82+
83+
// Your other public member functions go here
84+
85+
//##### PLearn::Variable methods #########################################
86+
// (PLEASE IMPLEMENT IN .cc)
87+
virtual void recomputeSize(int& l, int& w) const;
88+
virtual void fprop();
89+
virtual void bprop();
90+
91+
// ### These ones are not always implemented
92+
// virtual void bbprop();
93+
// virtual void symbolicBprop();
94+
// virtual void rfprop();
95+
96+
//##### PLearn::Object Protocol #########################################
97+
98+
// Declares other standard object methods.
99+
// ### If your class is not instantiatable (it has pure virtual methods)
100+
// ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
101+
PLEARN_DECLARE_OBJECT(AdditiveGaussianNoiseVariable);
102+
103+
// Simply calls inherited::build() then build_()
104+
virtual void build();
105+
106+
//! Transforms a shallow copy into a deep copy
107+
// (PLEASE IMPLEMENT IN .cc)
108+
virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
109+
110+
protected:
111+
//##### Protected Options ###############################################
112+
113+
// ### Declare protected option fields (such as learned parameters) here
114+
// ...
115+
116+
// will contain true where features have been corrupted
117+
TVec<bool> corrupted;
118+
119+
protected:
120+
//##### Protected Member Functions ######################################
121+
122+
//! Declares the class options.
123+
// (PLEASE IMPLEMENT IN .cc)
124+
static void declareOptions(OptionList& ol);
125+
126+
private:
127+
//##### Private Member Functions ########################################
128+
129+
//! This does the actual building.
130+
// (PLEASE IMPLEMENT IN .cc)
131+
void build_();
132+
133+
private:
134+
//##### Private Data Members ############################################
135+
136+
// The rest of the private stuff goes here
137+
};
138+
139+
// Declares a few other classes and functions related to this class
140+
DECLARE_OBJECT_PTR(AdditiveGaussianNoiseVariable);
141+
142+
// ### Put here a convenient method for building your variable.
143+
// ### e.g., if your class is TotoVariable, with two parameters foo_type foo
144+
// ### and bar_type bar, you could write:
145+
// inline Var toto(Var v, foo_type foo=default_foo, bar_type bar=default_bar)
146+
// { return new TotoVariable(v, foo, bar); }
147+
148+
} // end of namespace PLearn
149+
150+
#endif
151+
152+
153+
/*
154+
Local Variables:
155+
mode:c++
156+
c-basic-offset:4
157+
c-file-style:"stroustrup"
158+
c-file-offsets:((innamespace . 0)(inline-open . 0))
159+
indent-tabs-mode:nil
160+
fill-column:79
161+
End:
162+
*/
163+
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :

0 commit comments

Comments
 (0)