How to effectively learn TensorLayer¶
No matter what stage you are in, we recommend you to spend just 10 minutes to read the source code of TensorLayer and the Understand layer / Your layer in this website, you will find the abstract methods are very simple for everyone. Reading the source codes helps you to better understand TensorFlow and allows you to implement your own methods easily. For discussion, we recommend Gitter, Help Wanted Issues, QQ group and Wechat group.
For people who new to deep learning, the contributors provided a number of tutorials in this website, these tutorials will guide you to understand autoencoder, convolutional neural network, recurrent neural network, word embedding and deep reinforcement learning and etc. If your already understand the basic of deep learning, we recommend you to skip the tutorials and read the example codes on Github , then implement an example from scratch.
For people from industry, the contributors provided mass format-consistent examples covering computer vision, natural language processing and reinforcement learning. Besides, there are also many TensorFlow users already implemented product-level examples including image captioning, semantic/instance segmentation, machine translation, chatbot and etc., which can be found online. It is worth noting that a wrapper especially for computer vision Tf-Slim can be connected with TensorLayer seamlessly. Therefore, you may able to find the examples that can be used in your project.
For people from academia, TensorLayer was originally developed by PhD students who facing issues with other libraries on implement novel algorithm. Installing TensorLayer in editable mode is recommended, so you can extend your methods in TensorLayer. For research related to image processing such as image captioning, visual QA and etc., you may find it is very helpful to use the existing Tf-Slim pre-trained models with TensorLayer (a specially layer for connecting Tf-Slim is provided).
Exclude some layers from training¶
You may need to get the list of variables you want to update, TensorLayer provides two ways to get the variables list.
The first way is to use the all_params of a network, by default, it will store the variables in order.
You can print the variables information via
To choose which variables to update, you can do as below.
train_params = network.trainable_weights[3:]
The second way is to get the variables by a given name. For example, if you want to get all variables which the layer name contains
dense, you can do as below.
train_params = network.get_layer('dense').trainable_weights
After you get the variable list, you can define your optimizer like that so as to update only a part of the variables.
train_weights = network.trainable_weights optimizer.apply_gradients(zip(grad, train_weights))
TensorLayer adopts the Python logging module to log running information. The logging module would print logs to the console in default. If you want to configure the logging module, you shall follow its manual.
Cannot Save Image¶
If you run the script via SSH control, sometimes you may find the following error.
_tkinter.TclError: no display name and no $DISPLAY environment variable
If this happens, run
sudo apt-get install python3-tk or
import matplotlib and
import tensorlayer as tl.
Alternatively, add the following code into the top of
visualize.py or in your own code.
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt
Install Master Version¶
To use all new features of TensorLayer, you need to install the master version from Github. Before that, you need to make sure you already installed git.
[stable version] pip install tensorlayer [master version] pip install git+https://github.com/tensorlayer/tensorlayer.git
Download the TensorLayer folder from Github.
Before editing the TensorLayer
If your script and TensorLayer folder are in the same folder, when you edit the
.pyinside TensorLayer folder, your script can access the new features.
If your script and TensorLayer folder are not in the same folder, you need to run the following command in the folder contains
setup.pybefore you edit
.pyinside TensorLayer folder.pip install -e .
Note that, the
tl.files.load_npz() can only able to load the npz model saved by
If you have a model want to load into your TensorLayer network, you can first assign your parameters into a list in order,
tl.files.assign_params() to load the parameters into your TensorLayer model.