Skip to content

Instantly share code, notes, and snippets.

@ruslangrimov
ruslangrimov / keras_tf_sess_gpu_mem.py
Created May 30, 2018 02:33
Make Keras use only a fraction of GPU memory
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.33
set_session(tf.Session(config=config))
@ruslangrimov
ruslangrimov / show_base64_link.py
Created February 6, 2018 18:29
Shows base64 link to download a file from a jupyter notebook
class MyCallBack(Callback):
def __init__(self, x_v, y_v):
super(MyCallBack, self).__init__()
self.x_v, self.y_v = x_v, y_v
self.ps = []
def on_epoch_end(self, epoch, logs={}):
p = self.model.predict(self.x_v).flatten()
self.ps.append(p)
logs['val_loss'] = log_loss(self.x_y, p)
from keras import optimizers
from keras import losses
import numpy as np
input_img_data = np.random.random((1,) + K.int_shape(model.inputs[0])[1:])
input_img = K.variable(input_img_data)
inp = Input(tensor=input_img, batch_shape=input_img_data.shape)
out = model(inp)
@ruslangrimov
ruslangrimov / keras-tensorflow-model-profiling.py
Last active October 15, 2019 20:51
Profiling a Keras-TensorFlow model
import tensorflow as tf
from tensorflow.python.client import timeline
from keras import backend as K
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
model = ... # A Keras model
fn = K.function(model.inputs, model.outputs, options=run_options, run_metadata=run_metadata)
@ruslangrimov
ruslangrimov / perlin_noise.py
Last active October 27, 2017 12:19
A python realization of the 2D Perlin noise
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from itertools import product, count
# generate uniform unit vectors
def generate_grid_vectors(n):
'Generates matrix NxN of unit length vectors'
v = np.random.uniform(-1, 1, (n, n, 2))