最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch使用nn.Moudle實(shí)現(xiàn)邏輯回歸

 更新時(shí)間:2022年07月30日 15:42:35   作者:ALEN.Z  
這篇文章主要為大家詳細(xì)介紹了pytorch使用nn.Moudle實(shí)現(xiàn)邏輯回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了pytorch使用nn.Moudle實(shí)現(xiàn)邏輯回歸的具體代碼,供大家參考,具體內(nèi)容如下

內(nèi)容

pytorch使用nn.Moudle實(shí)現(xiàn)邏輯回歸

問題

loss下降不明顯

解決方法

#源代碼 out的數(shù)據(jù)接收方式
? ? ?if torch.cuda.is_available():
? ? ? ? ?x_data=Variable(x).cuda()
? ? ? ? ?y_data=Variable(y).cuda()
? ? ?else:
? ? ? ? ?x_data=Variable(x)
? ? ? ? ?y_data=Variable(y)
? ??
? ? out=logistic_model(x_data) ?#根據(jù)邏輯回歸模型擬合出的y值
? ? loss=criterion(out.squeeze(),y_data) ?#計(jì)算損失函數(shù)
#源代碼 out的數(shù)據(jù)有拼裝數(shù)據(jù)直接輸入
# ? ? if torch.cuda.is_available():
# ? ? ? ? x_data=Variable(x).cuda()
# ? ? ? ? y_data=Variable(y).cuda()
# ? ? else:
# ? ? ? ? x_data=Variable(x)
# ? ? ? ? y_data=Variable(y)
? ??
? ? out=logistic_model(x_data) ?#根據(jù)邏輯回歸模型擬合出的y值
? ? loss=criterion(out.squeeze(),y_data) ?#計(jì)算損失函數(shù)
? ? print_loss=loss.data.item() ?#得出損失函數(shù)值

源代碼

import torch
from torch import nn
from torch.autograd import Variable
import matplotlib.pyplot as plt
import numpy as np

#生成數(shù)據(jù)
sample_nums = 100
mean_value = 1.7
bias = 1
n_data = torch.ones(sample_nums, 2)
x0 = torch.normal(mean_value * n_data, 1) + bias ? ? ?# 類別0 數(shù)據(jù) shape=(100, 2)
y0 = torch.zeros(sample_nums) ? ? ? ? ? ? ? ? ? ? ? ? # 類別0 標(biāo)簽 shape=(100, 1)
x1 = torch.normal(-mean_value * n_data, 1) + bias ? ? # 類別1 數(shù)據(jù) shape=(100, 2)
y1 = torch.ones(sample_nums) ? ? ? ? ? ? ? ? ? ? ? ? ?# 類別1 標(biāo)簽 shape=(100, 1)
x_data = torch.cat((x0, x1), 0) ?#按維數(shù)0行拼接
y_data = torch.cat((y0, y1), 0)

#畫圖
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
plt.show()

# 利用torch.nn實(shí)現(xiàn)邏輯回歸
class LogisticRegression(nn.Module):
? ? def __init__(self):
? ? ? ? super(LogisticRegression, self).__init__()
? ? ? ? self.lr = nn.Linear(2, 1)
? ? ? ? self.sm = nn.Sigmoid()

? ? def forward(self, x):
? ? ? ? x = self.lr(x)
? ? ? ? x = self.sm(x)
? ? ? ? return x
? ??
logistic_model = LogisticRegression()
# if torch.cuda.is_available():
# ? ? logistic_model.cuda()

#loss函數(shù)和優(yōu)化
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(logistic_model.parameters(), lr=0.01, momentum=0.9)
#開始訓(xùn)練
#訓(xùn)練10000次
for epoch in range(10000):
# ? ? if torch.cuda.is_available():
# ? ? ? ? x_data=Variable(x).cuda()
# ? ? ? ? y_data=Variable(y).cuda()
# ? ? else:
# ? ? ? ? x_data=Variable(x)
# ? ? ? ? y_data=Variable(y)
? ??
? ? out=logistic_model(x_data) ?#根據(jù)邏輯回歸模型擬合出的y值
? ? loss=criterion(out.squeeze(),y_data) ?#計(jì)算損失函數(shù)
? ? print_loss=loss.data.item() ?#得出損失函數(shù)值
? ? #反向傳播
? ? loss.backward()
? ? optimizer.step()
? ? optimizer.zero_grad()
? ??
? ? mask=out.ge(0.5).float() ?#以0.5為閾值進(jìn)行分類
? ? correct=(mask==y_data).sum().squeeze() ?#計(jì)算正確預(yù)測的樣本個(gè)數(shù)
? ? acc=correct.item()/x_data.size(0) ?#計(jì)算精度
? ? #每隔20輪打印一下當(dāng)前的誤差和精度
? ? if (epoch+1)%100==0:
? ? ? ? print('*'*10)
? ? ? ? print('epoch {}'.format(epoch+1)) ?#誤差
? ? ? ? print('loss is {:.4f}'.format(print_loss))
? ? ? ? print('acc is {:.4f}'.format(acc)) ?#精度
? ? ? ??
? ? ? ??
w0, w1 = logistic_model.lr.weight[0]
w0 = float(w0.item())
w1 = float(w1.item())
b = float(logistic_model.lr.bias.item())
plot_x = np.arange(-7, 7, 0.1)
plot_y = (-w0 * plot_x - b) / w1
plt.xlim(-5, 7)
plt.ylim(-7, 7)
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=logistic_model(x_data)[:,0].cpu().data.numpy(), s=100, lw=0, cmap='RdYlGn')
plt.plot(plot_x, plot_y)
plt.show()

