COUSERA Machine Learning Week06

Ats
28 min readDec 3, 2020

--

ソフトウェアエンジニアが Cousera の機械学習コーシ(6週目)に参加して学んだことをメモ代わりに共有します。今週の後半は、僕がデザインスクールで学んでいるようなことを書きましたが、改めてプロトタイプの大切さを機械学習の分野でも痛感しました。

Photo by Michael Dziedzic on Unsplash

5週目の記事もあるので良かったら読んでみてください。

Evaluating a Learning Algorithm

この章では、機械学習してできた仮説をどのように評価するかを説明します。

Evaluating a Hypothesis

この節では、まず機械学習の結果をどのように評価するかを説明します。直感的には、仮説 h(x) をプロットして何が起きているかを見てみるということを考えると思います。 しかし、多くのフィーチャーがある問題に対しては仮説をプロットするのが 難しかったり、時には不可能だったりします。なので、仮説評価する他の手段が必要です。

その手段として有効なのが、データセットを2つに分ける方法です。 最初の部分は通常のトレーニングセットとして扱い、残りの部分はテストセットとして扱います。おおよそ、7:3の比率になるようにデータセットをランダムにトレーニング用と、テスト用に分けます。そして、トレーニング用のデータセットを使って、今まで通りに機械学習を行い仮説 h(x) を得ます。その後、テスト用のデータセットを使って、その仮説の誤差を計算します。その際に、用いる数式も機械学習を実行する時に使ったコスト関数と同じです。下記に、線形回帰とロジスティック回帰の場合の誤差の計算方法を示します。

線形回帰のコスト関数
ロジスティック回帰のコスト関数

この誤差を計算して、自分たちの目的に合う精度の仮説を得るように改善を加えていきます。

最後にこの節のサマリーを載せておきます。

Once we have done some trouble shooting for errors in our predictions by:

- Getting more training examples
- Trying smaller sets of features
- Trying additional features
- Trying polynomial features
- Increasing or decreasing λ

We can move on to evaluate our new hypothesis.

A hypothesis may have a low error for the training examples but still be inaccurate (because of overfitting). Thus, to evaluate a hypothesis, given a dataset of training examples, we can split up the data into two sets: a training set and a test set. Typically, the training set consists of 70 % of your data and the test set is the remaining 30 %.

The new procedure using these two sets is then:

1. Learn Θ and minimize Jtrain​(Θ) using the training set
2. Compute the test set error Jtest​(Θ)

The test set error

1. For linear regression

2. For classification ~ Misclassification error (aka 0/1 misclassification error):

This gives us a binary 0 or 1 error result based on a misclassification. The average test error for the test set is:

This gives us the proportion of the test data that was misclassified.

Model Selection and Train/Validation/Test Sets

この節では、何次の多項式を使うかを判定する方法を説明します。この問題をモデル選択問題と呼びます。モデル選択問題では、一般的にデータセットをトレーニングセットとテストセットの二つに分割するだけでなく、データセットをトレーニングセット、クロスバリデーションセット、テストセットの三つに分割する方法をが使われます。だいたい、60%をトレーニングセット、20%をクロスバリデーションセット、20%をテストセットのように分割します。続いてそれぞれのデータセットがどのように使われるか説明します。

まず、トレーニングセットは前節と同様に、仮説を導出するために使います。ただ、この段階では何次の多項式を使うかを分かっていないので、考えられる多項式を虱潰しに計算して仮説を導出します。次に、どの多項式の精度が良いかを判定するために、クロスバリデーションセットを使って誤差を計算します。これは前節と同様に、コスト関数を使います。最後に、テストセットを用いて誤差を計算し、仮説の改善するための施策を実施します。

講義内では、たまにクロスバリデーションセットとテストセットに同一のデータセットを用いて行うケースがある言及されます。講義内でその方法は正しくなく、おすすめしないと教わります。理由としては、モデル選択問題にテストセットを使って、その後の誤差の計算にまたデータセットを使うのは公平に仮説を評価しているとは言えないからです。具体的に説明すると、データセットに合う多項式を選択したのだから、そのデータセットを使って誤差を最小化するのは、特定のデータセットに当てはまるオーバーフィッティングに繋がりやすいということです。なので、また別のデータセットであるクロスバリデーションセットを使って、モデル選択をします。その後、中立的なテストセットで仮説の評価をする。この3分割の方法を強く薦められました。

最後にこの節のサマリーを載せておきます。

Just because a learning algorithm fits a training set well, that does not mean it is a good hypothesis. It could over fit and as a result your predictions on the test set would be poor. The error of your hypothesis as measured on the data set with which you trained the parameters will be lower than the error on any other data set.

