Skip to content

Instantly share code, notes, and snippets.

View ageron's full-sized avatar

Aurélien Geron ageron

View GitHub Profile
@ageron
ageron / session_output_tensorflow_issue_24632.pycon
Created December 30, 2018 16:11
Python session for TensorFlow issue 24632
(tf2) ageron@macmix:~/dev/py/ml/kiwisoft_courses/dl_course$ python
Python 3.6.6 (default, Jun 28 2018, 05:43:53)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> import numpy as np
>>> import tensorflow as tf
>>> from tensorflow import keras
>>>
>>> X_train = np.random.rand(1000, 10)
import tensorflow as tf
tf.enable_eager_execution()
import tensorflow.contrib.eager as tfe
# predict the future
tf.function = tfe.defun # I vote for that name
tf.method = tfe.defun # Okay for what we will use it for
tf.Checkpointable = tfe.Checkpointable
from __future__ import division, print_function
import tensorflow as tf
"""
This program tries to test whether or not TensorFlow implements an inter-op thread pool on GPUs. In other words,
it checks whether or not operations that don't depend on each other can actually run in parallel.
To check this, it creates a TensorFlow graph that computes 1 + 1/2 + 1/4 + 1/8 + ...
There are two variables `x` and `y`, and two operations that modify these variables:
* `add` computes x <- x + y
* `divide` computes y <- y / 2
@ageron
ageron / batch_norm_variable_names.txt
Created June 1, 2017 07:15
Batch norm variable names in TensorFlow
>>> import tensorflow as tf
>>> X = tf.placeholder(shape=[None, 5], dtype=tf.float32, name="X")
>>> with tf.variable_scope("layer1"):
... hidden1 = tf.layers.dense(X, 100, name="hidden1")
... bn1 = tf.layers.batch_normalization(hidden1)
...
>>> for v in tf.global_variables():
... print(v.name)
...
layer1/hidden1/kernel:0
from __future__ import division, print_function
import numpy as np
women_ratio = 0.513 # ratio of women in the US
min_ratio = 0.49 # roughly 5% more than women_ratio
max_ratio = 0.54 # roughly 5% less than women_ratio
sample_size = 1000
n_samples = 10000
####################################
>>> from mongoengine import *
>>> class FacebookUser(Document):
... meta = { "allow_inheritance": False }
... facebook_id = StringField(primary_key = True, max_length = 20)
... name = StringField()
... def __unicode__(self):
... return u"/".join((self.facebook_id, self.name or u""))
...
>>> connect("test")
Connection('localhost', 27017)
>>> from mongoengine import *
>>> class FacebookUser(Document):
... meta = { "allow_inheritance": False }
... facebook_id = StringField(primary_key = True, max_length = 20)
...
>>> connect("test")
Connection('localhost', 27017)
>>> FacebookUser.objects.delete()
>>> FacebookUser.objects
[]
@ageron
ageron / nth_percentile.py
Created March 21, 2013 11:38
Explanation of 90th percentile
def nth_percentile(dataset, percentile = 90):
sorted_dataset = sorted(dataset)
new_length = len(sorted_dataset) * percentile / 100
return sorted_dataset[0:new_length]
def mean(dataset):
return sum(dataset)/float(len(dataset))
dataset = [5, 9, 7, 101, 4, 8, 109, 104, 6, 1, 110, 106, 3, 107, 105, 2, 102, 10, 103, 108]
percentile_90 = nth_percentile(dataset)