Skip to content

Instantly share code, notes, and snippets.

View AlexanderSavochkin's full-sized avatar

Alexander Savochkin AlexanderSavochkin

View GitHub Profile
@AlexanderSavochkin
AlexanderSavochkin / essentials-mlr.h
Last active November 30, 2015 05:04
Multinomial logistic regression model header file
class MultinomialLogRelModel
{
//Основные параметры модели
int num_classes; //Количество классов
int num_sample_features; //Количество признаков в объектах
valarray<double> model_weights; //Веса модели
double regularization_coeff; //Коэффициент регуляризации
double learning_rate; //Параметр шага в методе градиентного спуска
public:
...
@AlexanderSavochkin
AlexanderSavochkin / mlt_gradient_for_sample.cpp
Created December 3, 2015 07:26
Piece of code for calculation of likehood gradient in multinomial logistic regression for one training sample
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);
//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)
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);
}
@AlexanderSavochkin
AlexanderSavochkin / blink-lpc1114.s
Last active March 25, 2016 17:16
Simple LED blink program for LPC1114 in assembly
@AlexanderSavochkin
AlexanderSavochkin / blink-lpc1114.ld
Last active March 25, 2016 07:49
LPC1114 minimalistic linker script
@AlexanderSavochkin
AlexanderSavochkin / uber-install-213.sh
Created April 12, 2016 03:23 — forked from dannguyen/uber-install-213.sh
Make everything install on Ubuntu 14.04 and 14.10, specifically for AWS and DigitalOcean
## 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
@AlexanderSavochkin
AlexanderSavochkin / blink-lpc1114.c
Last active April 21, 2016 16:29
LED blink with LPC1114 in C
@AlexanderSavochkin
AlexanderSavochkin / startup-lpc1114.s
Last active April 15, 2016 04:40
LPC1114 startup code in assembly
.syntax unified
/*Инструктируем транслятор генерировать 16-битные инструкции Thumb*/
.thumb
/*То же самое что ключ командной строки -mcpu=cortex-m0 .
Говорим транслятору, что хотим код под архитектуру Cortex-M0*/
.cpu cortex-m0
/* Опрделяем секцию .vectors, в которой описываем таблицу прерываний */
@AlexanderSavochkin
AlexanderSavochkin / startup-lpc1114.c
Created April 15, 2016 18:39
Startup code for lpc1114 MCU in C
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 */