Skip to content

Commit 26cc883

Browse files
Add TensorFlow v2 examples (#302)
* add tf v2 examples * update README * add README * update doc
1 parent 573b050 commit 26cc883

16 files changed

+4207
-3
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# TensorFlow Examples
22

3-
This tutorial was designed for easily diving into TensorFlow, through examples. For readability, it includes both notebooks and source codes with explanation.
3+
This tutorial was designed for easily diving into TensorFlow, through examples. For readability, it includes both notebooks and source codes with explanation for both TF v1 & v2.
44

55
It is suitable for beginners who want to find clear and concise examples about TensorFlow. Besides the traditional 'raw' TensorFlow implementations, you can also find the latest TensorFlow API practices (such as `layers`, `estimator`, `dataset`, ...).
66

7-
**Update (07/25/2018):** Add new examples (GBDT, Word2Vec) + TF1.9 compatibility! (TF v1.9+ recommended).
7+
**Update (04/03/2019):** Starting to add [TensorFlow v2 examples](tensorflow_v2)! (more coming soon).
88

99
*If you are using older TensorFlow version (0.11 and under), please take a [look here](https://github.com/aymericdamien/TensorFlow-Examples/tree/0.11).*
1010

@@ -61,11 +61,15 @@ It is suitable for beginners who want to find clear and concise examples about T
6161
- **Basic Operations on multi-GPU** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/6_MultiGPU/multigpu_basics.ipynb)) ([code](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/6_MultiGPU/multigpu_basics.py)). A simple example to introduce multi-GPU in TensorFlow.
6262
- **Train a Neural Network on multi-GPU** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/6_MultiGPU/multigpu_cnn.ipynb)) ([code](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/6_MultiGPU/multigpu_cnn.py)). A clear and simple TensorFlow implementation to train a convolutional neural network on multiple GPUs.
6363

64+
## TensorFlow v2
65+
66+
The tutorial index for TF v2 is available here: [TensorFlow v2 Examples](tensorflow_v2).
67+
6468
## Dataset
6569
Some examples require MNIST dataset for training and testing. Don't worry, this dataset will automatically be downloaded when running examples.
6670
MNIST is a database of handwritten digits, for a quick description of that dataset, you can check [this notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/0_Prerequisite/mnist_dataset_intro.ipynb).
6771

