Loss is a numerical metric that describes how wrong a model's predictions are. Loss فاصله بین پیش بینی های مدل و برچسب های واقعی را اندازه گیری می کند. The goal of training a model is to minimize the loss, reducing it to its lowest possible value.
In the following image, you can visualize loss as arrows drawn from the data points to the model. The arrows show how far the model's predictions are from the actual values.
شکل 9 . Loss is measured from the actual value to the predicted value.
فاصله از دست دادن
In statistics and machine learning, loss measures the difference between the predicted and actual values. Loss focuses on the distance between the values, not the direction. For example, if a model predicts 2, but the actual value is 5, we don't care that the loss is negative ($ 2-5=-3 $). Instead, we care that the distance between the values is $ 3 $. Thus, all methods for calculating loss remove the sign.
دو روش رایج برای حذف علامت عبارتند از:
- Take the absolute value of the difference between the actual value and the prediction.
- Square the difference between the actual value and the prediction.
انواع ضرر
In linear regression, there are four main types of loss, which are outlined in the following table.
نوع ضرر | تعریف | معادله |
---|---|---|
L 1 باخت | The sum of the absolute values of the difference between the predicted values and the actual values. | $ ∑ | واقعی\ ارزش - پیش بینی شده\ ارزش | $ |
میانگین خطای مطلق (MAE) | The average of L 1 losses across a set of *N* examples. | $ \frac{1}{N} ∑ | واقعی\ ارزش - پیش بینی شده\ ارزش | $ |
L 2 باخت | The sum of the squared difference between the predicted values and the actual values. | $ ∑ (مقدار\ واقعی - مقدار\ پیش بینی شده)^2 $ |
میانگین مربعات خطا (MSE) | The average of L 2 losses across a set of *N* examples. | $ \frac{1}{N} ∑ (actual\ value - predicted\ value)^2 $ |
The functional difference between L 1 loss and L 2 loss (or between MAE and MSE) is squaring. When the difference between the prediction and label is large, squaring makes the loss even larger. When the difference is small (less than 1), squaring makes the loss even smaller.
هنگام پردازش چندین مثال به طور همزمان ، توصیه می کنیم به طور متوسط ضررها را در تمام مثالها ، چه با استفاده از MAE یا MSE ، انجام دهید.
مثال محاسبه ضرر
Using the previous best fit line , we'll calculate L 2 loss for a single example. From the best fit line, we had the following values for weight and bias:
- $ \small{وزن: -4.6} $
- $ \small{تعصب: 34} $
اگر مدل پیشبینی میکند که یک خودروی 2370 پوندی 23.1 مایل در هر گالن دریافت میکند، اما در واقع 26 مایل در هر گالن دریافت میکند، ضرر L 2 را به صورت زیر محاسبه میکنیم:
ارزش | معادله | نتیجه |
---|---|---|
پیش بینی | $\small{bias + (وزن * ویژگی\ ارزش)}$ $\small{34 + (-4.6*2.37)}$ | $\small{23.1}$ |
ارزش واقعی | $ \small{ label } $ | $ \small{ 26 } $ |
L 2 باخت | $ \small{ (مقدار\ واقعی - پیش بینی شده\ ارزش)^2 } $ $\small{ (26 - 23.1)^2 }$ | $\small{8.41}$ |
In this example, the L 2 loss for that single data point is 8.41.
انتخاب ضرر
تصمیمگیری در مورد استفاده از MAE یا MSE میتواند به مجموعه دادهها و روشی که میخواهید پیشبینیهای خاصی را مدیریت کنید بستگی دارد. اکثر مقادیر ویژگی در یک مجموعه داده معمولاً در یک محدوده مجزا قرار می گیرند. For example, cars are normally between 2000 and 5000 pounds and get between 8 to 50 miles per gallon. An 8,000-pound car, or a car that gets 100 miles per gallon, is outside the typical range and would be considered an outlier .
پرت همچنین می تواند به این اشاره داشته باشد که پیش بینی های یک مدل چقدر از مقادیر واقعی فاصله دارد. به عنوان مثال، 3000 پوند در محدوده وزن معمولی خودرو قرار دارد و 40 مایل در هر گالن در محدوده بازده سوخت معمولی است. با این حال، یک خودروی 3000 پوندی که 40 مایل در هر گالن مصرف میکند، از نظر پیشبینی مدل، یک خودروی پرت خواهد بود، زیرا این مدل پیشبینی میکند که یک خودروی 3000 پوندی حدود 20 مایل در هر گالن خواهد داشت.
هنگام انتخاب بهترین تابع ضرر، نحوه رفتار مدل را در نظر بگیرید. به عنوان مثال، MSE مدل را بیشتر به سمت نقاط پرت حرکت می دهد، در حالی که MAE این کار را نمی کند. از دست دادن L 2 جریمه بسیار بالاتری را برای باخت دورتر از L 1 متحمل می شود. به عنوان مثال، تصاویر زیر یک مدل آموزش دیده با استفاده از MAE و یک مدل آموزش دیده با استفاده از MSE را نشان می دهد. خط قرمز نشان دهنده یک مدل کاملا آموزش دیده است که برای پیش بینی استفاده می شود. نقاط پرت به مدل آموزش دیده با MSE نزدیکتر از مدل آموزش دیده با MAE است.
شکل 10 . یک مدل آموزش دیده با MSE مدل را به نقاط پرت نزدیکتر می کند.
شکل 11 . مدلی که با MAE آموزش دیده است، دورتر از موارد پرت است.
به رابطه بین مدل و داده ها توجه داشته باشید:
MSE . مدل به نقاط پرت نزدیکتر است اما از بسیاری از نقاط داده دیگر دورتر است.
MAE مدل دورتر از نقاط پرت است اما به بسیاری از نقاط داده دیگر نزدیکتر است.
درک خود را بررسی کنید
دو نمودار زیر را از یک مدل خطی متناسب با یک مجموعه داده در نظر بگیرید:
![]() | ![]() |