TF Novice Tutorial (i)

Source: Internet
Author: User
Tags versions xgboost
Introduction

Written in the previous words:
In the process of deep learning, we inevitably need to understand and use some deep learning framework, among which the most famous should be the TensorFlow deep learning framework supported by Google.
TensorFlow is an open source software library that uses data flow graphs technology to perform numerical calculations.

A data flow diagram is a graph that uses a node (usually a circular or square description that represents a mathematical operation or the starting point of a data entry and the end point of the output) and a line (representing a number, matrix, or tensor tensor) to describe a mathematical calculation. The flow chart can easily allocate each node to different computing devices to complete asynchronous parallel computing, which is suitable for large-scale machine learning applications.

The first contact with TensorFlow will be shocked by its huge and updated iterative fast code base, which needs to be explained in TensorFlow's official website, which is defined as "an open source software library for machine Intelligence", that is, TensorFlow is an open source software library that uses data flow graphs for numerical calculations. Instead of including TensorFlow in the deep learning framework, they are included with Theano in the graph compiler (graph compilers) category.
TensorFlow to the C API as a boundary can be divided into the front-end system (provide programming model, responsible for building calculation diagram) and back-end system (provide the operating environment, responsible for the implementation of the calculation diagram):

My feeling is that TensorFlow is a very good frame, but very low-level. With TensorFlow you need to write a lot of code and you have to reinvent the wheel over and over. Therefore, with the development of deep learning and the extension of application fields, more platforms appear on the above layer: Slim, prettytensor, Keras, Tflearn and so on.
Here I chose Tensorlayer, as a Python package in the later habitat, it is not only quick to use, examples are many, and in the speed of operation, engineering and scalability of excellent performance.
From the installation to the use, as a novice, I appeared a lot of problems, took many detours, so I hereby write down the blog, to their own vigilance, but also hope to the latter with advice and direction. installation 1. Cuda and Cudnn

I chose to install the GPU version of TensorFlow, so I need to pre-preinstall Cuda and CUDNN before you need to confirm that your graphics card is supported. These can be found in the official TensorFlow tutorials, which are similar in every version of the Web.
The problem I met here is that although the paths are added exactly as they are, there is always no way to invoke the GPU version of TensorFlow in the final test (each import TensorFlow will error)
There are more ways to solve this problem on the Internet, but there is no effect on the basic test. Finally, a simple and crude solution was found:
copy and paste the Cudnn file directly into the Cuda folder with the same name , then you can solve the call problem, pro-test effective. 2.anaconda Quick Install and direct Mount Python

If you give me a chance to choose, I will choose to install Python directly. Although the online said Anaconda installation concise, will come with a lot of installation package, interface friendly and so on, but the following is my use of the process encountered several problems.

The Spyder style is similar to MATLAB, for researchers may be more friendly, for programmers more prone to the code of the dog may be more inconvenient to use, and I personally like the Pycharm interface change function, white background for a long time will be more hurt eyes.

Anaconda's own version of Python is currently 3.6 (may be updated later), but TensorFlow currently supports only 3.5 versions of Python. So the use of Anaconda directly after the installation of a fool (if the novice may not initially consider the version compatibility issue), will be in the process of installing TF error, there are generally two solutions: the first is to reinstall the 3.5 version of Anaconda, the second is to create a python= 3.5 Environment, install TensorFlow in this environment.

I chose a more convenient and efficient second method, directly built a new environment, Based on the python3.5, and then installed on this basis TensorFlow, but in the test process always error (here I installed GPU version, if the demand is not recommended to install the CPU version, really convenient and good installation), this time with the shortcomings of Anaconda fully exposed: Most of the use of T Ensorflow learners are directly installed Python or in the Ubuntu environment, so the solution to Anaconda encounter TensorFlow related problems is really very little, sometimes the same error, problem-solving code can not be universal, Some command statements are not the same (the difference between Pip and Conda)

Here I met a lot of problems, looked up a lot of information, almost every blogger mentioned the solution I have tried once (but really very little information), the final solution is to solve the problem is: the anaconda in the python3.5 environment directly re-installed again . This is the line of code:

Create-n TensorFlow python=3.5 Anaconda

Summing up, this Part I go a lot of detours, the most recommended is that the direct installation of python3.5, or in the 3.5 environment of the Anaconda directly to reload it again. Use Cases Code Modification

As mentioned above, TensorFlow is a rapidly evolving learning framework that requires a lot of dependencies in tensorflow execution, such as numpy,scipy,scikit-image,matplotlib and NLTK, etc. , and the updates to these dependent packages themselves are developing at a different pace, with inconsistent schedules. This also directly leads to, in the process of using TensorFlow, will continue to meet because the version is not supported, or the interface has changed, or because the version of the upgrade caused by the code is not available, and so on.
The biggest headache here is the code conversion problem between python2.x and Python3.
All the test cases in TensorFlow's official tutorial are based on Python2, and now Python2 is no longer updated, instead of Python3, following the principle of learning new and not learning the old, I chose python3.5-based TensorFlow, but between the two versions, both the function name, the interface class, and many other things have changed enormously, which has led to an official use case that is not operational at the time of actual operation.
Here to paste the partially modified Python3-based code, the pro-test can be run:

The official first test case:

The import TensorFlow
as TF import NumPy

