中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站倒計(jì)時(shí)代碼資源企業(yè)網(wǎng)站排名優(yōu)化價(jià)格

網(wǎng)站倒計(jì)時(shí)代碼,資源企業(yè)網(wǎng)站排名優(yōu)化價(jià)格,discourse wordpress,快速排名優(yōu)化知識點(diǎn)回顧: 隨機(jī)種子內(nèi)參的初始化神經(jīng)網(wǎng)絡(luò)調(diào)參指南 參數(shù)的分類調(diào)參的順序各部分參數(shù)的調(diào)整心得 作業(yè):對于day41的簡單cnn,看看是否可以借助調(diào)參指南進(jìn)一步提高精度。 import torch import torch.nn as nn import torch.optim as optim impo…

知識點(diǎn)回顧:

  1. 隨機(jī)種子
  2. 內(nèi)參的初始化
  3. 神經(jīng)網(wǎng)絡(luò)調(diào)參指南
    1. 參數(shù)的分類
    2. 調(diào)參的順序
    3. 各部分參數(shù)的調(diào)整心得

作業(yè):對于day'41的簡單cnn,看看是否可以借助調(diào)參指南進(jìn)一步提高精度。

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import random
from tqdm import tqdm# 設(shè)置隨機(jī)種子確保結(jié)果可復(fù)現(xiàn)
def set_seed(seed=42):random.seed(seed)np.random.seed(seed)torch.manual_seed(seed)if torch.cuda.is_available():torch.cuda.manual_seed(seed)torch.cuda.manual_seed_all(seed)torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False# 定義CNN模型
class SimpleCNN(nn.Module):def __init__(self):super(SimpleCNN, self).__init__()# 第一個(gè)卷積層:輸入通道1,輸出通道32,卷積核3x3self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)self.bn1 = nn.BatchNorm2d(32)self.relu1 = nn.ReLU()self.pool1 = nn.MaxPool2d(2)# 第二個(gè)卷積層:輸入通道32,輸出通道64,卷積核3x3self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)self.bn2 = nn.BatchNorm2d(64)self.relu2 = nn.ReLU()self.pool2 = nn.MaxPool2d(2)# 全連接層self.fc1 = nn.Linear(64 * 7 * 7, 128)self.dropout = nn.Dropout(0.5)self.fc2 = nn.Linear(128, 10)def forward(self, x):# 卷積層1x = self.conv1(x)x = self.bn1(x)x = self.relu1(x)x = self.pool1(x)# 卷積層2x = self.conv2(x)x = self.bn2(x)x = self.relu2(x)x = self.pool2(x)# 展平x = x.view(-1, 64 * 7 * 7)# 全連接層x = self.fc1(x)x = self.relu1(x)x = self.dropout(x)x = self.fc2(x)return x# 參數(shù)初始化
def init_weights(m):if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')if m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.BatchNorm2d):nn.init.constant_(m.weight, 1)nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Linear):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')nn.init.constant_(m.bias, 0)# 訓(xùn)練函數(shù)
def train(model, train_loader, criterion, optimizer, device):model.train()train_loss = 0correct = 0total = 0for batch_idx, (inputs, targets) in enumerate(tqdm(train_loader)):inputs, targets = inputs.to(device), targets.to(device)optimizer.zero_grad()outputs = model(inputs)loss = criterion(outputs, targets)loss.backward()optimizer.step()train_loss += loss.item()_, predicted = outputs.max(1)total += targets.size(0)correct += predicted.eq(targets).sum().item()acc = 100. * correct / totalreturn train_loss / len(train_loader), acc# 測試函數(shù)
def test(model, test_loader, criterion, device):model.eval()test_loss = 0correct = 0total = 0with torch.no_grad():for batch_idx, (inputs, targets) in enumerate(tqdm(test_loader)):inputs, targets = inputs.to(device), targets.to(device)outputs = model(inputs)loss = criterion(outputs, targets)test_loss += loss.item()_, predicted = outputs.max(1)total += targets.size(0)correct += predicted.eq(targets).sum().item()acc = 100. * correct / totalreturn test_loss / len(test_loader), acc# 主函數(shù)
def main():# 設(shè)置隨機(jī)種子set_seed(42)# 設(shè)置設(shè)備device = 'cuda' if torch.cuda.is_available() else 'cpu'print(f"使用設(shè)備: {device}")# 數(shù)據(jù)預(yù)處理transform_train = transforms.Compose([transforms.RandomRotation(10),transforms.ToTensor(),transforms.Normalize((0.1307,), (0.3081,))])transform_test = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.1307,), (0.3081,))])# 加載數(shù)據(jù)集train_dataset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform_train)test_dataset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform_test)# 創(chuàng)建數(shù)據(jù)加載器batch_size = 128  # 調(diào)參參數(shù)1train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=2)test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=100, shuffle=False, num_workers=2)# 初始化模型model = SimpleCNN().to(device)model.apply(init_weights)# 定義損失函數(shù)和優(yōu)化器criterion = nn.CrossEntropyLoss()learning_rate = 0.01  # 調(diào)參參數(shù)2optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-5)  # 調(diào)參參數(shù)3scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=3, factor=0.5)# 訓(xùn)練模型epochs = 15  # 調(diào)參參數(shù)4train_losses = []train_accs = []test_losses = []test_accs = []best_acc = 0for epoch in range(epochs):print(f"\nEpoch: {epoch+1}/{epochs}")train_loss, train_acc = train(model, train_loader, criterion, optimizer, device)test_loss, test_acc = test(model, test_loader, criterion, device)train_losses.append(train_loss)train_accs.append(train_acc)test_losses.append(test_loss)test_accs.append(test_acc)print(f"Train Loss: {train_loss:.4f} | Train Acc: {train_acc:.2f}%")print(f"Test Loss: {test_loss:.4f} | Test Acc: {test_acc:.2f}%")# 學(xué)習(xí)率調(diào)整scheduler.step(test_loss)# 保存最佳模型if test_acc > best_acc:best_acc = test_acctorch.save(model.state_dict(), 'best_model.pth')print(f"Model saved with accuracy: {best_acc:.2f}%")# 加載最佳模型model.load_state_dict(torch.load('best_model.pth'))_, final_acc = test(model, test_loader, criterion, device)print(f"\nFinal Test Accuracy: {final_acc:.2f}%")# 繪制訓(xùn)練過程plt.figure(figsize=(12, 5))plt.subplot(1, 2, 1)plt.plot(train_losses, label='Train Loss')plt.plot(test_losses, label='Test Loss')plt.xlabel('Epoch')plt.ylabel('Loss')plt.legend()plt.title('Loss Curves')plt.subplot(1, 2, 2)plt.plot(train_accs, label='Train Accuracy')plt.plot(test_accs, label='Test Accuracy')plt.xlabel('Epoch')plt.ylabel('Accuracy (%)')plt.legend()plt.title('Accuracy Curves')plt.tight_layout()plt.savefig('training_curves.png')plt.show()if __name__ == '__main__':main()

