cGAN代码实现详解(一)

步骤一:初始化以及定义参数

import argparse
import os
import numpy as np
import math

import torchvision.transforms as transforms
from torchvision.utils import save_image

from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable

import torch.nn as nn
import torch.nn.functional as F
import torch

os.makedirs("images", exist_ok=True)

parser = argparse.ArgumentParser()
parser.add_argument("--n_epochs", type=int, default=200, help="number of epochs of training")
parser.add_argument("--batch_size", type=int, default=64, help="size of the batches")
parser.add_argument("--lr", type=float, default=0.0002, help="adam: learning rate")
parser.add_argument("--b1", type=float, default=0.5, help="adam: decay of first order momentum of gradient")
parser.add_argument("--b2", type=float, default=0.999, help="adam: decay of first order momentum of gradient")
parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=64, help="dimensionality of the latent space")
parser.add_argument("--n_classes", type=int, default=10, help="number of classes for dataset")
parser.add_argument("--img_size", type=int, default=28, help="size of each image dimension")
parser.add_argument("--channels", type=int, default=1, help="number of image channels")
parser.add_argument("--sample_interval", type=int, default=400, help="interval between image sampling")
opt = parser.parse_args()
print(opt)

二、建立生成器模型

// An highlighted block
 #生成器
class Generator(nn.Module):  #z=torch.Size([100, 100]);labels.shape=torch.Size([100]   100-9)
    def __init__(self):
        super(Generator, self).__init__()

        self.label_emb = nn.Embedding(opt.n_classes, opt.n_classes)

        self.model = nn.Sequential( #74--128--256--512--1024--imgshape  [输入的是(16,100)--->(1,28,28)]
            nn.Linear(opt.latent_dim+opt.n_classes, 128),
            nn.Linear(128, 256),
            torch.nn.ReLU(inplace=True),
            nn.Linear(256, 512),
            torch.nn.ReLU(inplace=True),
            nn.Linear(512, 1024),
            torch.nn.ReLU(inplace=True),
            nn.Linear(1024, int(np.prod(img_shape))),
            nn.Tanh()
        )

    def forward(self,z,labels):
        # Concatenate label embedding and image to produce input
        gen_input=torch.cat((self.label_emb(labels),z),-1)
        img = self.model(gen_input)
        #forward中的z是在程序后面的定义的高斯噪声信号,形状为64 100
        img = img.view(img.size(0), *img_shape)
        #img.size(0)的话,它为64,也就是一批次训练的数目
        return img

验证生成器是否有作用

#查看生成器是否成功运行
use_gpu = True if torch.cuda.is_available() else False
FloatTensor = torch.cuda.FloatTensor if use_gpu else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_gpu else torch.LongTensor

n_row=10
z = Variable(FloatTensor(np.random.normal(0, 1, (n_row ** 2, opt.latent_dim))))
# Get labels ranging from 0 to n_classes for n rows
labels = np.array([num for _ in range(n_row) for num in range(n_row)])
labels = Variable(LongTensor(labels))

#实例化
generator=Generator()

if use_gpu:
    print("use gpu for training")
    generator = generator.cuda()
    z = z.to("cuda")
    labels=labels.to("cuda")

print(z.shape)
print(labels.shape)

imgs=generator(z,labels)

print(imgs.shape)
 

运行结果如下

Namespace(b1=0.5, b2=0.999, batch_size=64, channels=1, img_size=32, latent_dim=100, lr=0.0002, n_classes=10, n_cpu=8, n_epochs=200, sample_interval=400)
use gpu for training
torch.Size([100, 64])
torch.Size([100])
torch.Size([100, 1, 28, 28])
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的CGAN代码示例: ```python import torch import torch.nn as nn import numpy as np # 定义 generator 和 discriminator 的网络结构 class Generator(nn.Module): def __init__(self, input_dim, output_dim, hidden_dim): super(Generator, self).__init__() self.fc1 = nn.Linear(input_dim, hidden_dim) self.fc2 = nn.Linear(hidden_dim, hidden_dim) self.fc3 = nn.Linear(hidden_dim, output_dim) def forward(self, x, y): z = torch.cat([x, y], 1) z = nn.functional.relu(self.fc1(z)) z = nn.functional.relu(self.fc2(z)) z = torch.tanh(self.fc3(z)) return z class Discriminator(nn.Module): def __init__(self, input_dim, hidden_dim): super(Discriminator, self).__init__() self.fc1 = nn.Linear(input_dim, hidden_dim) self.fc2 = nn.Linear(hidden_dim, hidden_dim) self.fc3 = nn.Linear(hidden_dim, 1) def forward(self, x, y): z = torch.cat([x, y], 1) z = nn.functional.relu(self.fc1(z)) z = nn.functional.relu(self.fc2(z)) z = torch.sigmoid(self.fc3(z)) return z # 定义损失函数和优化器 criterion = nn.BCELoss() # 二分类交叉熵损失函数 G_optimizer = torch.optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999)) D_optimizer = torch.optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999)) # 定义训练函数 def train_GAN(num_epochs, data_loader): for epoch in range(num_epochs): for i, (real_data, real_label) in enumerate(data_loader): # 训练 discriminator D_optimizer.zero_grad() fake_label = torch.zeros(real_label.shape[0], 1) real_label = real_label.float().view(-1, 1) real_data = real_data.view(-1, input_dim) real_decision = discriminator(real_data, real_label) D_real_loss = criterion(real_decision, real_label) fake_data = generator(torch.randn(real_data.shape[0], z_dim), real_label) fake_decision = discriminator(fake_data, fake_label) D_fake_loss = criterion(fake_decision, fake_label) D_loss = D_real_loss + D_fake_loss D_loss.backward() D_optimizer.step() # 训练 generator G_optimizer.zero_grad() fake_label = torch.ones(real_label.shape[0], 1) fake_data = generator(torch.randn(real_data.shape[0], z_dim), real_label) fake_decision = discriminator(fake_data, fake_label) G_loss = criterion(fake_decision, fake_label) G_loss.backward() G_optimizer.step() # 打印训练信息 if (i+1) % 20 == 0: print("Epoch [{}/{}], Step [{}/{}], Discriminator Loss: {:.4f}, Generator Loss: {:.4f}" .format(epoch+1, num_epochs, i+1, len(data_loader), D_loss.item(), G_loss.item())) # 定义数据集和参数 input_dim = 2 # 输入数据维度 output_dim = 2 # 输出数据维度 hidden_dim = 128 # 隐藏层维度 z_dim = 10 # 随机噪声维度 batch_size = 64 # 每批次训练数据量 # 准备数据集 data = np.random.multivariate_normal([0,0], [[1,0],[0,1]], size=1000) # 生成1000个二维高斯分布的数据 label = np.zeros((1000, 1)) # 标签全为0,表示数据是真实数据 dataset = torch.utils.data.TensorDataset(torch.tensor(data), torch.tensor(label)) data_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True) # 初始化 generator 和 discriminator generator = Generator(input_dim+z_dim, output_dim, hidden_dim) discriminator = Discriminator(input_dim+1, hidden_dim) # 训练 GAN num_epochs = 50 train_GAN(num_epochs, data_loader) ``` 这个CGAN代码中,Generator和Discriminator的网络结构都比较简单,只有3层全连接层。在训练过程中,我们先训练Discriminator,然后再训练Generator,交替进行,期望通过这个过程让Generator生成的假数据越来越逼近真实数据的分布。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值