This file contains 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
""" | |
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 |
This file contains 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
""" | |
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 |
This file contains 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
""" | |
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 |
This file contains 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
""" | |
An example of usage of __new__ magic method to implement dynamic computational | |
logic dispatching. | |
For more information see: https://iliazaitsev.me/blog/2018/02/14/python-new | |
""" | |
import abc | |
class TemperatureConverter(metaclass=abc.ABCMeta): |
This file contains 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 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': |
This file contains 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 _CelsiusConverter(TemperatureConverter): | |
""" | |
Concrete implementation of temperature converted which converts from Kelvin | |
into Celsius degrees. | |
""" | |
symbol = '°C' | |
def _convert(self, value): | |
return value - 273.15 |
This file contains 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
""" | |
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 |
This file contains 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
""" | |
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 |
This file contains 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 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 |
This file contains 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
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( |
OlderNewer