Skip to content

Commit faa26e6

Browse files
author
Lukas Herman
committed
Refractor
1 parent e64e960 commit faa26e6

6 files changed

+153
-15
lines changed

1_MLP.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424

2525
mnist = input_data.read_data_sets('data', one_hot=True)
2626

27+
2728
def get_dict(train=True, batch=True):
2829
if train:
2930
if batch:
3031
batch_x, batch_y = mnist.train.next_batch(batch_size)
31-
return {x:batch_x, y_:batch_y}
32+
return {x: batch_x, y_: batch_y}
3233
else:
33-
return {x:mnist.train.images, y_:mnist.train.labels}
34+
return {x: mnist.train.images, y_: mnist.train.labels}
3435
else:
3536
if batch:
3637
batch_x, batch_y = mnist.test.next_batch(batch_size)
37-
return {x:batch_x, y_:batch_y}
38+
return {x: batch_x, y_: batch_y}
3839
else:
39-
return {x:mnist.test.images, y_:mnist.test.labels}
40+
return {x: mnist.test.images, y_: mnist.test.labels}
41+
4042

4143
with tf.name_scope('ActualValue'):
4244
y_ = tf.placeholder(tf.float32, shape=[None, n_classes], name='y_')
@@ -54,13 +56,13 @@ def get_dict(train=True, batch=True):
5456

5557
with tf.name_scope('Train'):
5658
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,
57-
logits=y), name='loss')
59+
logits=y), name='loss')
5860
train = tf.train.AdamOptimizer().minimize(loss)
5961

6062
with tf.name_scope('Accuracy'):
6163
correct_predictions = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_, axis=1))
6264
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32),
63-
name='accuracy')
65+
name='accuracy')
6466

6567
# Add scalar summaries
6668
tf.summary.scalar('Loss', loss)
@@ -76,7 +78,7 @@ def get_dict(train=True, batch=True):
7678
test_writer = tf.summary.FileWriter(test_dir)
7779

7880
sess.run(init_op)
79-
for n_train in range(1, n_trains+1):
81+
for n_train in range(1, n_trains + 1):
8082
print("Training {}...".format(n_train))
8183
_ = sess.run([train], feed_dict=get_dict(train=True, batch=True))
8284
if n_train % 100 == 0:
Binary file not shown.
Binary file not shown.

7_DBN.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import tensorflow as tf
2+
from tensorflow.examples.tutorials.mnist import input_data
3+
from layers import layers
4+
import config
5+
6+
train_dir = config.TRAIN_DIR
7+
test_dir = config.TEST_DIR
8+
n_trains = config.N_TRAINS
9+
batch_size = config.BATCH_SIZE
10+
11+
width = config.WIDTH
12+
height = config.HEIGHT
13+
channels = config.CHANNELS
14+
flat = config.FLAT
15+
n_classes = config.N_CLASSES
16+
17+
k = 500
18+
l = 250
19+
m = 125
20+
num_imgs = 3
21+
22+
mnist = input_data.read_data_sets('data', one_hot=True)
23+
24+
25+
def get_dict(train=True, batch=True):
26+
if train:
27+
if batch:
28+
batch_x, _ = mnist.train.next_batch(batch_size)
29+
return {x: batch_x}
30+
else:
31+
return {x: mnist.train.images}
32+
else:
33+
if batch:
34+
batch_x, _ = mnist.test.next_batch(batch_size)
35+
return {x: batch_x}
36+
else:
37+
return {x: mnist.test.images}
38+
39+
40+
with tf.name_scope('InputLayer'):
41+
x = tf.placeholder(tf.float32, shape=[None, flat], name='x')
42+
43+
with tf.name_scope('ActualValue'):
44+
y_ = tf.placeholder(tf.float32, shape=[None, n_classes], name='y_')
45+
46+
47+
def train_rbm(y):
48+
with tf.name_scope('Train'):
49+
loss = tf.reduce_mean(tf.pow(y - x, 2), name='loss')
50+
train = tf.train.AdamOptimizer().minimize(loss)
51+
52+
with tf.name_scope('Accuracy'):
53+
accuracy = 1 - loss
54+
55+
# Add image summaries
56+
x_img = tf.reshape(x, [-1, height, width, channels]) # input
57+
y_img = tf.reshape(y, [-1, height, width, channels]) # reconstructed
58+
tf.summary.image('InputImage', x_img, max_outputs=num_imgs)
59+
tf.summary.image('OutputImage', y_img, max_outputs=num_imgs)
60+
61+
# Add scalar summaries
62+
tf.summary.scalar('Loss', loss)
63+
tf.summary.scalar('Accuracy', accuracy)
64+
65+
init_op = tf.global_variables_initializer()
66+
summary_op = tf.summary.merge_all()
67+
68+
with tf.Session() as sess:
69+
# Open protocol for writing files
70+
train_writer = tf.summary.FileWriter(train_dir)
71+
train_writer.add_graph(sess.graph)
72+
test_writer = tf.summary.FileWriter(test_dir)
73+
74+
sess.run(init_op)
75+
for n_train in range(1, n_trains + 1):
76+
print("Training {}...".format(n_train))
77+
_ = sess.run([train], feed_dict=get_dict(train=True, batch=True))
78+
if n_train % 100 == 0:
79+
# Train
80+
s = sess.run(summary_op, feed_dict=get_dict(train=True, batch=False))
81+
train_writer.add_summary(s, n_train)
82+
# Test
83+
s = sess.run(summary_op, feed_dict=get_dict(train=False, batch=False))
84+
test_writer.add_summary(s, n_train)
85+
86+
with tf.name_scope('NetworkModel'):
87+
y1 = layers.rbm_layer(x, flat, k)
88+
y2 = layers.rbm_layer(y1, k, l)
89+
y3 = layers.rbm_layer(y2, l, m)
90+
y = layers.output_layer(y3, m, n_classes)
91+
92+
# Train First
93+
train(y1)
94+
train(y2)
95+
train(y3)
96+
97+
with tf.name_scope('Train'):
98+
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,
99+
logits=y), name='loss')
100+
train = tf.train.AdamOptimizer().minimize(loss)
101+
102+
with tf.name_scope('Accuracy'):
103+
correct_predictions = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_, axis=1))
104+
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32),
105+
name='accuracy')
106+
107+
# Add scalar summaries
108+
tf.summary.scalar('Loss', loss)
109+
tf.summary.scalar('Accuracy', accuracy)
110+
111+
init_op = tf.global_variables_initializer()
112+
summary_op = tf.summary.merge_all()
113+
114+
with tf.Session() as sess:
115+
# Open protocol for writing files
116+
train_writer = tf.summary.FileWriter(train_dir)
117+
train_writer.add_graph(sess.graph)
118+
test_writer = tf.summary.FileWriter(test_dir)
119+
120+
sess.run(init_op)
121+
for n_train in range(1, n_trains + 1):
122+
print("Training {}...".format(n_train))
123+
_ = sess.run([train], feed_dict=get_dict(train=True, batch=True))
124+
if n_train % 100 == 0:
125+
# Train
126+
s = sess.run(summary_op, feed_dict=get_dict(train=True, batch=False))
127+
train_writer.add_summary(s, n_train)
128+
# Test
129+
s = sess.run(summary_op, feed_dict=get_dict(train=False, batch=False))
130+
test_writer.add_summary(s, n_train)
76 Bytes
Binary file not shown.

