Skip to content

Instantly share code, notes, and snippets.

View bzamecnik's full-sized avatar

Bohumír Zámečník bzamecnik

View GitHub Profile
@bzamecnik
bzamecnik / keras_imdb_words_from_indexes.py
Created January 2, 2018 15:26
Keras IMDB - words from indexes
from __future__ import print_function
from keras.datasets imporrt imdb
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=20000)
x_train = sequence.pad_sequences(x_train, maxlen=80)
word_index = keras.datasets.imdb.get_word_index()
# or items() in Python 3
inverted_word_index = {index:word for (word, index) in word_index.iteritems()}
@bzamecnik
bzamecnik / install_cuda_8_on_aws_ec2.md
Created December 21, 2017 16:25
Install on AWS G3 K80: TensorFlow 1.4, CUDA 8.0, CuDNN 6, nvprof

TensorFlow 1.4 - CUDA 8

Driver:

  • Version: 384.81
  • Release Date: 2017.9.25
  • Operating System: Linux 64-bit Ubuntu 16.04
# CUDA Drivers
# http://www.nvidia.com/download/driverResults.aspx/124729/en-us
@bzamecnik
bzamecnik / keras_ms_progbar.patch
Created December 15, 2017 03:57
Keras progress bar with millisecond precision by default (for both epoch and step time).
diff --git a/keras/utils/generic_utils.py b/keras/utils/generic_utils.py
index 6a97448..d1db340 100644
--- a/keras/utils/generic_utils.py
+++ b/keras/utils/generic_utils.py
@@ -292,7 +292,7 @@ class Progbar(object):
self.seen_so_far = current
now = time.time()
- info = ' - %.0fs' % (now - self.start)
+ info = ' - %.0f ms' % (1e3 * (now - self.start))
@bzamecnik
bzamecnik / nvprof_training.sh
Created December 12, 2017 04:35
Run nvprof for some ML model training and save db with commit and date.
CUDA_VISIBLE_DEVICES=0 \
/usr/local/cuda/bin/nvprof \
-o model_$(git rev-parse --short HEAD)_$(date +%Y%m%d-%H%M%S).sqlite \
python train.py
@bzamecnik
bzamecnik / key_distance.py
Created November 12, 2017 00:23
Distance of pitch class to key tonic with respect to dissonance
# https://github.com/bzamecnik/ideas/blob/master/key_coloring.md
import numpy as np
def key_distance(pitch_class, key):
"""
Distance between a pitch class and tonic of a key
which roughly approximates dissonance.
Eg. in key of C the pitch classes sorted from most consonant
# https://github.com/fchollet/keras/pull/8286
#
# An example how pass additional substitutions to the training function
# via TensorFlow feed_dict argument to tf.Session.run().
#
# Note that `feed_dict` keys are `tf.Placeholder`s and values can be
# ordinary numpy arrays or other Python values.
#
# We pass additional arguments to model.compile() -> K.function() as **kwargs.
# The trick is that the feed_dict is passed as a reference, ie. even though
# Is it possible to utilize Keras callbacks to encapsulate the logic? Yes.
#
# We decouple feeding inputs from StagingArea.put() - both can be called in
# a separate Session.run(). Thus it's not needed to hack Keras inputs too much.
# Instead in one run() we assign a numpy array to a Variable (via feed_dict)
# and in another run() we perform StagingArea.put().
#
# We make a callback StagingAreaCallback which perform the initial assign and put()
# in its on_epoch_begin() method. Then in each on_batch_begin() it just runs an
# assign. Then get() and put() is ran by Keras in the training function.
# GTX 980 Ti
# plain: 68.50 images/sec
# pipeline: 68.71 images/sec
import math
import tensorflow as tf
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Dense, Input, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.utils import to_categorical
# It works!
#
# GTX 980 Ti
# plain model: ~14370 images/sec
# prefetch model: ~14670 images/sec
#
# In nvprof we can see that that HtoD memcpy is really async!
# What remains is just sync feed_dict to move from numpy to a CPU Variable.
import math
import numpy as np
import tensorflow as tf
from tensorflow.python.ops.data_flow_ops import StagingArea
dataset_range = tf.contrib.data.Dataset.range(10)
iter = dataset_range.make_one_shot_iterator()
next_item = iter.get_next()
area = StagingArea(dtypes=[tf.int64])
area_put = area.put([next_item])