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
8 changes: 2 additions & 6 deletions example/tutorial_atari_pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ def prepro(I):
prev_x = None

if reward != 0:
print(
(
'episode %d: game %d took %.5fs, reward: %f' %
(episode_number, game_number, time.time() - start_time, reward)
), ('' if reward == -1 else ' !!!!!!!!')
)
print(('episode %d: game %d took %.5fs, reward: %f' % (episode_number, game_number, \
time.time() - start_time, reward)), ('' if reward == -1 else ' !!!!!!!!'))
start_time = time.time()
game_number += 1
92 changes: 16 additions & 76 deletions example/tutorial_binarynet_cifar10_tfrecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,86 +148,31 @@ def read_and_decode(filename, is_train=None):
# prepare data in cpu
x_train_, y_train_ = read_and_decode("train.cifar10", True)
x_test_, y_test_ = read_and_decode("test.cifar10", False)

x_train_batch, y_train_batch = tf.train.shuffle_batch(
[x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32
) # set the number of threads here
# set the number of threads here
x_train_batch, y_train_batch = tf.train.shuffle_batch([x_train_, y_train_], \
batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32)
# for testing, uses batch instead of shuffle_batch
x_test_batch, y_test_batch = tf.train.batch(
[x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32
)
x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], \
batch_size=batch_size, capacity=50000, num_threads=32)

def model(x_crop, y_, reuse):
""" For more simplified CNN APIs, check tensorlayer.org """
W_init = tf.truncated_normal_initializer(stddev=5e-2)
W_init2 = tf.truncated_normal_initializer(stddev=0.04)
b_init2 = tf.constant_initializer(value=0.1)
with tf.variable_scope("model", reuse=reuse):
net = tl.layers.InputLayer(x_crop, name='input')
net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1')
net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', name='cnn1')
net = tl.layers.SignLayer(net)
net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1')
net = tl.layers.LocalResponseNormLayer(
net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1'
)
net = tl.layers.BinaryConv2d(
net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2'
)
net = tl.layers.LocalResponseNormLayer(
net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2'
)
net = tl.layers.LocalResponseNormLayer(net, 4, 1.0, 0.001 / 9.0, 0.75, name='norm1')
net = tl.layers.BinaryConv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', name='cnn2')
net = tl.layers.LocalResponseNormLayer(net, 4, 1.0, 0.001 / 9.0, 0.75, name='norm2')
net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2')
net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
net = tl.layers.FlattenLayer(net, name='flatten')
net = tl.layers.SignLayer(net)
net = tl.layers.BinaryDenseLayer(
net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu'
) # output: (batch_size, 384)
net = tl.layers.BinaryDenseLayer(net, 384, act=tf.nn.relu, name='d1relu')
net = tl.layers.SignLayer(net)
net = tl.layers.BinaryDenseLayer(
net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu'
) # output: (batch_size, 192)
net = tl.layers.DenseLayer(
net, n_units=10, act=tf.identity, W_init=W_init2, name='output'
) # output: (batch_size, 10)
y = net.outputs

ce = tl.cost.cross_entropy(y, y_, name='cost')
# L2 for the MLP, without this, the accuracy will be reduced by 15%.
L2 = 0
for p in tl.layers.get_variables_with_name('relu/W', True, True):
L2 += tf.contrib.layers.l2_regularizer(0.004)(p)
cost = ce + L2
net = tl.layers.BinaryDenseLayer(net, 192, act=tf.nn.relu, name='d2relu')
net = tl.layers.DenseLayer(net, 10, act=tf.identity, name='output')

# correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.int32), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

return net, cost, acc

def model_batch_norm(x_crop, y_, reuse, is_train):
""" Batch normalization should be placed before rectifier. """
W_init = tf.truncated_normal_initializer(stddev=5e-2)
W_init2 = tf.truncated_normal_initializer(stddev=0.04)
b_init2 = tf.constant_initializer(value=0.1)
with tf.variable_scope("model", reuse=reuse):
net = InputLayer(x_crop, name='input')

net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), padding='SAME', W_init=W_init, b_init=None, name='cnn1')
net = tl.layers.BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch1')
net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1')
net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), padding='SAME', W_init=W_init, b_init=None, name='cnn2')
net = tl.layers.BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2')
net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2')
net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
net = tl.layers.DenseLayer(
net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu'
) # output: (batch_size, 384)
net = tl.layers.DenseLayer(
net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu'
) # output: (batch_size, 192)
net = tl.layers.DenseLayer(
net, n_units=10, act=tf.identity, W_init=W_init2, name='output'
) # output: (batch_size, 10)
y = net.outputs

ce = tl.cost.cross_entropy(y, y_, name='cost')
Expand All @@ -237,6 +182,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
L2 += tf.contrib.layers.l2_regularizer(0.004)(p)
cost = ce + L2

# correct_prediction = tf.equal(tf.argmax(tf.nn.softmax(y), 1), y_)
correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.int32), y_)
acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

Expand All @@ -249,12 +195,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
# cost, acc, network = model(x_crop, y_, None)

with tf.device('/gpu:0'): # <-- remove it if you don't have GPU
## using local response normalization
network, cost, acc, = model(x_train_batch, y_train_batch, False)
_, cost_test, acc_test = model(x_test_batch, y_test_batch, True)
## you may want to try batch normalization
# network, cost, acc, = model_batch_norm(x_train_batch, y_train_batch, None, is_train=True)
# _, cost_test, acc_test = model_batch_norm(x_test_batch, y_test_batch, True, is_train=False)

