菜品三级分类_分类器的惊人替代品
菜品三級分類
Many tasks in Machine Learning are setup as classification tasks. The name would imply you have to learn a classifier to solve such a task. However, there are many popular alternatives to solving classification tasks which do not involve training a classifier at all!
機器學習中的許多任務都設置為分類任務。 該名稱意味著您必須學習分類器才能解決此類任務。 但是,有許多解決分類任務的流行替代方法,這些方法根本不需要訓練分類器!
分類任務示例 (An Example Classification Task)
For the purpose of illustrating these alternative methods, let’s use a very simple, yet visual classification problem. Our dataset shall be a random image from the internet. But only a single image… of Ironman!
為了說明這些替代方法,讓我們使用一個非常簡單但直觀的分類問題。 我們的數據集應該是來自互聯網的隨機圖像。 但是只有一個圖像……鐵人三項!
Each image pixel of this image will count as a unique data sample. Our set of inputs (X) shall be the pixel coordinates and our set of outputs (Y) shall be the pixel colours.
該圖像的每個圖像像素將被視為唯一的數據樣本。 我們的一組輸入(X)應該是像素坐標,而我們的一組輸出(Y)應該是像素顏色。
The classification task is to predict a pixel colour (y) given a coordinate (x).
分類任務是在給定坐標(x)的情況下預測像素顏色(y)。
- Inputs (X) = pixel coordinates 輸入(X)=像素坐標
- Outputs (Y) = pixel colour class 輸出(Y)=像素顏色類別
We can convert the pixel’s continuous RGBA colour vector into a discrete, categorical class index, so that this toy classification task is simplified.
我們可以將像素的連續RGBA顏色向量轉換為離散的分類類別索引,從而簡化了此玩具分類任務。
The 5 colour classes for our toy classification task我們的玩具分類任務的5種顏色類別火車測試拆分 (The Train-Test Split)
As a small aside, when splitting the dataset into a training set and a test set, they should have a similar distribution. This means we cant simply split the data down the middle, because the distribution of data that we would test our results on would be very different to the data we used in training (thus leading to bad and/or misleading results)
另外,將數據集分為訓練集和測試集時,它們應該具有相似的分布。 這意味著我們不能簡單地將數據拆分到中間,因為我們用來測試結果的數據分布將與我們在訓練中使用的數據完全不同(從而導致不良和/或誤導性的結果)
(Left) How NOT to split your data. (Right) Bad results. Test Predictions from a classifier trained on the top Train data which has a very different distribution to the test data(左)如何不分割數據。 (正確)不好的結果。 來自在頂部訓練的分類器的測試預測訓練數據與測試數據的分布非常不同That is why you should take care when preparing your data (this is all taken care of in the background when using statistical libraries like sklearn)
這就是為什么在準備數據時要小心(在使用sklearn之類的統計庫時,所有這些操作都在后臺處理)
from sklearn.model_selection import train_test_splitX_train, X_test, Y_train, Y_test = train_test_split(X, Y)Much better. The training set and test set have been split in such a way to keep similar distributions.好多了。 訓練集和測試集已按照保持相似分布的方式進行了拆分。混亂矩陣 (The Confusion Matrix)
We shall use a confusion matrix to plot how well our method classified the colour for each pixel coordinate in the test set. A perfect classification would look like the confusion matrix below:
我們將使用混淆矩陣來繪制我們的方法對測試集中每個像素坐標的顏色分類的程度。 完美的分類看起來像下面的混淆矩陣:
Confusion Matrix plotting an Ideal Classification混淆矩陣繪制理想分類1.直接方法:學習分類器 (1. The Direct Approach: Learn a Classifier)
Now most people solve classification tasks by training a classifier — i.e. Learning a nonlinear function (f) (the classifier) which maps a set of inputs (X) to a set of outputs (Y).
現在,大多數人通過訓練分類器來解決分類任務,即學習將一組輸入(X)映射到一組輸出(Y)的非線性函數(f)(分類器)。
Let us do that too so we have a baseline solution to this classification task to compare against. There are a range of ML models out there to approximate a nonlinear function (the classifier) — but we shall simply use a feed-forward neural network with 3 hidden layers (100 neurons per layer)
讓我們也這樣做,以便針對此分類任務有一個基準解決方案。 有大量的ML模型可以近似非線性函數(分類器),但是我們將簡單地使用具有3個隱藏層(每層100個神經元)的前饋神經網絡。
training our classifier訓練我們的分類器The results were not bad. They weren’t perfect — but it does a good enough job on our toy classification task (despite occasionally missing off the right eye of ironman)
結果還不錯。 它們并不完美-但這在我們的玩具分類任務中做得足夠好(盡管偶爾會遺忘Ironman的右眼)
Three trained instances of our classifier predicting the pixel colours on the ironman test set.分類器的三個經過訓練的實例可以預測Ironman測試集上的像素顏色。2.替代方法:排名 (2. Alternative Approaches: Ranking)
Ranking (aka Retrieval) is a widely used alternative to training a function (classifier) which directly predicts an output for a given input. By measuring the distance of an input (x) to an output (y), a given input can be ranked against a range of outputs and the best can then be selected (retrieved). Ranking like this is an indirect way to map a given input (x) to an output (y) without requiring a trained classifier (f). Or another way to think about it is — you have decomposed your classifier (f) into an evaluation/distance function (Q) and ranking algorithm (arg max).
排名(又名檢索)是訓練功能(分類器)的一種廣泛使用的替代方法,該功能可直接預測給定輸入的輸出。 通過測量輸入(x)到輸出(y)的距離,可以將給定輸入與一系列輸出進行比較,然后選擇最佳(檢索)。 這樣的排名是一種將給定輸入(x)映射到輸出(y)的間接方法,而無需訓練有素的分類器(f)。 或考慮它的另一種方法是-您已將分類器(f)分解為評估/距離函數(Q)和排名算法(arg max)。
The infamous Q-function equation in RL is actually an example of ranking to classify if we interpret the notation as: input (s), output (a), distance function (Q)RL中臭名昭著的Q函數方程實際上是排名的一個示例,如果我們將符號解釋為以下類別,則可以將其解釋為:輸入(s),輸出(a),距離函數(Q)However, doesn’t this mean you just substituted one type of problem for another ? Haven’t you just shifted the problem of learning one function (the classifier) for another function (the distance function)? Yes — but… the latter function may be much easier to learn or you may not even need to learn a distance function if you can use a predefined distance measure (e.g. euclidean distance, cosine similarity, etc).
但是,這是否意味著您只是將一種問題替換為另一種問題? 您是否剛剛將學習一個功能(分類器)的問題轉移到了另一個功能(距離函數)的問題? 是的,但是…如果您可以使用預定義的距離度量(例如,歐幾里德距離,余弦相似度等),則后者功能可能更容易學習,或者甚至不需要學習距離功能。
2.a K最近的鄰居 (2.a K Nearest Neighbours)
K nearest neighbours (KNN) is actually a very well known ranking algorithm used to solve classification tasks. It essentially ranks the new input (x) against a set of K example inputs using a linear distance measure (e.g. euclidean, cosine, etc) and then uses the output (y) of the most similar example input as its prediction.
K最近鄰居(KNN)實際上是一種非常著名的排序算法,用于解決分類任務。 它實質上使用線性距離度量(例如,歐幾里得,余弦等)針對一組K個示例輸入對新輸入(x)進行排名,然后將最相似的示例輸入的輸出(y)用作其預測。
The KNN algorithm is one of the simpler forms of machine learning known as example-based learning (aka retrieval-based learning, instance-based learning, lazy learning, etc), as it relies on memorising examples to avoid learning / approximating a function. It simply uses an off-the-shelf distance function to compare inputs against memorised, exemplar inputs (embedded in the same space).
KNN算法是機器學習的一種較簡單形式,稱為基于實例的學習(又名基于檢索的學習,基于實例的學習,惰性學習等),因為它依靠記憶示例來避免學習/逼近功能。 它僅使用現成的距離函數將輸入與記憶的,示例性的輸入(嵌入在同一空間中)進行比較。
from sklearn.neighbors import KNeighborsClassifiernearest_neighbour_algorithm = KNeighborsClassifier(n_neighbors=3)nearest_neighbour_algorithm.fit(X_train, Y_train)
Y_predicted = nearest_neighbour_algorithm.predict(X_test)Almost perfect Test Predictions for the KNN methodKNN方法的幾乎完美的測試預測
Siamese Networks actually work on a very similar principal to KNNs (i.e. they compare new inputs to exemplar inputs to retrieve their corresponding outputs), however it learns its own, custom distance function.
暹羅網絡實際上在與KNN相似的原理上工作(即,它們將新輸入與示例輸入進行比較以檢索其相應的輸出),但是它學習了自己的自定義距離函數。
Siamese Neural Network暹羅神經網絡2.b聯合嵌入 (2.b Joint Embeddings)
We can get away with using custom distance functions by comparing inputs (x) with other inputs (x) which are embedded in the same space. However, this is more tricky when measuring the distance between inputs (x) with outputs (y) directly due to them being embedded in two different spaces. We can overcome this by using joint embeddings (i.e. embedding the inputs and outputs in a shared space).
通過將輸入(x)與嵌入在同一空間中的其他輸入(x)進行比較,我們可以使用自定義距離函數。 但是,由于直接將輸入(x)嵌入兩個不同的空間中,因此在直接測量輸入(x)與輸出(y)之間的距離時,這比較棘手。 我們可以通過使用聯合嵌入(即將輸入和輸出嵌入到共享空間中)來克服這一問題。
examples of joint embeddings聯合嵌入的例子Therefore this method involves learning joint embeddings such that the inputs (X) and outputs (Y) can be embedded into a shared space. Then we can rank a new input against the outputs using linear distance metrics (e.g. euclidean distance, cosine distance, etc). There are a few different ways to embed the outputs and inputs onto a shared manifold — such as taking the learnt feature representations of a neural model (e.g. Recurrent Embedding Dialogue Policy, etc).
因此,該方法涉及學習聯合嵌入,以便可以將輸入(X)和輸出(Y)嵌入到共享空間中。 然后,我們可以使用線性距離度量(例如,歐幾里得距離,余弦距離等)對輸出進行新排序。 有幾種不同的方法可以將輸出和輸入嵌入到共享的流形中,例如采用神經模型的學習到的特征表示(例如,循環嵌入對話策略等)。
We shall be using Matrix Factorisation.
我們將使用矩陣分解。
(Right) The Outputs embedded into the input’s XY coordinate space using Matrix Factorisation(右)使用矩陣分解將輸出嵌入到輸入的XY坐標空間中 Test Predictions using the learnt joint embeddings使用學習的聯合嵌入進行測試預測2.c學習距離函數 (2.c Learning a Distance Function)
You can also approximate a custom, nonlinear distance measure which would have learnt to measure the relative distances of input-output pairs (despite being embedded differently). We can do this by feeding in a set of concatenated input-output vectors as the model input, and training the model to predict a distance score (a value between 0 and 1) for the concatenated input-output pair.
您也可以近似自定義的非線性距離度量,該度量將學會測量輸入-輸出對的相對距離(盡管嵌入方式有所不同)。 我們可以通過輸入一組串聯的輸入-輸出向量作為模型輸入,并訓練模型以預測串聯的輸入-輸出對的距離得分(0到1之間的值)來做到這一點。
We therefore modify the training data by concatenating inputs and outputs randomly (to become our new training inputs) and setting their expected distance to 0 if the input-output pair is a valid mapping,and 1 if its an invalid input-output mapping. The model used to approximate the distance function is simply a feedforward neural network (with 3 hidden layers, each with 100 neurons) almost identical to the one used for the classifier method (except this neural network is set up for learning a regression task as opposed to a classification task).
因此,我們通過以下方式修改訓練數據:將輸入和輸出隨機串聯(以成為新的訓練輸入),如果輸入/輸出對是有效映射,則將其期望距離設置為0;如果輸入/輸出對是無效輸入,則將其期望距離設置為1。 用于近似距離函數的模型只是一個前饋神經網絡(具有3個隱藏層,每個具有100個神經元),幾乎與用于分類器方法的模型相同(除了該神經網絡是為學習回歸任務而設置的)到分類任務)。
Training a feedforward neural network to be a nonlinear distance function (aka cost function). Input = Input-Output pair. Ouput=distance.將前饋神經網絡訓練為非線性距離函數(又稱成本函數)。 輸入=輸入-輸出對。 輸出=距離。 Test Predictions using the Learnt Distance Function使用學習的距離函數進行測試預測結論 (Conclusion)
Performance wise, the results of the alternative methods were similar to the classifier, however, retrieval based methods do present additional benefits over a classifier in other areas, like adding a stochastic element (if the top ranking result is always chosen it is identical to using a classifier or another deterministic mapping, however, adding some uncertainty can be done by ensuring the top ranking result is not always chosen), or like updating classes more easily, solving much larger multiclass classification problems (i.e. those with hundreds of classes) and being able to solve multilabel classification problems (i.e. predicting the top N applicable outputs for a given input), etc.
在性能方面,替代方法的結果類似于分類器,但是基于檢索的方法在其他領域確實比分類器具有更多優勢,例如添加隨機元素(如果始終選擇排名最高的結果,則等同于使用分類器或其他確定性映射,但是,可以通過確保不總是選擇排名最高的結果來增加一些不確定性,或者像更輕松地更新類,解決更大的多類分類問題(即具有數百個類的問題)并能夠解決多標簽分類問題(即預測給定輸入的前N個適用輸出)等。
謝謝 (Thanks)
Thanks for reading. All the code is included in the attached Colab notebook. Have fun experimenting
謝謝閱讀。 所有代碼都包含在隨附的Colab筆記本中。 嘗試愉快
翻譯自: https://medium.com/swlh/amazing-alternatives-to-classifiers-b7bd7e85b60d
菜品三級分類
總結
以上是生活随笔為你收集整理的菜品三级分类_分类器的惊人替代品的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方舟手游怎么加点(方舟生存进化)
- 下一篇: 开关变压器绕制教程_教程:如何将变压器权