layers/layers.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import tensorflow as tf
22

3+
34
def conv_layer(input, side, channels_in, channels_out, stride=2, name='conv_layer'):
45
with tf.name_scope(name):
56
w = tf.Variable(tf.truncated_normal([side, side, channels_in,
6-
channels_out]), name='W')
7+
channels_out]), name='W')
78
b = tf.Variable(tf.truncated_normal([channels_out]),
89
name='B')
910

10-
conv = tf.nn.conv2d(input, w, [1, 1, 1, 1], padding='SAME')
11+
conv = tf.nn.conv2d(input, w, [1, 1, 1, 1], padding='SAME')
1112
act = tf.nn.relu(conv + b)
1213

1314
tf.summary.histogram('weights', w)
1415
tf.summary.histogram('biases', b)
1516
return tf.nn.max_pool(act, [1, 2, 2, 1], [1, stride, stride, 1],
1617
padding='SAME')
1718

19+
1820
def fc_layer(input, channels_in, channels_out, name='fc_layer'):
1921
with tf.name_scope(name):
2022
w = tf.Variable(tf.truncated_normal([channels_in, channels_out]),
2123
name='W')
2224
b = tf.Variable(tf.truncated_normal([channels_out]),
2325
name='B')
2426
pkeep = tf.constant(0.75, dtype=tf.float32, name='pkeep')
25-
27+
dropout = tf.nn.dropout(input, pkeep)
2628
tf.summary.histogram('weights', w)
2729
tf.summary.histogram('biases', b)
28-
return tf.nn.relu(tf.matmul(input, w) + b)
30+
31+
return tf.nn.relu(tf.matmul(dropout, w) + b)
32+
2933

3034
def output_layer(input, channels_in, channels_out, name='output_layer'):
3135
with tf.name_scope(name):
@@ -38,6 +42,7 @@ def output_layer(input, channels_in, channels_out, name='output_layer'):
3842
tf.summary.histogram('biases', b)
3943
return tf.matmul(input, w) + b
4044

45+
4146
def ae_layer(input, channels_in, channels_out, name='ae_layer'):
4247
with tf.name_scope(name):
4348
w = tf.Variable(tf.truncated_normal([channels_in, channels_out]),
@@ -49,15 +54,16 @@ def ae_layer(input, channels_in, channels_out, name='ae_layer'):
4954
tf.summary.histogram('biases', b)
5055
return tf.nn.sigmoid(tf.matmul(input, w) + b)
5156

57+
5258
def rbm_layer(input, channels_in, channels_out, name='rbm_layer'):
5359
with tf.name_scope(name):
5460
# Encoder Variables
55-
w = tf.Variable(tf.truncated_normal([channels_in, channels_out]))
56-
_b = tf.Variable(tf.truncated_normal([channels_out]))
61+
w = tf.Variable(tf.truncated_normal([channels_in, channels_out]),
62+
name='W')
63+
_b = tf.Variable(tf.truncated_normal([channels_out]), name='_B')
5764

5865
# reconstructor Varables
59-
b = tf.Variable(tf.truncated_normal([channels_in]))
60-
66+
b = tf.Variable(tf.truncated_normal([channels_in]), name='B')
6167

6268
_y = tf.nn.sigmoid(tf.matmul(input, w) + _b)
6369
y = tf.nn.sigmoid(tf.matmul(_y, tf.transpose(w)) + b)

0 commit comments

Comments
 (0)