python中class类学习补充

对象=属性+方法

类:用来批量生产对象 class声明 开头大写

方法就是写在类里的函数

属性就是写在类里面的变量

class Tu():
    a=1
    b=2
    c=3
    def Auu(self):
        print("sss")
    def Buu(self):
        print("fffff")

t1=Tu()  #现在 t1就是Tu类的一个对象  也叫实例对象 t1就拥有了Tu这个类的属性和方法
print(t1.b)

运行效果

可以用Tu这个类创造出无数的对象。t2,t3,t4,t5都是Tu类的一个对象,且都拥有Tu这个类的属性和方法。

当对象被创建出来后,拥有了类的属性,可以对类的属性进行修改

同一个类创建的不同类,不会共享数据,同一个类的不同对象改变属性值,对象之间不会相互影响

可以在已有的对象中格外添加新的属性

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GAN算法的实现需要使用深度学习框架,如TensorFlow、PyTorch等。以下是一个使用PyTorch实现GAN生成手写数字图片的示例代码: ```python import torch import torch.nn as nn import torch.optim as optim import torchvision.datasets as dset import torchvision.transforms as transforms import torchvision.utils as vutils # 定义生成器 class Generator(nn.Module): def __init__(self, ngpu): super(Generator, self).__init__() self.ngpu = ngpu self.main = nn.Sequential( nn.ConvTranspose2d(100, 64 * 4, 4, 1, 0, bias=False), nn.BatchNorm2d(64 * 4), nn.ReLU(True), nn.ConvTranspose2d(64 * 4, 64 * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(64 * 2), nn.ReLU(True), nn.ConvTranspose2d(64 * 2, 64, 4, 2, 1, bias=False), nn.BatchNorm2d(64), nn.ReLU(True), nn.ConvTranspose2d(64, 1, 4, 2, 1, bias=False), nn.Tanh() ) def forward(self, input): return self.main(input) # 定义判别器 class Discriminator(nn.Module): def __init__(self, ngpu): super(Discriminator, self).__init__() self.ngpu = ngpu self.main = nn.Sequential( nn.Conv2d(1, 64, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), nn.Conv2d(64, 64 * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(64 * 2), nn.LeakyReLU(0.2, inplace=True), nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(64 * 4), nn.LeakyReLU(0.2, inplace=True), nn.Conv2d(64 * 4, 1, 4, 1, 0, bias=False), nn.Sigmoid() ) def forward(self, input): return self.main(input) # 定义超参数 batch_size = 128 image_size = 64 nc = 1 ngf = 64 ndf = 64 nz = 100 num_epochs = 100 lr = 0.0002 beta1 = 0.5 ngpu = 1 # 加载手写数字数据集 dataset = dset.MNIST(root='./data', download=True, transform=transforms.Compose([ transforms.Resize(image_size), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ])) dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=2) # 定义生成器、判别器和优化器 netG = Generator(ngpu).cuda() netD = Discriminator(ngpu).cuda() criterion = nn.BCELoss() optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999)) # 训练GAN for epoch in range(num_epochs): for i, data in enumerate(dataloader, 0): # 更新判别器 netD.zero_grad() real = data[0].cuda() batch_size = real.size(0) label = torch.full((batch_size,), 1, device='cuda') output = netD(real).view(-1) errD_real = criterion(output, label) errD_real.backward() D_x = output.mean().item() noise = torch.randn(batch_size, nz, 1, 1, device='cuda') fake = netG(noise) label.fill_(0) output = netD(fake.detach()).view(-1) errD_fake = criterion(output, label) errD_fake.backward() D_G_z1 = output.mean().item() errD = errD_real + errD_fake optimizerD.step() # 更新生成器 netG.zero_grad() label.fill_(1) output = netD(fake).view(-1) errG = criterion(output, label) errG.backward() D_G_z2 = output.mean().item() optimizerG.step() # 输出训练状态 if i % 50 == 0: print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f' % (epoch, num_epochs, i, len(dataloader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2)) if epoch == 0 and i == 0: vutils.save_image(real, '%s/real_samples.png' % "./results", normalize=True) if i % 100 == 0: vutils.save_image(fake.detach(), '%s/fake_samples_epoch_%03d_iterations_%03d.png' % ("./results", epoch, i), normalize=True) ``` 这段代码实现了一个生成手写数字图片的GAN模型,其包括一个生成器和一个判别器。在训练过程,首先更新判别器,然后更新生成器,最后输出训练状态和生成的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值