Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
organize chapter 9
Browse files Browse the repository at this point in the history
  • Loading branch information
keon committed Sep 3, 2019
1 parent db245db commit baec02a
Show file tree
Hide file tree
Showing 8 changed files with 805 additions and 1,569 deletions.
2 changes: 1 addition & 1 deletion 09-경쟁하며_학습하는_GAN/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# 경쟁을 통해 성장하는 GAN
# 경쟁하며 학습하는 GAN
51 changes: 14 additions & 37 deletions 09-경쟁하며_학습하는_GAN/conditional_gan.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<torch._C.Generator at 0x7f600d469bf0>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.manual_seed(1) # reproducible"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand All @@ -58,7 +38,7 @@
}
],
"source": [
"# Hyper Parameters\n",
"# 하이퍼파라미터\n",
"EPOCHS = 300\n",
"BATCH_SIZE = 100\n",
"USE_CUDA = torch.cuda.is_available()\n",
Expand All @@ -72,7 +52,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Fashion MNIST digits dataset\n",
"# Fashion MNIST 데이터셋\n",
"trainset = datasets.FashionMNIST('./.data',\n",
" train=True,\n",
" download=True,\n",
Expand Down Expand Up @@ -103,7 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Discriminator\n",
"# 생성자 (Generator)\n",
"D = nn.Sequential(\n",
" nn.Linear(784, 256),\n",
" nn.LeakyReLU(0.2),\n",
Expand All @@ -119,7 +99,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Generator \n",
"# 판별자 (Discriminator)\n",
"G = nn.Sequential(\n",
" nn.Linear(64 + 10, 256),\n",
" nn.ReLU(),\n",
Expand All @@ -135,12 +115,12 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Device setting\n",
"# 모델의 가중치를 지정한 장치로 보내기\n",
"D = D.to(DEVICE)\n",
"G = G.to(DEVICE)\n",
"\n",
"# Binary cross entropy loss and optimizer\n",
"# 이진 크로스 엔트로피 (Binary cross entropy) 오차 함수와\n",
"# 생성자와 판별자를 최적화할 Adam 모듈\n",
"criterion = nn.BCELoss()\n",
"d_optimizer = optim.Adam(D.parameters(), lr=0.0002)\n",
"g_optimizer = optim.Adam(G.parameters(), lr=0.0002)"
Expand Down Expand Up @@ -1156,16 +1136,13 @@
" g_loss.backward()\n",
" g_optimizer.step()\n",
" \n",
" if (i+1) % 200 == 0:\n",
" print('Epoch [{}/{}], Step [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}' \n",
" .format(epoch,\n",
" EPOCHS,\n",
" i+1,\n",
" total_step,\n",
" d_loss.item(),\n",
" g_loss.item(), \n",
" real_score.mean().item(),\n",
" fake_score.mean().item()))"
" print('Epoch [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}' \n",
" .format(epoch,\n",
" EPOCHS,\n",
" d_loss.item(),\n",
" g_loss.item(), \n",
" real_score.mean().item(),\n",
" fake_score.mean().item()))"
]
},
{
Expand Down
34 changes: 14 additions & 20 deletions 09-경쟁하며_학습하는_GAN/conditional_gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
import numpy as np


torch.manual_seed(1) # reproducible


# Hyper Parameters
# 하이퍼파라미터
EPOCHS = 300
BATCH_SIZE = 100
USE_CUDA = torch.cuda.is_available()
DEVICE = torch.device("cuda" if USE_CUDA else "cpu")
print("Using Device:", DEVICE)


# Fashion MNIST digits dataset
# Fashion MNIST 데이터셋
trainset = datasets.FashionMNIST('./.data',
train=True,
download=True,
Expand All @@ -44,7 +41,7 @@ def one_hot_embedding(labels, num_classes):
return y[labels]


# Discriminator
# 생성자 (Generator)
D = nn.Sequential(
nn.Linear(784, 256),
nn.LeakyReLU(0.2),
Expand All @@ -54,7 +51,7 @@ def one_hot_embedding(labels, num_classes):
nn.Sigmoid())


# Generator
# 판별자 (Discriminator)
G = nn.Sequential(
nn.Linear(64 + 10, 256),
nn.ReLU(),
Expand All @@ -64,12 +61,12 @@ def one_hot_embedding(labels, num_classes):
nn.Tanh())



# Device setting
# 모델의 가중치를 지정한 장치로 보내기
D = D.to(DEVICE)
G = G.to(DEVICE)

# Binary cross entropy loss and optimizer
# 이진 크로스 엔트로피 (Binary cross entropy) 오차 함수와
# 생성자와 판별자를 최적화할 Adam 모듈
criterion = nn.BCELoss()
d_optimizer = optim.Adam(D.parameters(), lr=0.0002)
g_optimizer = optim.Adam(G.parameters(), lr=0.0002)
Expand Down Expand Up @@ -118,16 +115,13 @@ def one_hot_embedding(labels, num_classes):
g_loss.backward()
g_optimizer.step()

if (i+1) % 200 == 0:
print('Epoch [{}/{}], Step [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}'
.format(epoch,
EPOCHS,
i+1,
total_step,
d_loss.item(),
g_loss.item(),
real_score.mean().item(),
fake_score.mean().item()))
print('Epoch [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}'
.format(epoch,
EPOCHS,
d_loss.item(),
g_loss.item(),
real_score.mean().item(),
fake_score.mean().item()))


for i in range(100):
Expand Down
Loading

0 comments on commit baec02a

Please sign in to comment.