【菜菜的CV进阶之路-Tensorflow深入学习-一】TensorFlow Tutorial For Beginners

看了Tensorflow中文官方版的教程和英文官方版的教程,给我的感觉都是:啥啥啥…Tensorflow本质到底是个啥..这函数啥意思来着..这咋又来个函数…还不带给讲一下的!!……

原文地址:https://www.datacamp.com/community/tutorials/tensorflow-tutorial


TensorFlow Tutorial For Beginners

Learn how to build a neural network and how to train, evaluate and optimize it with TensorFlow

Deep learning is a subfield of machine learning that is a set of algorithms that is inspired by the structure and function of the brain.

TensorFlow is the second machine learning framework that Google created and used to design, build, and train deep learning models. You can use the TensorFlow library do to numerical computations, which in itself doesn’t seem all too special, but these computations are done with data flow graphs. In these graphs, nodes represent mathematical operations, while the edges represent the data, which usually are multidimensional data arrays or tensors, that are communicated between these edges.

You seeThe name “TensorFlow” is derived from the operations which neural networks perform on multidimensional data arrays or tensors! It’s literally a flow of tensors. For now, this is all you need to know about tensors, but you’ll go deeper into this in the next sections!

Today’s TensorFlow tutorial for beginners will introduce you to performing deep learning in an interactive way:

  • You’ll first learn more about tensors;
  • Then, the tutorial you’ll briefly go over some of the ways that you can install TensorFlow on your system so that you’re able to get started and load data in your workspace;
  • After this, you’ll go over some of the TensorFlow basics: you’ll see how you can easily get started with simple computations.
  • After this, you get started on the real work: you’ll load in data on Belgian traffic signs and exploring it with simple statistics and plotting.
  • In your exploration, you’ll see that there is a need to manipulate your data in such a way that you can feed it to your model. That’s why you’ll take the time to rescale your images and convert them to grayscale.
  • Next, you can finally get started on your neural network model! You’ll build up your model layer per layer;
  • Once the architecture is set up, you can use it to train your model interactively and to eventually also evaluate it by feeding some test data to it.
  • Lastly, you’ll get some pointers for further improvements that you can do to the model you just constructed and how you can continue your learning with TensorFlow.

Download the notebook of this tutorial here.

Also, you could be interested in a course on Deep Learning in Python, DataCamp’s Keras tutorialor the keras with R tutorial.

Introducing Tensors

To understand tensors well, it’s good to have some working knowledge of linear algebra and vector calculus. You already read in the introduction that tensors are implemented in TensorFlow as multidimensional data arrays, but some more introduction is maybe needed in order to completely grasp tensors and their use in machine learning.

Plane Vectors

Before you go into plane vectors, it’s a good idea to shortly revise the concept of “vectors”; Vectors are special types of matrices, which are rectangular arrays of numbers. Because vectors are ordered collections of numbers, they are often seen as column matrices: they have just one column and a certain number of rows. In other terms, you could also consider vectors as scalar magnitudes that have been given a direction.

Remember: an example of a scalar is “5 meters” or “60 m/sec”, while a vector is, for example, “5 meters north” or “60 m/sec East”. The difference between these two is obviously that the vector has a direction. Nevertheless, these examples that you have seen up until now might seem far off from the vectors that you might encounter when you’re working with machine learning problems. This is normal; The length of a mathematical vector is a pure number: it is absolute. The direction, on the other hand, is relative: it is measured relative to some reference direction and has units of radians or degrees. You usually assume that the direction is positive and in counterclockwise rotation from the reference direction.

Visually, of course, you represent vectors as arrows, as you can see in the picture above. This means that you can consider vectors also as arrows that have direction and length. The direction is indicated by the arrow’s head, while the length is indicated by the length of the arrow.

So what about plane vectors then/p>

Plane vectors are the most straightforward setup of tensors. They are much like regular vectors as you have seen above, with the sole difference that they find themselves in a vector space. To understand this better, let’s start with an example: you have a vector that is 2 X 1. This means that the vector belongs to the set of real numbers that come paired two at a time. Or, stated differently, they are part of two-space. In such cases, you can represent vectors on the coordinate (x,y) plane with arrows or rays.

Working from this coordinate plane in a standard position where vectors have their endpoint at the origin (0,0), you can derive the x coordinate by looking at the first row of the vector, while you’ll find the y coordinate in the second row. Of course, this standard position doesn’t always need to be maintained: vectors can move parallel to themselves in the plane without experiencing changes.

Note that similarly, for vectors that are of size 3 X 1, you talk about the three-space. You can represent the vector as a three-dimensional figure with arrows pointing to positions in the vectors pace: they are drawn on the standard xy and z axes.

