One example of machine learning: Learning Machine (5)

We all know that tensorflow (referred to as tf) is a framework for machine learning, it can be done using machine learning. Then use tf to show you how to do machine learning it, so you have a specific feeling: the original is so ah!

As used herein, tensorflow do a demo machine learning. However, tensorflow understanding with use, is available as a separate topic to explain, and this I would add.

Machine learning there are two key points, one sample, it is a model. The role of the samples, including for training (tagged) with the test (with or without labels), characterized by a large number of good and accurate label of training sample, is the key. Model, simple to understand, is a function that accepts a function continuously adjust the parameters to achieve the best prediction of the state, such as a simple linear regression model mentioned earlier.

"Prototype" of this article is this address: https://colab.research.google.com/notebooks/mlcc/first_steps_with_tensor_flow.ipynb?hl=zh-cn#scrollTo=9ivCDWnwE2Zx , you can access the contents of this page.

I will try to use own words, focused, and adhere to the "do not complete, not perfect" ideas.

(A) sample

Different problem scenarios, using different samples. Sometimes, acquisition or creation of the sample, is very time-consuming, but also very critical thing.

Here, the problem to be solved, is "predicted house prices", and a sample has already been prepared, the address is: https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv

For the reading and analysis of the sample, using PANDAS (described previously have separately). When using pandas to read, you can read this address, because pandas support remote reading network, you can also download the file to the local, and then use the pandas to read.

A small way to download the sample files, such as using this command:

curl -o housing.csv "https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv"

Then, using pandas to read, show some content, and view general statistics:
Read sample code

Execute the code, the effect is as follows:
Sample Information

This sample, the house is an area of ​​information, the goal is a small way this information, predicted in the new feature information (such as latitude and longitude, Building age, area, etc.), what kind of median_house_value, ie the value of the house median.

So far, have solved the problem of the sample, this sample is mainly used for training the model.

(B) model

First look at the work of drawing machine learning:
Iterative Learning

The overall figure for the iterative process of learning machine. Here the "model", referring to the overall iterative learning process, including input, predictive model, weight adjustment and loss calculation.

In order to better organize and implement the learning process, the process of small steps on the map plus division, please note the comment in the following figure:
Model of organization

(1) Input

For the training (i.e., training "predictive model"), to enter the combination of features of the tag (i.e., sample). Here, the predicted label (also the target) is set to the median value of the house that is median_house_value, so the sample label also set median_house_value. For features, for simplicity in the presentation, select the total number of the house where a street that is total_rooms as a single feature.

Thus, the characteristics of the label on the training sample finalized, you can write code to get:
Obtaining training samples of features and label

The above features and tag down clear, however, when the sample (combination of features of the tag) is provided to the trained predictive model, there are some "pre-processing" may be implemented in order to obtain a better training effect, this pre-treatment including converting the sample into tensorflow Dataset slice, whether randomly selected samples for training, data reuse it. the number of iterative learning, the number of samples used in each iteration, and so on.

This pretreatment, also called "input function" in the implementation of training forecasting model, you need to specify the input function .

Before implementing the input function, first explain a few concepts.

epoch,数据被(重复)使用的次数。比如epoch为1则所有样本只使用一次,epoch为2则所有样本使用两次。比如有三个样本为{a,b,c},epoch为2时,就是使用2次,变成{a,b,c,a,b,c},当然样本的顺序是可以打乱的。

迭代,一次迭代包括了标签预测、损失计算与权重调整的过程,一次迭代一般只使用小批量(batch_size个)样本。

step,迭代的总次数。

batch_size,每次迭代使用的样本的个数(并不一定要所有样本都使用上,比如小批量的梯度下降法)。

每次step(迭代),都调用一次输入函数,返回指定大小的数据集,直至step次数处理完,或者样本数据已经用完。

这个输入函数可以这样实现,请留意下图中的解释:
Input function

(2)预测模型

之前提到,简单线性回归是一个简单的预测模型,这个预测模型适用于从x到y的映射。本例中,就是从total_rooms预测出median_house_value,所以使用简单线性回归模型即可解决问题。

在tf.estimator模块中,有一个LinearRegressor类,它的对象就是一个线性回归模型。

在创建LinearRegressor模型时,需要指定调整模型参数的优化器,这里使用这前介绍的“小批量随机梯度下降”的优化器,代码如下:
Creating predictive models

创建预测模型之后,就可以执行训练了:
Predictive models for training

