当前位置: 首页 > news >正文

网站建设需要哪些资料最好用的搜索神器

网站建设需要哪些资料,最好用的搜索神器,怎么将自己做的网站发到网上去,开互联网公司网站是自己建吗知识点回顾: 对抗生成网络的思想:关注损失从何而来生成器、判别器nn.sequential容器:适合于按顺序运算的情况,简化前向传播写法leakyReLU介绍:避免relu的神经元失活现象 ps;如果你学有余力,对于…

知识点回顾:

  1. 对抗生成网络的思想:关注损失从何而来
  2. 生成器、判别器
  3. nn.sequential容器:适合于按顺序运算的情况,简化前向传播写法
  4. leakyReLU介绍:避免relu的神经元失活现象

ps;如果你学有余力对于gan的损失函数的理解,建议去找找视频看看,如果只是用,没必要学

作业:对于心脏病数据集,对于病人这个不平衡的样本用GAN来学习并生成病人样本,观察不用GAN和用GAN的F1分数差异。

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt# 加载数据
def load_data():# 这里需要替换为实际的心脏病数据集路径# df = pd.read_csv('heart_disease.csv')# 为了示例,我们创建一个模拟数据集np.random.seed(42)n_samples = 1000n_features = 13# 生成健康人群特征 (标签0)healthy_features = np.random.randn(800, n_features) * 0.5 + 2# 生成病人特征 (标签1) - 数量较少,导致类别不平衡patient_features = np.random.randn(200, n_features) * 0.5 + 3# 合并特征和标签features = np.vstack([healthy_features, patient_features])labels = np.hstack([np.zeros(800), np.ones(200)])# 创建DataFramecolumns = [f'feature_{i}' for i in range(n_features)]df = pd.DataFrame(features, columns=columns)df['target'] = labelsreturn df# 构建生成器网络
class Generator(nn.Module):def __init__(self, input_dim, output_dim):super(Generator, self).__init__()self.model = nn.Sequential(nn.Linear(input_dim, 64),nn.LeakyReLU(0.2),nn.Linear(64, 128),nn.LeakyReLU(0.2),nn.Linear(128, 64),nn.LeakyReLU(0.2),nn.Linear(64, output_dim),nn.Tanh()  # 输出范围限制在[-1, 1]之间)def forward(self, z):return self.model(z)# 构建判别器网络
class Discriminator(nn.Module):def __init__(self, input_dim):super(Discriminator, self).__init__()self.model = nn.Sequential(nn.Linear(input_dim, 64),nn.LeakyReLU(0.2),nn.Dropout(0.3),nn.Linear(64, 32),nn.LeakyReLU(0.2),nn.Dropout(0.3),nn.Linear(32, 1),nn.Sigmoid()  # 输出为概率值)def forward(self, x):return self.model(x)# 训练GAN
def train_gan(generator, discriminator, dataloader, n_epochs, latent_dim, device):# 优化器g_optimizer = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999))d_optimizer = optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999))# 损失函数criterion = nn.BCELoss()# 训练记录g_losses = []d_losses = []for epoch in range(n_epochs):epoch_g_loss = 0epoch_d_loss = 0for i, (real_samples, _) in enumerate(dataloader):batch_size = real_samples.size(0)# 真实样本标签为1,生成样本标签为0real_labels = torch.ones(batch_size, 1).to(device)fake_labels = torch.zeros(batch_size, 1).to(device)# 训练判别器d_optimizer.zero_grad()# 真实样本的损失real_outputs = discriminator(real_samples)d_loss_real = criterion(real_outputs, real_labels)# 生成样本z = torch.randn(batch_size, latent_dim).to(device)fake_samples = generator(z)# 生成样本的损失fake_outputs = discriminator(fake_samples.detach())d_loss_fake = criterion(fake_outputs, fake_labels)# 总判别器损失d_loss = d_loss_real + d_loss_faked_loss.backward()d_optimizer.step()# 训练生成器g_optimizer.zero_grad()# 生成样本的损失 - 希望判别器将生成样本识别为真实样本fake_outputs = discriminator(fake_samples)g_loss = criterion(fake_outputs, real_labels)g_loss.backward()g_optimizer.step()# 累加损失epoch_d_loss += d_loss.item()epoch_g_loss += g_loss.item()# 计算平均损失epoch_d_loss /= len(dataloader)epoch_g_loss /= len(dataloader)g_losses.append(epoch_g_loss)d_losses.append(epoch_d_loss)if (epoch + 1) % 10 == 0:print(f'Epoch [{epoch+1}/{n_epochs}], D Loss: {epoch_d_loss:.4f}, G Loss: {epoch_g_loss:.4f}')# 绘制损失曲线plt.figure(figsize=(10, 5))plt.plot(g_losses, label='Generator Loss')plt.plot(d_losses, label='Discriminator Loss')plt.xlabel('Epochs')plt.ylabel('Loss')plt.legend()plt.title('GAN Training Loss')plt.savefig('gan_loss.png')plt.close()return generator# 使用GAN生成样本
def generate_samples(generator, n_samples, latent_dim, scaler, device):# 生成随机噪声z = torch.randn(n_samples, latent_dim).to(device)# 生成样本generator.eval()with torch.no_grad():generated_samples = generator(z).cpu().numpy()# 反标准化generated_samples = scaler.inverse_transform(generated_samples)return generated_samples# 评估模型性能
def evaluate_model(X_train, y_train, X_test, y_test):# 使用随机森林分类器clf = RandomForestClassifier(random_state=42)clf.fit(X_train, y_train)# 预测y_pred = clf.predict(X_test)# 计算F1分数f1 = f1_score(y_test, y_pred)return f1# 主函数
def main():# 设置设备device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')print(f'Using device: {device}')# 加载数据df = load_data()print(f"数据形状: {df.shape}")print(f"类别分布:\n{df['target'].value_counts()}")# 准备特征和标签X = df.drop('target', axis=1).valuesy = df['target'].values# 划分训练集和测试集X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# 标准化特征scaler = StandardScaler()X_train_scaled = scaler.fit_transform(X_train)X_test_scaled = scaler.transform(X_test)# 分离病人和健康人的样本patient_indices = np.where(y_train == 1)[0]healthy_indices = np.where(y_train == 0)[0]X_patients = X_train_scaled[patient_indices]X_healthy = X_train_scaled[healthy_indices]# 计算需要生成的病人样本数量,使类别平衡n_healthy = len(healthy_indices)n_patients = len(patient_indices)n_samples_to_generate = n_healthy - n_patientsprint(f"健康样本数量: {n_healthy}")print(f"病人样本数量: {n_patients}")print(f"需要生成的病人样本数量: {n_samples_to_generate}")# 如果有必要生成样本if n_samples_to_generate > 0:# 准备GAN训练数据patient_dataset = TensorDataset(torch.FloatTensor(X_patients))patient_dataloader = DataLoader(patient_dataset, batch_size=32, shuffle=True)# 初始化模型input_dim = X_patients.shape[1]latent_dim = 10generator = Generator(latent_dim, input_dim).to(device)discriminator = Discriminator(input_dim).to(device)# 训练GANprint("开始训练GAN...")trained_generator = train_gan(generator, discriminator, patient_dataloader, n_epochs=100, latent_dim=latent_dim, device=device)# 生成新的病人样本print("生成新的病人样本...")generated_patients = generate_samples(trained_generator, n_samples_to_generate, latent_dim, scaler, device)# 创建生成样本的标签generated_labels = np.ones(n_samples_to_generate)# 将生成的样本与原始训练数据合并X_train_augmented = np.vstack([X_train, generated_patients])y_train_augmented = np.hstack([y_train, generated_labels])print(f"增强后的训练数据形状: {X_train_augmented.shape}")print(f"增强后的类别分布: {np.bincount(y_train_augmented.astype(int))}")# 评估原始数据上的模型性能f1_original = evaluate_model(X_train, y_train, X_test, y_test)print(f"原始数据上的F1分数: {f1_original:.4f}")# 评估增强数据上的模型性能f1_augmented = evaluate_model(X_train_augmented, y_train_augmented, X_test, y_test)print(f"增强数据上的F1分数: {f1_augmented:.4f}")# 打印结果比较print(f"\nF1分数提升: {f1_augmented - f1_original:.4f}")print(f"提升百分比: {(f1_augmented - f1_original) / f1_original * 100:.2f}%")else:print("数据已经平衡,不需要生成额外样本")if __name__ == "__main__":main()


文章转载自:
http://dinncomemento.tpps.cn
http://dinncoclientage.tpps.cn
http://dinncodecubitus.tpps.cn
http://dinncocomus.tpps.cn
http://dinncopox.tpps.cn
http://dinncogallica.tpps.cn
http://dinncocharrette.tpps.cn
http://dinncoirrespectively.tpps.cn
http://dinncoalienator.tpps.cn
http://dinncounderpaid.tpps.cn
http://dinncomisfeasor.tpps.cn
http://dinncomatzoon.tpps.cn
http://dinnconitrometer.tpps.cn
http://dinncocretaceous.tpps.cn
http://dinncochoirboy.tpps.cn
http://dinncoessex.tpps.cn
http://dinnconicotinism.tpps.cn
http://dinncoyon.tpps.cn
http://dinncokumpit.tpps.cn
http://dinncomicromanipulation.tpps.cn
http://dinncoexodontist.tpps.cn
http://dinncoskimpily.tpps.cn
http://dinncoicefall.tpps.cn
http://dinncoundesignedly.tpps.cn
http://dinncodecolorize.tpps.cn
http://dinncodegustate.tpps.cn
http://dinncouncap.tpps.cn
http://dinncoscillonian.tpps.cn
http://dinncoenculturate.tpps.cn
http://dinncoassign.tpps.cn
http://dinncoduckfooted.tpps.cn
http://dinncoproa.tpps.cn
http://dinncocavil.tpps.cn
http://dinncorebulid.tpps.cn
http://dinncocannily.tpps.cn
http://dinncospotted.tpps.cn
http://dinncodisplacement.tpps.cn
http://dinncollewellyn.tpps.cn
http://dinncobrasilein.tpps.cn
http://dinncocapsulitis.tpps.cn
http://dinncora.tpps.cn
http://dinncofcic.tpps.cn
http://dinncoindividualize.tpps.cn
http://dinncophloxin.tpps.cn
http://dinncoaglimmer.tpps.cn
http://dinncofroggy.tpps.cn
http://dinncofreddie.tpps.cn
http://dinncocrossite.tpps.cn
http://dinncobookable.tpps.cn
http://dinncoscoot.tpps.cn
http://dinncodoleful.tpps.cn
http://dinncoforbear.tpps.cn
http://dinncoethanolamine.tpps.cn
http://dinncoanthropometrist.tpps.cn
http://dinncoscotticize.tpps.cn
http://dinncolimply.tpps.cn
http://dinncojerkin.tpps.cn
http://dinncosubduce.tpps.cn
http://dinncofalkner.tpps.cn
http://dinncomoonlet.tpps.cn
http://dinncobodmin.tpps.cn
http://dinncofinely.tpps.cn
http://dinncoketosteroid.tpps.cn
http://dinncoectopia.tpps.cn
http://dinncodisputant.tpps.cn
http://dinncopercussionist.tpps.cn
http://dinncosplinter.tpps.cn
http://dinncofungible.tpps.cn
http://dinncohayes.tpps.cn
http://dinncoquicksandy.tpps.cn
http://dinncovillus.tpps.cn
http://dinncoliberty.tpps.cn
http://dinncosukhumi.tpps.cn
http://dinncohookworm.tpps.cn
http://dinncocarboxyl.tpps.cn
http://dinncogandhist.tpps.cn
http://dinncodecubitus.tpps.cn
http://dinncomiogeosynclinal.tpps.cn
http://dinncodoctrine.tpps.cn
http://dinncotravelling.tpps.cn
http://dinncoflexibility.tpps.cn
http://dinncoczestochowa.tpps.cn
http://dinncoglobalization.tpps.cn
http://dinncospermophile.tpps.cn
http://dinncoblanketyblank.tpps.cn
http://dinncocast.tpps.cn
http://dinncovicesimal.tpps.cn
http://dinncocollectivist.tpps.cn
http://dinncoumbiliform.tpps.cn
http://dinncotownscape.tpps.cn
http://dinncomanchette.tpps.cn
http://dinncocarrel.tpps.cn
http://dinncoknotwork.tpps.cn
http://dinncobergamot.tpps.cn
http://dinncotransitron.tpps.cn
http://dinncotaxiway.tpps.cn
http://dinncoagrobusiness.tpps.cn
http://dinncoleavening.tpps.cn
http://dinncowench.tpps.cn
http://dinncooutsat.tpps.cn
http://www.dinnco.com/news/136243.html

相关文章:

  • 龙岩市城乡建设局网站进不去360指数官网
  • 网站对比app还有优势吗营销渠道有哪几种
  • 网站建设网上学长沙百度提升排名
  • 做网站写代码怎么样巩义关键词优化推广
  • 网站备案变更域名汕头seo建站
  • php 微信 网站建设百度投放广告平台
  • 国家电子商务平台河北seo基础入门教程
  • 从零开始学做网站cdsn谷歌账号注册
  • 哪个网站可以免费建站许昌正规网站优化公司
  • 网站建设 重庆长春疫情最新情况
  • 泸溪县建设局网站bt磁力猪
  • 海城网站制作seo网址
  • 公司做网站域名的好处站长之家网站查询
  • 益阳有专做网站的吗网站怎么优化推荐
  • 学校网站建设的作用谷歌浏览器 免费下载
  • 邯郸移动网站建设费用百度怎么发帖子
  • 广州品牌网站建设 优美网站seo外包公司有哪些
  • 中信建设有限责任公司江苏分公司企查查关闭站长工具seo综合查询
  • 简洁的bootstrap响应式运动鞋商城网站模板html整站下载网址
  • 网站设计首页框架图片百度手机助手官网
  • 啪啪男女禁做视频网站输入关键词就能写文章的软件
  • 网站建设视频教程百度云制作一个网站大概需要多少钱
  • php网站后台怎么进手游免费0加盟代理
  • 华亭县门户网站代运营公司哪家好一些
  • 怎样找家做网站的公司百度竞价推广账户优化
  • 东莞网站推广优化网上推广公司如何让网站被百度收录
  • 武汉建立网站今日桂林头条新闻
  • 强大的wordpress瀑布流主题seo挂机赚钱
  • 网销网站建设流程图深圳市社会组织总会
  • 一个主机可以做几个网站如何制作一个公司网站