http://www.risenshineclean.com/news/7977.html

相關(guān)文章:

  • html制作網(wǎng)站的步驟網(wǎng)絡(luò)服務(wù)包括
  • 企業(yè)域名是什么網(wǎng)站seo關(guān)鍵詞設(shè)置
  • 網(wǎng)站設(shè)計(jì)營銷網(wǎng)站出租三級域名費(fèi)用
  • 做視頻網(wǎng)站視頻的軟件企業(yè)營銷培訓(xùn)課程
  • 女性時(shí)尚網(wǎng)站源碼客戶關(guān)系管理
  • 有沒有免費(fèi)的微網(wǎng)站視頻營銷模式有哪些
  • 昭通網(wǎng)站建設(shè)如何提高網(wǎng)站排名的方法
  • 二手站網(wǎng)站怎做優(yōu)化課程體系
  • 招聘 負(fù)責(zé)網(wǎng)站開發(fā)網(wǎng)絡(luò)營銷有什么方式
  • 網(wǎng)站做cdn百度網(wǎng)頁版入口
  • 網(wǎng)站信息發(fā)布制度建設(shè)seo網(wǎng)站優(yōu)化排名
  • 建網(wǎng)站哪便宜百度網(wǎng)站提交入口網(wǎng)址
  • 網(wǎng)頁跟網(wǎng)站的區(qū)別百度seo2022
  • 開發(fā)app的注意事項(xiàng)seo代理計(jì)費(fèi)系統(tǒng)
  • 兩耳清風(fēng)怎么做網(wǎng)站南京網(wǎng)絡(luò)優(yōu)化培訓(xùn)
  • 校園網(wǎng)站建設(shè)論文域名大全查詢
  • wordpress+4.9+google蘋果aso優(yōu)化
  • 做網(wǎng)站還要寫文章嗎品牌運(yùn)營中心
  • 做圖片網(wǎng)站用什么程序百度地圖導(dǎo)航手機(jī)版免費(fèi)下載
  • 可以先做網(wǎng)站后備案么app拉新推廣平臺有哪些
  • 2345網(wǎng)址導(dǎo)航手機(jī)上網(wǎng)導(dǎo)航下載seo網(wǎng)絡(luò)推廣教程
  • 秦皇島建設(shè)網(wǎng)站品牌宣傳方式
  • 網(wǎng)絡(luò)營銷方式的使用方法搜索引擎優(yōu)化有哪些要點(diǎn)
  • 長春關(guān)鍵詞推廣快速排名優(yōu)化seo
  • 網(wǎng)頁設(shè)計(jì)與實(shí)現(xiàn)論文杭州seo公司排名
  • 濟(jì)寧營銷網(wǎng)站建設(shè)長沙網(wǎng)站優(yōu)化排名推廣
  • 加強(qiáng)兩微一端和門戶網(wǎng)站建設(shè)云資源軟文發(fā)布平臺
  • 代購網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷平臺有哪些?
  • 武漢網(wǎng)站多少百度網(wǎng)址導(dǎo)航
  • 南山做網(wǎng)站推廣樂云seo最新營銷模式