在创建预测模型时指定的梯度下降优化器,完成迭代学习过程中模型参数(比如权重)的调整。只要触发训练,这个优化器就会调整参数,这个过程不需要读者介入,包括下面的损失计算,也是优化器完成的工作。而我们进行损失计算,是为了观察收敛的情况,进而调整训练的参数,而不是模型的参数(如权重,这个是tensorflow的参数调整器来做的)。

(3)损失计算

预测模型在训练的过程中,会自动地,进行预测与损失计算,进而自动地调整模型的参数。

这一步,也叫评估。

需要注意,损失计算是创建预测模型时指定的优化器自动完成的事情,而这里计算损失,是为了调整模型的训练参数(比如步长、epoch等,这是你要做的事情)。

预测所有样本,得到预测值,再把预测值与真实的标签值,进行损失计算,分别计算出均方误差(MSE)跟均根方误差(RMSE),代码与执行效果如下:
Calculation error
Calculating an error effects

以上对所有样本进行了预测,并计算了误差MSE跟RMSE,一般来说根据RMSE进行误差大小的判断即可,那么,现在的RMSE值,到底有多大呢?可以看一下median_house_value的最大值跟最小值,再来理解一下,现在的RMSE值是一个什么样的概念,代码与效果如下:
Size RMSE values

由上图的值来看,RMSE的值已经达到实际标签值的一半的误差,所以这个误差是巨大的。

为了更直观地观察预测与实际值的差别,一个办法是使用pandas的统计分析来对比,另一个办法是绘制拟合线,这里分别演示一下。

以下代码使用pandas进行分析,对比预测与实际值的差别,代码与效果如下:
pandas analyze the differences predicted and actual values

另一个办法是绘制当前误差下的拟合线,代码与效果如下:
Output feedback error
Feedback errors fit line

拟合线的目的是尽可能地拟合所有的点,但上图只训练了一次的模型,明显没有拟合的效果。为了得到更好的拟合效果,应该根据反馈的误差信息,调整训练参数,并进行反复的训练。

(三)调整训练参数

在样本跟模型(包括输入、创建预测模型、损失计算)确定下来之后,就可以反复地训练这个模型。

然后,根据反馈的误差信息,调整训练参数。

这里先“随意”地设置一下训练参数,再进行若干次训练,来观察一下误差的信息,把之前的代码调整一下,如下:
Trainer 1
Trainer 2

可以看到这样的输出:
Training model error feedback 1
Training model error feedback 2

上面演示的最终误差还是很大,这时,为了让模型取得更好的收敛,应该调整训练参数,比如调整为:

train_model(learning_rate=0.0001, step=500, batch_size=10)

读者可以尝试使用不同的训练参数,并留意误差收敛的情况(某些训练参数下误差并不会一直减小)。

The final effect is not demonstrated a small way, because the final result is not important, you should understand that in order to achieve better training results, training parameters should be adjusted according to the quantity and quality of samples (qualitative characteristics) (required trial and error), generally speaking, you can try using a smaller learning_rate + big step + larger batch_size training, but eventually see results, including the error convergence, as well as the final usage.

Adjust training parameters, is likely to be a process of trial and error, like a scientific experiment, repeated "draw conclusions", "verification", "adjust conclusion" once again "verification" process.

(Iv) use the model to predict

Use the model to predict end-use model that is trained, part belonging to the test model, using a method similar to predict the course of the training, I remember when the "Audio Tab" of the explanation has already been mentioned, and there is not fine Say.

Roseau it again. Model is simply a function, a function that accepts parameters to adjust, can continue to evolve, to get a better prediction. For model, if you can not write, it does not matter, tensorflow including some effective models and even adjust model parameters gradient optimizer also built (you do not have to adjust the parameters of the process of intervention). How to set parameters of the model train, got the idea that you want, and set training parameters, with the quantity and quality of samples have a relationship, even with your experience in a relationship. On the other hand, you got the idea to most, is how to sample? How you acquire or create a large number of well-characterized samples for training? How much time you want to spend?

In summary, this paper demonstrates a practical example of machine learning, the processes, including obtaining a sample to create the model (including sample input, predictive model is created, gradient descent optimizer designated, loss calculation, etc.), training and training parameters adjustment, the test model. This article hopes, allowing you to manipulate the traditional machine learning, there is a specific perceptual awareness.


smile

Guess you like

Origin www.cnblogs.com/freeself/p/10936423.html