Given many models with different polynomial degrees, we can use a systematic approach to identify the ‘best’ function. In order to choose the model of your hypothesis, you can test each degree of polynomial and look at the error result.

One way to break down our dataset into the three sets is:

- Training set: 60%
- Cross validation set: 20%
- Test set: 20%

We can now calculate three separate error values for the three different sets using the following method:

1. Optimize the parameters in Θ using the training set for each polynomial degree.
2. Find the polynomial degree d with the least error using the cross validation set.
3. Estimate the generalization error using the test set with Jtest​(Θ(d)), (d = theta from polynomial with lower error);

This way, the degree of the polynomial d has not been trained using the test set.

Bias vs. Variance

この章では、評価した仮説をどのように改善するのかを説明します。

Diagnosing Bias vs. Variance

この節では、テストセットを用いて、仮説がアンダーフィッティングしているのか、オーバーフィッティングしているのかを見分ける方法を説明します。学習アルゴリズムを走らせて期待ほど良い結果で無ければ、だいたいその原因は高いバイアス問題か、高い分散問題のどちらかです。言い換えると、それはアンダーフィット問題かオーバーフィット問題のどちらかということです。そしてこの場合、これらの問題のどちらなのかを 見分けるのは凄く重要です。何故なら、どちらの問題なのか次第で、対応する改善策が異なるからです。そして、その判定にはトレーニングセットの誤差とクロスバリデーションセットの誤差を使って判定します。

上記の図は、次元数と誤算(コスト関数)のグラフです。上記のように、トレーニングセットとクロスバリデーションセットの両方の誤差が高い時は高バイアスであり、アンダーフィッティングしている状態です。一方、クロスバリデーションセットの誤差は小さいが、トレーニングセットの誤差がクロスバリデーションセットの誤差と比べて大きい時は、高分散であり、オーバーフィッティングしている状態です。このように、アンダーフィッティングとオーバーフィッティングを見分けます。

最後にこの節のサマリーを載せておきます。

In this section we examine the relationship between the degree of the polynomial d and the underfitting or overfitting of our hypothesis.

- We need to distinguish whether bias or variance is the problem contributing to bad predictions.
- High bias is underfitting and high variance is overfitting. Ideally, we need to find a golden mean between these two.

The training error will tend to decrease as we increase the degree d of the polynomial.

At the same time, the cross validation error will tend to decrease as we increase d up to a point, and then it will increase as d is increased, forming a convex curve.

The is summarized in the figure below:

Regularization and Bias/Variance

この節では、オーバーフィッティングを防ぐための正則化の λ の選び方を説明します。まず、下記が正則化を考慮したコスト関数です。

λ の選び方の手順を下記に示します。

  1. λ の候補を決める
  2. その候補に対して、正則化とトレーニングセットを使って各 λ に対するθ を決める
  3. 2で求めた θ を用いて、正則化なしでクロスバリデーションセットを用いて誤差を計算する
  4. 3の結果一番誤差が小さくなった λ を選ぶ

3 で正則化を用いないのは、単純にインプットとアウトプットの誤差が欲しいだけなので、正則化の項は不要です。

最後にこの節のサマリーを載せておきます。

In the figure above, we see that as λ increases, our fit becomes more rigid. On the other hand, as λ approaches 0, we tend to over overfit the data. So how do we choose our parameter λ to get it ‘just right’ ? In order to choose the model and the regularization term λ, we need to:

1. Create a list of lambdas (i.e. λ∈{0,0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24});
2. Create a set of models with different degrees or any other variants.
3. Iterate through the λs and for each λ go through all the models to learn some Θ.
4. Compute the cross validation error using the learned Θ (computed with λ) on the JCV​(Θ) without regularization or λ = 0.
5. Select the best combo that produces the lowest error on the cross validation set.
6. Using the best combo Θ and λ, apply it on Jtest​(Θ) to see if it has a good generalization of the problem.

Learning Curves

この節では学習曲線を説明します。学習曲線とは横軸にデータセット数として、トレーニングセットのコスト関数 Jtrain(θ) とクロスバリデーションセットのコスト関数 JCV(θ) をプロットしたグラフです。クロスバリデーションセットのコスト関数でなくて、テストセットのコスト関数 Jtest(θ) を使うことも可能です。学習曲線はプロットするとアルゴリズムがちゃんと機能しているの正当性チェックをしたい時や、アルゴリズムのパフォーマンスを改善したい時に役に立ちます。うまくできた仮説の学習曲線は、Jtrain(θ) と JCV(θ) が低い値に収束するようなグラフになります。

