第2.6.3,教材提供的示例在:https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mlp_dropout2.py
与上一节2.6.2一样,我直接删了tl.logging.set_verbosity(tl.logging.DEBUG)这条,并直接把act=tf.identity定义了。
示例里是少了tl.layers.set_name_reuse(reuse)这行的,但教材是有的,如果这行没有的话是会报错:
Exception: Layer 'MLP/input' already exists, please choice other 'name' or reuse this layer
import time
import tensorflow as tf
import tensorlayer as tl
tf.logging.set_verbosity(tf.logging.DEBUG)
sess = tf.InteractiveSession()
X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 784))
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None], name='y_')
def mlp(x, is_train=True, reuse=False):
with tf.variable_scope("MLP", reuse=reuse):
tl.layers.set_name_reuse(reuse)
network = tl.layers.InputLayer(x, name='input')
network = tl.layers.DropoutLayer(network, keep=0.8, is_fix=True, is_train=is_train, name='drop1')
network = tl.layers.DenseLayer(network, n_units=800, act=tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True, is_train=is_train, name='drop2')
network = tl.layers.DenseLayer(network, n_units=800, act=tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True,