Code Monkey home page Code Monkey logo

tl-tricks's Introduction

如何使用Tensorlayer

随着深度学习在全世界的普及,我们必须学会使用一些技巧来实现深度学习的算法。Tensorlayer是一个基于tensorflow的高层api,使用tensorlayer可以让我们更好的搭建自己的深度学习模型。

这里有一些关于使用Tensorlayer的技巧,当然你也可以在FQA发现更多的技巧.假如你在实践中发现里一些有用的小技巧,请pull一下我们。如果我们发现这个技巧是合理的,经过确认我们会把它总结在这里。

1. 安装

  • 为了使你的TL保持最新的版本,并能够轻易的修改源代码,你可以通过以下命令下载整个tl项目 git clone https://github.com/zsdonghao/tensorlayer.git,然后把整个tensorlayer文件夹放在你的项目里
  • TL更新的速度十分频繁,如果你使用pip安装,我们建议你安装master版本
  • 如果你要进行自然语言处理的相关操作,我们建议你安装 NLTK and NLTK data

2. TF 和 TL之间的衔接

3. 训练/测试切换

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')

4. 获取训练的变量

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)
  • 这个方法可以用来在训练时冻结某些层,只要简单的不获取这些变量
  • 其他方法 issues17, issues26, FQA

5. 预训练的 CNN 和 Resnet

6. 数据增强

7. 批量数据使用

8. 句子切分

9. 动态RNN和句子长度

b_sentence_ids = tl.prepro.pad_sequences(b_sentence_ids, padding='post')

10. 共性的问题

  • 导入Tensorlayer时Matplotlib出现问题 issues, FQA

11. 其他技巧

  • 取消控制台打印: 如果你正在构建一个非常深的神经网络,并不想在控制台看到相关的信息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")

12. 使用其他的TF包装器

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

13. 不同版本TF的适用

有用的链接

作者

  • Zhang Rui
  • You
  • Icy 翻译

tl-tricks's People

Contributors

xls1994 avatar

Stargazers

模糊限制信息平台 avatar  avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.