菜品三级分类_分类器的惊人替代品
菜品三級(jí)分類
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!
機(jī)器學(xué)習(xí)中的許多任務(wù)都設(shè)置為分類任務(wù)。 該名稱意味著您必須學(xué)習(xí)分類器才能解決此類任務(wù)。 但是,有許多解決分類任務(wù)的流行替代方法,這些方法根本不需要訓(xùn)練分類器!
分類任務(wù)示例 (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!
為了說明這些替代方法,讓我們使用一個(gè)非常簡單但直觀的分類問題。 我們的數(shù)據(jù)集應(yīng)該是來自互聯(lián)網(wǎng)的隨機(jī)圖像。 但是只有一個(gè)圖像……鐵人三項(xiàng)!
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.
該圖像的每個(gè)圖像像素將被視為唯一的數(shù)據(jù)樣本。 我們的一組輸入(X)應(yīng)該是像素坐標(biāo),而我們的一組輸出(Y)應(yīng)該是像素顏色。
The classification task is to predict a pixel colour (y) given a coordinate (x).
分類任務(wù)是在給定坐標(biāo)(x)的情況下預(yù)測(cè)像素顏色(y)。
- Inputs (X) = pixel coordinates 輸入(X)=像素坐標(biāo)
- 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.
我們可以將像素的連續(xù)RGBA顏色向量轉(zhuǎn)換為離散的分類類別索引,從而簡化了此玩具分類任務(wù)。
The 5 colour classes for our toy classification task我們的玩具分類任務(wù)的5種顏色類別火車測(cè)試拆分 (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)
另外,將數(shù)據(jù)集分為訓(xùn)練集和測(cè)試集時(shí),它們應(yīng)該具有相似的分布。 這意味著我們不能簡單地將數(shù)據(jù)拆分到中間,因?yàn)槲覀冇脕頊y(cè)試結(jié)果的數(shù)據(jù)分布將與我們?cè)谟?xùn)練中使用的數(shù)據(jù)完全不同(從而導(dǎo)致不良和/或誤導(dǎo)性的結(jié)果)
(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(左)如何不分割數(shù)據(jù)。 (正確)不好的結(jié)果。 來自在頂部訓(xùn)練的分類器的測(cè)試預(yù)測(cè)訓(xùn)練數(shù)據(jù)與測(cè)試數(shù)據(jù)的分布非常不同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)
這就是為什么在準(zhǔn)備數(shù)據(jù)時(shí)要小心(在使用sklearn之類的統(tǒng)計(jì)庫時(shí),所有這些操作都在后臺(tái)處理)
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.好多了。 訓(xùn)練集和測(cè)試集已按照保持相似分布的方式進(jìn)行了拆分。混亂矩陣 (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:
我們將使用混淆矩陣來繪制我們的方法對(duì)測(cè)試集中每個(gè)像素坐標(biāo)的顏色分類的程度。 完美的分類看起來像下面的混淆矩陣:
Confusion Matrix plotting an Ideal Classification混淆矩陣?yán)L制理想分類1.直接方法:學(xué)習(xí)分類器 (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).
現(xiàn)在,大多數(shù)人通過訓(xùn)練分類器來解決分類任務(wù),即學(xué)習(xí)將一組輸入(X)映射到一組輸出(Y)的非線性函數(shù)(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)
讓我們也這樣做,以便針對(duì)此分類任務(wù)有一個(gè)基準(zhǔn)解決方案。 有大量的ML模型可以近似非線性函數(shù)(分類器),但是我們將簡單地使用具有3個(gè)隱藏層(每層100個(gè)神經(jīng)元)的前饋神經(jīng)網(wǎng)絡(luò)。
training our classifier訓(xùn)練我們的分類器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)
結(jié)果還不錯(cuò)。 它們并不完美-但這在我們的玩具分類任務(wù)中做得足夠好(盡管偶爾會(huì)遺忘Ironman的右眼)
Three trained instances of our classifier predicting the pixel colours on the ironman test set.分類器的三個(gè)經(jīng)過訓(xùn)練的實(shí)例可以預(yù)測(cè)Ironman測(cè)試集上的像素顏色。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ùn)練功能(分類器)的一種廣泛使用的替代方法,該功能可直接預(yù)測(cè)給定輸入的輸出。 通過測(cè)量輸入(x)到輸出(y)的距離,可以將給定輸入與一系列輸出進(jìn)行比較,然后選擇最佳(檢索)。 這樣的排名是一種將給定輸入(x)映射到輸出(y)的間接方法,而無需訓(xùn)練有素的分類器(f)。 或考慮它的另一種方法是-您已將分類器(f)分解為評(píng)估/距離函數(shù)(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函數(shù)方程實(shí)際上是排名的一個(gè)示例,如果我們將符號(hào)解釋為以下類別,則可以將其解釋為:輸入(s),輸出(a),距離函數(shù)(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).
但是,這是否意味著您只是將一種問題替換為另一種問題? 您是否剛剛將學(xué)習(xí)一個(gè)功能(分類器)的問題轉(zhuǎn)移到了另一個(gè)功能(距離函數(shù))的問題? 是的,但是…如果您可以使用預(yù)定義的距離度量(例如,歐幾里德距離,余弦相似度等),則后者功能可能更容易學(xué)習(xí),或者甚至不需要學(xué)習(xí)距離功能。
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)實(shí)際上是一種非常著名的排序算法,用于解決分類任務(wù)。 它實(shí)質(zhì)上使用線性距離度量(例如,歐幾里得,余弦等)針對(duì)一組K個(gè)示例輸入對(duì)新輸入(x)進(jìn)行排名,然后將最相似的示例輸入的輸出(y)用作其預(yù)測(cè)。
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算法是機(jī)器學(xué)習(xí)的一種較簡單形式,稱為基于實(shí)例的學(xué)習(xí)(又名基于檢索的學(xué)習(xí),基于實(shí)例的學(xué)習(xí),惰性學(xué)習(xí)等),因?yàn)樗揽坑洃浭纠齺肀苊鈱W(xué)習(xí)/逼近功能。 它僅使用現(xiàn)成的距離函數(shù)將輸入與記憶的,示例性的輸入(嵌入在同一空間中)進(jìn)行比較。
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方法的幾乎完美的測(cè)試預(yù)測(cè)
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.
暹羅網(wǎng)絡(luò)實(shí)際上在與KNN相似的原理上工作(即,它們將新輸入與示例輸入進(jìn)行比較以檢索其相應(yīng)的輸出),但是它學(xué)習(xí)了自己的自定義距離函數(shù)。
Siamese Neural Network暹羅神經(jīng)網(wǎng)絡(luò)2.b聯(lián)合嵌入 (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)進(jìn)行比較,我們可以使用自定義距離函數(shù)。 但是,由于直接將輸入(x)嵌入兩個(gè)不同的空間中,因此在直接測(cè)量輸入(x)與輸出(y)之間的距離時(shí),這比較棘手。 我們可以通過使用聯(lián)合嵌入(即將輸入和輸出嵌入到共享空間中)來克服這一問題。
examples of joint embeddings聯(lián)合嵌入的例子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).
因此,該方法涉及學(xué)習(xí)聯(lián)合嵌入,以便可以將輸入(X)和輸出(Y)嵌入到共享空間中。 然后,我們可以使用線性距離度量(例如,歐幾里得距離,余弦距離等)對(duì)輸出進(jìn)行新排序。 有幾種不同的方法可以將輸出和輸入嵌入到共享的流形中,例如采用神經(jīng)模型的學(xué)習(xí)到的特征表示(例如,循環(huán)嵌入對(duì)話策略等)。
We shall be using Matrix Factorisation.
我們將使用矩陣分解。
(Right) The Outputs embedded into the input’s XY coordinate space using Matrix Factorisation(右)使用矩陣分解將輸出嵌入到輸入的XY坐標(biāo)空間中 Test Predictions using the learnt joint embeddings使用學(xué)習(xí)的聯(lián)合嵌入進(jìn)行測(cè)試預(yù)測(cè)2.c學(xué)習(xí)距離函數(shù) (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.
您也可以近似自定義的非線性距離度量,該度量將學(xué)會(huì)測(cè)量輸入-輸出對(duì)的相對(duì)距離(盡管嵌入方式有所不同)。 我們可以通過輸入一組串聯(lián)的輸入-輸出向量作為模型輸入,并訓(xùn)練模型以預(yù)測(cè)串聯(lián)的輸入-輸出對(duì)的距離得分(0到1之間的值)來做到這一點(diǎn)。
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).
因此,我們通過以下方式修改訓(xùn)練數(shù)據(jù):將輸入和輸出隨機(jī)串聯(lián)(以成為新的訓(xùn)練輸入),如果輸入/輸出對(duì)是有效映射,則將其期望距離設(shè)置為0;如果輸入/輸出對(duì)是無效輸入,則將其期望距離設(shè)置為1。 用于近似距離函數(shù)的模型只是一個(gè)前饋神經(jīng)網(wǎng)絡(luò)(具有3個(gè)隱藏層,每個(gè)具有100個(gè)神經(jīng)元),幾乎與用于分類器方法的模型相同(除了該神經(jīng)網(wǎng)絡(luò)是為學(xué)習(xí)回歸任務(wù)而設(shè)置的)到分類任務(wù))。
Training a feedforward neural network to be a nonlinear distance function (aka cost function). Input = Input-Output pair. Ouput=distance.將前饋神經(jīng)網(wǎng)絡(luò)訓(xùn)練為非線性距離函數(shù)(又稱成本函數(shù))。 輸入=輸入-輸出對(duì)。 輸出=距離。 Test Predictions using the Learnt Distance Function使用學(xué)習(xí)的距離函數(shù)進(jìn)行測(cè)試預(yù)測(cè)結(jié)論 (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.
在性能方面,替代方法的結(jié)果類似于分類器,但是基于檢索的方法在其他領(lǐng)域確實(shí)比分類器具有更多優(yōu)勢(shì),例如添加隨機(jī)元素(如果始終選擇排名最高的結(jié)果,則等同于使用分類器或其他確定性映射,但是,可以通過確保不總是選擇排名最高的結(jié)果來增加一些不確定性,或者像更輕松地更新類,解決更大的多類分類問題(即具有數(shù)百個(gè)類的問題)并能夠解決多標(biāo)簽分類問題(即預(yù)測(cè)給定輸入的前N個(gè)適用輸出)等。
謝謝 (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
菜品三級(jí)分類
總結(jié)
以上是生活随笔為你收集整理的菜品三级分类_分类器的惊人替代品的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方舟手游怎么加点(方舟生存进化)
- 下一篇: 苹果7自带翻译功能在哪