-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
74 lines (66 loc) · 2.98 KB
/
Copy pathcode.py
File metadata and controls
74 lines (66 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#Importing the required packages
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data")
#Define Generator
def generator(z,reuse=None):
with tf.variable_scope('gen',reuse=reuse):
hidden1=tf.layers.dense(inputs=z,units=128,activation=tf.nn.leaky_relu)
hidden2=tf.layers.dense(inputs=hidden1,units=128,activation=tf.nn.leaky_relu)
output=tf.layers.dense(inputs=hidden2,units=784,activation=tf.nn.tanh)
return output
#Define Discriminator
def discriminator(X,reuse=None):
with tf.variable_scope('dis',reuse=reuse):
hidden1=tf.layers.dense(inputs=X,units=128,activation=tf.nn.leaky_relu)
hidden2=tf.layers.dense(inputs=hidden1,units=128,activation=tf.nn.leaky_relu)
logits=tf.layers.dense(hidden2,units=1)
output=tf.sigmoid(logits)
return output,logit
# Data Placeholder
tf.reset_default_graph()
real_images=tf.placeholder(tf.float32,shape=[None,784])
z=tf.placeholder(tf.float32,shape=[None,100])
G=generator(z)
D_output_real,D_logits_real=discriminator(real_images)
D_output_fake,D_logits_fake=discriminator(G,reuse=True)
# Sigmoid Cross entopy as our loss function
def loss_func(logits_in,labels_in):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_in,labels=labels_in))
D_real_loss=loss_func(D_logits_real,tf.ones_like(D_logits_real)*0.9) #Smoothing for generalization
D_fake_loss=loss_func(D_logits_fake,tf.zeros_like(D_logits_real))
D_loss=D_real_loss+D_fake_loss
G_loss= loss_func(D_logits_fake,tf.ones_like(D_logits_fake))
lr=0.001
#Do this when multiple networks interact with each other
#Using Adam Optimizer
tvars=tf.trainable_variables()
#returns all variables created(the two variable scopes) and makes trainable true
d_vars=[var for var in tvars if 'dis' in var.name]
g_vars=[var for var in tvars if 'gen' in var.name]
D_trainer=tf.train.AdamOptimizer(lr).minimize(D_loss,var_list=d_vars)
G_trainer=tf.train.AdamOptimizer(lr).minimize(G_loss,var_list=g_vars)
batch_size=100
epochs=100
init=tf.global_variables_initializer()
#Run Session
samples=[]
with tf.Session() as sess:
sess.run(init)
for epoch in range(epochs):
num_batches=mnist.train.num_examples//batch_size
for i in range(num_batches):
batch=mnist.train.next_batch(batch_size)
batch_images=batch[0].reshape((batch_size,784))
batch_images=batch_images*2-1
batch_z=np.random.uniform(-1,1,size=(batch_size,100))
_=sess.run(D_trainer,feed_dict={real_images:batch_images,z:batch_z})
_=sess.run(G_trainer,feed_dict={z:batch_z})
print("on epoch{}".format(epoch))
sample_z=np.random.uniform(-1,1,size=(1,100))
gen_sample=sess.run(generator(z,reuse=True),feed_dict={z:sample_z})
samples.append(gen_sample)
plt.imshow(samples[0].reshape(28,28))
plt.imshow(samples[99].reshape(28,28))