It’s nice to have these vectors and to represent them on the coordinate plane, but in essence, you have these vectors so that you can perform operations on them and one thing that can help you in doing this is by expressing your vectors as bases or unit vectors.

Unit vectors are vectors with a magnitude of one. You’ll often recognize the unit vector by a lowercase letter with a circumflex, or “hat”. Unit vectors will come in convenient if you want to express a 2-D or 3-D vector as a sum of two or three orthogonal components, such as the xand yxes, or the zxis.

And when you are talking about expressing one vector, for example, as sums of components, you’ll see that you’re talking about component vectors, which are two or more vectors whose sum is that given vector.

Tip: watch this video, which explains what tensors are with the help of simple household objects!

Tensors

Next to plane vectors, also covectors and linear operators are two other cases that all three together have one thing in common: they are specific cases of tensors. You still remember how a vector was characterized in the previous section as scalar magnitudes that have been given a direction. A tensor, then, is the mathematical representation of a physical entity that may be characterized by magnitude and multiple directions.

And, just like you represent a scalar with a single number and a vector with a sequence of three numbers in a 3-dimensional space, for example, a tensor can be represented by an array of 3R numbers in a 3-dimensional space.

The “R” in this notation represents the rank of the tensor: this means that in a 3-dimensional space, a second-rank tensor can be represented by 3 to the power of 2 or 9 numbers. In an N-dimensional space, scalars will still require only one number, while vectors will require N numbers, and tensors will require N^R numbers. This explains why you often hear that scalars are tensors of rank 0: since they have no direction, you can represent them with one number.

With this in mind, it’s relatively easy to recognize scalars, vectors, and tensors and to set them apart: scalars can be represented by a single number, vectors by an ordered set of numbers, and tensors by an array of numbers.

What makes tensors so unique is the combination of components and basis vectors: basis vectors transform one way between reference frames and the components transform in just such a way as to keep the combination between components and basis vectors the same.

Installing TensorFlow

Now that you know more about TensorFlow, it’s time to get started and install the library. Here, it’s good to know that TensorFlow provides APIs for Python, C++, Haskell, Java, Go, Rust, and there’s also a third-party package for R called .

Tip: if you want to know more about deep learning packages in R, consider checking out DataCamp’s keras: Deep Learning in R Tutorial.

In this tutorial, you will download a version of TensorFlow that will enable you to write the code for your deep learning project in Python. On the TensorFlow installation webpage, you’ll see some of the most common ways and latest instructions to install TensorFlow using , , Docker and lastly, there are also some of the other ways of installing TensorFlow on your personal computer.

Note You can also install TensorFlow with Conda if you’re working on Windows. However, since the installation of TensorFlow is community supported, it’s best to check the official installation instructions.

Now that you have gone through the installation process, it’s time to double check that you have installed TensorFlow correctly by importing it into your workspace under the alias :

Note that the alias that you used in the line of code above is sort of a convention – It’s used to ensure that you remain consistent with other developers that are using TensorFlow in data science projects on the one hand, and with open-source TensorFlow projects on the other hand.

Getting Started With TensorFlow: Basics

You’ll generally write TensorFlow programs, which you run as a chunk; This is at first sight kind of contradictory when you’re working with Python. However, if you would like, you can also use TensorFlow’s Interactive Session, which you can use to work more interactively with the library. This is especially handy when you’re used to working with IPython.

For this tutorial, you’ll focus on the second option: this will help you to get kickstarted with deep learning in TensorFlow. But before you go any further into this, let’s first try out some minor stuff before you start with the heavy lifting.

First, import the  library under the alias , as you have seen in the previous section. Then initialize two variables that are actually constants. Pass an array of four numbers to the  function.

Note that you could potentially also pass in an integer, but that more often than not, you’ll find yourself working with arrays. As you saw in the introduction, tensors are all about arrays! So make sure that you pass in an array :) Next, you can use  to multiply your two variables. Store the result in the  variable. Lastly, print out the  with the help of the function.

 Note that you have defined constants in the DataCamp Light code chunk above. However, there are two other types of values that you can potentially use, namely placeholders, which are values that are unassigned and that will be initialized by the session when you run it. Like the name already gave away, it’s just a placeholder for a tensor that will always be fed when the session is run; There are also Variables, which are values that can change. The constants, as you might have already gathered, are values that don’t change.

The result of the lines of code is an abstract tensor in the computation graph. However, contrary to what you might expect, the  doesn’t actually get calculated. It just defined the model, but no process ran to calculate the result. You can see this in the print-out: there’s not really a result that you want to see (namely, 30). This means that TensorFlow has a lazy evaluation!

However, if you do want to see the result, you have to run this code in an interactive session. You can do this in a few ways, as is demonstrated in the DataCamp Light code chunks below:

 Note that you can also use the following lines of code to start up an interactive Session, run the  and close the Session automatically again after printing the :

