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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/tutorial_bipedalwalker_a3c_continuous_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def work(self):
self.AC.v_target: buffer_v_target,
}
# update gradients on global network
test = self.AC.update_global(feed_dict)
self.AC.update_global(feed_dict)
buffer_s, buffer_a, buffer_r = [], [], []

# update local network from global network
Expand Down
4 changes: 2 additions & 2 deletions example/tutorial_cifar10_tfrecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def read_and_decode(filename, is_train=None):
# 5. Subtract off the mean and divide by the variance of the pixels.
try: # TF 0.12+
img = tf.image.per_image_standardization(img)
except: # earlier TF versions
except Exception: # earlier TF versions
img = tf.image.per_image_whitening(img)

elif is_train == False:
Expand All @@ -130,7 +130,7 @@ def read_and_decode(filename, is_train=None):
# 2. Subtract off the mean and divide by the variance of the pixels.
try: # TF 0.12+
img = tf.image.per_image_standardization(img)
except: # earlier TF versions
except Exception: # earlier TF versions
img = tf.image.per_image_whitening(img)
elif is_train == None:
img = img
Expand Down
4 changes: 2 additions & 2 deletions example/tutorial_imagenet_inceptionV3_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ def calculate_metrics(predicted_batch, real_batch, threshold=0.5, is_training=Fa
def run_evaluator(task_spec, checkpoints_path, batch_size=32):
with tf.Graph().as_default():
# load dataset
images_input, one_hot_classes, num_classes, dataset_size = \
images_input, one_hot_classes, num_classes, _dataset_size = \
load_data(file=VAL_FILE,
task_spec=task_spec,
batch_size=batch_size,
epochs=1)
network, predictions = build_network(images_input, num_classes=num_classes, is_training=False)
_network, predictions = build_network(images_input, num_classes=num_classes, is_training=False)
saver = tf.train.Saver()
# metrics
metrics_init_ops, _, metrics_ops = \
Expand Down
2 changes: 1 addition & 1 deletion example/tutorial_inceptionV3_tfslim.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def print_prob(prob):
exit()
try: # TF12+
saver.restore(sess, "./inception_v3.ckpt")
except: # TF11
except Exception: # TF11
saver.restore(sess, "inception_v3.ckpt")
print("Model Restored")

Expand Down
38 changes: 21 additions & 17 deletions example/tutorial_vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
"""

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 *
Expand All @@ -52,15 +48,16 @@


def conv_layers(net_in):
with tf.name_scope('preprocess') as scope:
with tf.name_scope('preprocess'):
"""
Notice that we include a preprocessing layer that takes the RGB image
with pixels values in the range of 0-255 and subtracts the mean image
values (calculated over the entire ImageNet training set).
"""
mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean')
net_in.outputs = net_in.outputs - mean
""" conv1 """

# conv1
network = Conv2dLayer(
net_in,
act=tf.nn.relu,
Expand All @@ -76,7 +73,8 @@ def conv_layers(net_in):
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,
Expand All @@ -92,7 +90,8 @@ def conv_layers(net_in):
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,
Expand All @@ -115,7 +114,8 @@ def conv_layers(net_in):
padding='SAME',
name='conv3_3')
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,
Expand All @@ -138,7 +138,8 @@ def conv_layers(net_in):
padding='SAME',
name='conv4_3')
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,
Expand Down Expand Up @@ -173,25 +174,30 @@ def conv_layers_simple_api(net_in):
"""
mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean')
net_in.outputs = net_in.outputs - mean
""" 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 = 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 = 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')
Expand Down Expand Up @@ -249,5 +255,3 @@ def fc_layers(net):
preds = (np.argsort(prob)[::-1])[0:5]
for p in preds:
print(class_names[p], prob[p])

#
47 changes: 21 additions & 26 deletions example/tutorial_word2vec_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@

"""

import collections
import math
import os
import random
import time

import numpy as np
Expand All @@ -56,10 +52,9 @@
def main_word2vec_basic():
# sess = tf.InteractiveSession()
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
""" Step 1: Download the data, read the context into a list of strings.
Set hyperparameters.
"""

# Step 1: Download the data, read the context into a list of strings.
# Set hyperparameters.
words = tl.files.load_matt_mahoney_text8_dataset()
data_size = len(words)
print(data_size) # 17005207
Expand Down Expand Up @@ -126,8 +121,8 @@ def main_word2vec_basic():
print('%d Steps in a Epoch, total Epochs %d' % (int(data_size / batch_size), n_epoch))
print(' learning_rate: %f' % learning_rate)
print(' batch_size: %d' % batch_size)
""" Step 2: Build the dictionary and replace rare words with 'UNK' token.
"""

# Step 2: Build the dictionary and replace rare words with 'UNK' token.
print()
if resume:
print("Load existing data and dictionaries" + "!" * 10)
Expand All @@ -146,21 +141,21 @@ def main_word2vec_basic():
]) # [5243, 3081, 12, 6, 195, 2, 3135, 46, 59, 156] [b'anarchism', b'originated', b'as', b'a', b'term', b'of', b'abuse', b'first', b'used', b'against']

