Giter VIP home page Giter VIP logo

tensorlayer-tricks's Introduction

How to use TensorLayer

While research in Deep Learning continues to improve the world, we use a bunch of tricks to implement algorithms with TensorLayer day to day.

Here are a summary of the tricks to use TensorLayer, you can also find more tricks in FQA.

If you find a trick that is particularly useful in practice, please open a Pull Request to add it to the document. If we find it to be reasonable and verified, we will merge it in.

1. Installation

  • To keep your TL version and edit the source code easily, you can download the whole repository by excuting git clone https://github.com/zsdonghao/tensorlayer.git in your terminal, then copy the tensorlayer folder into your project
  • As TL is growing very fast, if you want to use pip install, we suggest you to install the master version
  • For NLP application, you will need to install NLTK and NLTK data

2. Interaction between TF and TL

3. Training/Testing switching

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. Get variables for training

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)
  • This method can also be used to freeze some layers during training, just simply don't get some variables
  • Other methods issues17, issues26, FQA

5. Pre-trained CNN and Resnet

6. Data augmentation

7. Batch of data

8. Sentences tokenization

9. Dynamic RNN and sequence length

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

10. Common problems

  • Matplotlib issue arise when importing TensorLayer issues, FQA

11. Other tricks

  • Disable console logging: if you are building a very deep network and don't want to view them in the terminal, disable print by with 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. Compatibility with other TF wrappers

TL can interact with other TF wrappers, which means if you find some codes or models implemented by other wrappers, you can just use it !

  • 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 !!!)
  • I think more libraries will be compatible with TL

13. Compatibility with different TF versions

Useful links

Author

  • Zhang Rui
  • You

tensorlayer-tricks's People

Contributors

wagamamaz avatar

Watchers

Chen Shi 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.