随着深度学习在全世界的普及,我们必须学会使用一些技巧来实现深度学习的算法。Tensorlayer是一个基于tensorflow的高层api,使用tensorlayer可以让我们更好的搭建自己的深度学习模型。
这里有一些关于使用Tensorlayer的技巧,当然你也可以在FQA发现更多的技巧.假如你在实践中发现里一些有用的小技巧,请pull一下我们。如果我们发现这个技巧是合理的,经过确认我们会把它总结在这里。
- 为了使你的TL保持最新的版本,并能够轻易的修改源代码,你可以通过以下命令下载整个tl项目
git clone https://github.com/zsdonghao/tensorlayer.git
,然后把整个tensorlayer
文件夹放在你的项目里 - TL更新的速度十分频繁,如果你使用
pip
安装,我们建议你安装master版本 - 如果你要进行自然语言处理的相关操作,我们建议你安装 NLTK and NLTK data
- TF to TL : use InputLayer
- TL to TF : use network.outputs
- 其他方法 issues7, 多输入问题 issues31
- 使用 network.all_drop 控制训练和测试不同阶段的dropout (for DropoutLayer only) see tutorial_mnist.py and Understand Basic layer
- 或者, 把
is_fix
设置为True
in DropoutLayer, 并通过使用不同的参数,为训练和测试构建不同的图. 你也可以设置不同的batch_size
噪音概率来创建不同的图。这个方法在你使用GaussianNoiseLayer, BatchNormLayer 等,是十分有效的手段. 这里有一个简单的例子:
def mlp(x, is_train=True, reuse=False):
with tf.variable_scope("MLP", reuse=reuse):
tl.layers.set_name_reuse(reuse)
net = InputLayer(x, name='in')
net = DropoutLayer(net, 0.8, True, is_train, 'drop1')
net = DenseLayer(net, 800, tf.nn.relu, 'dense1')
net = DropoutLayer(net, 0.8, True, is_train, 'drop2')
net = DenseLayer(net, 800, tf.nn.relu, 'dense2')
net = DropoutLayer(net, 0.8, True, is_train, 'drop3')
net = DenseLayer(net, 10, tf.identity, 'out')
logits = net.outputs
net.outputs = tf.nn.sigmoid(net.outputs)
return net, logits
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')
net_train, logits = mlp(x, is_train=True, reuse=False)
net_test, _ = mlp(x, is_train=False, reuse=True)
cost = tl.cost.cross_entropy(logits, y_, name='cost')
train_vars = tl.layers.get_variables_with_name('MLP', True, True)
train_op = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost, var_list=train_vars)
- 预训练的 CNN
- 许多应用需要预训练的CNN模型
- TL 的例子里提供了预训练的 VGG16, VGG19, Inception and etc : TL/example
- tl.layers.SlimNetsLayer 允许你使用所有 Tf-Slim pre-trained models
- Resnet
- 利用"for"实现 issues85
- 其他 by @ritchieng
- Use TFRecord, see cifar10 and tfrecord examples; good wrapper: imageflow
- Use python-threading with tl.prepro.threading_data and the functions for images augmentation see tutorial_image_preprocess.py
- 如果你的数据足够小,能够加载到内存里
- 使用 tl.iterate.minibatches 来返回打乱顺序的batch大小的数据和标签.
- 时间序列的数据可以使用 tl.iterate.seq_minibatches, tl.iterate.seq_minibatches2, tl.iterate.ptb_iterator and etc
- 如果你的数据十分庞大
- 使用 tl.prepro.threading_data to read a batch of data at the beginning of every step
- 使用 TFRecord,请参考 cifar10 and tfrecord examples
- 使用 tl.nlp.process_sentence来切分你的句子 ,这个函数需要 NLTK and NLTK data
- 然后使用 tl.nlp.create_vocab 创建一个词典,保存为txt文件(它会返回一个tl.nlp.SimpleVocabulary object让词和词的编号进行对应)
- 最后使用 tl.nlp.Vocabulary 在
tl.nlp.create_vocab
创建的txt文件中创建一个字典对象 - 更多关于句子预处理的函数请查看 tl.prepro 和 tl.nlp
- 使用 tl.layers.retrieve_seq_length_op2 自动从placeholder中计算句子的长度,然后把他输入到
sequence_length
DynamicRNNLayer - 给batch里的数据添加一共0的填充,可以使用下面这个例子:
b_sentence_ids = tl.prepro.pad_sequences(b_sentence_ids, padding='post')
- 其他方法 issues18
- 取消控制台打印: 如果你正在构建一个非常深的神经网络,并不想在控制台看到相关的信息d。你可以使用 tl.ops.suppress_stdout():`:
print("You can see me")
with tl.ops.suppress_stdout():
print("You can't see me") # build your graphs here
print("You can see me")
TL可以和其他TF的包装器在一起使用,如果你使用其他的API编写的代码,或者别人提供的代码,你可以轻而易举的使用它 !
- Keras to TL: KerasLayer (if you find some codes implemented by Keras, just use it. example here)
- TF-Slim to TL: SlimNetsLayer (you can use all Google's pre-trained convolutional models with this layer !!!)
- 将来应该会适配更多的高层API
- RNN cell_fn: 使用 tf.contrib.rnn.{cell_fn} for TF1.0+, 或者 tf.nn.rnn_cell.{cell_fn} for TF1.0-
- cross_entropy: TF1.0+必须定义一个独特的名字
- TL官方网站: Docs, 中文文档, Github
- Learning Deep Learning with TF and TL
- Follow zsdonghao for further examples
- Zhang Rui
- You
- Icy 翻译