forked from shogun-toolbox/shogun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron.cpp
92 lines (77 loc) · 2.18 KB
/
Perceptron.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Sergey Lisitsyn, Giovanni De Toni,
* Michele Mazzoni, Heiko Strathmann, Fernando Iglesias
*/
#include <shogun/base/progress.h>
#include <shogun/base/range.h>
#include <shogun/classifier/Perceptron.h>
#include <shogun/features/iterators/DotIterator.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/Labels.h>
#include <shogun/lib/Signal.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
using namespace shogun;
CPerceptron::CPerceptron() : CIterativeMachine<CLinearMachine>()
{
m_max_iterations = 1000;
learn_rate = 0.1;
m_initialize_hyperplane = true;
SG_ADD(
&m_initialize_hyperplane, "initialize_hyperplane",
"Whether to initialize hyperplane.", ParameterProperties::HYPER);
SG_ADD(&learn_rate, "learn_rate", "Learning rate.", ParameterProperties::HYPER);
}
CPerceptron::~CPerceptron()
{
}
void CPerceptron::init_model(CFeatures* data)
{
if (data)
{
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n")
set_features((CDotFeatures*) data);
}
int32_t num_feat = features->get_dim_feature_space();
SGVector<float64_t> w;
if (m_initialize_hyperplane)
{
w = SGVector<float64_t>(num_feat);
set_w(w);
//start with uniform w, bias=0
w.set_const(1.0 / num_feat);
bias=0;
}
}
void CPerceptron::iteration()
{
bool converged = true;
SGVector<float64_t> w = get_w();
auto labels = binary_labels(m_labels)->get_int_labels();
auto iter_train_labels = labels.begin();
for (const auto& v : DotIterator(features))
{
const auto true_label = *(iter_train_labels++);
auto predicted_label = v.dot(w) + bias;
if (CMath::sign<float64_t>(predicted_label) != true_label)
{
converged = false;
const auto gradient = learn_rate * true_label;
put("bias", bias + gradient);
v.add(gradient, w);
put("w", w);
}
}
m_complete = converged;
}
void CPerceptron::set_initialize_hyperplane(bool initialize_hyperplane)
{
m_initialize_hyperplane = initialize_hyperplane;
}
bool CPerceptron::get_initialize_hyperplane()
{
return m_initialize_hyperplane;
}