一方で、上記のようなアンダーフィッティングしている学習曲線は、Jtrain(θ) と JCV(θ) が高い値で、一定になっています。この場合、グラフから見ても分かる通り、データセット数を増やしてもほぼ結果は横ばいになります。なので、根本的な解決策は、モデル選択からやり直すことです。

次に、上記のようなオーバーフィッティングしている学習曲線は、Jtrain(θ) と JCV(θ) の間に大きな差が生まれます。これは、データセット数が増えれば収束していくのが見えるので、モデル選択をするよりもデータセット数を増やすことをした方が精度を上げることができます。混乱を防ぐために補足しておくと、J(θ) と Jtrain(θ) は同じくトレーニングセットを使っていますが、J(θ) は正則化の項を含み、Jtrain(θ) は正則化の項を含みません。

最後にこの節のサマリーを載せておきます。

Training an algorithm on a very few number of data points (such as 1, 2 or 3) will easily have 0 errors because we can always find a quadratic curve that touches exactly those number of points. Hence:

- As the training set gets larger, the error for a quadratic function increases.
- The error value will plateau out after a certain m, or training set size.

Experiencing high bias:

Low training set size: causes Jtrain​(Θ) to be low and JCV​(Θ) to be high.
Large training set size: causes both Jtrain​(Θ) and JCV​(Θ) to be high with Jtrain​(Θ) ≈ JCV​(Θ).

If a learning algorithm is suffering from high bias, getting more training data will not (by itself) help much.

Experiencing high variance:

Low training set size: Jtrain​(Θ) will be low and JCV​(Θ) will be high.
Large training set size: Jtrain​(Θ) increases with training set size and JCV​(Θ) continues to decrease without leveling off. Also, Jtrain​(Θ) < JCV​(Θ) but the difference between them remains significant.

If a learning algorithm is suffering from high variance, getting more training data is likely to help.

Deciding What to Do Next Revisited

この節では、どのような対応をして仮説を修正するのか説明し、それをニューラルネットワークへどのように応用するのかを説明します。まずはオーバーフィッティングとアンダーフィッティングした場合の対応策を下記に列挙します。

オーバーフィッティング

  • データセット数を増やす
  • 特徴量を減らす
  • λ を大きくする

アンダーフィッティング

  • 特徴量を増やす
  • 仮説の次数を増やす
  • λ を小さくする

また、ニューラルネットワークにおいて学習曲線は、線形回帰やロジスティック回帰と同様に使うことができます。なので、学習曲線からオーバーフィッティングしているかアンダーフィッティングしているか判断することができます。また、ニューラルネットワークでは、隠れレイヤーや隠れユニット数を減らすとアンダーフィッティングしやすいです。逆に、隠れレイヤーや隠れユニット数を増やすと多くのパラメーターを持つことと同義でオーバーフィッティングしやすいです。ただ、ニューラルネットワークにおいては、隠れレイヤーや隠れユニット数を減らしてオーバーフィッティングを解決するよりも、正則化を用いてオーバーフィッティングを解決するほうが効果的なことが多いです。ただ、注意しなくてはならないのは、隠れレイヤーや隠れユニット数が多いほど計算量が増えるので、処理が遅くなるという問題が別にあります。

最後にこの節のサマリーを載せておきます。

Our decision process can be broken down as follows:

- Getting more training examples: Fixes high variance
- Trying smaller sets of features: Fixes high variance
- Adding features: Fixes high bias
- Adding polynomial features: Fixes high bias
- Decreasing λ: Fixes high bias
- Increasing λ: Fixes high variance.

Diagnosing Neural Networks

- A neural network with fewer parameters is prone to underfitting. It is also computationally cheaper.
- A large neural network with more parameters is prone to overfitting. It is also computationally expensive. In this case you can use regularization (increase λ) to address the overfitting.

Using a single hidden layer is a good starting default. You can train your neural network on a number of hidden layers using your cross validation set. You can then select the one that performs best.

Model Complexity Effects:

- Lower-order polynomials (low model complexity) have high bias and low variance. In this case, the model fits poorly consistently.
- Higher-order polynomials (high model complexity) fit the training data extremely well and the test data extremely poorly. These have low bias on the training data, but very high variance.
- In reality, we would want to choose a model somewhere in between, that can generalize well but also fits the data reasonably well.

Machine Learning System Design

この章では、機械学習を実装する際に遭遇する問題の対処について説明します。数学的というよりはどちらかと言うとティップスのような感じです。

Error Analysis

機械学習のモデルを実装後に、それをどのように改善していくのかはとても大事なことです。そして、闇雲に時間をかけるのではなく、適材適所に時間を割くをことが望ましいです。その際に、役に立つのが3つのティップスを説明します。

