Skip to content

Instantly share code, notes, and snippets.

View ageron's full-sized avatar

Aurélien Geron ageron

View GitHub Profile
>>> 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 / stereogram.py
Created March 11, 2019 07:06
Converts one or more 3D images for red/blue 3D glasses into stereogram images
"""
Converts one or more 3D images for red/blue 3D glasses into stereogram images. Example:
Download:
https://www.nasa.gov/sites/default/files/styles/full_width/public/thumbnails/image/nh-ut_stereo_bluered_030619.png
Then run:
python stereogram.py nh-ut_stereo_bluered_030619.png
This will generate:
@ageron
ageron / custom_train_test.py
Created March 25, 2019 15:06
Test of a custom training loop
import numpy as np
import tensorflow as tf
from tensorflow import keras
model = keras.models.Sequential([keras.layers.Dense(1, input_shape=[5])])
optimizer = keras.optimizers.SGD()
def step(model, optimizer, X_batch, y_batch):
with tf.GradientTape() as tape:
y_pred = model(X_batch)