久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

np.dot 和 np.multiply 與 np.sum 在二進制交叉熵損失計

Difference between np.dot and np.multiply with np.sum in binary cross-entropy loss calculation(np.dot 和 np.multiply 與 np.sum 在二進制交叉熵損失計算中的區(qū)別)
本文介紹了np.dot 和 np.multiply 與 np.sum 在二進制交叉熵損失計算中的區(qū)別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我嘗試了以下代碼,但沒有發(fā)現(xiàn) np.dotnp.multiply 與 np.sum 之間的區(qū)別

這里是 np.dot 代碼

logprobs = np.dot(Y, (np.log(A2)).T) + np.dot((1.0-Y),(np.log(1 - A2)).T)打印(logprobs.shape)打印(日志問題)成本 = (-1/m) * logprobs打印(成本.形狀)打印(類型(成本))打印(成本)

它的輸出是

(1, 1)[[-2.07917628]](1, 1)<類'numpy.ndarray'>[[ 0.693058761039 ]]

這是 np.multiply 與 np.sum 的代碼

logprobs = np.sum(np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2)))打印(logprobs.shape)打印(日志問題)成本 = - logprobs/m打印(成本.形狀)打印(類型(成本))打印(成本)

它的輸出是

<代碼>()-2.07917628312()<類'numpy.float64'>0.693058761039

我無法理解類型和形狀的差異,而兩種情況下的結(jié)果值相同

即使在壓縮前代碼的情況下成本值與后相同但類型保持相同

cost = np.squeeze(cost)打印(類型(成本))打印(成本)

輸出是

<class 'numpy.ndarray'>0.6930587610394646

解決方案

你正在做的是計算 二元交叉熵損失,用于衡量模型的預(yù)測(此處為:A2)與真實輸出(這里:Y).

這是您的案例的可重現(xiàn)示例,它應(yīng)該解釋為什么您在第二種情況下使用 np.sum

得到一個標量

在[88]中:Y = np.array([[1, 0, 1, 1, 0, 1, 0, 0]])在 [89] 中:A2 = np.array([[0.8, 0.2, 0.95, 0.92, 0.01, 0.93, 0.1, 0.02]])在 [90] 中:logprobs = np.dot(Y, (np.log(A2)).T) + np.dot((1.0-Y),(np.log(1 - A2)).T)# `np.dot` 返回二維數(shù)組,因為它的參數(shù)是二維數(shù)組在 [91] 中:logprobs出[91]:數(shù)組([[-0.78914626]])在 [92] 中:成本 = (-1/m) * logprobs在 [93] 中:成本出[93]:數(shù)組([[ 0.09864328]])在 [94] 中:logprobs = np.sum(np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2)))# np.sum 返回標量,因為它對 2D 數(shù)組中的所有內(nèi)容求和在 [95] 中:logprobs輸出[95]:-0.78914625761870361

請注意,np.dot 僅對與此處 (1x8) 和 (8x1) 匹配的內(nèi)部尺寸求和.因此,8s 將在點積或矩陣乘法期間消失,產(chǎn)生的結(jié)果為 (1x1),這只是一個 標量,但返回作為形狀 (1,1).

的二維數(shù)組

另外,最重要的是注意這里 np.dotnp.matmul 完全相同,因為輸入是二維數(shù)組(即矩陣)

在[107]中:logprobs = np.matmul(Y, (np.log(A2)).T) + np.matmul((1.0-Y),(np.log(1 - A2)).T)在 [108] 中:logprobs出[108]:數(shù)組([[-0.78914626]])在 [109] 中:logprobs.shape出 [109]: (1, 1)


標量值的形式返回結(jié)果

np.dotnp.matmul 根據(jù)輸入數(shù)組返回任何結(jié)果數(shù)組形狀.如果輸入是二維數(shù)組,即使使用 out= 參數(shù)也無法返回 標量.但是,我們可以使用 np.asscalar() 如果結(jié)果數(shù)組的形狀為 (1,1) (或更一般地說是 scalar 包裹在 nD 數(shù)組中的值)

在 [123]: np.asscalar(logprobs)輸出[123]:-0.7891462576187036在 [124] 中:類型(np.asscalar(logprobs))出[124]:浮動


<塊引用>

ndarray 大小為 1 到 標量

在 [127]: np.asscalar(np.array([[[23.2]]]))出局[127]:23.2在 [128] 中:np.asscalar(np.array([[[[23.2]]]]))出局[128]:23.2

I have tried the following code but didn't find the difference between np.dot and np.multiply with np.sum

Here is np.dot code

