Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[![Documentation Status](https://readthedocs.org/projects/tensorlayer/badge/?version=latest)](http://tensorlayer.readthedocs.io/en/latest/?badge=latest)
[![Docker Pulls](https://img.shields.io/docker/pulls/tensorlayer/tensorlayer.svg?maxAge=604800)](https://hub.docker.com/r/tensorlayer/tensorlayer/)

TensorLayer is a deep learning and reinforcement learning library on top of [TensorFlow](https://www.tensorflow.org). It provides rich neural layers and utility functions to help researchers and engineers build real-world AI applications. TensorLayer is awarded the 2017 Best Open Source Software Award by the prestigious [ACM Multimedia Society](http://www.acmmm.org/2017/mm-2017-awardees/).
TensorLayer is a deep learning and reinforcement learning library on top of [TensorFlow](https://www.tensorflow.org). It provides rich neural layers and utility functions to help researchers and engineers build real-world AI applications. TensorLayer is awarded the 2017 Best Open Source Software by the prestigious [ACM Multimedia Society](http://www.acmmm.org/2017/mm-2017-awardees/).

- Useful links: [Documentation](http://tensorlayer.readthedocs.io), [Examples](http://tensorlayer.readthedocs.io/en/latest/user/example.html), [中文文档](https://tensorlayercn.readthedocs.io), [中文书](http://www.broadview.com.cn/book/5059)

Expand Down Expand Up @@ -116,7 +116,7 @@ Examples can be found [in this folder](https://github.com/zsdonghao/tensorlayer/
- Float 16 half-precision model, see [tutorial\_mnist_float16.py](https://github.com/zsdonghao/tensorlayer/blob/master/example/tutorial_mnist_float16.py)

## Notes
TensorLayer provides two set of Convolutional layer APIs, see [(Professional)](http://tensorlayer.readthedocs.io/en/latest/modules/layers.html#convolutional-layer-pro) and [(Simplified)](http://tensorlayer.readthedocs.io/en/latest/modules/layers.html#convolutional-layer-simplified) on readthedocs website.
TensorLayer provides two set of Convolutional layer APIs, see [(Advanced)](http://tensorlayer.readthedocs.io/en/latest/modules/layers.html#convolutional-layer-pro) and [(Basic)](http://tensorlayer.readthedocs.io/en/latest/modules/layers.html#convolutional-layer-simplified) on readthedocs website.
<!--
* If you get into trouble, you can start a discussion on [Slack](https://join.slack.com/t/tensorlayer/shared_invite/MjI1NjQ5NTUxOTY5LTE1MDI3MDYwNTItYzYwNmFiZmZkOA), [Gitter](https://gitter.im/tensorlayer/Lobby#?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge>),
[Help Wanted Issues](https://waffle.io/zsdonghao/tensorlayer),
Expand All @@ -128,11 +128,11 @@ TensorLayer provides two set of Convolutional layer APIs, see [(Professional)](h

## Design Philosophy

As deep learning practitioners, we have been looking for a TensorFlow wrapper library that can serve for various development phases. This library is easy for beginners by offering rich neural network implementations,
examples and tutorials. Later, its APIs do not prohibit users from manipulating the low-level powerful features of TensorFlow, which is necessary in tackling real-world problems. In the end, the extra wrappers shall not compromise TensorFlow performance, and thus suit for production deployment. TensorLayer is a novel library that aims to satisfy these requirements that can occur in various phases. It has three key features:
As TensorFlow users, we have been looking for a library that can serve for various development phases. This library is easy for beginners by providing rich neural network implementations,
examples and tutorials. Later, its APIs shall naturally allow users to leverage the powerful features of TensorFlow, exhibiting best performance in addressing real-world problems. In the end, the extra abstraction shall not compromise TensorFlow performance, and thus suit for production deployment. TensorLayer is a novel library that aims to satisfy these requirements. It has three key features:

- *Simplicity* : TensorLayer lifts the low-level dataflow abstraction of TensorFlow to **high-level** layers. It also provides users with massive examples and tutorials to help bootstrap.
- *Flexibility* : TensorLayer APIs are transparent: it does not mask TensorFlow from users but leaving massive hooks that allow **low-level tuning**.
- *Simplicity* : TensorLayer lifts the low-level dataflow abstraction of TensorFlow to **high-level** layers. It also provides users with massive examples and tutorials to minimize learning barrier.
- *Flexibility* : TensorLayer APIs are transparent: it does not mask TensorFlow from users; but leaving massive hooks that support diverse **low-level tuning**.
- *Zero-cost Abstraction* : TensorLayer is able to achieve the **full performance** of TensorFlow.

## Negligible Overhead
Expand All @@ -150,12 +150,12 @@ on a Titan X Pascal GPU. Here are the training speeds of respective tasks:

Similar to TensorLayer, Keras and TFLearn are also popular TensorFlow wrapper libraries.
These libraries are comfortable to start with. They provide high-level abstractions;
but in turn mask the underlying engine features from users. Though good for bootstrap,
it becomes hard to manipulate the low-level powerful features of TensorFlow.
but mask the underlying engine from users. It is thus hard to
customize model behaviors and touch the essential features of TensorFlow.
Without compromise in simplicity, TensorLayer APIs are generally more flexible and transparent.
Users often find it easy to start with the examples and tutorials of TensorLayer, and
then dive into the TensorFlow low-level APIs only if need.
TensorLayer does not intend to create library lock-in. Users can easily import models from Keras, TFSlim and TFLearn into
TensorLayer does not create library lock-in. Users can easily import models from Keras, TFSlim and TFLearn into
a TensorLayer environment.


Expand Down
2 changes: 1 addition & 1 deletion example/tutorial_atari_pong.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
""" Monte-Carlo Policy Network π(a|s) (REINFORCE)
""" Monte-Carlo Policy Network π(a|s) (REINFORCE).

To understand Reinforcement Learning, we let computer to learn how to play
Pong game from the original screen inputs. Before we start, we highly recommend
Expand Down
3 changes: 2 additions & 1 deletion example/tutorial_bipedalwalker_a3c_continuous_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
speed, angular velocity, horizontal speed, vertical speed, position of joints
and joints angular speed, legs contact with ground, and 10 lidar rangefinder
measurements. There's no coordinates in the state vector.

"""

import multiprocessing
Expand Down Expand Up @@ -181,7 +182,7 @@ def work(self):
if self.name == 'Worker_0' and total_step % 30 == 0:
self.env.render()
a = self.AC.choose_action(s)
s_, r, done, info = self.env.step(a)
s_, r, done, _info = self.env.step(a)

# set robot falls reward to -2 instead of -100
if r == -100: r = -2
Expand Down
4 changes: 2 additions & 2 deletions example/tutorial_cartpole_ac.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Actor-Critic using TD-error as the Advantage, Reinforcement Learning.
"""Actor-Critic using TD-error as the Advantage, Reinforcement Learning.

Actor Critic History
----------------------
Expand Down Expand Up @@ -30,6 +29,7 @@
A reward of +1 is provided for every timestep that the pole remains upright.
The episode ends when the pole is more than 15 degrees from vertical, or the
cart moves more than 2.4 units from the center.

"""

import time
Expand Down
6 changes: 2 additions & 4 deletions example/tutorial_cifar10.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
""" tl.prepro for data augmentation """

import io
import os
# tl.prepro for data augmentation

import time

import numpy as np
import tensorflow as tf
import tensorlayer as tl
from PIL import Image
from tensorlayer.layers import *

sess = tf.InteractiveSession()
Expand Down
25 changes: 13 additions & 12 deletions example/tutorial_cifar10_tfrecord.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-

import io
import os
import time

import numpy as np
import tensorflow as tf
import tensorlayer as tl
from PIL import Image
from tensorlayer.layers import *
"""Reimplementation of the TensorFlow official CIFAR-10 CNN tutorials:
"""Reimplementation of the TensorFlow official CIFAR-10 CNN tutorials.

- 1. This model has 1,068,298 paramters, after few hours of training with GPU,
accurcy of 86% was found.
Expand Down Expand Up @@ -46,7 +36,18 @@
Reading images from disk and distorting them can use a non-trivial amount
of processing time. To prevent these operations from slowing down training,
we run them inside 16 separate threads which continuously fill a TensorFlow queue.

"""

import io
import os
import time
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from PIL import Image
from tensorlayer.layers import *

model_file_name = "model_cifar10_tfrecord.ckpt"
resume = False # load model, resume from previous checkpoint?

Expand All @@ -71,7 +72,7 @@ def data_to_tfrecord(images, labels, filename):
print("%s exists" % filename)
return
print("Converting data into %s ..." % filename)
cwd = os.getcwd()
# cwd = os.getcwd()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just delete this line if now used.

writer = tf.python_io.TFRecordWriter(filename)
for index, img in enumerate(images):
img_raw = img.tobytes()
Expand Down
48 changes: 25 additions & 23 deletions example/tutorial_frozenlake_dqn.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import random
import time

import gym
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *
""" Q-Network Q(a, s) - TD Learning, Off-Policy, e-Greedy Exploration (GLIE)
"""Q-Network Q(a, s) - TD Learning, Off-Policy, e-Greedy Exploration (GLIE).

Q(S, A) <- Q(S, A) + alpha * (R + lambda * Q(newS, newA) - Q(S, A))
delta_w = R + lambda * Q(newS, newA)
Expand All @@ -18,20 +9,31 @@
CN: https://zhuanlan.zhihu.com/p/25710327

Note: Policy Network has been proved to be better than Q-Learning, see tutorial_atari_pong.py

# The FrozenLake v0 environment
https://gym.openai.com/envs/FrozenLake-v0
The agent controls the movement of a character in a grid world. Some tiles of
the grid are walkable, and others lead to the agent falling into the water.
Additionally, the movement direction of the agent is uncertain and only partially
depends on the chosen direction. The agent is rewarded for finding a walkable
path to a goal tile.
SFFF (S: starting point, safe)
FHFH (F: frozen surface, safe)
FFFH (H: hole, fall to your doom)
HFFG (G: goal, where the frisbee is located)
The episode ends when you reach the goal or fall in a hole. You receive a reward
of 1 if you reach the goal, and zero otherwise.

"""
## The FrozenLake v0 environment
# https://gym.openai.com/envs/FrozenLake-v0
# The agent controls the movement of a character in a grid world. Some tiles of
# the grid are walkable, and others lead to the agent falling into the water.
# Additionally, the movement direction of the agent is uncertain and only partially
# depends on the chosen direction. The agent is rewarded for finding a walkable
# path to a goal tile.
# SFFF (S: starting point, safe)
# FHFH (F: frozen surface, safe)
# FFFH (H: hole, fall to your doom)
# HFFG (G: goal, where the frisbee is located)
# The episode ends when you reach the goal or fall in a hole. You receive a reward
# of 1 if you reach the goal, and zero otherwise.

import time
import gym
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *

env = gym.make('FrozenLake-v0')


Expand Down
11 changes: 7 additions & 4 deletions example/tutorial_frozenlake_q_table.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import time
"""Q-Table learning algorithm.

import gym
import numpy as np
"""Q-Table learning algorithm, non deep learning - TD Learning, Off-Policy, e-Greedy Exploration
Non deep learning - TD Learning, Off-Policy, e-Greedy Exploration

Q(S, A) <- Q(S, A) + alpha * (R + lambda * Q(newS, newA) - Q(S, A))

Expand All @@ -12,8 +10,13 @@

EN: https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part-0-q-learning-with-tables-and-neural-networks-d195264329d0#.5m3361vlw
CN: https://zhuanlan.zhihu.com/p/25710327

"""

import time
import gym
import numpy as np

## Load the environment
env = gym.make('FrozenLake-v0')
render = False # display the game environment
Expand Down
15 changes: 7 additions & 8 deletions example/tutorial_generate_text.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2016 TensorLayer. All Rights Reserved.
# Copyright 2018 TensorLayer. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,15 +13,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Example of Synced sequence input and output.
"""
Example of Synced sequence input and output.

Generate text using LSTM.

Data: https://github.com/zsdonghao/tensorlayer/tree/master/example/data/

"""

import os
import re
import time

Expand Down Expand Up @@ -154,7 +153,6 @@ def main_restore_embedding_layer():
load_params = tl.files.load_npz(name=model_file_name + '.npz')

x = tf.placeholder(tf.int32, shape=[batch_size])
y_ = tf.placeholder(tf.int32, shape=[batch_size, 1])

emb_net = tl.layers.EmbeddingInputlayer(inputs=x, vocabulary_size=vocabulary_size, embedding_size=embedding_size, name='embedding_layer')

Expand Down Expand Up @@ -369,9 +367,10 @@ def loss_fn(outputs, targets, batch_size, sequence_length):

if __name__ == '__main__':
sess = tf.InteractiveSession()
"""Restore a pretrained embedding matrix."""
# Restore a pretrained embedding matrix
# main_restore_embedding_layer()
"""How to generate text from a given context."""

# How to generate text from a given context
main_lstm_generate_text()

#
13 changes: 5 additions & 8 deletions example/tutorial_image_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import time

import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.prepro import *
"""
Data Augmentation by numpy, scipy, threading and queue.
"""Data Augmentation by numpy, scipy, threading and queue.

Alternatively, we can use TFRecord to preprocess data,
see `tutorial_cifar10_tfrecord.py` for more details.

"""

import time
import tensorlayer as tl

X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False)


Expand Down
11 changes: 7 additions & 4 deletions example/tutorial_imagenet_inceptionV3_distributed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""Example of training an Inception V3 model with ImageNet.

# Example of training an Inception V3 model with ImageNet. The parameters are set as in the
# best results of the paper: https://arxiv.org/abs/1512.00567
# The dataset can be downloaded from http://www.image-net.org/ or from the Kaggle competition:
# https://www.kaggle.com/c/imagenet-object-localization-challenge/data
The parameters are set as in the best results of the paper: https://arxiv.org/abs/1512.00567

The dataset can be downloaded from http://www.image-net.org/ or from the Kaggle competition:
https://www.kaggle.com/c/imagenet-object-localization-challenge/data

"""

import argparse
import logging
Expand Down
45 changes: 22 additions & 23 deletions example/tutorial_inceptionV3_tfslim.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""You will learn.

import os
import time

import numpy as np
# from tensorflow.contrib.slim.python.slim.nets.resnet_v2 import resnet_v2_152
# from tensorflow.contrib.slim.python.slim.nets.vgg import vgg_16
import skimage
import skimage.io
import skimage.transform
import tensorflow as tf
import tensorlayer as tl
from scipy.misc import imread, imresize
from tensorflow.contrib.slim.python.slim.nets.alexnet import alexnet_v2
from tensorflow.contrib.slim.python.slim.nets.inception_v3 import (inception_v3, inception_v3_arg_scope, inception_v3_base)

slim = tf.contrib.slim
try:
from data.imagenet_classes import *
except Exception as e:
raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e))
"""
You will learn:
1. What is TF-Slim ?
1. How to combine TensorLayer and TF-Slim ?

Expand All @@ -33,15 +12,35 @@
All models in the following link, end with `return net, end_points`` are available.
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim/python/slim/nets


Bugs
-----
tf.variable_scope :
https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/RoxrU3UnbFA
load inception_v3 for prediction:
http://stackoverflow.com/questions/39357454/restore-checkpoint-in-tensorflow-tensor-name-not-found

"""

import os
import time
import numpy as np
# from tensorflow.contrib.slim.python.slim.nets.resnet_v2 import resnet_v2_152
# from tensorflow.contrib.slim.python.slim.nets.vgg import vgg_16
import skimage
import skimage.io
import skimage.transform
import tensorflow as tf
import tensorlayer as tl
# from scipy.misc import imread, imresize
# from tensorflow.contrib.slim.python.slim.nets.alexnet import alexnet_v2
from tensorflow.contrib.slim.python.slim.nets.inception_v3 import (inception_v3, inception_v3_arg_scope) #, inception_v3_base)

slim = tf.contrib.slim
try:
from data.imagenet_classes import *
except Exception as e:
raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e))


def load_image(path):
# load image
Expand Down
Loading