Skip to content

Instantly share code, notes, and snippets.

View devforfu's full-sized avatar

Ilia devforfu

View GitHub Profile
@devforfu
devforfu / cartpole.py
Last active August 23, 2016 05:54
Cart Pole Balancing
"""
Combination of Sutton's approach dividing space into boxes with simple
TD-learning algorithm (see basic_rl.py somewhere on gym portal).
Some simulation paramteres are hardcoded and learner is not universal.
"""
from collections import defaultdict
import math
@devforfu
devforfu / cartpole_continuous.py
Last active August 23, 2016 12:09
Cart Pole balancing using randomized strategy
"""
Cart pole environment learner that uses something like Cross Entropy Method.
"Something like" b/c not sure if implemented it correctly. And it is EXTREMELY slow. Anyway,
looks like some kind of randomized search.
"""
from operator import itemgetter
import heapq
@devforfu
devforfu / word2vec_error.py
Created November 29, 2017 07:37
Word2Vec SkipGrams faulty code
"""
A source code converted from:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/5_word2vec.ipynb
Into single script. But somehow, this implementation doesnt' show any improvement after 100000 iterations.
"""
import os
import math
@devforfu
devforfu / new_simple_step1.py
Last active February 14, 2018 13:05
Simple Example: Step 1
class TemperatureConverter(metaclass=abc.ABCMeta):
"""
Base class of temperature converters which supports dynamic substitution
of implementations.
"""
symbol = 'K'
def __new__(cls, convert_to='celsius'):
if issubclass(cls, TemperatureConverter):
if convert_to == 'celsius':
class _CelsiusConverter(TemperatureConverter):
"""
Concrete implementation of temperature converted which converts from Kelvin
into Celsius degrees.
"""
symbol = '°C'
def _convert(self, value):
return value - 273.15
@devforfu
devforfu / new_involved.py
Created February 15, 2018 09:42
Serving notifications from local and remote data sources
"""
A bit more involved demonstration of __new__ method usage.
For more information see: https://iliazaitsev.me/blog/2018/02/14/python-new
"""
import io
import abc
import json
from os.path import exists
from textwrap import dedent
@devforfu
devforfu / generators1.py
Created March 28, 2018 15:19
Generators example (1)
"""
Simple example shows usage of generators as coroutines accepting values from
outside and yield processed values to caller.
"""
import re
def get_loss(regex='^[\d\w]+(\d.\d+).hdf5$'):
"""
Checks if name of file with model weights matches regular expression and
@devforfu
devforfu / features_extractor_pseudocode.py
Created April 8, 2018 09:29
For Medium post on Dog Breed Identification
class FeaturesExtractor:
"""Runs pretrained model without top layers on dataset and saves generated
bottleneck features onto disk.
"""
def __init__(self, build_fn, preprocess_fn, source,
target_size=(299, 299, 3), batch_size=128):
self.build_fn = build_fn
self.preprocess_fn = preprocess_fn
self.source = source
@devforfu
devforfu / sgd_training.py
Created April 8, 2018 10:09
SGD training snippet for Medium post
def sgd(x_train, y_train, x_valid, y_valid, variance_threshold=0.1):
threshold = VarianceThreshold(variance_threshold)
sgd_classifier = SGDClassifier(
alpha=1./len(x_train),
class_weight='balanced',
loss='log', penalty='elasticnet',
fit_intercept=False, tol=0.001, n_jobs=-1)
bagging = BaggingClassifier(
@devforfu
devforfu / tf_oop_init.py
Created April 18, 2018 04:41
DNNClassifier Init
class DNNClassifier:
def __init__(self, hidden_layers, n_classes, activation=tf.nn.elu):
self.hidden_layers = hidden_layers
self.n_classes = n_classes
self.activation = activation
self._graph = None
self._session = None
self._saver = None
self.stop_training = False