68-
Official Website: [http://yann.lecun.com/exdb/mnist/](http://yann.lecun.com/exdb/mnist/)
72+
Official Website: [http://yann.lecun.com/exdb/mnist/](http://yann.lecun.com/exdb/mnist/).
6973

7074
## Installation
7175

tensorflow_v2/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## TensorFlow v2 Examples
2+
3+
*** More examples to be added later... ***
4+
5+
#### 0 - Prerequisite
6+
- [Introduction to Machine Learning](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/0_Prerequisite/ml_introduction.ipynb).
7+
- [Introduction to MNIST Dataset](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/0_Prerequisite/mnist_dataset_intro.ipynb).
8+
9+
#### 1 - Introduction
10+
- **Hello World** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/1_Introduction/helloworld.ipynb)). Very simple example to learn how to print "hello world" using TensorFlow v2.
11+
- **Basic Operations** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/1_Introduction/basic_operations.ipynb)). A simple example that cover TensorFlow v2 basic operations.
12+
13+
#### 2 - Basic Models
14+
- **Linear Regression** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/2_BasicModels/linear_regression.ipynb)). Implement a Linear Regression with TensorFlow v2.
15+
- **Logistic Regression** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/2_BasicModels/logistic_regression.ipynb)). Implement a Logistic Regression with TensorFlow v2.
16+
17+
#### 3 - Neural Networks
18+
##### Supervised
19+
20+
- **Simple Neural Network** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/neural_network.ipynb)). Use TensorFlow v2 'layers' and 'model' API to build a simple neural network to classify MNIST digits dataset.
21+
- **Simple Neural Network (low-level)** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/neural_network_raw.ipynb)). Raw implementation of a simple neural network to classify MNIST digits dataset.
22+
- **Convolutional Neural Network** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/convolutional_network.ipynb)). Use TensorFlow v2 'layers' and 'model' API to build a convolutional neural network to classify MNIST digits dataset.
23+
- **Convolutional Neural Network (low-level)** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/convolutional_network_raw.ipynb)). Raw implementation of a convolutional neural network to classify MNIST digits dataset.
24+
25+
##### Unsupervised
26+
- **Auto-Encoder** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/autoencoder.ipynb)). Build an auto-encoder to encode an image to a lower dimension and re-construct it.
27+
- **DCGAN (Deep Convolutional Generative Adversarial Networks)** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/3_NeuralNetworks/tensorflow_v2/dcgan.ipynb)). Build a Deep Convolutional Generative Adversarial Network (DCGAN) to generate images from noise.
28+
29+
#### 4 - Utilities
30+
- **Save and Restore a model** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/4_Utils/save_restore_model.ipynb)). Save and Restore a model with TensorFlow v2.
31+
- **Build Custom Layers & Modules** ([notebook](https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/4_Utils/build_costum_layers.ipynb)). Learn how to build your own layers / modules and integrate them into TensorFlow v2 Models.
32+
33+
## Installation
34+
35+
To install TensorFlow v2, simply run:
36+
```
37+
pip install tensorflow==2.0.0a0
38+
```
39+
40+
or (if you want GPU support):
41+
```
42+
pip install tensorflow_gpu==2.0.0a0
43+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Machine Learning\n",
10+
"\n",
11+
"Prior to start browsing the examples, it may be useful that you get familiar with machine learning, as TensorFlow is mostly used for machine learning tasks (especially Neural Networks). You can find below a list of useful links, that can give you the basic knowledge required for this TensorFlow Tutorial.\n",
12+
"\n",
13+
"## Machine Learning\n",
14+
"\n",
15+
"- [An Introduction to Machine Learning Theory and Its Applications: A Visual Tutorial with Examples](https://www.toptal.com/machine-learning/machine-learning-theory-an-introductory-primer)\n",
16+
"- [A Gentle Guide to Machine Learning](https://blog.monkeylearn.com/a-gentle-guide-to-machine-learning/)\n",
17+
"- [A Visual Introduction to Machine Learning](http://www.r2d3.us/visual-intro-to-machine-learning-part-1/)\n",
18+
"- [Introduction to Machine Learning](http://alex.smola.org/drafts/thebook.pdf)\n",
19+
"\n",
20+
"## Deep Learning & Neural Networks\n",
21+
"\n",
22+
"- [An Introduction to Neural Networks](http://www.cs.stir.ac.uk/~lss/NNIntro/InvSlides.html)\n",
23+
"- [An Introduction to Image Recognition with Deep Learning](https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721)\n",
24+
"- [Neural Networks and Deep Learning](http://neuralnetworksanddeeplearning.com/index.html)\n",
25+
"\n"
26+
]
27+
}
28+
],
29+
"metadata": {
30+
"kernelspec": {
31+
"display_name": "IPython (Python 2.7)",
32+
"language": "python",
33+
"name": "python2"
34+
},
35+
"language_info": {
36+
"codemirror_mode": {
37+
"name": "ipython",
38+
"version": 2
39+
},
40+
"file_extension": ".py",
41+
"mimetype": "text/x-python",
42+
"name": "python",
43+
"nbconvert_exporter": "python",
44+
"pygments_lexer": "ipython2",
45+
"version": "2.7.11"
46+
}
47+
},
48+
"nbformat": 4,
49+
"nbformat_minor": 0
50+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"\n",
10+
"# MNIST Dataset Introduction\n",
11+
"\n",
12+
"Most examples are using MNIST dataset of handwritten digits. The dataset contains 60,000 examples for training and 10,000 examples for testing. The digits have been size-normalized and centered in a fixed-size image (28x28 pixels) with values from 0 to 1. For simplicity, each image has been flatten and converted to a 1-D numpy array of 784 features (28*28).\n",
13+
"\n",
14+
"## Overview\n",
15+
"\n",
16+
"![MNIST Digits](http://neuralnetworksanddeeplearning.com/images/mnist_100_digits.png)\n",
17+
"\n",
18+
"## Usage\n",
19+
"In our examples, we are using TensorFlow [input_data.py](https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/tutorials/mnist/input_data.py) script to load that dataset.\n",
20+
"It is quite useful for managing our data, and handle:\n",
21+
"\n",
22+
"- Dataset downloading\n",
23+
"\n",
24+
"- Loading the entire dataset into numpy array: \n",
25+
"\n",
26+
"\n"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"metadata": {
33+
"collapsed": true
34+
},
35+
"outputs": [],
36+
"source": [
37+
"# Import MNIST\n",
38+
"from tensorflow.examples.tutorials.mnist import input_data\n",
39+
"mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\n",
40+
"\n",
41+
"# Load data\n",
42+
"X_train = mnist.train.images\n",
43+
"Y_train = mnist.train.labels\n",
44+
"X_test = mnist.test.images\n",
45+
"Y_test = mnist.test.labels"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"- A `next_batch` function that can iterate over the whole dataset and return only the desired fraction of the dataset samples (in order to save memory and avoid to load the entire dataset)."
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"metadata": {
59+
"collapsed": true
60+
},
61+
"outputs": [],
62+
"source": [
63+
"# Get the next 64 images array and labels\n",
64+
"batch_X, batch_Y = mnist.train.next_batch(64)"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"Link: http://yann.lecun.com/exdb/mnist/"
72+
]
73+
}
74+
],
75+
"metadata": {
76+
"kernelspec": {
77+
"display_name": "Python 2",
78+
"language": "python",
79+
"name": "python2"
80+
},
81+
"language_info": {
82+
"codemirror_mode": {
83+
"name": "ipython",
84+
"version": 2
85+
},
86+
"file_extension": ".py",
87+
"mimetype": "text/x-python",
88+
"name": "python",
89+
"nbconvert_exporter": "python",
90+
"pygments_lexer": "ipython2",
91+
"version": "2.7.13"
92+
}
93+
},
94+
"nbformat": 4,
95+
"nbformat_minor": 0
96+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Basic Tensor Operations\n",
8+
"\n",
9+
"Basic tensor operations using TensorFlow v2.\n",
10+
"\n",
11+
"- Author: Aymeric Damien\n",
12+
"- Project: https://github.com/aymericdamien/TensorFlow-Examples/"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from __future__ import print_function\n",
22+
"import tensorflow as tf"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"# Define tensor constants.\n",
32+
"a = tf.constant(2)\n",
33+
"b = tf.constant(3)\n",
34+
"c = tf.constant(5)"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"metadata": {},
41+
"outputs": [
42+
{
43+
"name": "stdout",
44+
"output_type": "stream",
45+
"text": [
46+
"add = 5\n",
47+
"sub = -1\n",
48+
"mul = 6\n",
49+
"div = 0.6666666666666666\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"# Various tensor operations.\n",
55+
"# Note: Tensors also support python operators (+, *, ...)\n",
56+
"add = tf.add(a, b)\n",
57+
"sub = tf.subtract(a, b)\n",
58+
"mul = tf.multiply(a, b)\n",
59+
"div = tf.divide(a, b)\n",
60+
"\n",
61+
"# Access tensors value.\n",
62+
"print(\"add =\", add.numpy())\n",
63+
"print(\"sub =\", sub.numpy())\n",
64+
"print(\"mul =\", mul.numpy())\n",
65+
"print(\"div =\", div.numpy())"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 4,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"mean = 3\n",
78+
"sum = 10\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"# Some more operations.\n",
84+
"mean = tf.reduce_mean([a, b, c])\n",
85+
"sum = tf.reduce_sum([a, b, c])\n",
86+
"\n",
87+
"# Access tensors value.\n",
88+
"print(\"mean =\", mean.numpy())\n",
89+
"print(\"sum =\", sum.numpy())"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 5,
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"# Matrix multiplications.\n",
99+
"matrix1 = tf.constant([[1., 2.], [3., 4.]])\n",
100+
"matrix2 = tf.constant([[5., 6.], [7., 8.]])\n",
101+
"\n",
102+
"product = tf.matmul(matrix1, matrix2)"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 6,
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"data": {
112+
"text/plain": [
113+
"<tf.Tensor: id=31, shape=(2, 2), dtype=float32, numpy=\n",
114+
"array([[19., 22.],\n",
115+
" [43., 50.]], dtype=float32)>"
116+
]
117+
},
118+
"execution_count": 6,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"# Display Tensor.\n",
125+
"product"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 7,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"data": {
135+
"text/plain": [
136+
"array([[19., 22.],\n",
137+
" [43., 50.]], dtype=float32)"
138+
]
139+
},
140+
"execution_count": 7,
141+
"metadata": {},
142+
"output_type": "execute_result"
143+
}
144+
],
145+
"source": [
146+
"# Convert Tensor to Numpy.\n",
147+
"product.numpy()"
148+
]
149+
}
150+
],
151+
"metadata": {
152+
"kernelspec": {
153+
"display_name": "Python 2",
154+
"language": "python",
155+
"name": "python2"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 2
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython2",
167+
"version": "2.7.15"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 2
172+
}

0 commit comments

Comments
 (0)