del words # Hint to reduce memory.
""" Step 3: Function to generate a training batch for the Skip-Gram model.
"""

# Step 3: Function to generate a training batch for the Skip-Gram model.
print()
data_index = 0

batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=4, skip_window=2, data_index=0)
for i in range(8):
print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]])

batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0)
for i in range(8):
print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]])
# exit()
""" Step 4: Build a Skip-Gram model.
"""

# Step 4: Build a Skip-Gram model.
print()

# We pick a random validation set to sample nearest neighbors. Here we limit the
# validation samples to the words that have a low numeric ID, which by
# construction are also the most frequent.
Expand Down Expand Up @@ -208,9 +203,10 @@ def main_word2vec_basic():
similarity = tf.matmul(valid_embed, normalized_embeddings, transpose_b=True)
# multiply all valid word vector with all word vector.
# transpose_b=True, normalized_embeddings is transposed before multiplication.
""" Step 5: Start training.
"""

# Step 5: Start training.
print()

tl.layers.initialize_global_variables(sess)
if resume:
print("Load existing model" + "!" * 10)
Expand All @@ -229,7 +225,7 @@ def main_word2vec_basic():
average_loss = 0
step = 0
print_freq = 2000
while (step < num_steps):
while step < num_steps:
start_time = time.time()
batch_inputs, batch_labels, data_index = tl.nlp.generate_skip_gram_batch(
data=data, batch_size=batch_size, num_skips=num_skips, skip_window=skip_window, data_index=data_index)
Expand Down Expand Up @@ -279,16 +275,17 @@ def main_word2vec_basic():
# learning_rate = float(input("Input new learning rate: "))
# train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
step += 1
""" Step 6: Visualize the normalized embedding matrix by t-SNE.
"""

# Step 6: Visualize the normalized embedding matrix by t-SNE.
print()

final_embeddings = sess.run(normalized_embeddings) #.eval()
tl.visualize.tsne_embedding(final_embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='word2vec_basic')
""" Step 7: Evaluate by analogy questions.
see tensorflow/models/embedding/word2vec_optimized.py
"""

# Step 7: Evaluate by analogy questions. see tensorflow/models/embedding/word2vec_optimized.py
print()
# from tensorflow/models/embedding/word2vec.py

# from tensorflow/models/embedding/word2vec.py
analogy_questions = tl.nlp.read_analogies_file( \
eval_file='questions-words.txt', word2id=dictionary)
# The eval feeds three vectors of word ids for a, b, c, each of
Expand Down Expand Up @@ -352,5 +349,3 @@ def predict(analogy):

if __name__ == '__main__':
main_word2vec_basic()

#
4 changes: 4 additions & 0 deletions tensorlayer/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@

def info(fmt, *args):
logging.info(fmt, *args)


def warning(fmt, *args):
logging.warning(fmt, *args)
16 changes: 8 additions & 8 deletions tensorlayer/cli/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
(Alpha release - usage might change later)

The tensorlayer.cli.train module provides the ``tl train`` subcommand.
It helps the user bootstrap a TensorFlow/TensorLayer program for distributed training
It helps the user bootstrap a TensorFlow/TensorLayer program for distributed training
using multiple GPU cards or CPUs on a computer.

You need to first setup the `CUDA_VISIBLE_DEVICES <http://acceleware.com/blog/cudavisibledevices-masking-gpus>`_
You need to first setup the `CUDA_VISIBLE_DEVICES <http://acceleware.com/blog/cudavisibledevices-masking-gpus>`_
to tell ``tl train`` which GPUs are available. If the CUDA_VISIBLE_DEVICES is not given,
``tl train`` would try best to discover all available GPUs.
``tl train`` would try best to discover all available GPUs.

In distribute training, each TensorFlow program needs a TF_CONFIG environment variable to describe
the cluster. It also needs a master daemon to
the cluster. It also needs a master daemon to
monitor all trainers. ``tl train`` is responsible
for automatically managing these two tasks.
for automatically managing these two tasks.

Usage
-----

tl train [-h] [-p NUM_PSS] [-c CPU_TRAINERS] <file> [args [args ...]]

.. code-block:: bash

# example of using GPU 0 and 1 for training mnist
CUDA_VISIBLE_DEVICES="0,1"
tl train example/tutorial_mnist_distributed.py
Expand Down Expand Up @@ -56,13 +56,13 @@
-----
A parallel training program would require multiple parameter servers
to help parallel trainers to exchange intermediate gradients.
The best number of parameter servers is often proportional to the
The best number of parameter servers is often proportional to the
size of your model as well as the number of CPUs available.
You can control the number of parameter servers using the ``-p`` parameter.

If you have a single computer with massive CPUs, you can use the ``-c`` parameter
to enable CPU-only parallel training.
The reason we are not supporting GPU-CPU co-training is because GPU and
The reason we are not supporting GPU-CPU co-training is because GPU and
CPU are running at different speeds. Using them together in training would
incur stragglers.

Expand Down
Loading