## train
n_epoch = 50000
Expand Down Expand Up @@ -297,10 +239,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train):
n_batch += 1

if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
print(
"Epoch %d : Step %d-%d of %d took %fs" %
(epoch, step, step + n_step_epoch, n_step, time.time() - start_time)
)
print("Epoch %d : Step %d-%d of %d took %fs" % \
(epoch, step, step + n_step_epoch, n_step, time.time() - start_time))
print(" train loss: %f" % (train_loss / n_batch))
print(" train acc: %f" % (train_acc / n_batch))

Expand Down
2 changes: 0 additions & 2 deletions example/tutorial_binarynet_mnist_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# -*- coding: utf-8 -*-

import time

import tensorflow as tf

import tensorlayer as tl

X_train, y_train, X_val, y_val, X_test, y_test = \
Expand Down
11 changes: 4 additions & 7 deletions example/tutorial_cartpole_ac.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ def learn(self, s, r, s_):
sess = tf.Session()

actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A)
critic = Critic(
sess, n_features=N_F, lr=LR_C
) # we need a good teacher, so the teacher should learn faster than the actor
# we need a good teacher, so the teacher should learn faster than the actor
critic = Critic(sess, n_features=N_F, lr=LR_C)

tl.layers.initialize_global_variables(sess)

Expand Down Expand Up @@ -193,10 +192,8 @@ def learn(self, s, r, s_):
running_reward = running_reward * 0.95 + ep_rs_sum * 0.05
# start rending if running_reward greater than a threshold
# if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True
print(
"Episode: %d reward: %f running_reward %f took: %.5f" %
(i_episode, ep_rs_sum, running_reward, time.time() - episode_time)
)
print("Episode: %d reward: %f running_reward %f took: %.5f" % \
(i_episode, ep_rs_sum, running_reward, time.time() - episode_time))

# Early Stopping for quick check
if t >= MAX_EP_STEPS:
Expand Down
56 changes: 8 additions & 48 deletions example/tutorial_cifar10.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-

# tl.prepro for data augmentation

import time

import numpy as np
import tensorflow as tf

import tensorlayer as tl
from tensorlayer.layers import *

Expand All @@ -23,36 +19,17 @@ def model(x, y_, reuse):
with tf.variable_scope("model", reuse=reuse):
net = InputLayer(x, name='input')
net = Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1')
# net = Conv2dLayer(net, act=tf.nn.relu, shape=[5, 5, 3, 64],
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
# W_init=W_init, name ='cnn1') # output: (batch_size, 24, 24, 64)
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1')
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
# padding='SAME', pool = tf.nn.max_pool, name ='pool1',)# output: (batch_size, 12, 12, 64)
net = LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
# net.outputs = tf.nn.lrn(net.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
# beta=0.75, name='norm1')

net = Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2')
# net = Conv2dLayer(net, act=tf.nn.relu, shape=[5, 5, 64, 64],
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5 patch
# W_init=W_init, name ='cnn2') # output: (batch_size, 12, 12, 64)
net = LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
# net.outputs = tf.nn.lrn(net.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
# beta=0.75, name='norm2')
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2')
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
# padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)
net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
net = DenseLayer(
net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu'
) # output: (batch_size, 384)
net = DenseLayer(
net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu'
) # output: (batch_size, 192)
net = DenseLayer(
net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output'
) # output: (batch_size, 10)

net = FlattenLayer(net, name='flatten')
net = DenseLayer(net, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu')
net = DenseLayer(net, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu')
net = DenseLayer(net, 10, act=tf.identity, W_init=W_init2, name='output')
y = net.outputs

ce = tl.cost.cross_entropy(y, y_, name='cost')
Expand All @@ -75,35 +52,18 @@ def model_batch_norm(x, y_, reuse, is_train):
b_init2 = tf.constant_initializer(value=0.1)
with tf.variable_scope("model", reuse=reuse):
net = InputLayer(x, name='input')

net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME', W_init=W_init, b_init=None, name='cnn1')
# net = Conv2dLayer(net, act=tf.identity, shape=[5, 5, 3, 64],
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5x3 patch
# W_init=W_init, b_init=None, name='cnn1') # output: (batch_size, 24, 24, 64)
net = BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch1')
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1')
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
# padding='SAME', pool=tf.nn.max_pool, name='pool1',) # output: (batch_size, 12, 12, 64)

net = Conv2d(net, 64, (5, 5), (1, 1), padding='SAME', W_init=W_init, b_init=None, name='cnn2')
# net = Conv2dLayer(net, act=tf.identity, shape=[5, 5, 64, 64],
# strides=[1, 1, 1, 1], padding='SAME', # 64 features for each 5x5 patch
# W_init=W_init, b_init=None, name ='cnn2') # output: (batch_size, 12, 12, 64)
net = BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2')
net = MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2')
# net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
# padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64)

net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304)
net = DenseLayer(
net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu'
) # output: (batch_size, 384)
net = DenseLayer(
net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu'
) # output: (batch_size, 192)
net = DenseLayer(
net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output'
) # output: (batch_size, 10)
net = DenseLayer(net, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu')
net = DenseLayer(net, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu')
net = DenseLayer(net, 10, act=tf.identity, W_init=W_init2, name='output')
y = net.outputs

ce = tl.cost.cross_entropy(y, y_, name='cost')
Expand Down
Loading