Skip to content

Instantly share code, notes, and snippets.

View ageron's full-sized avatar

Aurélien Geron ageron

View GitHub Profile
@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)
>>> 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
[]
>>> 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 __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
####################################
@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 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
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
@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)
@ageron
ageron / list_procs.py
Created January 16, 2019 03:20
A few utility functions to list procs by name or by their command line arguments
"""
Usage:
>>> for pid, name in search_procs_by_name("python").items():
... print(pid, name)
...
11882 python3.6
47599 python3.6
51877 python3.6
51924 python3.6
@ageron
ageron / tf2-colab.sh
Created January 21, 2019 08:21
Installs everything you need to run TF 2.0-preview on an Ubuntu 18.04 server (includes the Colab connector) - run as regular user with sudo rights
#!/bin/bash
cat <<EOF > install_cuda_10_and_nvidia_driver_384.sh
#!/bin/bash
apt-get update && apt-get install -y --no-install-recommends gnupg2 curl ca-certificates && \
curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub | apt-key add - && \
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \
echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list
export CUDA_VERSION=10.0.130