輸出結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中的TCP socket寫法示例

    Python中的TCP socket寫法示例

    最近在學(xué)習(xí)腳本語言python,所以下面這篇文章主要給大家介紹了關(guān)于Python中TCP socket寫法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們一起來看看吧
    2018-05-05
  • Python 命令行非阻塞輸入的小例子

    Python 命令行非阻塞輸入的小例子

    很久很久以前,系windows平臺(tái)下,用C語言寫過一款貪食蛇游戲,cmd界面,用kbhit()函數(shù)實(shí)現(xiàn)非阻塞輸入。系windows平臺(tái)下用python依然可以調(diào)用msvcrt.khbit實(shí)現(xiàn)非阻塞監(jiān)聽。但系喺linux下面就冇呢支歌仔唱
    2013-09-09
  • PyCharm-錯(cuò)誤-找不到指定文件python.exe的解決方法

    PyCharm-錯(cuò)誤-找不到指定文件python.exe的解決方法

    今天小編就為大家分享一篇PyCharm-錯(cuò)誤-找不到指定文件python.exe的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python隨機(jī)函數(shù)random隨機(jī)獲取數(shù)字、字符串、列表等使用詳解

    Python隨機(jī)函數(shù)random隨機(jī)獲取數(shù)字、字符串、列表等使用詳解

    這篇文章主要介紹了Python隨機(jī)函數(shù)random使用詳解包含了Python隨機(jī)數(shù)字,Python隨機(jī)字符串,Python隨機(jī)列表等,需要的朋友可以參考下
    2021-04-04
  • python學(xué)習(xí)入門細(xì)節(jié)知識(shí)點(diǎn)

    python學(xué)習(xí)入門細(xì)節(jié)知識(shí)點(diǎn)

    我們整理了關(guān)于python入門學(xué)習(xí)的一些細(xì)節(jié)知識(shí)點(diǎn),對于學(xué)習(xí)python的初學(xué)者很有用,一起學(xué)習(xí)下。
    2018-03-03
  • Python3實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲

    Python3實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲

    這篇文章主要為大家詳細(xì)介紹了Python3實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Python?numpy.transpose使用詳解

    Python?numpy.transpose使用詳解

    本文主要介紹了Python?numpy.transpose使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • python字符串驗(yàn)證的幾種實(shí)現(xiàn)方法

    python字符串驗(yàn)證的幾種實(shí)現(xiàn)方法

    字符串的驗(yàn)證是確保數(shù)據(jù)符合特定要求的關(guān)鍵步驟之一,本文主要介紹了python字符串驗(yàn)證的幾種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列詳解

    Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列,詳細(xì)分析了隊(duì)列的定義、功能與Python實(shí)現(xiàn)隊(duì)列的相關(guān)技巧,以及具體的用法,需要的朋友可以參考下
    2015-04-04
  • 詳解 Python中LEGB和閉包及裝飾器

    詳解 Python中LEGB和閉包及裝飾器

    這篇文章主要介紹了詳解 Python中LEGB和閉包及裝飾器的相關(guān)資料,主要介紹了函數(shù)作用域和閉包的理解和使用方法及Python中的裝飾器,需要的朋友可以參考下
    2017-08-08

最新評論

勐海县| 石门县| 高台县| 康平县| 濉溪县| 镇赉县| 涞水县| 栾城县| 拜泉县| 贡觉县| 福建省| 丁青县| 惠州市| 革吉县| 巨鹿县| 永和县| 天镇县| 绵阳市| 崇州市| 彭州市| 石台县| 阜康市| 定边县| 衡南县| 河北区| 乌兰察布市| 涞源县| 定南县| 会同县| 沁水县| 兴安盟| 蕲春县| 泰兴市| 克东县| 延庆县| 青龙| 梁山县| 五大连池市| 大关县| 枣强县| 汽车|