We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
dcgan fails on "fake" dataset
I have been using the dcgan example as a stress test for a machine. To save time, I have made use of the `fake` dataset.

`python main.py --dataset 'fake'` fails because `dataroot` is a required parameter. However, the `fake` dataset, does not need such information.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dcgan/main.py`
Content:
```
1 from __future__ import print_function
2 import argparse
3 import os
4 import random
5 import torch
6 import torch.nn as nn
7 import torch.nn.parallel
8 import torch.backends.cudnn as cudnn
9 import torch.optim as optim
10 import torch.utils.data
11 import torchvision.datasets as dset
12 import torchvision.transforms as transforms
13 import torchvision.utils as vutils
14 
15 
16 parser = argparse.ArgumentParser()
17 parser.add_argument('--dataset', required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')
18 parser.add_argument('--dataroot', required=True, help='path to dataset')
19 parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
20 parser.add_argument('--batchSize', type=int, default=64, help='input batch size')
21 parser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')
22 parser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')
23 parser.add_argument('--ngf', type=int, default=64)
24 parser.add_argument('--ndf', type=int, default=64)
25 parser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')
26 parser.add_argument('--lr', type=float, default=0.0002, help='learning rate, default=0.0002')
27 parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
28 parser.add_argument('--cuda', action='store_true', help='enables cuda')
29 parser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use')
30 parser.add_argument('--netG', default='', help="path to netG (to continue training)")
31 parser.add_argument('--netD', default='', help="path to netD (to continue training)")
32 parser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')
33 parser.add_argument('--manualSeed', type=int, help='manual seed')
34 parser.add_argument('--classes', default='bedroom', help='comma separated list of classes for the lsun data set')
35 
36 opt = parser.parse_args()
37 print(opt)
38 
39 try:
40     os.makedirs(opt.outf)
41 except OSError:
42     pass
43 
44 if opt.manualSeed is None:
45     opt.manualSeed = random.randint(1, 10000)
46 print("Random Seed: ", opt.manualSeed)
47 random.seed(opt.manualSeed)
48 torch.manual_seed(opt.manualSeed)
49 
50 cudnn.benchmark = True
51 
52 if torch.cuda.is_available() and not opt.cuda:
53     print("WARNING: You have a CUDA device, so you should probably run with --cuda")
54 
55 if opt.dataset in ['imagenet', 'folder', 'lfw']:
56     # folder dataset
57     dataset = dset.ImageFolder(root=opt.dataroot,
58                                transform=transforms.Compose([
59                                    transforms.Resize(opt.imageSize),
60                                    transforms.CenterCrop(opt.imageSize),
61                                    transforms.ToTensor(),
62                                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
63                                ]))
64     nc=3
65 elif opt.dataset == 'lsun':
66     classes = [ c + '_train' for c in opt.classes.split(',')]
67     dataset = dset.LSUN(root=opt.dataroot, classes=classes,
68                         transform=transforms.Compose([
69                             transforms.Resize(opt.imageSize),
70                             transforms.CenterCrop(opt.imageSize),
71                             transforms.ToTensor(),
72                             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
73                         ]))
74     nc=3
75 elif opt.dataset == 'cifar10':
76     dataset = dset.CIFAR10(root=opt.dataroot, download=True,
77                            transform=transforms.Compose([
78                                transforms.Resize(opt.imageSize),
79                                transforms.ToTensor(),
80                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
81                            ]))
82     nc=3
83 
84 elif opt.dataset == 'mnist':
85         dataset = dset.MNIST(root=opt.dataroot, download=True,
86                            transform=transforms.Compose([
87                                transforms.Resize(opt.imageSize),
88                                transforms.ToTensor(),
89                                transforms.Normalize((0.5,), (0.5,)),
90                            ]))
91         nc=1
92 
93 elif opt.dataset == 'fake':
94     dataset = dset.FakeData(image_size=(3, opt.imageSize, opt.imageSize),
95                             transform=transforms.ToTensor())
96     nc=3
97 
98 assert dataset
99 dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
100                                          shuffle=True, num_workers=int(opt.workers))
101 
102 device = torch.device("cuda:0" if opt.cuda else "cpu")
103 ngpu = int(opt.ngpu)
104 nz = int(opt.nz)
105 ngf = int(opt.ngf)
106 ndf = int(opt.ndf)
107 
108 
109 # custom weights initialization called on netG and netD
110 def weights_init(m):
111     classname = m.__class__.__name__
112     if classname.find('Conv') != -1:
113         m.weight.data.normal_(0.0, 0.02)
114     elif classname.find('BatchNorm') != -1:
115         m.weight.data.normal_(1.0, 0.02)
116         m.bias.data.fill_(0)
117 
118 
119 class Generator(nn.Module):
120     def __init__(self, ngpu):
121         super(Generator, self).__init__()
122         self.ngpu = ngpu
123         self.main = nn.Sequential(
124             # input is Z, going into a convolution
125             nn.ConvTranspose2d(     nz, ngf * 8, 4, 1, 0, bias=False),
126             nn.BatchNorm2d(ngf * 8),
127             nn.ReLU(True),
128             # state size. (ngf*8) x 4 x 4
129             nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
130             nn.BatchNorm2d(ngf * 4),
131             nn.ReLU(True),
132             # state size. (ngf*4) x 8 x 8
133             nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
134             nn.BatchNorm2d(ngf * 2),
135             nn.ReLU(True),
136             # state size. (ngf*2) x 16 x 16
137             nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),
138             nn.BatchNorm2d(ngf),
139             nn.ReLU(True),
140             # state size. (ngf) x 32 x 32
141             nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),
142             nn.Tanh()
143             # state size. (nc) x 64 x 64
144         )
145 
146     def forward(self, input):
147         if input.is_cuda and self.ngpu > 1:
148             output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
149         else:
150             output = self.main(input)
151         return output
152 
153 
154 netG = Generator(ngpu).to(device)
155 netG.apply(weights_init)
156 if opt.netG != '':
157     netG.load_state_dict(torch.load(opt.netG))
158 print(netG)
159 
160 
161 class Discriminator(nn.Module):
162     def __init__(self, ngpu):
163         super(Discriminator, self).__init__()
164         self.ngpu = ngpu
165         self.main = nn.Sequential(
166             # input is (nc) x 64 x 64
167             nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
168             nn.LeakyReLU(0.2, inplace=True),
169             # state size. (ndf) x 32 x 32
170             nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
171             nn.BatchNorm2d(ndf * 2),
172             nn.LeakyReLU(0.2, inplace=True),
173             # state size. (ndf*2) x 16 x 16
174             nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
175             nn.BatchNorm2d(ndf * 4),
176             nn.LeakyReLU(0.2, inplace=True),
177             # state size. (ndf*4) x 8 x 8
178             nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
179             nn.BatchNorm2d(ndf * 8),
180             nn.LeakyReLU(0.2, inplace=True),
181             # state size. (ndf*8) x 4 x 4
182             nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
183             nn.Sigmoid()
184         )
185 
186     def forward(self, input):
187         if input.is_cuda and self.ngpu > 1:
188             output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
189         else:
190             output = self.main(input)
191 
192         return output.view(-1, 1).squeeze(1)
193 
194 
195 netD = Discriminator(ngpu).to(device)
196 netD.apply(weights_init)
197 if opt.netD != '':
198     netD.load_state_dict(torch.load(opt.netD))
199 print(netD)
200 
201 criterion = nn.BCELoss()
202 
203 fixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device)
204 real_label = 1
205 fake_label = 0
206 
207 # setup optimizer
208 optimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
209 optimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
210 
211 for epoch in range(opt.niter):
212     for i, data in enumerate(dataloader, 0):
213         ############################
214         # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
215         ###########################
216         # train with real
217         netD.zero_grad()
218         real_cpu = data[0].to(device)
219         batch_size = real_cpu.size(0)
220         label = torch.full((batch_size,), real_label, device=device)
221 
222         output = netD(real_cpu)
223         errD_real = criterion(output, label)
224         errD_real.backward()
225         D_x = output.mean().item()
226 
227         # train with fake
228         noise = torch.randn(batch_size, nz, 1, 1, device=device)
229         fake = netG(noise)
230         label.fill_(fake_label)
231         output = netD(fake.detach())
232         errD_fake = criterion(output, label)
233         errD_fake.backward()
234         D_G_z1 = output.mean().item()
235         errD = errD_real + errD_fake
236         optimizerD.step()
237 
238         ############################
239         # (2) Update G network: maximize log(D(G(z)))
240         ###########################
241         netG.zero_grad()
242         label.fill_(real_label)  # fake labels are real for generator cost
243         output = netD(fake)
244         errG = criterion(output, label)
245         errG.backward()
246         D_G_z2 = output.mean().item()
247         optimizerG.step()
248 
249         print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f'
250               % (epoch, opt.niter, i, len(dataloader),
251                  errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
252         if i % 100 == 0:
253             vutils.save_image(real_cpu,
254                     '%s/real_samples.png' % opt.outf,
255                     normalize=True)
256             fake = netG(fixed_noise)
257             vutils.save_image(fake.detach(),
258                     '%s/fake_samples_epoch_%03d.png' % (opt.outf, epoch),
259                     normalize=True)
260 
261     # do checkpointing
262     torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (opt.outf, epoch))
263     torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (opt.outf, epoch))
264
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/dcgan/main.py b/dcgan/main.py
--- a/dcgan/main.py
+++ b/dcgan/main.py
@@ -15,7 +15,7 @@
 
 parser = argparse.ArgumentParser()
 parser.add_argument('--dataset', required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')
-parser.add_argument('--dataroot', required=True, help='path to dataset')
+parser.add_argument('--dataroot', required=False, help='path to dataset')
 parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
 parser.add_argument('--batchSize', type=int, default=64, help='input batch size')
 parser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')
@@ -51,6 +51,9 @@
 
 if torch.cuda.is_available() and not opt.cuda:
     print("WARNING: You have a CUDA device, so you should probably run with --cuda")
+  
+if opt.dataroot is None and str(opt.dataset).lower() != 'fake':
+    raise ValueError("`dataroot` parameter is required for dataset \"%s\"" % opt.dataset)
 
 if opt.dataset in ['imagenet', 'folder', 'lfw']:
     # folder dataset

{"golden_diff": "diff --git a/dcgan/main.py b/dcgan/main.py\n--- a/dcgan/main.py\n+++ b/dcgan/main.py\n@@ -15,7 +15,7 @@\n \n parser = argparse.ArgumentParser()\n parser.add_argument('--dataset', required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')\n-parser.add_argument('--dataroot', required=True, help='path to dataset')\n+parser.add_argument('--dataroot', required=False, help='path to dataset')\n parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)\n parser.add_argument('--batchSize', type=int, default=64, help='input batch size')\n parser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')\n@@ -51,6 +51,9 @@\n \n if torch.cuda.is_available() and not opt.cuda:\n     print(\"WARNING: You have a CUDA device, so you should probably run with --cuda\")\n+  \n+if opt.dataroot is None and str(opt.dataset).lower() != 'fake':\n+    raise ValueError(\"`dataroot` parameter is required for dataset \\\"%s\\\"\" % opt.dataset)\n \n if opt.dataset in ['imagenet', 'folder', 'lfw']:\n     # folder dataset\n", "issue": "dcgan fails on \"fake\" dataset\nI have been using the dcgan example as a stress test for a machine. To save time, I have made use of the `fake` dataset.\r\n\r\n`python main.py --dataset 'fake'` fails because `dataroot` is a required parameter. However, the `fake` dataset, does not need such information.\n", "before_files": [{"content": "from __future__ import print_function\nimport argparse\nimport os\nimport random\nimport torch\nimport torch.nn as nn\nimport torch.nn.parallel\nimport torch.backends.cudnn as cudnn\nimport torch.optim as optim\nimport torch.utils.data\nimport torchvision.datasets as dset\nimport torchvision.transforms as transforms\nimport torchvision.utils as vutils\n\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--dataset', required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')\nparser.add_argument('--dataroot', required=True, help='path to dataset')\nparser.add_argument('--workers', type=int, help='number of data loading workers', default=2)\nparser.add_argument('--batchSize', type=int, default=64, help='input batch size')\nparser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')\nparser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')\nparser.add_argument('--ngf', type=int, default=64)\nparser.add_argument('--ndf', type=int, default=64)\nparser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')\nparser.add_argument('--lr', type=float, default=0.0002, help='learning rate, default=0.0002')\nparser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')\nparser.add_argument('--cuda', action='store_true', help='enables cuda')\nparser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use')\nparser.add_argument('--netG', default='', help=\"path to netG (to continue training)\")\nparser.add_argument('--netD', default='', help=\"path to netD (to continue training)\")\nparser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')\nparser.add_argument('--manualSeed', type=int, help='manual seed')\nparser.add_argument('--classes', default='bedroom', help='comma separated list of classes for the lsun data set')\n\nopt = parser.parse_args()\nprint(opt)\n\ntry:\n    os.makedirs(opt.outf)\nexcept OSError:\n    pass\n\nif opt.manualSeed is None:\n    opt.manualSeed = random.randint(1, 10000)\nprint(\"Random Seed: \", opt.manualSeed)\nrandom.seed(opt.manualSeed)\ntorch.manual_seed(opt.manualSeed)\n\ncudnn.benchmark = True\n\nif torch.cuda.is_available() and not opt.cuda:\n    print(\"WARNING: You have a CUDA device, so you should probably run with --cuda\")\n\nif opt.dataset in ['imagenet', 'folder', 'lfw']:\n    # folder dataset\n    dataset = dset.ImageFolder(root=opt.dataroot,\n                               transform=transforms.Compose([\n                                   transforms.Resize(opt.imageSize),\n                                   transforms.CenterCrop(opt.imageSize),\n                                   transforms.ToTensor(),\n                                   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                               ]))\n    nc=3\nelif opt.dataset == 'lsun':\n    classes = [ c + '_train' for c in opt.classes.split(',')]\n    dataset = dset.LSUN(root=opt.dataroot, classes=classes,\n                        transform=transforms.Compose([\n                            transforms.Resize(opt.imageSize),\n                            transforms.CenterCrop(opt.imageSize),\n                            transforms.ToTensor(),\n                            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                        ]))\n    nc=3\nelif opt.dataset == 'cifar10':\n    dataset = dset.CIFAR10(root=opt.dataroot, download=True,\n                           transform=transforms.Compose([\n                               transforms.Resize(opt.imageSize),\n                               transforms.ToTensor(),\n                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                           ]))\n    nc=3\n\nelif opt.dataset == 'mnist':\n        dataset = dset.MNIST(root=opt.dataroot, download=True,\n                           transform=transforms.Compose([\n                               transforms.Resize(opt.imageSize),\n                               transforms.ToTensor(),\n                               transforms.Normalize((0.5,), (0.5,)),\n                           ]))\n        nc=1\n\nelif opt.dataset == 'fake':\n    dataset = dset.FakeData(image_size=(3, opt.imageSize, opt.imageSize),\n                            transform=transforms.ToTensor())\n    nc=3\n\nassert dataset\ndataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,\n                                         shuffle=True, num_workers=int(opt.workers))\n\ndevice = torch.device(\"cuda:0\" if opt.cuda else \"cpu\")\nngpu = int(opt.ngpu)\nnz = int(opt.nz)\nngf = int(opt.ngf)\nndf = int(opt.ndf)\n\n\n# custom weights initialization called on netG and netD\ndef weights_init(m):\n    classname = m.__class__.__name__\n    if classname.find('Conv') != -1:\n        m.weight.data.normal_(0.0, 0.02)\n    elif classname.find('BatchNorm') != -1:\n        m.weight.data.normal_(1.0, 0.02)\n        m.bias.data.fill_(0)\n\n\nclass Generator(nn.Module):\n    def __init__(self, ngpu):\n        super(Generator, self).__init__()\n        self.ngpu = ngpu\n        self.main = nn.Sequential(\n            # input is Z, going into a convolution\n            nn.ConvTranspose2d(     nz, ngf * 8, 4, 1, 0, bias=False),\n            nn.BatchNorm2d(ngf * 8),\n            nn.ReLU(True),\n            # state size. (ngf*8) x 4 x 4\n            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf * 4),\n            nn.ReLU(True),\n            # state size. (ngf*4) x 8 x 8\n            nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf * 2),\n            nn.ReLU(True),\n            # state size. (ngf*2) x 16 x 16\n            nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf),\n            nn.ReLU(True),\n            # state size. (ngf) x 32 x 32\n            nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),\n            nn.Tanh()\n            # state size. (nc) x 64 x 64\n        )\n\n    def forward(self, input):\n        if input.is_cuda and self.ngpu > 1:\n            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))\n        else:\n            output = self.main(input)\n        return output\n\n\nnetG = Generator(ngpu).to(device)\nnetG.apply(weights_init)\nif opt.netG != '':\n    netG.load_state_dict(torch.load(opt.netG))\nprint(netG)\n\n\nclass Discriminator(nn.Module):\n    def __init__(self, ngpu):\n        super(Discriminator, self).__init__()\n        self.ngpu = ngpu\n        self.main = nn.Sequential(\n            # input is (nc) x 64 x 64\n            nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf) x 32 x 32\n            nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 2),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*2) x 16 x 16\n            nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 4),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*4) x 8 x 8\n            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 8),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*8) x 4 x 4\n            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),\n            nn.Sigmoid()\n        )\n\n    def forward(self, input):\n        if input.is_cuda and self.ngpu > 1:\n            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))\n        else:\n            output = self.main(input)\n\n        return output.view(-1, 1).squeeze(1)\n\n\nnetD = Discriminator(ngpu).to(device)\nnetD.apply(weights_init)\nif opt.netD != '':\n    netD.load_state_dict(torch.load(opt.netD))\nprint(netD)\n\ncriterion = nn.BCELoss()\n\nfixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device)\nreal_label = 1\nfake_label = 0\n\n# setup optimizer\noptimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))\noptimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))\n\nfor epoch in range(opt.niter):\n    for i, data in enumerate(dataloader, 0):\n        ############################\n        # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))\n        ###########################\n        # train with real\n        netD.zero_grad()\n        real_cpu = data[0].to(device)\n        batch_size = real_cpu.size(0)\n        label = torch.full((batch_size,), real_label, device=device)\n\n        output = netD(real_cpu)\n        errD_real = criterion(output, label)\n        errD_real.backward()\n        D_x = output.mean().item()\n\n        # train with fake\n        noise = torch.randn(batch_size, nz, 1, 1, device=device)\n        fake = netG(noise)\n        label.fill_(fake_label)\n        output = netD(fake.detach())\n        errD_fake = criterion(output, label)\n        errD_fake.backward()\n        D_G_z1 = output.mean().item()\n        errD = errD_real + errD_fake\n        optimizerD.step()\n\n        ############################\n        # (2) Update G network: maximize log(D(G(z)))\n        ###########################\n        netG.zero_grad()\n        label.fill_(real_label)  # fake labels are real for generator cost\n        output = netD(fake)\n        errG = criterion(output, label)\n        errG.backward()\n        D_G_z2 = output.mean().item()\n        optimizerG.step()\n\n        print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f'\n              % (epoch, opt.niter, i, len(dataloader),\n                 errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))\n        if i % 100 == 0:\n            vutils.save_image(real_cpu,\n                    '%s/real_samples.png' % opt.outf,\n                    normalize=True)\n            fake = netG(fixed_noise)\n            vutils.save_image(fake.detach(),\n                    '%s/fake_samples_epoch_%03d.png' % (opt.outf, epoch),\n                    normalize=True)\n\n    # do checkpointing\n    torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (opt.outf, epoch))\n    torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (opt.outf, epoch))\n", "path": "dcgan/main.py"}], "after_files": [{"content": "from __future__ import print_function\nimport argparse\nimport os\nimport random\nimport torch\nimport torch.nn as nn\nimport torch.nn.parallel\nimport torch.backends.cudnn as cudnn\nimport torch.optim as optim\nimport torch.utils.data\nimport torchvision.datasets as dset\nimport torchvision.transforms as transforms\nimport torchvision.utils as vutils\n\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--dataset', required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')\nparser.add_argument('--dataroot', required=False, help='path to dataset')\nparser.add_argument('--workers', type=int, help='number of data loading workers', default=2)\nparser.add_argument('--batchSize', type=int, default=64, help='input batch size')\nparser.add_argument('--imageSize', type=int, default=64, help='the height / width of the input image to network')\nparser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')\nparser.add_argument('--ngf', type=int, default=64)\nparser.add_argument('--ndf', type=int, default=64)\nparser.add_argument('--niter', type=int, default=25, help='number of epochs to train for')\nparser.add_argument('--lr', type=float, default=0.0002, help='learning rate, default=0.0002')\nparser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')\nparser.add_argument('--cuda', action='store_true', help='enables cuda')\nparser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use')\nparser.add_argument('--netG', default='', help=\"path to netG (to continue training)\")\nparser.add_argument('--netD', default='', help=\"path to netD (to continue training)\")\nparser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')\nparser.add_argument('--manualSeed', type=int, help='manual seed')\nparser.add_argument('--classes', default='bedroom', help='comma separated list of classes for the lsun data set')\n\nopt = parser.parse_args()\nprint(opt)\n\ntry:\n    os.makedirs(opt.outf)\nexcept OSError:\n    pass\n\nif opt.manualSeed is None:\n    opt.manualSeed = random.randint(1, 10000)\nprint(\"Random Seed: \", opt.manualSeed)\nrandom.seed(opt.manualSeed)\ntorch.manual_seed(opt.manualSeed)\n\ncudnn.benchmark = True\n\nif torch.cuda.is_available() and not opt.cuda:\n    print(\"WARNING: You have a CUDA device, so you should probably run with --cuda\")\n  \nif opt.dataroot is None and str(opt.dataset).lower() != 'fake':\n    raise ValueError(\"`dataroot` parameter is required for dataset \\\"%s\\\"\" % opt.dataset)\n\nif opt.dataset in ['imagenet', 'folder', 'lfw']:\n    # folder dataset\n    dataset = dset.ImageFolder(root=opt.dataroot,\n                               transform=transforms.Compose([\n                                   transforms.Resize(opt.imageSize),\n                                   transforms.CenterCrop(opt.imageSize),\n                                   transforms.ToTensor(),\n                                   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                               ]))\n    nc=3\nelif opt.dataset == 'lsun':\n    classes = [ c + '_train' for c in opt.classes.split(',')]\n    dataset = dset.LSUN(root=opt.dataroot, classes=classes,\n                        transform=transforms.Compose([\n                            transforms.Resize(opt.imageSize),\n                            transforms.CenterCrop(opt.imageSize),\n                            transforms.ToTensor(),\n                            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                        ]))\n    nc=3\nelif opt.dataset == 'cifar10':\n    dataset = dset.CIFAR10(root=opt.dataroot, download=True,\n                           transform=transforms.Compose([\n                               transforms.Resize(opt.imageSize),\n                               transforms.ToTensor(),\n                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n                           ]))\n    nc=3\n\nelif opt.dataset == 'mnist':\n        dataset = dset.MNIST(root=opt.dataroot, download=True,\n                           transform=transforms.Compose([\n                               transforms.Resize(opt.imageSize),\n                               transforms.ToTensor(),\n                               transforms.Normalize((0.5,), (0.5,)),\n                           ]))\n        nc=1\n\nelif opt.dataset == 'fake':\n    dataset = dset.FakeData(image_size=(3, opt.imageSize, opt.imageSize),\n                            transform=transforms.ToTensor())\n    nc=3\n\nassert dataset\ndataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,\n                                         shuffle=True, num_workers=int(opt.workers))\n\ndevice = torch.device(\"cuda:0\" if opt.cuda else \"cpu\")\nngpu = int(opt.ngpu)\nnz = int(opt.nz)\nngf = int(opt.ngf)\nndf = int(opt.ndf)\n\n\n# custom weights initialization called on netG and netD\ndef weights_init(m):\n    classname = m.__class__.__name__\n    if classname.find('Conv') != -1:\n        m.weight.data.normal_(0.0, 0.02)\n    elif classname.find('BatchNorm') != -1:\n        m.weight.data.normal_(1.0, 0.02)\n        m.bias.data.fill_(0)\n\n\nclass Generator(nn.Module):\n    def __init__(self, ngpu):\n        super(Generator, self).__init__()\n        self.ngpu = ngpu\n        self.main = nn.Sequential(\n            # input is Z, going into a convolution\n            nn.ConvTranspose2d(     nz, ngf * 8, 4, 1, 0, bias=False),\n            nn.BatchNorm2d(ngf * 8),\n            nn.ReLU(True),\n            # state size. (ngf*8) x 4 x 4\n            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf * 4),\n            nn.ReLU(True),\n            # state size. (ngf*4) x 8 x 8\n            nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf * 2),\n            nn.ReLU(True),\n            # state size. (ngf*2) x 16 x 16\n            nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ngf),\n            nn.ReLU(True),\n            # state size. (ngf) x 32 x 32\n            nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),\n            nn.Tanh()\n            # state size. (nc) x 64 x 64\n        )\n\n    def forward(self, input):\n        if input.is_cuda and self.ngpu > 1:\n            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))\n        else:\n            output = self.main(input)\n        return output\n\n\nnetG = Generator(ngpu).to(device)\nnetG.apply(weights_init)\nif opt.netG != '':\n    netG.load_state_dict(torch.load(opt.netG))\nprint(netG)\n\n\nclass Discriminator(nn.Module):\n    def __init__(self, ngpu):\n        super(Discriminator, self).__init__()\n        self.ngpu = ngpu\n        self.main = nn.Sequential(\n            # input is (nc) x 64 x 64\n            nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf) x 32 x 32\n            nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 2),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*2) x 16 x 16\n            nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 4),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*4) x 8 x 8\n            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),\n            nn.BatchNorm2d(ndf * 8),\n            nn.LeakyReLU(0.2, inplace=True),\n            # state size. (ndf*8) x 4 x 4\n            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),\n            nn.Sigmoid()\n        )\n\n    def forward(self, input):\n        if input.is_cuda and self.ngpu > 1:\n            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))\n        else:\n            output = self.main(input)\n\n        return output.view(-1, 1).squeeze(1)\n\n\nnetD = Discriminator(ngpu).to(device)\nnetD.apply(weights_init)\nif opt.netD != '':\n    netD.load_state_dict(torch.load(opt.netD))\nprint(netD)\n\ncriterion = nn.BCELoss()\n\nfixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device)\nreal_label = 1\nfake_label = 0\n\n# setup optimizer\noptimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))\noptimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))\n\nfor epoch in range(opt.niter):\n    for i, data in enumerate(dataloader, 0):\n        ############################\n        # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))\n        ###########################\n        # train with real\n        netD.zero_grad()\n        real_cpu = data[0].to(device)\n        batch_size = real_cpu.size(0)\n        label = torch.full((batch_size,), real_label, device=device)\n\n        output = netD(real_cpu)\n        errD_real = criterion(output, label)\n        errD_real.backward()\n        D_x = output.mean().item()\n\n        # train with fake\n        noise = torch.randn(batch_size, nz, 1, 1, device=device)\n        fake = netG(noise)\n        label.fill_(fake_label)\n        output = netD(fake.detach())\n        errD_fake = criterion(output, label)\n        errD_fake.backward()\n        D_G_z1 = output.mean().item()\n        errD = errD_real + errD_fake\n        optimizerD.step()\n\n        ############################\n        # (2) Update G network: maximize log(D(G(z)))\n        ###########################\n        netG.zero_grad()\n        label.fill_(real_label)  # fake labels are real for generator cost\n        output = netD(fake)\n        errG = criterion(output, label)\n        errG.backward()\n        D_G_z2 = output.mean().item()\n        optimizerG.step()\n\n        print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f'\n              % (epoch, opt.niter, i, len(dataloader),\n                 errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))\n        if i % 100 == 0:\n            vutils.save_image(real_cpu,\n                    '%s/real_samples.png' % opt.outf,\n                    normalize=True)\n            fake = netG(fixed_noise)\n            vutils.save_image(fake.detach(),\n                    '%s/fake_samples_epoch_%03d.png' % (opt.outf, epoch),\n                    normalize=True)\n\n    # do checkpointing\n    torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (opt.outf, epoch))\n    torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (opt.outf, epoch))\n", "path": "dcgan/main.py"}]}