In the code chunks above you have just defined a default Session, but it’s also good to know that you can pass in options as well. You can, for example, specify the  argument and then use the  protocol buffer to add configuration options for your session.

For example, if you add

to your Session, you make sure that you log the GPU or CPU device that is assigned to an operation. You will then get information which devices are used in the session for each operation. You could use the following configuration session also, for example, when you use soft constraints for the device placement:

Now that you’ve got TensorFlow installed and imported into your workspace and you’ve gone through the basics of working with this package, it’s time to leave this aside for a moment and turn your attention to your data. Just like always, you’ll first take your time to explore and understand your data better before you start modeling your neural network.

Belgian Traffic Signs: Background

Even though traffic is a topic that is generally known amongst you all, it doesn’t hurt going briefly over the observations that are included in this dataset to see if you understand everything before you start. In essence, in this section, you’ll get up to speed with the domain knowledge that you need to have to go further with this tutorial.

Of course, because I’m Belgian, I’ll make sure you’ll also get some anecdotes :)

  • Belgian traffic signs are usually in Dutch and French. This is good to know, but for the dataset that you’ll be working with, it’s not too important!
  • There are six categories of traffic signs in Belgium: warning signs, priority signs, prohibitory signs, mandatory signs, signs related to parking and standing still on the road and, lastly, designatory signs.
  • On January 1st, 2017, more than 30,000 traffic signs were removed from Belgian roads. These were all prohibitory signs relating to speed.
  • Talking about removal, the overwhelming presence of traffic signs has been an ongoing discussion in Belgium (and by extension, the entire European Union).

Loading And Exploring The Data

Now that you have gathered some more background information, it’s time to download the dataset here. You should get the two zip files listed next to “BelgiumTS for Classification (cropped images), which are called “BelgiumTSC_Training” and “BelgiumTSC_Testing”.

Tip: if you have downloaded the files or will do so after completing this tutorial, take a look at the folder structure of the data that you’ve downloaded! You’ll see that the testing, as well as the training data folders, contain 61 subfolders, which are the 62 types of traffic signs that you’ll use for classification in this tutorial. Additionally, you’ll find that the files have the file extension or Portable Pixmap Format. You have downloaded images of the traffic signs!

Let’s get started with importing the data into your workspace. Let’s start with the lines of code that appear below the User-Defined Function (UDF) :

  • First, set your . This path is the one where you have made the directory with your training and test data.
  • Next, you can add the specific paths to your  with the help of the  function. You store these two specific paths in  and .
  • You see that after, you can call the  function and pass in the  to it.
  • Now, the  function itself starts off by gathering all the subdirectories that are present in the ; It does so with the help of list comprehension, which is quite a natural way of constructing lists – it basically says that, if you find something in the , you’ll double check whether this is a directory, and if it is one, you’ll add it to your list. Remember that each subdirectory represents a label.
  • Next, you have to loop through the subdirectories. You first initialize two lists,  and . Next, you gather the paths of the subdirectories and the file names of the images that are stored in these subdirectories. After, you can collect the data in the two lists with the help of the  function.

Note that in the above code chunk, the training and test data are located in folders named “Training” and “Testing”, which are both subdirectories of another directory “TrafficSigns”. On a local machine, this could look something like “/Users/Name/Downloads/TrafficSigns”, with then two subfolders called “Training” and “Testing”.

Tip: review how to write functions in Python with DataCamp’s Python Functions Tutorial.

Traffic Sign Statistics

With your data loaded in, it’s time for some data inspection! You can start with a pretty simple analysis with the help of the  and  attributes of the  array:

Note that the  and  variables are lists, so you might need to use  to convert the variables to an array in your own workspace. This has been done for you here!

Note that the  that you printed out is, in fact, one single image that is represented by arrays in arrays! This might seem counterintuitive at first, but it’s something that you’ll get used to as you go further into working with images in machine learning or deep learning applications.

Next, you can also take a small look at the , but you shouldn’t see too many surprises at this point:

 These numbers already give you some insights into how successful your import was and the exact size of your data. At first sight, everything has been executed the way you expected it to, and you see that the size of the array is considerable if you take into account that you’re dealing with arrays within arrays.

Tip try adding the following attributes to your arrays to get more information about the memory layout, the length of one array element in bytes and the total consumed bytes by the array’s elements with the , , and  attributes. You can test this out in the IPython console in the DataCamp Light chunk above!

Next, you can also take a look at the distribution of the traffic signs:

 Awesome job! Now let’s take a closer look at the histogram that you made!

 You clearly see that not all types of traffic signs are equally represented in the dataset. This is something that you’ll deal with later when you’re manipulating the data before you start modeling your neu

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年2月25日
下一篇 2019年2月25日

相关推荐