<jupyter_start><jupyter_text>LSun bedroom data<jupyter_code>from fastai.vision import *
from fastai.vision.gan import *<jupyter_output><empty_output><jupyter_text>数据获取<jupyter_code>path = untar_data(URLs.LSUN_BEDROOMS)
path
path.ls()<jupyter_output><empty_output><jupyter_text>feature losses<jupyter_code>def get_data(bs, size):
    """
    不分割训练集和验证集
    """
    return (GANItemList.from_folder(path, noise_sz=100)
               .split_none()
               .label_from_func(noop)
               .transform(tfms=[[crop_pad(size=size, row_pct=(0,1), col_pct=(0,1))], []], size=size, tfm_y=True)
               .databunch(bs=bs)
               .normalize(stats = [torch.tensor([0.5,0.5,0.5]), torch.tensor([0.5,0.5,0.5])], do_x=False, do_y=True))
data = get_data(128, 32)
data.show_batch(rows=5)<jupyter_output><empty_output><jupyter_text>GAN 表示 Generative Adversarial Nets 由 <NAME> 发明. 同时训练两个模型，一个生成器一个判别器. 生成器生成类似于真实数据的新数据，判别器分类新数据是否是真实的。生成器返回图片，判别器返回数值，(usually 0. for fake images and 1. for real ones). - freeze生成器训练判别器的步骤:    - 获取一个batch的真实图片    - 生成一个batch的假图片    - 判别器计算损失;     - 奖励真实图片，惩罚假图片    - 更新判别器的权重- Freeze 判别器训练生成器的步骤:    - 生成一个batch的假图片    - 判别器评价生成的图片    - 奖励真实图片，惩罚假图片    - 更新生成器的权重- [Wassertein GAN](https://arxiv.org/pdf/1701.07875.pdf).<jupyter_code>generator = basic_generator(in_size=32, n_channels=3, n_extra_layers=1)
critic    = basic_critic   (in_size=32, n_channels=3, n_extra_layers=1)
generator
critic
learn = GANLearner.wgan(data, generator, critic, switch_eval=False,
                        opt_func = partial(optim.Adam, betas = (0.,0.99)), wd=0.001)
learn.model
learn.fit(30, 1e-3)<jupyter_output><empty_output><jupyter_text>- 切换到生成器并生成预测结果<jupyter_code>learn.gan_trainer.switch(gen_mode=True)
learn.show_results(ds_type=DatasetType.Train, rows=8)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13789299503585217, 'fraction_numerical': 0.03143960286817429, 'mean_word_length': 4.633540372670807, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '247871', 'license': 'MIT', 'n_tokens_mistral': 883, 'n_tokens_neox': 906, 'n_words': 94, 'path': 'fastai_DL_nbs/lesson07_wgan.ipynb', 'repo_name': 'AutuanLiu/Fastai-Notes-V3', 'size': '1821'}