as NP # uses NumPy to generate false data (phony data) with a total of 100 points.
X_data = Np.float32 (Np.random.rand (2, 100)) # random input
y_data = Np.dot ([0.100, 0.200], x_data) + 0.300

# Constructs a linear model
# 
B = tf. Variable (Tf.zeros ([1]))
W = tf. Variable (Tf.random_uniform ([1, 2], -1.0, 1.0))
y = Tf.matmul (W, x_data) + b

# minimize variance
loss = Tf.reduce_mean (TF . Square (Y-y_data))
optimizer = Tf.train.GradientDescentOptimizer (0.5)
train = optimizer.minimize (loss)

# Initialize variable
init = Tf.global_variables_initializer ()

# Start graph (graph)
sess = tf. Session ()
sess.run (init)

# Fitting Planar
for step in range (0, 201):
    Sess.run (train)
    if step% 20 = = 0:< C22/>print (step, Sess.run (W), Sess.run (b))

# get the best fit result W: [[0.100  0.200]], B: [0.300]

The official second test case (handwritten string recognition):

#-*-Coding:utf-8-*-import tensorflow as TF import tensorlayer as tl Sess = tf. InteractiveSession () # Prepare Data x_train, Y_train, X_val, Y_val, x_test, y_test = \ TL . Files.load_mnist_dataset (Shape= ( -1,784)) # define Placeholder x = Tf.placeholder (Tf.float32, Shape=[none, 784], name= ' X ') Y_ = Tf.placeholder (Tf.int64, Shape=[none,], name= ' Y_ ') # define the network Network = Tl.layers.InputLayer (x, name= ' Input_layer ') network = Tl.layers.DropoutLayer (Network, keep=0.8, name= ' drop1 ') network = Tl.layers.DenseLayer ( Network, n_units=800, act = tf.nn.relu, name= ' relu1 ') network = Tl.layers.DropoutLayer (net Work, keep=0.5, name= ' drop2 ') network = Tl.layers.DenseLayer (Network, n_units=800, act = t F.nn.relu, name= ' relu2 ') network = Tl.layers.DropoutLayer (Network, keep=0.5, name= ' drop3 ') # The Softmax is implemented in Ternally in Tl.cost.cross_entropy (Y, Y_, "cost") to # Speed up computation, so we use the identity here.
                                # See tf.nn.sparse_softmax_cross_entropy_with_logits () network = Tl.layers.DenseLayer (Network, n_units=10, act = tf.identity, name= ' Output_layer ') # define cost function and metric
. y = network.outputs cost = tl.cost.cross_entropy (Y, y_, ' cost ') Correct_prediction = Tf.equal (Tf.argmax (y, 1), y_) acc = t F.reduce_mean (Tf.cast (correct_prediction, tf.float32)) Y_op = Tf.argmax (Tf.nn.softmax (y), 1) # define the optimizer Trai
                            N_params = Network.all_params Train_op = Tf.train.AdamOptimizer (learning_rate=0.0001, beta1=0.9, beta2=0.999,  epsilon=1e-08, Use_locking=false). Minimize (Cost, Var_list=train_params) # Initialize all variables in the Session Tl.layers.initialize_global_variables (Sess) # Print network Information network.print_params () Network.print_ Layers () # Train the network Tl.utils.fit (sess, network, TRAIN_OP, cost, X_train, Y_train, X, Y_, ACC=ACC, batch_size=500, n_epoch=500, print_freq=5, X_val=x_val, Y_val=y_val, Eval_train=false) # evaluation TL.UTILS.T EST (sess, Network, ACC, X_test, Y_test, X, Y_, Batch_size=none, Cost=cost) # Save the network to. npz file Tl.files.save_ Npz (Network.all_params, name= ' Model.npz ') sess.close ()

Here I come across a problem like this:
Runtimeerror:module compiled against API version 0xb but this version of the NumPy is 0xa

That is, I am prompted that the version of API interface and NumPy version do not correspond. Here need uninstall numpy then reinstall, if unsuccessful, try to restart the compiler or the computer, if it is still unsuccessful, may be the computer has a numpy version of the two, then the Spyder default use of the old version, You need to delete the specified version of NumPy and then reinstall the new Edition (here I did not encounter, it should be my direct lazy installation, in 3.5 of the environment to reinstall the Anaconda reason, so directly uninstall can)

Here will be directly exposed to a previously mentioned problem, online this aspect of Anaconda solution is not much, most of the PIP command in the Conda modified can not be used, so meet the problem to solve it is really troublesome, It would be friendly to install Python directly under Ubuntu.

PS: When I wrote here, I found this small example in TensorFlow's practical tutorial:
Introduce the TensorFlow library and review the version to ensure that the latest stable version (such as 1.2.1) is installed.

From __future__ import print_function, Division 
import TensorFlow as tf
 print (' Loaded tf version ', Tf.__version_ _)
 # output:loaded TF version 1.2.1  

For compatibility with the print and division functions of Python2 and Python3, it is recommended that each file used is introduced from the future
Import Print_function, Division

I do not know whether Python version compatibility issues, will be added in the continuation of learning later.

The next section will be written in the following sections:

How GitHub Desktop is used (it can greatly improve the speed of reading and writing code and the clone code base)

In-depth study of related documents:
The links are as follows :
1. TensorFlow Official Tutorials:
2. Official documentation for Tensorlyer: xgboostan fool Installation

Anaconda Search-t Conda Xgboost

Conda install-c Mndrake Xgboost

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.