まず一つは、クロスバリデーションセットを使ってモデルを評価します。その評価結果の間違ったデータセットを人力で見て、法則性を見つけ出す方法です。これによって、新しいフィーチャーをデザインする手助けになったり、 現在のシステムの短所などを教えてくれたりします。ここで、前章と同様にテストセットで行わずに、クロスバリデーションセットを使うことで、最終評価ようのテストセットに最適化するのを防ぎます。

次に、モデルを評価する数値設定をします。単一の実数値による評価指標があれば、 単にそれを見て、改善したのか改悪したのかを判断できます。そして、それを用いて、より素早く、 新しいアイデアを試す事が出来ます。この評価指標がないと、意思決定を行う時に良くなるのか、悪くなるのかを見るのが 難しくなってしまいます。簡単なところで言うとクロスバリデーションセットの誤差などから始めると良いと思います。下記の記事のように、高精度な分類問題を解いている場合は、適合率や再現率などを評価指標にします。

最後に、とにかく素早く雑にプロトタイプを実装するということです。一般的に、最初の早くて汚いはずの実装に時間をかけすぎるということがよくあります。「手早すぎる」や「汚すぎる」は心配しなくて良いです。そうでは無く、自分の出来るかぎり早く、 何かしらを実装することを心がけます。そして、ひとたび最初の実装を得たなら、その次にあなたが 何に時間を使うべきかを決定するとてもパワフルなツールになります。何故なら、まずそれの出力するエラーを見て、この種のエラー分析を行って何が誤りを生んでいるのかを見て、それを使ってさらなる開発をインスパイアしていけるからです。

もし、早くて汚い実装が、 単一実数の評価指標と共に用いる事が出来たなら、これは様々なアイデアを行き来する大きな手助けになってくれます。 そして素早く別々のアイデアが、 アルゴリズムのパフォーマンスを改善するかを 試していく事を可能にしてくれて、ゆえに、何は要らないか、 何はアルゴリズムと共に使うと良いかを 素早く意思決定していく事を 可能にしてくれます。

最後の方は、僕がデザインスクールで学んでいるようなことを書きましたが、改めてプロトタイプの大切さを痛感します。また、デザインスクールではそんなに効果計測の観点を学ばないので、評価指標を持つということにハッとさせれれました。

最後にこの節のサマリーを載せておきます

System Design Example:

Given a data set of emails, we could construct a vector for each email. Each entry in this vector represents a word. The vector normally contains 10,000 to 50,000 entries gathered by finding the most frequently used words in our data set. If a word is to be found in the email, we would assign its respective entry a 1, else if it is not found, that entry would be a 0. Once we have all our x vectors ready, we train our algorithm and finally, we could use it to classify if an email is a spam or not.

So how could you spend your time to improve the accuracy of this classifier?

- Collect lots of data (for example “honeypot” project but doesn’t always work)
- Develop sophisticated features (for example: using email header data in spam emails)
- Develop algorithms to process your input in different ways (recognizing misspellings in spam).

It is difficult to tell which of the options will be most helpful.

The recommended approach to solving machine learning problems is to:

- Start with a simple algorithm, implement it quickly, and test it early on your cross validation data.
- Plot learning curves to decide if more data, more features, etc. are likely to help.
- Manually examine the errors on examples in the cross validation set and try to spot a trend where most of the errors were made.

For example, assume that we have 500 emails and our algorithm misclassifies a 100 of them. We could manually analyze the 100 emails and categorize them based on what type of emails they are. We could then try to come up with new cues and features that would help us classify these 100 emails correctly. Hence, if most of our misclassified emails are those which try to steal passwords, then we could find some features that are particular to those emails and add them to our model. We could also see how classifying each word according to its root changes our error rate:

It is very important to get error results as a single, numerical value. Otherwise it is difficult to assess your algorithm’s performance. For example if we use stemming, which is the process of treating the same word with different forms (fail/failing/failed) as one word (fail), and get a 3% error rate instead of 5%, then we should definitely add it to our model. However, if we try to distinguish between upper case and lower case letters and end up getting a 3.2% error rate instead of 3%, then we should avoid using this new feature. Hence, we should try new things, get a numerical value for our error rate, and based on our result decide whether we want to keep the new feature or not.

最後に今週の分の宿題のコードを載せておきます。今週は数学的に簡単だったのですぐに終わりました。

--

--

Ats
Ats

Written by Ats

I like building something tangible like touch, gesture, and voice. Ruby on Rails / React Native / Yocto / Raspberry Pi / Interaction Design / CIID IDP alumni

No responses yet