logprobs = np.dot(Y, (np.log(A2)).T) + np.dot((1.0-Y),(np.log(1 - A2)).T)
print(logprobs.shape)
print(logprobs)
cost = (-1/m) * logprobs
print(cost.shape)
print(type(cost))
print(cost)

Its output is

(1, 1)
[[-2.07917628]]
(1, 1)
<class 'numpy.ndarray'>
[[ 0.693058761039 ]]

Here is the code for np.multiply with np.sum

logprobs = np.sum(np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2)))
print(logprobs.shape)         
print(logprobs)
cost = - logprobs / m
print(cost.shape)
print(type(cost))
print(cost)

Its output is

()
-2.07917628312
()
<class 'numpy.float64'>
0.693058761039

I'm unable to understand the type and shape difference whereas the result value is same in both cases

Even in the case of squeezing former code cost value become same as later but type remains same

cost = np.squeeze(cost)
print(type(cost))
print(cost)

output is

<class 'numpy.ndarray'>
0.6930587610394646

解決方案

What you're doing is calculating the binary cross-entropy loss which measures how bad the predictions (here: A2) of the model are when compared to the true outputs (here: Y).

Here is a reproducible example for your case, which should explain why you get a scalar in the second case using np.sum

In [88]: Y = np.array([[1, 0, 1, 1, 0, 1, 0, 0]])

In [89]: A2 = np.array([[0.8, 0.2, 0.95, 0.92, 0.01, 0.93, 0.1, 0.02]])

In [90]: logprobs = np.dot(Y, (np.log(A2)).T) + np.dot((1.0-Y),(np.log(1 - A2)).T)

# `np.dot` returns 2D array since its arguments are 2D arrays
In [91]: logprobs
Out[91]: array([[-0.78914626]])

In [92]: cost = (-1/m) * logprobs

In [93]: cost
Out[93]: array([[ 0.09864328]])

In [94]: logprobs = np.sum(np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2)))

# np.sum returns scalar since it sums everything in the 2D array
In [95]: logprobs
Out[95]: -0.78914625761870361

Note that the np.dot sums along only the inner dimensions which match here (1x8) and (8x1). So, the 8s will be gone during the dot product or matrix multiplication yielding the result as (1x1) which is just a scalar but returned as 2D array of shape (1,1).


Also, most importantly note that here np.dot is exactly same as doing np.matmul since the inputs are 2D arrays (i.e. matrices)

In [107]: logprobs = np.matmul(Y, (np.log(A2)).T) + np.matmul((1.0-Y),(np.log(1 - A2)).T)

In [108]: logprobs
Out[108]: array([[-0.78914626]])

In [109]: logprobs.shape
Out[109]: (1, 1)


Return result as a scalar value

np.dot or np.matmul returns whatever the resulting array shape would be, based on input arrays. Even with out= argument it's not possible to return a scalar, if the inputs are 2D arrays. However, we can use np.asscalar() on the result to convert it to a scalar if the result array is of shape (1,1) (or more generally a scalar value wrapped in an nD array)

In [123]: np.asscalar(logprobs)
Out[123]: -0.7891462576187036

In [124]: type(np.asscalar(logprobs))
Out[124]: float


ndarray of size 1 to scalar value

In [127]: np.asscalar(np.array([[[23.2]]]))
Out[127]: 23.2

In [128]: np.asscalar(np.array([[[[23.2]]]]))
Out[128]: 23.2

這篇關(guān)于np.dot 和 np.multiply 與 np.sum 在二進制交叉熵損失計算中的區(qū)別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: av 一区二区三区 | 日本羞羞影院 | 69福利影院| 精品视频免费 | 亚洲免费网站 | 日韩精品一区在线观看 | 91久久久久久久久久久久久 | 欧美一区二区大片 | 欧美一区视频 | 中文字幕一区二区三区精彩视频 | 日韩精品免费看 | 精品国产乱码一区二区三区 | 久久99这里只有精品 | 狼人伊人影院 | 亚洲精品一区二区 | 99热视| 日韩av在线一区 | 中文字幕日韩一区 | 亚洲午夜三级 | 成人一区二区视频 | 成人免费网站在线 | 亚洲免费久久久 | aaa在线 | 91免费在线| 天天操网| 国产1区 | 在线观看中文字幕一区二区 | 亚洲综合色网站 | 东方伊人免费在线观看 | 午夜精品久久久久久久99黑人 | 欧美在线亚洲 | 色网站入口 | 国产精品久久久久久二区 | 成人免费视频网站在线看 | 区一区二在线观看 | 国产成人jvid在线播放 | 久在线视频播放免费视频 | 亚洲码欧美码一区二区三区 | 欧美在线免费 | 91久久精品视频 | 欧美在线一区二区三区 |