A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d5/db0/adaline__learning_8cpp_source.html below:

TheAlgorithms/C++: machine_learning/adaline_learning.cpp Source File

55 explicit adaline

(

int

num_features,

const double eta

= 0.01f,

59

std::cerr <<

"learning rate should be positive and nonzero" 61

std::exit(EXIT_FAILURE);

69 for

(

double

&weight :

weights

) weight = 1.f;

78 for

(

int

i = 0; i < ada.

weights

.size(); i++) {

80 if

(i < ada.

weights

.size() - 1) {

95 int predict

(

const

std::vector<double> &x,

double

*out =

nullptr

) {

103

y = std::inner_product(x.begin(), x.end(),

weights

.begin(), y);

105 if

(out !=

nullptr

) {

119 double fit

(

const

std::vector<double> &x,

const int

&y) {

126 int

prediction_error = y - p;

127 double

correction_factor =

eta

* prediction_error;

130 for

(

int

i = 0; i < x.size(); i++) {

131 weights

[i] += correction_factor * x[i];

133 weights

[x.size()] += correction_factor;

135 return

correction_factor;

145 void fit

(std::array<std::vector<double>, N>

const

&X,

146

std::array<int, N>

const

&Y) {

147 double

avg_pred_error = 1.f;

152

avg_pred_error = 0.f;

155 for

(

int

i = 0; i < N; i++) {

156 double

err =

fit

(X[i], Y[i]);

157

avg_pred_error += std::abs(err);

163

std::cout <<

"\tIter "

<< iter <<

": Training weights: "

<< *

this 164

<<

"\tAvg error: "

<< avg_pred_error << std::endl;

168

std::cout <<

"Converged after "

<< iter <<

" iterations." 171

std::cout <<

"Did not converge after "

<< iter <<

" iterations." 197 if

(x.size() != (

weights

.size() - 1)) {

198

std::cerr << __func__ <<

": " 199

<<

"Number of features in x does not match the feature " 200 "dimension in model!" 229

std::array<std::vector<double>, N> X = {

230

std::vector<double>({0, 1}), std::vector<double>({1, -2}),

231

std::vector<double>({2, 3}), std::vector<double>({3, -1}),

232

std::vector<double>({4, 1}), std::vector<double>({6, -5}),

233

std::vector<double>({-7, -3}), std::vector<double>({-8, 5}),

234

std::vector<double>({-9, 2}), std::vector<double>({-10, -15})};

235

std::array<int, N> y = {1, -1, 1, -1, -1,

238

std::cout <<

"------- Test 1 -------"

<< std::endl;

239

std::cout <<

"Model before fit: "

<< ada << std::endl;

242

std::cout <<

"Model after fit: "

<< ada << std::endl;

244 int

predict = ada.

predict

({5, -3});

245

std::cout <<

"Predict for x=(5,-3): "

<< predict;

246

assert(predict == -1);

247

std::cout <<

" ...passed"

<< std::endl;

249

predict = ada.

predict

({5, 8});

250

std::cout <<

"Predict for x=(5,8): "

<< predict;

251

assert(predict == 1);

252

std::cout <<

" ...passed"

<< std::endl;

267

std::array<std::vector<double>, N> X;

268

std::array<int, N> Y{};

273 int

range2 = range >> 1;

274 for

(

int

i = 0; i < N; i++) {

275 double

x0 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

276 double

x1 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

277

X[i] = std::vector<double>({x0, x1});

278

Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;

281

std::cout <<

"------- Test 2 -------"

<< std::endl;

282

std::cout <<

"Model before fit: "

<< ada << std::endl;

285

std::cout <<

"Model after fit: "

<< ada << std::endl;

287 int

N_test_cases = 5;

288 for

(

int

i = 0; i < N_test_cases; i++) {

289 double

x0 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

290 double

x1 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

292 int

predict = ada.

predict

({x0, x1});

294

std::cout <<

"Predict for x=("

<< x0 <<

","

<< x1 <<

"): "

<< predict;

296 int

expected_val = (x0 + 3. * x1) > -1 ? 1 : -1;

297

assert(predict == expected_val);

298

std::cout <<

" ...passed"

<< std::endl;

318

std::array<std::vector<double>, N> X;

319

std::array<int, N> Y{};

324 int

range2 = range >> 1;

325 for

(

int

i = 0; i < N; i++) {

326 double

x0 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

327 double

x1 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

328 double

x2 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

329

X[i] = std::vector<double>({x0, x1, x2, x0 * x0, x1 * x1, x2 * x2});

330

Y[i] = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;

333

std::cout <<

"------- Test 3 -------"

<< std::endl;

334

std::cout <<

"Model before fit: "

<< ada << std::endl;

337

std::cout <<

"Model after fit: "

<< ada << std::endl;

339 int

N_test_cases = 5;

340 for

(

int

i = 0; i < N_test_cases; i++) {

341 double

x0 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

342 double

x1 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

343 double

x2 = (

static_cast<double>

(std::rand() % range) - range2) / 100.f;

345 int

predict = ada.

predict

({x0, x1, x2, x0 * x0, x1 * x1, x2 * x2});

347

std::cout <<

"Predict for x=("

<< x0 <<

","

<< x1 <<

","

<< x2

350 int

expected_val = ((x0 * x0) + (x1 * x1) + (x2 * x2)) <= 1.f ? 1 : -1;

351

assert(predict == expected_val);

352

std::cout <<

" ...passed"

<< std::endl;

357int main

(

int

argc,

char

**argv) {

358

std::srand(std::time(

nullptr

));

362

eta = strtof(argv[1],

nullptr

);

367

std::cout <<

"Press ENTER to continue..."

<< std::endl;

372

std::cout <<

"Press ENTER to continue..."

<< std::endl;

adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)

adaline(int num_features, const double eta=0.01f, const double accuracy=1e-5)

const double eta

learning rate of the algorithm

std::vector< double > weights

weights of the neural network

double fit(const std::vector< double > &x, const int &y)

void fit(std::array< std::vector< double >, N > const &X, std::array< int, N > const &Y)

const double accuracy

model fit convergence accuracy

int predict(const std::vector< double > &x, double *out=nullptr)

bool check_size_match(const std::vector< double > &x)

friend std::ostream & operator<<(std::ostream &out, const adaline &ada)

static void test2()

Self-implementations, 2nd test.

static void test1()

Self-test implementations, 1st test.

int main()

Main function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4