This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultinomialLogRelModel | |
{ | |
//Основные параметры модели | |
int num_classes; //Количество классов | |
int num_sample_features; //Количество признаков в объектах | |
valarray<double> model_weights; //Веса модели | |
double regularization_coeff; //Коэффициент регуляризации | |
double learning_rate; //Параметр шага в методе градиентного спуска | |
public: | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MultinomialLogRelModel::gradientForSample(const valarray<double>& sample_features, int label, valarray<double>& gradient) const | |
{ | |
double sum_exp = 0.0; | |
valarray<double> features(0.0, (num_sample_features + 1) * num_classes); | |
valarray<double> gradient_add(0.0, (num_sample_features + 1) * num_classes); | |
for (int k = 0; k < num_classes; ++k) | |
{ | |
featurize(sample_features, k, features); | |
double wf = inner_product(begin(model_weights), end(model_weights), begin(features), 0.0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Logistic regression example from Saprk documentation | |
val points = spark.textFile(...).map(parsePoint).cache() | |
var w = Vector.random(D) // current separating plane | |
for (i <- 1 to ITERATIONS) { | |
val gradient = points.map(p => | |
(1 / (1 + exp(-p.y*(w dot p.x))) - 1) * p.y * p.x | |
).reduce(_ + _) | |
w -= gradient | |
} | |
println("Final separating plane: " + w) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MultinomialLogRelModel::featurize(const valarray<double>& sample_features, int label, valarray<double>& modelFeatures) const | |
{ | |
for (int k = 0; k < num_classes; ++k) //Loop through classes | |
{ | |
for (int i = 0; i < num_sample_features; ++i ) //loop through features | |
{ | |
modelFeatures[k * (num_sample_features + 1) + i] = (k == label ? sample_features[i] : 0.0); | |
} | |
modelFeatures[(k + 1) * (num_sample_features + 1) - 1] = (k == label ? 1.0 : 0.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.syntax unified | |
/*Инструктируем транслятор генерировать 16-битные инструкции Thumb*/ | |
.thumb | |
/*То же самое что ключ командной строки -mcpu=cortex-m0 . | |
Говорим транслятору, что хотим код под архитектуру Cortex-M0*/ | |
.cpu cortex-m0 | |
/* Опрделяем секцию .vectors, в которой описываем таблицу прерываний */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* useful reference: www.linuxselfhelp.com/gnu/ld/html_chapter/ld_toc.html */ | |
MEMORY | |
{ | |
flash : org = 0x00000000, len = 32k | |
} | |
SECTIONS | |
{ | |
. = ORIGIN(flash); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Tested on 14.04 and 14.10. Runs as root because why not | |
## Installs: | |
# - Ubuntu developer tools | |
# - Python (using system 2.7.6 / 2.7.8) | |
# - scipy libs | |
# - scikit-learn | |
# - nltk | |
# - Ruby 2.1.5 | |
# - rbenv | |
# - nokogiri |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define REGISTER_32(ADDRESS) (*((volatile unsigned int *)(ADDRESS))) | |
#define SYSAHBCLKCTRL REGISTER_32(0x40048080) | |
#define GPIO0DATA REGISTER_32(0x50003ffc) | |
#define GPIO0DIR REGISTER_32(0x50008000) | |
int main() | |
{ | |
// Turn on clock for GPIO and IOCON | |
SYSAHBCLKCTRL |= (1 << 6) + (1 << 16); | |
GPIO0DIR = (1 << 7); // Make PIO0_7 an output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.syntax unified | |
/*Инструктируем транслятор генерировать 16-битные инструкции Thumb*/ | |
.thumb | |
/*То же самое что ключ командной строки -mcpu=cortex-m0 . | |
Говорим транслятору, что хотим код под архитектуру Cortex-M0*/ | |
.cpu cortex-m0 | |
/* Опрделяем секцию .vectors, в которой описываем таблицу прерываний */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Default_Handler(void); | |
void init(); | |
const void * Vectors[] __attribute__((section(".vectors"))) ={ | |
(void *)0x10002000, /* Top of stack */ | |
init, /* Reset Handler */ | |
Default_Handler, /* NMI */ | |
Default_Handler, /* Hard Fault */ | |
0, /* Reserved */ | |
0, /* Reserved */ |