** Practical machine learning with Scikit-Learn and TensorFlow-Scikit-Learn edition- ** is a continuation.
The first half (up to Chapter 8) was mostly about Scikit-Learn. In the second half (Chapter 9 and beyond), we'll move on to TensorFlow.
Before getting into the content, I will summarize the sites that will be helpful as a memo. ・ Sample code (GitHub) ・ TensorFlow official website ・ PFN Chainer Tutorial ・ Book Information
In conclusion, considering that TensorFlow2 is the current situation, I felt that learning with TensorFlow1 was not very efficient.
TensorFlow1 and 2 feel like different things, and TensorFlow2 seems to be much better. At the beginning, it has an atmosphere like "TensorFlow is characterized by starting and closing Session", but in TensorFlow2, this Session is abolished ...
It's like this from the beginning of TensorFlow, so when I try to put it into practice, I'm not sure what has changed and what hasn't changed, and it's already difficult. Even if you are looking it up, it will melt in time with just a little research.
I think there is an opinion that you should study with TensorFlow 1 for the time being, but for my purpose this time, I want to be able to use it in practice, not to study TensorFlow, so that's a little different. right. I wonder if I will change the book to one that supports TensorFlow 2 ... (This book was not cheap)
However, there are many things to study just because the version is different. I will read it again once I reach the stage where practice is set aside. Currently, it is a phase that I want you to move for the time being, so it seems that it will be a role to warm the bookshelf for a while.
For the time being, I wrote down to Qiita halfway through Chapter 9 (the first chapter of TensorFlow), so the article continues below, but it is subtle whether the crappy content is stale and worth reading.
By the way, out of all 16 chapters of practical machine learning by Scikit-Learn and TensorFlow, I have lost my motivation to read up to 13 chapters, so for the time being, studying Deep Learning is another opportunity. The number of good books has increased, so I will wait for it to saturate.
Study day: 2020/6/21
My knowledge begins with "What is TensorFlow in the first place? A library for deep learning ??" It's a crazy story, and it's hard to read from this level of knowledge.
In the book
① Being powerful open source software for numerical calculation (2) Being tuned especially for large-scale machine learning ③ The basic principle is that if you define a calculation graph to be executed in Python, TensorFlow will read the graph and execute it efficiently with optimized C ++ code. ④ The most important thing is that graphs can be processed in parallel by CPU or GPU. ⑤ Supports distributed computing
It is written that.
To be honest, when I read it at a glance, it felt like "what a mess", and what is the relationship between TensorFlow and calculation graphs in the first place? I had a question. If you read it carefully, it seems that TensorFlow basically follows the order of "creating a calculation graph → executing a calculation". I see.
There is an isolated environment, but I have never created one because I have never needed a virtual environment. I thought this was a good opportunity, so I researched this and created a virtual environment.
In the book, it was written that it was made with ** virtualenv **, but I wanted to make it with Anaconda, so I referred to the following site.
・ [Python] Building a virtual environment using Anaconda -[Anaconda] How to build a virtual environment with the conda command
I noticed when installing TensorFlow, but it seems that the version of TensorFlow in the book is 1, while the current version is 2. </ font> Moreover, it seems that the usability is different. .. ..
Hmmm, hmmm, uh-...
Well, for the time being, I wonder if I can study the details later or buy another book by using the tactics that I found out by sticking to the sample code.
I couldn't understand that "** Nothing happens when I make a calculation graph **", so I read it over and over again. It's just written, but it feels like "why" looped.
From the point of view of using TensorFlow for the first time, it was very novel to first create a box of variables.
After creating the graph, start the calculation execution session with the ** Session () ** command, and explicitly declare the end with ** Session.close () ** when the calculation is completed. Moreover, everything in the session is executed by ** Session.run () **.
This shows how to use ** with ** because there is a risk of doing other things even though the session is not closed. I personally like this one.
【reference】 ・ Introduction of tensorflow 2.0 (Japanese translation) -Changes in TensorFlow 2.0 from the Design Document
For example, suppose $ f = x ^ 2y + y + 2 $ is $ x = 3 \ ,, , y = 4 $ and you want to calculate using this graph.
In TensorFlow1,
import tensorflow as tf
#★ Construction of calculation graph
x = tf.Variable(3, name = "x")
y = tf.Variable(4, name = "y")
f = x*x*y + y +2
#★ Execution of calculation
sess = tf.Session() #Open a TensorFlow session
#It is absolutely necessary to perform calculations using a calculation graph
sess.run(x.initializer) #Variable initialization
sess.run(y.initializer)
result = sess.run(f) #Rating of f
print(result) #Display of calculation results
sess.close #Close TensorFlow
Not to mention the output
42
is. In because there is no concept of Session TensorFlow2 you this is not available, but I wonder how do you because the basic concept is not well understood. .. .. For the time being, check this and that, and Official says that tf.function is good, so I will use it.
import tensorflow as tf
#Set of variables
x = tf.Variable(3)
y = tf.Variable(4)
# tf.Create a function with function
@tf.function
def f(x, y):
return x*x*y + y + 2
result = f(x, y)
print(result)
The output looks like this. Certainly easy to understand. I mean, TensorFlow2 is more like Python.
tf.Tensor(42, shape=(), dtype=int32)
I wasn't sure what I was saying. I will look it up later.
It was like that. First of all, I was impressed by the fact that TensorFlow automatically picks up the dependencies between nodes, and then I was impressed by the fact that the calculation results are not reused.
Not being reused means, for example,
w = tf.constant(3)
x = w + 2
y = x + 5
z = x * 3
To evaluate $ y $ and $ z $ when there is a calculation
1⃣
1.
2⃣
1.
It seems that the process is done. It seems that $ w = 3 $ and $ x = w + 2 $ are both necessary when calculating $ y $ and when calculating $ z $, but they are recalculated. is. In other words, since the deficit part is the same, humans calculate it only once, but TensorFlow calculates it twice.
If you want to process it all at once, use ** with **.
with tf.Session() as sess:
y_val, z_val = sess.run([y, z])
But I wonder if this is also TensorFlow2 ...
Suddenly, TensorFlow is doing a linear regression using a California home price dataset. It's not just a library for deep learning.
It explains the calculation of three types of gradients. That is, ・ Manual gradient calculation ・ Use automatic differentiation ・ Use the optimizer is. The manual is a straightforward way to write code and calculate, and automatic differentiation seems to be an excellent thing that TensorFlow automatically calculates the gradient in an efficient way. The optimizer only mentions "better than automatic differentiation" in the explanation of the book, but is it a function for optimization?
Placeholders-The usage is written, but it seems that placeholders are abolished in TensorFlow2. I feel that TensorFlow 1 and 2 are very different, but I'm wondering if it's really best to study with this book.
Sometimes you want to restore a model so that you can reuse it, use it in other programs, or compare it to other models. In addition, if you want to start the training from the middle instead of starting from the beginning, you need to restore it. In TensorFlow, this restoration seems to be very easy.
The book describes how to use restore (), but Official showed how to use keras. This is certainly convenient.
[1] [TensorFlow 2.0 Major Changes (S-Analysis)](http://data-analysis-stats.jp/2019/06/09/tensorflow-2-0-%E4%B8%BB%E3% 81% AA% E5% A4% 89% E6% 9B% B4% E7% 82% B9 /) [2] Introduction of tensorflow 2.0 (Japanese translation) [3] Changes in TensorFlow 2.0 as seen from Design Document [4] TensorFlow 2.0 finally released!
Recommended Posts