From 84767497a6e7f2ddbb43bba3d771341af0990be2 Mon Sep 17 00:00:00 2001 From: lg Date: Thu, 22 Feb 2018 22:51:32 +0800 Subject: [PATCH 01/20] remove two dangerous default values --- tensorlayer/utils.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 90a47bef5..a63ff6e57 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -370,10 +370,10 @@ def evaluation(y_test=None, y_predict=None, n_classes=None): return c_mat, f1, acc, f1_macro -def dict_to_one(dp_dict={}): - """ - Input a dictionary, return a dictionary that all items are set to one, - use for disable dropout, dropconnect layer and so on. +def dict_to_one(dp_dict): + """Input a dictionary, return a dictionary that all items are set to one. + + Used for disable dropout, dropconnect layer and so on. Parameters ---------- @@ -390,9 +390,8 @@ def dict_to_one(dp_dict={}): return {x: 1 for x in dp_dict} -def flatten_list(list_of_list=[[], []]): - """ - Input a list of list, return a list that all items are in a list. +def flatten_list(list_of_list): + """Input a list of list, return a list that all items are in a list. Parameters ---------- From 7396beb8fce2af3c2352887438cc8bc2b2f4e309 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 00:08:53 +0800 Subject: [PATCH 02/20] fix mnist tutorial based on codacy --- example/tutorial_mnist.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index fb4c52ad9..fcf560c44 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -1,12 +1,5 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - -import time - -import numpy as np -import tensorflow as tf -import tensorlayer as tl -from tensorlayer.layers import set_keep """Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN. This tutorial uses placeholder to control all keeping probabilities, @@ -29,6 +22,13 @@ """ +import time + +import numpy as np +import tensorflow as tf +import tensorlayer as tl +from tensorlayer.layers import set_keep + def main_test_layers(model='relu'): X_train, y_train, X_val, y_val, X_test, y_test = \ @@ -91,7 +91,6 @@ def main_test_layers(model='relu'): # cost = cost + tl.cost.lo_regularizer(0.0001)(network.all_params[0]) + tl.cost.lo_regularizer(0.0001)(network.all_params[2]) # cost = cost + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[0]) + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[2]) - params = network.all_params # train n_epoch = 100 batch_size = 128 @@ -147,7 +146,7 @@ def main_test_layers(model='relu'): tl.vis.draw_weights(network.all_params[0].eval(), second=10, saveable=True, shape=[28, 28], name='w1_' + str(epoch + 1), fig_idx=2012) # You can also save the weight of 1st hidden layer to .npz file. # tl.files.save_npz([network.all_params[0]] , name='w1'+str(epoch+1)+'.npz') - except: + except: # pylint: disable=bare-except print("You should change vis.W(), if you want to save the feature images for different dataset") print('Evaluation') @@ -204,7 +203,7 @@ def main_test_denoise_AE(model='relu'): # placeholder x = tf.placeholder(tf.float32, shape=[None, 784], name='x') - y_ = tf.placeholder( + tf.placeholder( tf.int64, shape=[ None, ], name='y_') @@ -301,7 +300,7 @@ def main_test_stacked_denoise_AE(model='relu'): # Define fine-tune process y = network.outputs - y_op = tf.argmax(tf.nn.softmax(y), 1) + tf.argmax(tf.nn.softmax(y), 1) cost = tl.cost.cross_entropy(y, y_, name='cost') n_epoch = 200 @@ -371,7 +370,7 @@ def main_test_stacked_denoise_AE(model='relu'): try: # visualize the 1st hidden layer during fine-tune tl.vis.draw_weights(network.all_params[0].eval(), second=10, saveable=True, shape=[28, 28], name='w1_' + str(epoch + 1), fig_idx=2012) - except: + except: # pylint: disable=bare-except print("You should change vis.W(), if you want to save the feature images for different dataset") print('Evaluation') @@ -533,7 +532,7 @@ def main_test_cnn_layer(): print(" val acc: %f" % (val_acc / n_batch)) try: tl.vis.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_' + str(epoch + 1), fig_idx=2012) - except: + except: # pylint: disable=bare-except print("You should change vis.CNN(), if you want to save the feature images for different dataset") print('Evaluation') @@ -552,11 +551,15 @@ def main_test_cnn_layer(): if __name__ == '__main__': sess = tf.InteractiveSession() - """Dropout and Dropconnect""" + + # Dropout and Dropconnect main_test_layers(model='relu') # model = relu, dropconnect - """Single Denoising Autoencoder""" + + # Single Denoising Autoencoder # main_test_denoise_AE(model='sigmoid') # model = relu, sigmoid - """Stacked Denoising Autoencoder""" + + # Stacked Denoising Autoencoder # main_test_stacked_denoise_AE(model='relu') # model = relu, sigmoid - """CNN""" + + # CNN # main_test_cnn_layer() From 4088ef4df05d14d4de67ccec4128ad37c9e95f90 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 00:13:02 +0800 Subject: [PATCH 03/20] address hao's comments. --- example/tutorial_mnist.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index fcf560c44..e8efe7a0e 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -203,11 +203,7 @@ def main_test_denoise_AE(model='relu'): # placeholder x = tf.placeholder(tf.float32, shape=[None, 784], name='x') - tf.placeholder( - tf.int64, shape=[ - None, - ], name='y_') - + print("Build Network") if model == 'relu': network = tl.layers.InputLayer(x, name='input') From 228d4c78b9fb1cb68ebf12a7d9634e7ac8f16937 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:20:01 +0000 Subject: [PATCH 04/20] remove unused y_op --- example/tutorial_mnist.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index e8efe7a0e..1131335fc 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -203,7 +203,7 @@ def main_test_denoise_AE(model='relu'): # placeholder x = tf.placeholder(tf.float32, shape=[None, 784], name='x') - + print("Build Network") if model == 'relu': network = tl.layers.InputLayer(x, name='input') @@ -296,7 +296,6 @@ def main_test_stacked_denoise_AE(model='relu'): # Define fine-tune process y = network.outputs - tf.argmax(tf.nn.softmax(y), 1) cost = tl.cost.cross_entropy(y, y_, name='cost') n_epoch = 200 From 63b08d3afd0692e316719d8b304172744936b11e Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:29:50 +0000 Subject: [PATCH 05/20] hao conv.py --- tensorlayer/layers/convolution.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index a103739fc..49d76445e 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -59,7 +59,7 @@ def __init__( act = tf.identity logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (self.name, str(shape), str(stride), padding, act.__name__)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name):# as vs: W = tf.get_variable(name='W_conv1d', shape=shape, initializer=W_init, dtype=D_TYPE, **W_init_args) self.outputs = tf.nn.convolution( self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, ), data_format=data_format) # 1.2 From ef27e65da73c6be3611966874de6165c250c8f96 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:35:08 +0000 Subject: [PATCH 06/20] hao prepro.py --- tensorlayer/prepro.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 35d786d8d..bb0ffce2a 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -8,7 +8,7 @@ import scipy import scipy.ndimage as ndi import skimage -import tensorlayer as tl +# import tensorlayer as tl from scipy import linalg from scipy.ndimage.filters import gaussian_filter from scipy.ndimage.interpolation import map_coordinates @@ -16,11 +16,11 @@ from skimage import exposure, transform # import Queue # <-- donot work for py3 -is_py2 = sys.version[0] == '2' -if is_py2: - import Queue as queue -else: - import queue as queue +# is_py2 = sys.version[0] == '2' +# if is_py2: +# import Queue as queue +# else: +# import queue as queue # linalg https://docs.scipy.org/doc/scipy/reference/linalg.html # ndimage https://docs.scipy.org/doc/scipy/reference/ndimage.html @@ -1416,7 +1416,7 @@ def get_zca_whitening_principal_components_img(X): logging.info("zca : computing sigma ..") sigma = np.dot(flatX.T, flatX) / flatX.shape[0] logging.info("zca : computing U, S and V ..") - U, S, V = linalg.svd(sigma) + U, S, _ = linalg.svd(sigma) # USV logging.info("zca : computing principal components ..") principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + 10e-7))), U.T) return principal_components @@ -1884,7 +1884,7 @@ def binary_erosion(x, radius=3): A processed binary image. """ - from skimage.morphology import disk, dilation, binary_erosion + from skimage.morphology import disk, binary_erosion mask = disk(radius) x = binary_erosion(x, selem=mask) return x @@ -1907,7 +1907,7 @@ def erosion(x, radius=3): A processed greyscale image. """ - from skimage.morphology import disk, dilation, erosion + from skimage.morphology import disk, erosion mask = disk(radius) x = erosion(x, selem=mask) return x @@ -2913,7 +2913,7 @@ def process_sequences(sequences, end_id=0, pad_val=0, is_shorten=True, remain_en """ max_length = 0 - for i_s, seq in enumerate(sequences): + for _, seq in enumerate(sequences): is_end = False for i_w, n in enumerate(seq): if n == end_id and is_end == False: # 1st time to see end_id From f82ffc39c39c46cfa2a2cc91f79e584deada1cd3 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:40:36 +0000 Subject: [PATCH 07/20] hao files.py --- tensorlayer/files.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 1dabaa8fd..33a2b9ea5 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -205,9 +205,9 @@ def unpickle(file): plt.ion() # interactive mode count = 1 - for row in range(10): - for col in range(10): - a = fig.add_subplot(10, 10, count) + for _ in range(10): # each row + for _ in range(10): # each column + _ = fig.add_subplot(10, 10, count) if shape == (-1, 3, 32, 32): # plt.imshow(X_train[count-1], interpolation='nearest') plt.imshow(np.transpose(X_train[count - 1], (1, 2, 0)), interpolation='nearest') @@ -332,7 +332,7 @@ def load_matt_mahoney_text8_dataset(path='data'): with zipfile.ZipFile(os.path.join(path, filename)) as f: word_list = f.read(f.namelist()[0]).split() - for idx, word in enumerate(word_list): + for idx, _ in enumerate(word_list): word_list[idx] = word_list[idx].decode() return word_list @@ -667,7 +667,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab images_list = [] images_folder_list = [] for i in range(0, size): - images_folder_list += load_folder_list(path=path + '/images%d' % i) + # images_folder_list += load_folder_list(path=path + '/images%d' % i) + images_folder_list += load_folder_list(path=os.path.join(path, 'images%d' % i)) images_folder_list.sort(key=lambda s: int(s.split('/')[-1])) # folder/images/ddd # logging.info(images_folder_list) # exit() @@ -750,14 +751,14 @@ def if_2d_to_3d(images): # [h, w] --> [h, w, 3] return im_train_A, im_train_B, im_test_A, im_test_B -def download_file_from_google_drive(id, destination): +def download_file_from_google_drive(ID, destination): """Download file from Google Drive. See ``tl.files.load_celebA_dataset`` for example. Parameters -------------- - id : str + ID : str The driver ID. destination : str The destination for save file. @@ -782,11 +783,11 @@ def get_confirm_token(response): URL = "https://docs.google.com/uc?export=download" session = requests.Session() - response = session.get(URL, params={'id': id}, stream=True) + response = session.get(URL, params={'id': ID}, stream=True) token = get_confirm_token(response) if token: - params = {'id': id, 'confirm': token} + params = {'id': ID, 'confirm': token} response = session.get(URL, params=params, stream=True) save_response_content(response, destination) @@ -1613,7 +1614,7 @@ def load_file_list(path=None, regx='\.npz', printable=True): path = os.getcwd() file_list = os.listdir(path) return_list = [] - for idx, f in enumerate(file_list): + for _, f in enumerate(file_list): if re.search(regx, f): return_list.append(f) # return_list.sort() From 201c4dcf70f9c3a2e68c5807b9fa49f39b18df39 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:44:31 +0000 Subject: [PATCH 08/20] remove str statement --- example/tutorial_vgg19.py | 46 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index 765eca69f..5adc514da 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -17,16 +17,16 @@ 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)) -""" -VGG-19 for ImageNet --------------------- -Pre-trained model in this example - VGG19 NPZ and -trainable examples of VGG16/19 in TensorFlow can be found here: -https://github.com/machrisaa/tensorflow-vgg -For simplified CNN layer see "Convolutional layer (Simplified)" -in read the docs website. -""" +# VGG-19 for ImageNet +# -------------------- +# Pre-trained model in this example - VGG19 NPZ and +# trainable examples of VGG16/19 in TensorFlow can be found here: +# https://github.com/machrisaa/tensorflow-vgg +# +# For simplified CNN layer see "Convolutional layer (Simplified)" +# in read the docs website. + VGG_MEAN = [103.939, 116.779, 123.68] @@ -94,35 +94,35 @@ def Vgg19(rgb): red - VGG_MEAN[2], ], axis=3) assert bgr.get_shape().as_list()[1:] == [224, 224, 3] - """ input layer """ + # input layer net_in = InputLayer(bgr, name='input') - """ conv1 """ + # conv1 network = Conv2dLayer(net_in, act=tf.nn.relu, shape=[3, 3, 3, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_1') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 64, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') - """ conv2 """ + # conv2 network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 64, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_1') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 128, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') - """ conv3 """ + # conv3 network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 128, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_1') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_2') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_4') network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') - """ conv4 """ + # conv4 network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_1') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_2') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_4') network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') - """ conv5 """ + # conv5 network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_1') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_2') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_4') network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') - """ fc 6~8 """ + # fc 6~8 network = FlattenLayer(network, name='flatten') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc6') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc7') @@ -165,35 +165,35 @@ def Vgg19_simple_api(rgb): red - VGG_MEAN[2], ], axis=3) assert bgr.get_shape().as_list()[1:] == [224, 224, 3] - """ input layer """ + # input layer net_in = InputLayer(bgr, name='input') - """ conv1 """ + # conv1 network = Conv2d(net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') network = Conv2d(network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') - """ conv2 """ + # conv2 network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') - """ conv3 """ + # conv3 network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') - """ conv4 """ + # conv4 network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') - """ conv5 """ + # conv5 network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') - """ fc 6~8 """ + # fc 6~8 network = FlattenLayer(network, name='flatten') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc6') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc7') From 0d2afb5fa98fa700e210a74ef0e2cf6f6752ea10 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:47:11 +0000 Subject: [PATCH 09/20] hao example mnist --- example/tutorial_mnist.py | 40 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 1131335fc..1c71a812f 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -1,26 +1,26 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN. -This tutorial uses placeholder to control all keeping probabilities, -so we need to set the non-one probabilities during training, and set them to 1 -during evaluating and testing. +# Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN. +# +# This tutorial uses placeholder to control all keeping probabilities, +# so we need to set the non-one probabilities during training, and set them to 1 +# during evaluating and testing. +# +# $ Set keeping probabilities. +# >>> feed_dict = {x: X_train_a, y_: y_train_a} +# >>> feed_dict.update( network.all_drop ) +# +# $ Set all keeping probabilities to 1 for evaluating and testing. +# >>> dp_dict = tl.utils.dict_to_one( network.all_drop ) +# >>> feed_dict = {x: X_train_a, y_: y_train_a} +# >>> feed_dict.update(dp_dict) +# +# Alternatively, if you don't want to use placeholder to control them, you can +# build different inferences for training, evaluating and testing, +# and all inferences share the same model parameters. +# (see tutorial_ptb_lstm.py) -$ Set keeping probabilities. ->>> feed_dict = {x: X_train_a, y_: y_train_a} ->>> feed_dict.update( network.all_drop ) - -$ Set all keeping probabilities to 1 for evaluating and testing. ->>> dp_dict = tl.utils.dict_to_one( network.all_drop ) ->>> feed_dict = {x: X_train_a, y_: y_train_a} ->>> feed_dict.update(dp_dict) - -Alternatively, if you don't want to use placeholder to control them, you can -build different inferences for training, evaluating and testing, -and all inferences share the same model parameters. -(see tutorial_ptb_lstm.py) - -""" import time @@ -84,7 +84,7 @@ def main_test_layers(model='relu'): cost = tl.cost.cross_entropy(y, y_, name='xentropy') correct_prediction = tf.equal(tf.argmax(y, 1), y_) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) - y_op = tf.argmax(tf.nn.softmax(y), 1) + # y_op = tf.argmax(tf.nn.softmax(y), 1) # You can add more penalty to the cost function as follow. # cost = cost + tl.cost.maxnorm_regularizer(1.0)(network.all_params[0]) + tl.cost.maxnorm_regularizer(1.0)(network.all_params[2]) From 54e470ef6978b64e50470ca139bec96d7b6b8276 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:50:22 +0000 Subject: [PATCH 10/20] yapf --- example/tutorial_mnist.py | 1 - example/tutorial_vgg19.py | 3 +-- tensorlayer/files.py | 4 ++-- tensorlayer/layers/convolution.py | 2 +- tensorlayer/prepro.py | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 1c71a812f..5a93ebad7 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -21,7 +21,6 @@ # and all inferences share the same model parameters. # (see tutorial_ptb_lstm.py) - import time import numpy as np diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index 5adc514da..da389e323 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -27,7 +27,6 @@ # For simplified CNN layer see "Convolutional layer (Simplified)" # in read the docs website. - VGG_MEAN = [103.939, 116.779, 123.68] @@ -193,7 +192,7 @@ def Vgg19_simple_api(rgb): network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') - # fc 6~8 + # fc 6~8 network = FlattenLayer(network, name='flatten') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc6') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc7') diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 33a2b9ea5..f04016850 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -205,8 +205,8 @@ def unpickle(file): plt.ion() # interactive mode count = 1 - for _ in range(10): # each row - for _ in range(10): # each column + for _ in range(10): # each row + for _ in range(10): # each column _ = fig.add_subplot(10, 10, count) if shape == (-1, 3, 32, 32): # plt.imshow(X_train[count-1], interpolation='nearest') diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 49d76445e..33e6d1b82 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -59,7 +59,7 @@ def __init__( act = tf.identity logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (self.name, str(shape), str(stride), padding, act.__name__)) - with tf.variable_scope(name):# as vs: + with tf.variable_scope(name): # as vs: W = tf.get_variable(name='W_conv1d', shape=shape, initializer=W_init, dtype=D_TYPE, **W_init_args) self.outputs = tf.nn.convolution( self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, ), data_format=data_format) # 1.2 diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index bb0ffce2a..d0e755f1a 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -1416,7 +1416,7 @@ def get_zca_whitening_principal_components_img(X): logging.info("zca : computing sigma ..") sigma = np.dot(flatX.T, flatX) / flatX.shape[0] logging.info("zca : computing U, S and V ..") - U, S, _ = linalg.svd(sigma) # USV + U, S, _ = linalg.svd(sigma) # USV logging.info("zca : computing principal components ..") principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + 10e-7))), U.T) return principal_components From 4ec0b75135ff0726f8a58aec47415d64e71d10f2 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:52:08 +0000 Subject: [PATCH 11/20] hao cifar10 --- example/tutorial_cifar10_tfrecord.py | 76 ++++++++++++++-------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 2000ebcad..62d7079dd 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -10,43 +10,43 @@ import tensorlayer as tl from PIL import Image from tensorlayer.layers import * -"""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. - -- 2. For simplified CNN layers see "Convolutional layer (Simplified)" -in read the docs website. - -- 3. Data augmentation without TFRecord see `tutorial_image_preprocess.py` !! - -Links -------- -.. https://www.tensorflow.org/versions/r0.9/tutorials/deep_cnn/index.html -.. https://github.com/tensorflow/tensorflow/tree/r0.9/tensorflow/models/image/cifar10 - -Note ------- -The optimizers between official code and this code are different. - -Description ------------ -The images are processed as follows: -.. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training. -.. They are approximately whitened to make the model insensitive to dynamic range. - -For training, we additionally apply a series of random distortions to -artificially increase the data set size: -.. Randomly flip the image from left to right. -.. Randomly distort the image brightness. -.. Randomly distort the image contrast. - -Speed Up --------- -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. -""" +# 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. +# +# - 2. For simplified CNN layers see "Convolutional layer (Simplified)" +# in read the docs website. +# +# - 3. Data augmentation without TFRecord see `tutorial_image_preprocess.py` !! +# +# Links +# ------- +# .. https://www.tensorflow.org/versions/r0.9/tutorials/deep_cnn/index.html +# .. https://github.com/tensorflow/tensorflow/tree/r0.9/tensorflow/models/image/cifar10 +# +# Note +# ------ +# The optimizers between official code and this code are different. +# +# Description +# ----------- +# The images are processed as follows: +# .. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training. +# .. They are approximately whitened to make the model insensitive to dynamic range. +# +# For training, we additionally apply a series of random distortions to +# artificially increase the data set size: +# .. Randomly flip the image from left to right. +# .. Randomly distort the image brightness. +# .. Randomly distort the image contrast. +# +# Speed Up +# -------- +# 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. + model_file_name = "model_cifar10_tfrecord.ckpt" resume = False # load model, resume from previous checkpoint? @@ -71,7 +71,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() writer = tf.python_io.TFRecordWriter(filename) for index, img in enumerate(images): img_raw = img.tobytes() From 180ad9996c19d1bb1722438d3df305b7032f0a2d Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 16:55:29 +0000 Subject: [PATCH 12/20] hao inceptionv3 --- example/tutorial_inceptionV3_tfslim.py | 45 +++++++++++++------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index 99c849362..e5b05daff 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -12,35 +12,34 @@ 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) +# 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 ? - -Introduction of Slim : https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim -Slim Pre-trained Models : https://github.com/tensorflow/models/tree/master/research/slim - -With the help of SlimNetsLayer, all Slim Model can be combined into TensorLayer. -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 -""" + +# You will learn: +# 1. What is TF-Slim ? +# 1. How to combine TensorLayer and TF-Slim ? +# +# Introduction of Slim : https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim +# Slim Pre-trained Models : https://github.com/tensorflow/models/tree/master/research/slim +# +# With the help of SlimNetsLayer, all Slim Model can be combined into TensorLayer. +# 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 def load_image(path): From 5b5840151880f3044f4726f91965017411ac05d6 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 17:00:35 +0000 Subject: [PATCH 13/20] hao ptb tfrecord image processing --- example/tutorial_image_preprocess.py | 14 +- example/tutorial_ptb_lstm.py | 197 +++++++++++++-------------- example/tutorial_tfrecord2.py | 22 ++- 3 files changed, 111 insertions(+), 122 deletions(-) diff --git a/example/tutorial_image_preprocess.py b/example/tutorial_image_preprocess.py index 4f6d5713d..1d723fa98 100755 --- a/example/tutorial_image_preprocess.py +++ b/example/tutorial_image_preprocess.py @@ -1,15 +1,11 @@ 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. -Alternatively, we can use TFRecord to preprocess data, -see `tutorial_cifar10_tfrecord.py` for more details. -""" +# Data Augmentation by numpy, scipy, threading and queue. +# +# Alternatively, we can use TFRecord to preprocess data, +# see `tutorial_cifar10_tfrecord.py` for more details. + X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index e61704a22..6efc5231c 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -7,105 +7,104 @@ import tensorflow as tf import tensorlayer as tl from tensorlayer.layers import set_keep -"""Example of Synced sequence input and output. -This is a reimpmentation of the TensorFlow official PTB example in : -tensorflow/models/rnn/ptb - -The batch_size can be seem as how many concurrent computations.\n -As the following example shows, the first batch learn the sequence information by using 0 to 9.\n -The second batch learn the sequence information by using 10 to 19.\n -So it ignores the information from 9 to 10 !\n -If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n - -The meaning of batch_size here is not the same with the MNIST example. In MNIST example, -batch_size reflects how many examples we consider in each iteration, while in -PTB example, batch_size is how many concurrent processes (segments) -for speed up computation. - -Some Information will be ignored if batch_size > 1, however, if your dataset -is "long" enough (a text corpus usually has billions words), the ignored -information would not effect the final result. - -In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. -At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 -segments, then go through 20 segments separately. - -The training data will be generated as follow:\n - ->>> train_data = [i for i in range(20)] ->>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): ->>> x, y = batch ->>> print(x, '\n',y) -... [[ 0 1 2] <---x 1st subset/ iteration -... [10 11 12]] -... [[ 1 2 3] <---y -... [11 12 13]] -... -... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration -... [13 14 15]] <--- 2nd batch input -... [[ 4 5 6] <--- 1st batch target -... [14 15 16]] <--- 2nd batch target -... -... [[ 6 7 8] 3rd subset/ iteration -... [16 17 18]] -... [[ 7 8 9] -... [17 18 19]] - -Hao Dong: This example can also be considered as pre-training of the word -embedding matrix. - -About RNN ----------- -$ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ - -More TensorFlow official RNN examples can be found here ---------------------------------------------------------- -$ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks -$ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models -$ translation : tensorflow/models/rnn/translate - -""" -"""Example / benchmark for building a PTB LSTM model. - -Trains the model described in: -(Zaremba, et. al.) Recurrent Neural Network Regularization -http://arxiv.org/abs/1409.2329 - -There are 3 supported model configurations: -=========================================== -| config | epochs | train | valid | test -=========================================== -| small | 13 | 37.99 | 121.39 | 115.91 -| medium | 39 | 48.45 | 86.16 | 82.07 -| large | 55 | 37.87 | 82.62 | 78.29 -The exact results may vary depending on the random initialization. - -The hyperparameters used in the model: -- init_scale - the initial scale of the weights -- learning_rate - the initial value of the learning rate -- max_grad_norm - the maximum permissible norm of the gradient -- num_layers - the number of LSTM layers -- num_steps - the number of unrolled steps of LSTM -- hidden_size - the number of LSTM units -- max_epoch - the number of epochs trained with the initial learning rate -- max_max_epoch - the total number of epochs for training -- keep_prob - the probability of keeping weights in the dropout layer -- lr_decay - the decay of the learning rate for each epoch after "max_epoch" -- batch_size - the batch size - -The data required for this example is in the data/ dir of the -PTB dataset from Tomas Mikolov's webpage: - -$ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz -$ tar xvf simple-examples.tgz - - -A) use the zero_state function on the cell object +# Example of Synced sequence input and output. +# This is a reimpmentation of the TensorFlow official PTB example in : +# tensorflow/models/rnn/ptb +# +# The batch_size can be seem as how many concurrent computations.\n +# As the following example shows, the first batch learn the sequence information by using 0 to 9.\n +# The second batch learn the sequence information by using 10 to 19.\n +# So it ignores the information from 9 to 10 !\n +# If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n +# +# The meaning of batch_size here is not the same with the MNIST example. In MNIST example, +# batch_size reflects how many examples we consider in each iteration, while in +# PTB example, batch_size is how many concurrent processes (segments) +# for speed up computation. +# +# Some Information will be ignored if batch_size > 1, however, if your dataset +# is "long" enough (a text corpus usually has billions words), the ignored +# information would not effect the final result. +# +# In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. +# At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 +# segments, then go through 20 segments separately. +# +# The training data will be generated as follow:\n +# +# >>> train_data = [i for i in range(20)] +# >>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): +# >>> x, y = batch +# >>> print(x, '\n',y) +# ... [[ 0 1 2] <---x 1st subset/ iteration +# ... [10 11 12]] +# ... [[ 1 2 3] <---y +# ... [11 12 13]] +# ... +# ... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration +# ... [13 14 15]] <--- 2nd batch input +# ... [[ 4 5 6] <--- 1st batch target +# ... [14 15 16]] <--- 2nd batch target +# ... +# ... [[ 6 7 8] 3rd subset/ iteration +# ... [16 17 18]] +# ... [[ 7 8 9] +# ... [17 18 19]] +# +# Hao Dong: This example can also be considered as pre-training of the word +# embedding matrix. +# +# About RNN +# ---------- +# $ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ +# +# More TensorFlow official RNN examples can be found here +# --------------------------------------------------------- +# $ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks +# $ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models +# $ translation : tensorflow/models/rnn/translate +# +# Example / benchmark for building a PTB LSTM model. +# +# Trains the model described in: +# (Zaremba, et. al.) Recurrent Neural Network Regularization +# http://arxiv.org/abs/1409.2329 +# +# There are 3 supported model configurations: +# =========================================== +# | config | epochs | train | valid | test +# =========================================== +# | small | 13 | 37.99 | 121.39 | 115.91 +# | medium | 39 | 48.45 | 86.16 | 82.07 +# | large | 55 | 37.87 | 82.62 | 78.29 +# The exact results may vary depending on the random initialization. +# +# The hyperparameters used in the model: +# - init_scale - the initial scale of the weights +# - learning_rate - the initial value of the learning rate +# - max_grad_norm - the maximum permissible norm of the gradient +# - num_layers - the number of LSTM layers +# - num_steps - the number of unrolled steps of LSTM +# - hidden_size - the number of LSTM units +# - max_epoch - the number of epochs trained with the initial learning rate +# - max_max_epoch - the total number of epochs for training +# - keep_prob - the probability of keeping weights in the dropout layer +# - lr_decay - the decay of the learning rate for each epoch after "max_epoch" +# - batch_size - the batch size +# +# The data required for this example is in the data/ dir of the +# PTB dataset from Tomas Mikolov's webpage: +# +# $ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz +# $ tar xvf simple-examples.tgz +# +# +# A) use the zero_state function on the cell object +# +# B) for an rnn, all time steps share weights. We use one matrix to keep all +# gate weights. Split by column into 4 parts to get the 4 gate weight matrices. -B) for an rnn, all time steps share weights. We use one matrix to keep all -gate weights. Split by column into 4 parts to get the 4 gate weight matrices. -""" flags = tf.flags flags.DEFINE_string("model", "small", "A type of model. Possible options are: small, medium, large.") @@ -137,7 +136,7 @@ def main(_): init_scale = 0.05 learning_rate = 1.0 max_grad_norm = 5 - num_layers = 2 + # num_layers = 2 num_steps = 35 hidden_size = 650 max_epoch = 6 @@ -150,7 +149,7 @@ def main(_): init_scale = 0.04 learning_rate = 1.0 max_grad_norm = 10 - num_layers = 2 + # num_layers = 2 num_steps = 35 hidden_size = 1500 max_epoch = 14 diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index 3e778c0ce..870e4ae38 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -1,28 +1,22 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -import io -import os -import time +import os import numpy as np # import matplotlib # matplotlib.use('GTK') import tensorflow as tf import tensorlayer as tl -from PIL import Image -from tensorlayer.layers import set_keep -""" -You will learn: -1. How to convert CIFAR-10 dataset into TFRecord format file. -2. How to read CIFAR-10 from TFRecord format file. - -More: -1. tutorial_tfrecord.py -2. tutoral_cifar10_tfrecord.py +# You will learn: +# 1. How to convert CIFAR-10 dataset into TFRecord format file. +# 2. How to read CIFAR-10 from TFRecord format file. +# +# More: +# 1. tutorial_tfrecord.py +# 2. tutoral_cifar10_tfrecord.py -""" ## Download data, and convert to TFRecord format, see ```tutorial_tfrecord.py``` X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) From 5796021022f9ae1d8644670764955bb694dd2a8e Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 17:09:56 +0000 Subject: [PATCH 14/20] hao tutorials --- example/tutorial_cartpole_ac.py | 64 +++---- example/tutorial_cifar10.py | 6 +- example/tutorial_frozenlake_dqn.py | 24 ++- example/tutorial_frozenlake_q_table.py | 21 +- example/tutorial_generate_text.py | 17 +- example/tutorial_image_preprocess.py | 1 - example/tutorial_keras.py | 2 - example/tutorial_mlp_dropout1.py | 2 - example/tutorial_mlp_dropout2.py | 1 - example/tutorial_mnist_float16.py | 2 - example/tutorial_ptb_lstm.py | 2 - example/tutorial_ptb_lstm_state_is_tuple.py | 200 ++++++++++---------- example/tutorial_tfrecord.py | 36 ++-- example/tutorial_tfrecord2.py | 2 - 14 files changed, 176 insertions(+), 204 deletions(-) diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index dd660927f..9ac54bd7b 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -1,36 +1,34 @@ -""" -Actor-Critic using TD-error as the Advantage, Reinforcement Learning. - -Actor Critic History ----------------------- -A3C > DDPG > AC - -Advantage ----------- -AC converge faster than Policy Gradient. - -Disadvantage (IMPORTANT) ------------------------- -The Policy is oscillated (difficult to converge), DDPG can solve -this problem using advantage of DQN. - -Reference ----------- -View more on MorvanZhou's tutorial page: https://morvanzhou.github.io/tutorials/ - -Environment ------------- -CartPole-v0: https://gym.openai.com/envs/CartPole-v0 - -A pole is attached by an un-actuated joint to a cart, which moves along a -frictionless track. The system is controlled by applying a force of +1 or -1 -to the cart. The pendulum starts upright, and the goal is to prevent it from -falling over. - -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. -""" +# Actor-Critic using TD-error as the Advantage, Reinforcement Learning. +# +# Actor Critic History +# ---------------------- +# A3C > DDPG > AC +# +# Advantage +# ---------- +# AC converge faster than Policy Gradient. +# +# Disadvantage (IMPORTANT) +# ------------------------ +# The Policy is oscillated (difficult to converge), DDPG can solve +# this problem using advantage of DQN. +# +# Reference +# ---------- +# View more on MorvanZhou's tutorial page: https://morvanzhou.github.io/tutorials/ +# +# Environment +# ------------ +# CartPole-v0: https://gym.openai.com/envs/CartPole-v0 +# +# A pole is attached by an un-actuated joint to a cart, which moves along a +# frictionless track. The system is controlled by applying a force of +1 or -1 +# to the cart. The pendulum starts upright, and the goal is to prevent it from +# falling over. +# +# 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 diff --git a/example/tutorial_cifar10.py b/example/tutorial_cifar10.py index 89d353ed9..9b55e2156 100644 --- a/example/tutorial_cifar10.py +++ b/example/tutorial_cifar10.py @@ -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() diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index 578684b30..a506abe41 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -1,24 +1,22 @@ -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(S, A) <- Q(S, A) + alpha * (R + lambda * Q(newS, newA) - Q(S, A)) -delta_w = R + lambda * Q(newS, newA) - -See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. - -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 +# 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) +# +# See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. +# +# 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 +# +# Note: Policy Network has been proved to be better than Q-Learning, see tutorial_atari_pong.py -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 diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index 9168373b3..af6b349b9 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -1,18 +1,17 @@ import time - import gym import numpy as np -"""Q-Table learning algorithm, 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)) - -See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. - -For Q-Network, see tutorial_frozenlake_q_network.py -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 -""" +# Q-Table learning algorithm, 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)) +# +# See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. +# +# For Q-Network, see tutorial_frozenlake_q_network.py +# +# 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 ## Load the environment env = gym.make('FrozenLake-v0') diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index e019d9653..bc1786659 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -15,14 +15,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -"""Example of Synced sequence input and output. -Generate text using LSTM. - -Data: https://github.com/zsdonghao/tensorlayer/tree/master/example/data/ - -""" +# 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 @@ -154,7 +151,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') @@ -369,9 +365,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() # diff --git a/example/tutorial_image_preprocess.py b/example/tutorial_image_preprocess.py index 1d723fa98..a526feeae 100755 --- a/example/tutorial_image_preprocess.py +++ b/example/tutorial_image_preprocess.py @@ -6,7 +6,6 @@ # Alternatively, we can use TFRecord to preprocess data, # see `tutorial_cifar10_tfrecord.py` for more details. - X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) diff --git a/example/tutorial_keras.py b/example/tutorial_keras.py index d6126d67b..b60820d7a 100644 --- a/example/tutorial_keras.py +++ b/example/tutorial_keras.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- import time - -import numpy as np import tensorflow as tf import tensorlayer as tl from keras import backend as K diff --git a/example/tutorial_mlp_dropout1.py b/example/tutorial_mlp_dropout1.py index 4695667b4..ad0ea9d73 100644 --- a/example/tutorial_mlp_dropout1.py +++ b/example/tutorial_mlp_dropout1.py @@ -1,7 +1,5 @@ # train the network import time - -import numpy as np import tensorflow as tf import tensorlayer as tl diff --git a/example/tutorial_mlp_dropout2.py b/example/tutorial_mlp_dropout2.py index 9954a3eca..dfbeaa1af 100644 --- a/example/tutorial_mlp_dropout2.py +++ b/example/tutorial_mlp_dropout2.py @@ -1,6 +1,5 @@ # train the network import time - import tensorflow as tf import tensorlayer as tl diff --git a/example/tutorial_mnist_float16.py b/example/tutorial_mnist_float16.py index 5a74a21cc..e9740f6d6 100644 --- a/example/tutorial_mnist_float16.py +++ b/example/tutorial_mnist_float16.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- import time - -import numpy as np import tensorflow as tf import tensorlayer as tl from tensorlayer.layers import * diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index 6efc5231c..ac22785eb 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -104,8 +104,6 @@ # B) for an rnn, all time steps share weights. We use one matrix to keep all # gate weights. Split by column into 4 parts to get the 4 gate weight matrices. - - flags = tf.flags flags.DEFINE_string("model", "small", "A type of model. Possible options are: small, medium, large.") FLAGS = flags.FLAGS diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index 39fca7e2d..69ee558ea 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -7,105 +7,103 @@ import tensorflow as tf import tensorlayer as tl from tensorlayer.layers import set_keep -"""Example of Synced sequence input and output. -This is a reimpmentation of the TensorFlow official PTB example in : -tensorflow/models/rnn/ptb - -The batch_size can be seem as how many concurrent computations.\n -As the following example shows, the first batch learn the sequence information by using 0 to 9.\n -The second batch learn the sequence information by using 10 to 19.\n -So it ignores the information from 9 to 10 !\n -If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n - -The meaning of batch_size here is not the same with the MNIST example. In MNIST example, -batch_size reflects how many examples we consider in each iteration, while in -PTB example, batch_size is how many concurrent processes (segments) -for speed up computation. - -Some Information will be ignored if batch_size > 1, however, if your dataset -is "long" enough (a text corpus usually has billions words), the ignored -information would not effect the final result. - -In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. -At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 -segments, then go through 20 segments separately. - -The training data will be generated as follow:\n - ->>> train_data = [i for i in range(20)] ->>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): ->>> x, y = batch ->>> print(x, '\n',y) -... [[ 0 1 2] <---x 1st subset/ iteration -... [10 11 12]] -... [[ 1 2 3] <---y -... [11 12 13]] -... -... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration -... [13 14 15]] <--- 2nd batch input -... [[ 4 5 6] <--- 1st batch target -... [14 15 16]] <--- 2nd batch target -... -... [[ 6 7 8] 3rd subset/ iteration -... [16 17 18]] -... [[ 7 8 9] -... [17 18 19]] - -Hao Dong: This example can also be considered as pre-training of the word -embedding matrix. - -About RNN ----------- -$ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ - -More TensorFlow official RNN examples can be found here ---------------------------------------------------------- -$ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks -$ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models -$ translation : tensorflow/models/rnn/translate - -tensorflow (0.9.0) -""" -"""Example / benchmark for building a PTB LSTM model. - -Trains the model described in: -(Zaremba, et. al.) Recurrent Neural Network Regularization -http://arxiv.org/abs/1409.2329 - -There are 3 supported model configurations: -=========================================== -| config | epochs | train | valid | test -=========================================== -| small | 13 | 37.99 | 121.39 | 115.91 -| medium | 39 | 48.45 | 86.16 | 82.07 -| large | 55 | 37.87 | 82.62 | 78.29 -The exact results may vary depending on the random initialization. - -The hyperparameters used in the model: -- init_scale - the initial scale of the weights -- learning_rate - the initial value of the learning rate -- max_grad_norm - the maximum permissible norm of the gradient -- num_layers - the number of LSTM layers -- num_steps - the number of unrolled steps of LSTM -- hidden_size - the number of LSTM units -- max_epoch - the number of epochs trained with the initial learning rate -- max_max_epoch - the total number of epochs for training -- keep_prob - the probability of keeping weights in the dropout layer -- lr_decay - the decay of the learning rate for each epoch after "max_epoch" -- batch_size - the batch size - -The data required for this example is in the data/ dir of the -PTB dataset from Tomas Mikolov's webpage: - -$ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz -$ tar xvf simple-examples.tgz - -A) use the zero_state function on the cell object - -B) for an rnn, all time steps share weights. We use one matrix to keep all -gate weights. Split by column into 4 parts to get the 4 gate weight matrices. - -""" +# Example of Synced sequence input and output. +# This is a reimpmentation of the TensorFlow official PTB example in : +# tensorflow/models/rnn/ptb +# +# The batch_size can be seem as how many concurrent computations.\n +# As the following example shows, the first batch learn the sequence information by using 0 to 9.\n +# The second batch learn the sequence information by using 10 to 19.\n +# So it ignores the information from 9 to 10 !\n +# If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n +# +# The meaning of batch_size here is not the same with the MNIST example. In MNIST example, +# batch_size reflects how many examples we consider in each iteration, while in +# PTB example, batch_size is how many concurrent processes (segments) +# for speed up computation. +# +# Some Information will be ignored if batch_size > 1, however, if your dataset +# is "long" enough (a text corpus usually has billions words), the ignored +# information would not effect the final result. +# +# In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. +# At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 +# segments, then go through 20 segments separately. +# +# The training data will be generated as follow:\n +# +# >>> train_data = [i for i in range(20)] +# >>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): +# >>> x, y = batch +# >>> print(x, '\n',y) +# ... [[ 0 1 2] <---x 1st subset/ iteration +# ... [10 11 12]] +# ... [[ 1 2 3] <---y +# ... [11 12 13]] +# ... +# ... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration +# ... [13 14 15]] <--- 2nd batch input +# ... [[ 4 5 6] <--- 1st batch target +# ... [14 15 16]] <--- 2nd batch target +# ... +# ... [[ 6 7 8] 3rd subset/ iteration +# ... [16 17 18]] +# ... [[ 7 8 9] +# ... [17 18 19]] +# +# Hao Dong: This example can also be considered as pre-training of the word +# embedding matrix. +# +# About RNN +# ---------- +# $ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ +# +# More TensorFlow official RNN examples can be found here +# --------------------------------------------------------- +# $ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks +# $ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models +# $ translation : tensorflow/models/rnn/translate +# +# tensorflow (0.9.0) +# +# Example / benchmark for building a PTB LSTM model. +# +# Trains the model described in: +# (Zaremba, et. al.) Recurrent Neural Network Regularization +# http://arxiv.org/abs/1409.2329 +# +# There are 3 supported model configurations: +# =========================================== +# | config | epochs | train | valid | test +# =========================================== +# | small | 13 | 37.99 | 121.39 | 115.91 +# | medium | 39 | 48.45 | 86.16 | 82.07 +# | large | 55 | 37.87 | 82.62 | 78.29 +# The exact results may vary depending on the random initialization. +# +# The hyperparameters used in the model: +# - init_scale - the initial scale of the weights +# - learning_rate - the initial value of the learning rate +# - max_grad_norm - the maximum permissible norm of the gradient +# - num_layers - the number of LSTM layers +# - num_steps - the number of unrolled steps of LSTM +# - hidden_size - the number of LSTM units +# - max_epoch - the number of epochs trained with the initial learning rate +# - max_max_epoch - the total number of epochs for training +# - keep_prob - the probability of keeping weights in the dropout layer +# - lr_decay - the decay of the learning rate for each epoch after "max_epoch" +# - batch_size - the batch size +# +# The data required for this example is in the data/ dir of the +# PTB dataset from Tomas Mikolov's webpage: +# +# $ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz +# $ tar xvf simple-examples.tgz +# +# A) use the zero_state function on the cell object +# +# B) for an rnn, all time steps share weights. We use one matrix to keep all +# gate weights. Split by column into 4 parts to get the 4 gate weight matrices. flags = tf.flags flags.DEFINE_string("model", "small", "A type of model. Possible options are: small, medium, large.") @@ -137,7 +135,7 @@ def main(_): init_scale = 0.05 learning_rate = 1.0 max_grad_norm = 5 - num_layers = 2 + # num_layers = 2 num_steps = 35 hidden_size = 650 max_epoch = 6 @@ -150,7 +148,7 @@ def main(_): init_scale = 0.04 learning_rate = 1.0 max_grad_norm = 10 - num_layers = 2 + # num_layers = 2 num_steps = 35 hidden_size = 1500 max_epoch = 14 diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index f08da5285..5c15e5859 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -1,32 +1,28 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -import io import os - import numpy as np import tensorflow as tf import tensorlayer as tl from PIL import Image -""" -You will learn: -1. How to save data into TFRecord format file. -2. How to read data from TFRecord format file by using Queue and Thread. - -Reference: ------------ -English : https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/ - https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html - https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#readers -Chinese : http://blog.csdn.net/u012759136/article/details/52232266 - https://github.com/ycszen/tf_lab/blob/master/reading_data/TensorFlow高效加载数据的方法.md - -More ------- -1. tutorial_tfrecord2.py -2. tutorial_cifar10_tfrecord.py -""" +# You will learn: +# 1. How to save data into TFRecord format file. +# 2. How to read data from TFRecord format file by using Queue and Thread. +# +# Reference: +# ----------- +# English : https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/ +# https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html +# https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#readers +# Chinese : http://blog.csdn.net/u012759136/article/details/52232266 +# https://github.com/ycszen/tf_lab/blob/master/reading_data/TensorFlow高效加载数据的方法.md +# +# More +# ------ +# 1. tutorial_tfrecord2.py +# 2. tutorial_cifar10_tfrecord.py ## Save data ================================================================== classes = ['/data/cat', '/data/dog'] # cat is 0, dog is 1 diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index 870e4ae38..a757b788e 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -1,7 +1,6 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - import os import numpy as np # import matplotlib @@ -17,7 +16,6 @@ # 1. tutorial_tfrecord.py # 2. tutoral_cifar10_tfrecord.py - ## Download data, and convert to TFRecord format, see ```tutorial_tfrecord.py``` X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) From d60b8ddbb66e17146a22059093bbeccfd266a5ca Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 17:19:53 +0000 Subject: [PATCH 15/20] str comment --- example/tutorial_cartpole_ac.py | 63 +++--- example/tutorial_cifar10_tfrecord.py | 74 +++---- example/tutorial_frozenlake_dqn.py | 51 ++--- example/tutorial_frozenlake_q_table.py | 23 +- example/tutorial_generate_text.py | 36 ++-- example/tutorial_image_preprocess.py | 11 +- ...torial_imagenet_inceptionV3_distributed.py | 10 +- example/tutorial_inceptionV3_tfslim.py | 39 ++-- example/tutorial_mnist.py | 40 ++-- example/tutorial_mnist_distributed.py | 16 +- example/tutorial_ptb_lstm.py | 194 ++++++++--------- example/tutorial_ptb_lstm_state_is_tuple.py | 196 +++++++++--------- example/tutorial_tfrecord.py | 34 +-- example/tutorial_tfrecord2.py | 16 +- example/tutorial_tfrecord3.py | 21 +- example/tutorial_vgg16.py | 28 ++- example/tutorial_vgg19.py | 19 +- 17 files changed, 436 insertions(+), 435 deletions(-) diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index 9ac54bd7b..b2aa14fc5 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -1,34 +1,35 @@ -# Actor-Critic using TD-error as the Advantage, Reinforcement Learning. -# -# Actor Critic History -# ---------------------- -# A3C > DDPG > AC -# -# Advantage -# ---------- -# AC converge faster than Policy Gradient. -# -# Disadvantage (IMPORTANT) -# ------------------------ -# The Policy is oscillated (difficult to converge), DDPG can solve -# this problem using advantage of DQN. -# -# Reference -# ---------- -# View more on MorvanZhou's tutorial page: https://morvanzhou.github.io/tutorials/ -# -# Environment -# ------------ -# CartPole-v0: https://gym.openai.com/envs/CartPole-v0 -# -# A pole is attached by an un-actuated joint to a cart, which moves along a -# frictionless track. The system is controlled by applying a force of +1 or -1 -# to the cart. The pendulum starts upright, and the goal is to prevent it from -# falling over. -# -# 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. +"""Actor-Critic using TD-error as the Advantage, Reinforcement Learning. + +Actor Critic History +---------------------- +A3C > DDPG > AC + +Advantage +---------- +AC converge faster than Policy Gradient. + +Disadvantage (IMPORTANT) +------------------------ +The Policy is oscillated (difficult to converge), DDPG can solve +this problem using advantage of DQN. + +Reference +---------- +View more on MorvanZhou's tutorial page: https://morvanzhou.github.io/tutorials/ + +Environment +------------ +CartPole-v0: https://gym.openai.com/envs/CartPole-v0 + +A pole is attached by an un-actuated joint to a cart, which moves along a +frictionless track. The system is controlled by applying a force of +1 or -1 +to the cart. The pendulum starts upright, and the goal is to prevent it from +falling over. + +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 diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 62d7079dd..6ce247635 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -1,51 +1,51 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""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. + +- 2. For simplified CNN layers see "Convolutional layer (Simplified)" +in read the docs website. + +- 3. Data augmentation without TFRecord see `tutorial_image_preprocess.py` !! + +Links +------- +.. https://www.tensorflow.org/versions/r0.9/tutorials/deep_cnn/index.html +.. https://github.com/tensorflow/tensorflow/tree/r0.9/tensorflow/models/image/cifar10 + +Note +------ +The optimizers between official code and this code are different. + +Description +----------- +The images are processed as follows: +.. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training. +.. They are approximately whitened to make the model insensitive to dynamic range. + +For training, we additionally apply a series of random distortions to +artificially increase the data set size: +.. Randomly flip the image from left to right. +.. Randomly distort the image brightness. +.. Randomly distort the image contrast. + +Speed Up +-------- +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 * -# 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. -# -# - 2. For simplified CNN layers see "Convolutional layer (Simplified)" -# in read the docs website. -# -# - 3. Data augmentation without TFRecord see `tutorial_image_preprocess.py` !! -# -# Links -# ------- -# .. https://www.tensorflow.org/versions/r0.9/tutorials/deep_cnn/index.html -# .. https://github.com/tensorflow/tensorflow/tree/r0.9/tensorflow/models/image/cifar10 -# -# Note -# ------ -# The optimizers between official code and this code are different. -# -# Description -# ----------- -# The images are processed as follows: -# .. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training. -# .. They are approximately whitened to make the model insensitive to dynamic range. -# -# For training, we additionally apply a series of random distortions to -# artificially increase the data set size: -# .. Randomly flip the image from left to right. -# .. Randomly distort the image brightness. -# .. Randomly distort the image contrast. -# -# Speed Up -# -------- -# 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. model_file_name = "model_cifar10_tfrecord.ckpt" resume = False # load model, resume from previous checkpoint? diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index a506abe41..2717ea51a 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -1,3 +1,30 @@ +"""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) + +See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. + +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 + +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. +""" + import time import gym import matplotlib.pyplot as plt @@ -5,31 +32,7 @@ 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(S, A) <- Q(S, A) + alpha * (R + lambda * Q(newS, newA) - Q(S, A)) -# delta_w = R + lambda * Q(newS, newA) -# -# See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. -# -# 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 -# -# 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. env = gym.make('FrozenLake-v0') diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index af6b349b9..15ca9d2e8 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -1,18 +1,19 @@ +"""Q-Table learning algorithm, 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)) + +See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. + +For Q-Network, see tutorial_frozenlake_q_network.py + +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 -# Q-Table learning algorithm, 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)) -# -# See David Silver RL Tutorial Lecture 5 - Q-Learning for more details. -# -# For Q-Network, see tutorial_frozenlake_q_network.py -# -# 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 - ## Load the environment env = gym.make('FrozenLake-v0') render = False # display the game environment diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index bc1786659..fa8dd8ead 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -1,24 +1,24 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""Copyright 2016 TensorLayer. All Rights Reserved. -# Copyright 2016 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. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# 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. -# Generate text using LSTM. -# -# Data: https://github.com/zsdonghao/tensorlayer/tree/master/example/data/ +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. +Generate text using LSTM. + +Data: https://github.com/zsdonghao/tensorlayer/tree/master/example/data/ +""" import re import time diff --git a/example/tutorial_image_preprocess.py b/example/tutorial_image_preprocess.py index a526feeae..11da9cf23 100755 --- a/example/tutorial_image_preprocess.py +++ b/example/tutorial_image_preprocess.py @@ -1,11 +1,12 @@ +"""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 -# Data Augmentation by numpy, scipy, threading and queue. -# -# Alternatively, we can use TFRecord to preprocess data, -# see `tutorial_cifar10_tfrecord.py` for more details. - X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index 2c132c21c..ce120c13c 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -1,10 +1,10 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - -# 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 +"""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 +""" import argparse import logging diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index e5b05daff..8584a6a72 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -1,9 +1,27 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""You will learn: +1. What is TF-Slim ? +1. How to combine TensorLayer and TF-Slim ? + +Introduction of Slim : https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim +Slim Pre-trained Models : https://github.com/tensorflow/models/tree/master/research/slim + +With the help of SlimNetsLayer, all Slim Model can be combined into TensorLayer. +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 @@ -22,25 +40,6 @@ 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 ? -# -# Introduction of Slim : https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim -# Slim Pre-trained Models : https://github.com/tensorflow/models/tree/master/research/slim -# -# With the help of SlimNetsLayer, all Slim Model can be combined into TensorLayer. -# 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 - def load_image(path): # load image diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 5a93ebad7..f2a338957 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -1,25 +1,25 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - -# Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN. -# -# This tutorial uses placeholder to control all keeping probabilities, -# so we need to set the non-one probabilities during training, and set them to 1 -# during evaluating and testing. -# -# $ Set keeping probabilities. -# >>> feed_dict = {x: X_train_a, y_: y_train_a} -# >>> feed_dict.update( network.all_drop ) -# -# $ Set all keeping probabilities to 1 for evaluating and testing. -# >>> dp_dict = tl.utils.dict_to_one( network.all_drop ) -# >>> feed_dict = {x: X_train_a, y_: y_train_a} -# >>> feed_dict.update(dp_dict) -# -# Alternatively, if you don't want to use placeholder to control them, you can -# build different inferences for training, evaluating and testing, -# and all inferences share the same model parameters. -# (see tutorial_ptb_lstm.py) +"""Examples of Stacked Denoising Autoencoder, Dropout, Dropconnect and CNN. + +This tutorial uses placeholder to control all keeping probabilities, +so we need to set the non-one probabilities during training, and set them to 1 +during evaluating and testing. + +$ Set keeping probabilities. +>>> feed_dict = {x: X_train_a, y_: y_train_a} +>>> feed_dict.update( network.all_drop ) + +$ Set all keeping probabilities to 1 for evaluating and testing. +>>> dp_dict = tl.utils.dict_to_one( network.all_drop ) +>>> feed_dict = {x: X_train_a, y_: y_train_a} +>>> feed_dict.update(dp_dict) + +Alternatively, if you don't want to use placeholder to control them, you can +build different inferences for training, evaluating and testing, +and all inferences share the same model parameters. +(see tutorial_ptb_lstm.py) +""" import time diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index 584375190..502df1883 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -1,15 +1,15 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""Alpha Version for Distributed Training -# Alpha Version for Distributed Training +you can test this example in your local machine using 2 workers and 1 ps like below, +where CUDA_VISIBLE_DEVICES can be used to set the GPUs the process can use. -# you can test this example in your local machine using 2 workers and 1 ps like below, -# where CUDA_VISIBLE_DEVICES can be used to set the GPUs the process can use. -# -# CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "worker", "index": 0}}' python example/tutorial_mnist_distributed.py > output-master 2>&1 & -# CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "worker", "index": 1}}' python example/tutorial_mnist_distributed.py > output-worker 2>&1 & -# CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "ps", "index": 0}}' python example/tutorial_mnist_distributed.py > output-ps 2>&1 & -# Note: for GPU, please set CUDA_VISIBLE_DEVICES=GPU_ID +CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "worker", "index": 0}}' python example/tutorial_mnist_distributed.py > output-master 2>&1 & +CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "worker", "index": 1}}' python example/tutorial_mnist_distributed.py > output-worker 2>&1 & +CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "ps", "index": 0}}' python example/tutorial_mnist_distributed.py > output-ps 2>&1 & +Note: for GPU, please set CUDA_VISIBLE_DEVICES=GPU_ID +""" import tensorflow as tf import tensorlayer as tl diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index ac22785eb..0aa95bcd9 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -1,108 +1,108 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""Example of Synced sequence input and output. +This is a reimpmentation of the TensorFlow official PTB example in : +tensorflow/models/rnn/ptb -import time +The batch_size can be seem as how many concurrent computations.\n +As the following example shows, the first batch learn the sequence information by using 0 to 9.\n +The second batch learn the sequence information by using 10 to 19.\n +So it ignores the information from 9 to 10 !\n +If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n + +The meaning of batch_size here is not the same with the MNIST example. In MNIST example, +batch_size reflects how many examples we consider in each iteration, while in +PTB example, batch_size is how many concurrent processes (segments) +for speed up computation. + +Some Information will be ignored if batch_size > 1, however, if your dataset +is "long" enough (a text corpus usually has billions words), the ignored +information would not effect the final result. + +In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. +At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 +segments, then go through 20 segments separately. + +The training data will be generated as follow:\n + +>>> train_data = [i for i in range(20)] +>>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): +>>> x, y = batch +>>> print(x, '\n',y) +... [[ 0 1 2] <---x 1st subset/ iteration +... [10 11 12]] +... [[ 1 2 3] <---y +... [11 12 13]] +... +... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration +... [13 14 15]] <--- 2nd batch input +... [[ 4 5 6] <--- 1st batch target +... [14 15 16]] <--- 2nd batch target +... +... [[ 6 7 8] 3rd subset/ iteration +... [16 17 18]] +... [[ 7 8 9] +... [17 18 19]] + +Hao Dong: This example can also be considered as pre-training of the word +embedding matrix. + +About RNN +---------- +$ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ + +More TensorFlow official RNN examples can be found here +--------------------------------------------------------- +$ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks +$ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models +$ translation : tensorflow/models/rnn/translate + +Example / benchmark for building a PTB LSTM model. + +Trains the model described in: +(Zaremba, et. al.) Recurrent Neural Network Regularization +http://arxiv.org/abs/1409.2329 +There are 3 supported model configurations: +=========================================== +| config | epochs | train | valid | test +=========================================== +| small | 13 | 37.99 | 121.39 | 115.91 +| medium | 39 | 48.45 | 86.16 | 82.07 +| large | 55 | 37.87 | 82.62 | 78.29 +The exact results may vary depending on the random initialization. + +The hyperparameters used in the model: +- init_scale - the initial scale of the weights +- learning_rate - the initial value of the learning rate +- max_grad_norm - the maximum permissible norm of the gradient +- num_layers - the number of LSTM layers +- num_steps - the number of unrolled steps of LSTM +- hidden_size - the number of LSTM units +- max_epoch - the number of epochs trained with the initial learning rate +- max_max_epoch - the total number of epochs for training +- keep_prob - the probability of keeping weights in the dropout layer +- lr_decay - the decay of the learning rate for each epoch after "max_epoch" +- batch_size - the batch size + +The data required for this example is in the data/ dir of the +PTB dataset from Tomas Mikolov's webpage: + +$ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz +$ tar xvf simple-examples.tgz + + +A) use the zero_state function on the cell object + +B) for an rnn, all time steps share weights. We use one matrix to keep all +gate weights. Split by column into 4 parts to get the 4 gate weight matrices. +""" + +import time import numpy as np import tensorflow as tf import tensorlayer as tl from tensorlayer.layers import set_keep -# Example of Synced sequence input and output. -# This is a reimpmentation of the TensorFlow official PTB example in : -# tensorflow/models/rnn/ptb -# -# The batch_size can be seem as how many concurrent computations.\n -# As the following example shows, the first batch learn the sequence information by using 0 to 9.\n -# The second batch learn the sequence information by using 10 to 19.\n -# So it ignores the information from 9 to 10 !\n -# If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n -# -# The meaning of batch_size here is not the same with the MNIST example. In MNIST example, -# batch_size reflects how many examples we consider in each iteration, while in -# PTB example, batch_size is how many concurrent processes (segments) -# for speed up computation. -# -# Some Information will be ignored if batch_size > 1, however, if your dataset -# is "long" enough (a text corpus usually has billions words), the ignored -# information would not effect the final result. -# -# In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. -# At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 -# segments, then go through 20 segments separately. -# -# The training data will be generated as follow:\n -# -# >>> train_data = [i for i in range(20)] -# >>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): -# >>> x, y = batch -# >>> print(x, '\n',y) -# ... [[ 0 1 2] <---x 1st subset/ iteration -# ... [10 11 12]] -# ... [[ 1 2 3] <---y -# ... [11 12 13]] -# ... -# ... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration -# ... [13 14 15]] <--- 2nd batch input -# ... [[ 4 5 6] <--- 1st batch target -# ... [14 15 16]] <--- 2nd batch target -# ... -# ... [[ 6 7 8] 3rd subset/ iteration -# ... [16 17 18]] -# ... [[ 7 8 9] -# ... [17 18 19]] -# -# Hao Dong: This example can also be considered as pre-training of the word -# embedding matrix. -# -# About RNN -# ---------- -# $ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ -# -# More TensorFlow official RNN examples can be found here -# --------------------------------------------------------- -# $ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks -# $ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models -# $ translation : tensorflow/models/rnn/translate -# -# Example / benchmark for building a PTB LSTM model. -# -# Trains the model described in: -# (Zaremba, et. al.) Recurrent Neural Network Regularization -# http://arxiv.org/abs/1409.2329 -# -# There are 3 supported model configurations: -# =========================================== -# | config | epochs | train | valid | test -# =========================================== -# | small | 13 | 37.99 | 121.39 | 115.91 -# | medium | 39 | 48.45 | 86.16 | 82.07 -# | large | 55 | 37.87 | 82.62 | 78.29 -# The exact results may vary depending on the random initialization. -# -# The hyperparameters used in the model: -# - init_scale - the initial scale of the weights -# - learning_rate - the initial value of the learning rate -# - max_grad_norm - the maximum permissible norm of the gradient -# - num_layers - the number of LSTM layers -# - num_steps - the number of unrolled steps of LSTM -# - hidden_size - the number of LSTM units -# - max_epoch - the number of epochs trained with the initial learning rate -# - max_max_epoch - the total number of epochs for training -# - keep_prob - the probability of keeping weights in the dropout layer -# - lr_decay - the decay of the learning rate for each epoch after "max_epoch" -# - batch_size - the batch size -# -# The data required for this example is in the data/ dir of the -# PTB dataset from Tomas Mikolov's webpage: -# -# $ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz -# $ tar xvf simple-examples.tgz -# -# -# A) use the zero_state function on the cell object -# -# B) for an rnn, all time steps share weights. We use one matrix to keep all -# gate weights. Split by column into 4 parts to get the 4 gate weight matrices. flags = tf.flags flags.DEFINE_string("model", "small", "A type of model. Possible options are: small, medium, large.") diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index 69ee558ea..eb45b6100 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -1,109 +1,109 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""Example of Synced sequence input and output. +This is a reimpmentation of the TensorFlow official PTB example in : +tensorflow/models/rnn/ptb -import time +The batch_size can be seem as how many concurrent computations.\n +As the following example shows, the first batch learn the sequence information by using 0 to 9.\n +The second batch learn the sequence information by using 10 to 19.\n +So it ignores the information from 9 to 10 !\n +If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n + +The meaning of batch_size here is not the same with the MNIST example. In MNIST example, +batch_size reflects how many examples we consider in each iteration, while in +PTB example, batch_size is how many concurrent processes (segments) +for speed up computation. + +Some Information will be ignored if batch_size > 1, however, if your dataset +is "long" enough (a text corpus usually has billions words), the ignored +information would not effect the final result. + +In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. +At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 +segments, then go through 20 segments separately. + +The training data will be generated as follow:\n + +>>> train_data = [i for i in range(20)] +>>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): +>>> x, y = batch +>>> print(x, '\n',y) +... [[ 0 1 2] <---x 1st subset/ iteration +... [10 11 12]] +... [[ 1 2 3] <---y +... [11 12 13]] +... +... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration +... [13 14 15]] <--- 2nd batch input +... [[ 4 5 6] <--- 1st batch target +... [14 15 16]] <--- 2nd batch target +... +... [[ 6 7 8] 3rd subset/ iteration +... [16 17 18]] +... [[ 7 8 9] +... [17 18 19]] + +Hao Dong: This example can also be considered as pre-training of the word +embedding matrix. + +About RNN +---------- +$ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ + +More TensorFlow official RNN examples can be found here +--------------------------------------------------------- +$ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks +$ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models +$ translation : tensorflow/models/rnn/translate + +tensorflow (0.9.0) + +Example / benchmark for building a PTB LSTM model. +Trains the model described in: +(Zaremba, et. al.) Recurrent Neural Network Regularization +http://arxiv.org/abs/1409.2329 + +There are 3 supported model configurations: +=========================================== +| config | epochs | train | valid | test +=========================================== +| small | 13 | 37.99 | 121.39 | 115.91 +| medium | 39 | 48.45 | 86.16 | 82.07 +| large | 55 | 37.87 | 82.62 | 78.29 +The exact results may vary depending on the random initialization. + +The hyperparameters used in the model: +- init_scale - the initial scale of the weights +- learning_rate - the initial value of the learning rate +- max_grad_norm - the maximum permissible norm of the gradient +- num_layers - the number of LSTM layers +- num_steps - the number of unrolled steps of LSTM +- hidden_size - the number of LSTM units +- max_epoch - the number of epochs trained with the initial learning rate +- max_max_epoch - the total number of epochs for training +- keep_prob - the probability of keeping weights in the dropout layer +- lr_decay - the decay of the learning rate for each epoch after "max_epoch" +- batch_size - the batch size + +The data required for this example is in the data/ dir of the +PTB dataset from Tomas Mikolov's webpage: + +$ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz +$ tar xvf simple-examples.tgz + +A) use the zero_state function on the cell object + +B) for an rnn, all time steps share weights. We use one matrix to keep all +gate weights. Split by column into 4 parts to get the 4 gate weight matrices. +""" + +import time import numpy as np import tensorflow as tf import tensorlayer as tl from tensorlayer.layers import set_keep -# Example of Synced sequence input and output. -# This is a reimpmentation of the TensorFlow official PTB example in : -# tensorflow/models/rnn/ptb -# -# The batch_size can be seem as how many concurrent computations.\n -# As the following example shows, the first batch learn the sequence information by using 0 to 9.\n -# The second batch learn the sequence information by using 10 to 19.\n -# So it ignores the information from 9 to 10 !\n -# If only if we set the batch_size = 1, it will consider all information from 0 to 20.\n -# -# The meaning of batch_size here is not the same with the MNIST example. In MNIST example, -# batch_size reflects how many examples we consider in each iteration, while in -# PTB example, batch_size is how many concurrent processes (segments) -# for speed up computation. -# -# Some Information will be ignored if batch_size > 1, however, if your dataset -# is "long" enough (a text corpus usually has billions words), the ignored -# information would not effect the final result. -# -# In PTB tutorial, we setted batch_size = 20, so we cut the dataset into 20 segments. -# At the begining of each epoch, we initialize (reset) the 20 RNN states for 20 -# segments, then go through 20 segments separately. -# -# The training data will be generated as follow:\n -# -# >>> train_data = [i for i in range(20)] -# >>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): -# >>> x, y = batch -# >>> print(x, '\n',y) -# ... [[ 0 1 2] <---x 1st subset/ iteration -# ... [10 11 12]] -# ... [[ 1 2 3] <---y -# ... [11 12 13]] -# ... -# ... [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration -# ... [13 14 15]] <--- 2nd batch input -# ... [[ 4 5 6] <--- 1st batch target -# ... [14 15 16]] <--- 2nd batch target -# ... -# ... [[ 6 7 8] 3rd subset/ iteration -# ... [16 17 18]] -# ... [[ 7 8 9] -# ... [17 18 19]] -# -# Hao Dong: This example can also be considered as pre-training of the word -# embedding matrix. -# -# About RNN -# ---------- -# $ Karpathy Blog : http://karpathy.github.io/2015/05/21/rnn-effectiveness/ -# -# More TensorFlow official RNN examples can be found here -# --------------------------------------------------------- -# $ RNN for PTB : https://www.tensorflow.org/versions/master/tutorials/recurrent/index.html#recurrent-neural-networks -# $ Seq2seq : https://www.tensorflow.org/versions/master/tutorials/seq2seq/index.html#sequence-to-sequence-models -# $ translation : tensorflow/models/rnn/translate -# -# tensorflow (0.9.0) -# -# Example / benchmark for building a PTB LSTM model. -# -# Trains the model described in: -# (Zaremba, et. al.) Recurrent Neural Network Regularization -# http://arxiv.org/abs/1409.2329 -# -# There are 3 supported model configurations: -# =========================================== -# | config | epochs | train | valid | test -# =========================================== -# | small | 13 | 37.99 | 121.39 | 115.91 -# | medium | 39 | 48.45 | 86.16 | 82.07 -# | large | 55 | 37.87 | 82.62 | 78.29 -# The exact results may vary depending on the random initialization. -# -# The hyperparameters used in the model: -# - init_scale - the initial scale of the weights -# - learning_rate - the initial value of the learning rate -# - max_grad_norm - the maximum permissible norm of the gradient -# - num_layers - the number of LSTM layers -# - num_steps - the number of unrolled steps of LSTM -# - hidden_size - the number of LSTM units -# - max_epoch - the number of epochs trained with the initial learning rate -# - max_max_epoch - the total number of epochs for training -# - keep_prob - the probability of keeping weights in the dropout layer -# - lr_decay - the decay of the learning rate for each epoch after "max_epoch" -# - batch_size - the batch size -# -# The data required for this example is in the data/ dir of the -# PTB dataset from Tomas Mikolov's webpage: -# -# $ wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz -# $ tar xvf simple-examples.tgz -# -# A) use the zero_state function on the cell object -# -# B) for an rnn, all time steps share weights. We use one matrix to keep all -# gate weights. Split by column into 4 parts to get the 4 gate weight matrices. flags = tf.flags flags.DEFINE_string("model", "small", "A type of model. Possible options are: small, medium, large.") diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index 5c15e5859..88c764241 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -1,5 +1,22 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""You will learn: +1. How to save data into TFRecord format file. +2. How to read data from TFRecord format file by using Queue and Thread. + +Reference: +----------- +English : https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/ + https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html + https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#readers +Chinese : http://blog.csdn.net/u012759136/article/details/52232266 + https://github.com/ycszen/tf_lab/blob/master/reading_data/TensorFlow高效加载数据的方法.md + +More +------ +1. tutorial_tfrecord2.py +2. tutorial_cifar10_tfrecord.py +""" import os import numpy as np @@ -7,23 +24,6 @@ import tensorlayer as tl from PIL import Image -# You will learn: -# 1. How to save data into TFRecord format file. -# 2. How to read data from TFRecord format file by using Queue and Thread. -# -# Reference: -# ----------- -# English : https://indico.io/blog/tensorflow-data-inputs-part1-placeholders-protobufs-queues/ -# https://www.tensorflow.org/versions/master/how_tos/reading_data/index.html -# https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#readers -# Chinese : http://blog.csdn.net/u012759136/article/details/52232266 -# https://github.com/ycszen/tf_lab/blob/master/reading_data/TensorFlow高效加载数据的方法.md -# -# More -# ------ -# 1. tutorial_tfrecord2.py -# 2. tutorial_cifar10_tfrecord.py - ## Save data ================================================================== classes = ['/data/cat', '/data/dog'] # cat is 0, dog is 1 cwd = os.getcwd() diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index a757b788e..8cd4cf148 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -1,5 +1,13 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""You will learn: +1. How to convert CIFAR-10 dataset into TFRecord format file. +2. How to read CIFAR-10 from TFRecord format file. + +More: +1. tutorial_tfrecord.py +2. tutoral_cifar10_tfrecord.py +""" import os import numpy as np @@ -8,14 +16,6 @@ import tensorflow as tf import tensorlayer as tl -# You will learn: -# 1. How to convert CIFAR-10 dataset into TFRecord format file. -# 2. How to read CIFAR-10 from TFRecord format file. -# -# More: -# 1. tutorial_tfrecord.py -# 2. tutoral_cifar10_tfrecord.py - ## Download data, and convert to TFRecord format, see ```tutorial_tfrecord.py``` X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 6f05c3937..5ff44737c 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -1,16 +1,5 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - -import io -import json -import os -import time - -import numpy as np -import tensorflow as tf -import tensorlayer as tl -from PIL import Image -from tensorlayer.layers import set_keep """ You will learn: 1. How to save time-series data (e.g. sentence) into TFRecord format file. @@ -25,6 +14,16 @@ """ +import io +import json +import os +import time +import numpy as np +import tensorflow as tf +import tensorlayer as tl +from PIL import Image +from tensorlayer.layers import set_keep + def _int64_feature(value): """Wrapper for inserting an int64 Feature into a SequenceExample proto, diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 081c5ead5..8abac5e79 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -1,20 +1,5 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - -import os -import sys -import time - -import numpy as np -import tensorflow as tf -import tensorlayer as tl -from scipy.misc import imread, imresize -from tensorlayer.layers import * - -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)) """ VGG-16 for ImageNet @@ -49,6 +34,19 @@ >>> crop_y = (image_h - 224) / 2 >>> img = img[crop_y:crop_y+224,crop_x:crop_x+224,:] """ +import os +import sys +import time +import numpy as np +import tensorflow as tf +import tensorlayer as tl +from scipy.misc import imread, imresize +from tensorlayer.layers import * + +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 conv_layers(net_in): diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index da389e323..3c359e1aa 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -1,6 +1,14 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - +"""VGG-19 for ImageNet +-------------------- +Pre-trained model in this example - VGG19 NPZ and +trainable examples of VGG16/19 in TensorFlow can be found here: +https://github.com/machrisaa/tensorflow-vgg + +For simplified CNN layer see "Convolutional layer (Simplified)" +in read the docs website. +""" import inspect import os import time @@ -18,15 +26,6 @@ except Exception as e: raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) -# VGG-19 for ImageNet -# -------------------- -# Pre-trained model in this example - VGG19 NPZ and -# trainable examples of VGG16/19 in TensorFlow can be found here: -# https://github.com/machrisaa/tensorflow-vgg -# -# For simplified CNN layer see "Convolutional layer (Simplified)" -# in read the docs website. - VGG_MEAN = [103.939, 116.779, 123.68] From 71b6ecfe816dd891ead969f3cd9e8ba5cf72dbad Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Thu, 22 Feb 2018 17:28:43 +0000 Subject: [PATCH 16/20] str docs --- example/tutorial_atari_pong.py | 2 +- ...ial_bipedalwalker_a3c_continuous_action.py | 1 + example/tutorial_cartpole_ac.py | 1 + example/tutorial_cifar10_tfrecord.py | 3 +- example/tutorial_frozenlake_dqn.py | 3 +- example/tutorial_frozenlake_q_table.py | 5 +++- example/tutorial_generate_text.py | 30 ++++++++++--------- example/tutorial_image_preprocess.py | 1 + ...torial_imagenet_inceptionV3_distributed.py | 7 +++-- example/tutorial_inceptionV3_tfslim.py | 5 ++-- example/tutorial_mnist.py | 1 + example/tutorial_mnist_distributed.py | 1 + example/tutorial_ptb_lstm.py | 3 ++ example/tutorial_ptb_lstm_state_is_tuple.py | 2 ++ example/tutorial_tfrecord.py | 4 ++- example/tutorial_tfrecord2.py | 4 ++- example/tutorial_tfrecord3.py | 3 +- example/tutorial_vgg16.py | 4 ++- example/tutorial_vgg19.py | 7 +++-- example/tutorial_word2vec_basic.py | 5 ++-- 20 files changed, 61 insertions(+), 31 deletions(-) diff --git a/example/tutorial_atari_pong.py b/example/tutorial_atari_pong.py index c33034274..6f08bd020 100644 --- a/example/tutorial_atari_pong.py +++ b/example/tutorial_atari_pong.py @@ -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 diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index 9a9ce1d66..33613b7fe 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -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 diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index b2aa14fc5..afc2e4b39 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -29,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 diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 6ce247635..66a9e0d51 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -1,6 +1,6 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""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. @@ -36,6 +36,7 @@ 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 diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index 2717ea51a..1f93adc20 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -1,4 +1,4 @@ -"""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) @@ -23,6 +23,7 @@ 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 diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index 15ca9d2e8..8184d2b93 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -1,4 +1,6 @@ -"""Q-Table learning algorithm, non deep learning - TD Learning, Off-Policy, e-Greedy Exploration +"""Q-Table learning algorithm. + +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)) @@ -8,6 +10,7 @@ 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 diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index fa8dd8ead..404dc07df 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -1,23 +1,25 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""Copyright 2016 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. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -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. -============================================================================== +# 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + Generate text using LSTM. Data: https://github.com/zsdonghao/tensorlayer/tree/master/example/data/ + """ import re diff --git a/example/tutorial_image_preprocess.py b/example/tutorial_image_preprocess.py index 11da9cf23..2ea016c77 100755 --- a/example/tutorial_image_preprocess.py +++ b/example/tutorial_image_preprocess.py @@ -2,6 +2,7 @@ Alternatively, we can use TFRecord to preprocess data, see `tutorial_cifar10_tfrecord.py` for more details. + """ import time diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index ce120c13c..4cb8b7fd3 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -1,9 +1,12 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""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 +"""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 + """ import argparse diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index 8584a6a72..c90d15048 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -1,6 +1,7 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""You will learn: +"""You will learn. + 1. What is TF-Slim ? 1. How to combine TensorLayer and TF-Slim ? @@ -11,13 +12,13 @@ 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 diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index f2a338957..56e52b79f 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -19,6 +19,7 @@ build different inferences for training, evaluating and testing, and all inferences share the same model parameters. (see tutorial_ptb_lstm.py) + """ import time diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index 502df1883..2aa398214 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -9,6 +9,7 @@ CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "worker", "index": 1}}' python example/tutorial_mnist_distributed.py > output-worker 2>&1 & CUDA_VISIBLE_DEVICES= TF_CONFIG='{"cluster": {"ps": ["127.0.0.1:3001"], "worker": ["127.0.0.1:3002", "127.0.0.1:3003"]}, "task": {"type": "ps", "index": 0}}' python example/tutorial_mnist_distributed.py > output-ps 2>&1 & Note: for GPU, please set CUDA_VISIBLE_DEVICES=GPU_ID + """ import tensorflow as tf diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index 0aa95bcd9..d75f97940 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -1,6 +1,7 @@ #! /usr/bin/python # -*- coding: utf-8 -*- """Example of Synced sequence input and output. + This is a reimpmentation of the TensorFlow official PTB example in : tensorflow/models/rnn/ptb @@ -96,6 +97,7 @@ B) for an rnn, all time steps share weights. We use one matrix to keep all gate weights. Split by column into 4 parts to get the 4 gate weight matrices. + """ import time @@ -116,6 +118,7 @@ def main(_): sentence. The memory state of the network is initialized with a vector of zeros and gets updated after reading each word. Also, for computational reasons, we will process data in mini-batches of size batch_size. + """ if FLAGS.model == "small": diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index eb45b6100..6c1a7cf53 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -1,6 +1,7 @@ #! /usr/bin/python # -*- coding: utf-8 -*- """Example of Synced sequence input and output. + This is a reimpmentation of the TensorFlow official PTB example in : tensorflow/models/rnn/ptb @@ -97,6 +98,7 @@ B) for an rnn, all time steps share weights. We use one matrix to keep all gate weights. Split by column into 4 parts to get the 4 gate weight matrices. + """ import time diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index 88c764241..32a3fd4b1 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -1,6 +1,7 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""You will learn: +"""You will learn. + 1. How to save data into TFRecord format file. 2. How to read data from TFRecord format file by using Queue and Thread. @@ -16,6 +17,7 @@ ------ 1. tutorial_tfrecord2.py 2. tutorial_cifar10_tfrecord.py + """ import os diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index 8cd4cf148..00e8b3501 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -1,12 +1,14 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""You will learn: +"""You will learn. + 1. How to convert CIFAR-10 dataset into TFRecord format file. 2. How to read CIFAR-10 from TFRecord format file. More: 1. tutorial_tfrecord.py 2. tutoral_cifar10_tfrecord.py + """ import os diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 5ff44737c..0da7a8b83 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -1,7 +1,8 @@ #! /usr/bin/python # -*- coding: utf-8 -*- """ -You will learn: +You will learn. + 1. How to save time-series data (e.g. sentence) into TFRecord format file. 2. How to read time-series data from TFRecord format file. 3. How to create inputs, targets and mask. diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 8abac5e79..695d9cf95 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -1,7 +1,7 @@ #! /usr/bin/python # -*- coding: utf-8 -*- """ -VGG-16 for ImageNet +VGG-16 for ImageNet. Introduction ---------------- @@ -33,7 +33,9 @@ >>> crop_x = (image_w - 224) / 2 >>> crop_y = (image_h - 224) / 2 >>> img = img[crop_y:crop_y+224,crop_x:crop_x+224,:] + """ + import os import sys import time diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index 3c359e1aa..4a40fc48b 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -1,14 +1,17 @@ #! /usr/bin/python # -*- coding: utf-8 -*- -"""VGG-19 for ImageNet --------------------- +""" +VGG-19 for ImageNet. + Pre-trained model in this example - VGG19 NPZ and trainable examples of VGG16/19 in TensorFlow can be found here: https://github.com/machrisaa/tensorflow-vgg For simplified CNN layer see "Convolutional layer (Simplified)" in read the docs website. + """ + import inspect import os import time diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 85e3affe7..79654847e 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -""" -Vector Representations of Words ---------------------------------- +"""Vector Representations of Words. + This is the minimalistic reimplementation of tensorflow/examples/tutorials/word2vec/word2vec_basic.py This basic example contains the code needed to download some data, From 65635bf25d7af6e50fe6edf6144f7a12924399ba Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 02:06:09 +0800 Subject: [PATCH 17/20] Update README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f1cb61ae9..e5f217e80 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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.