Skip to content

Commit

Permalink
Mnist updates
Browse files Browse the repository at this point in the history
  • Loading branch information
malmaud committed Jun 22, 2017
1 parent 3d6d8b9 commit 28e4f5a
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions examples/mnist_full.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# using TensorFlow
using TensorFlow
using Distributions
include("mnist_loader.jl")

Expand All @@ -24,38 +24,41 @@ function max_pool_2x2(x)
nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")
end

x = placeholder(Float32)
y_ = placeholder(Float32)
@tf begin

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x = placeholder(Float32)
y_ = placeholder(Float32)

x_image = reshape(x, [-1, 28, 28, 1])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])

h_conv1 = nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
x_image = reshape(x, [-1, 28, 28, 1])

W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv1 = nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

h_conv2 = nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_conv2 = nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

h_pool2_flat = reshape(h_pool2, [-1, 7*7*64])
h_fc1 = nn.relu(h_pool2_flat * W_fc1 + b_fc1)
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])

keep_prob = placeholder(Float32)
h_fc1_drop = nn.dropout(h_fc1, keep_prob)
h_pool2_flat = reshape(h_pool2, [-1, 7*7*64])
h_fc1 = nn.relu(h_pool2_flat * W_fc1 + b_fc1)

W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
keep_prob = placeholder(Float32)
h_fc1_drop = nn.dropout(h_fc1, keep_prob)

y_conv = nn.softmax(h_fc1_drop * W_fc2 + b_fc2)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

cross_entropy = reduce_mean(-reduce_sum(y_.*log(y_conv), axis=[2]))
global y_conv = nn.softmax(h_fc1_drop * W_fc2 + b_fc2)

global cross_entropy = reduce_mean(-reduce_sum(y_.*log(y_conv), axis=[2]))
end

train_step = train.minimize(train.AdamOptimizer(1e-4), cross_entropy)

Expand All @@ -65,7 +68,7 @@ accuracy = reduce_mean(cast(correct_prediction, Float32))

run(session, global_variables_initializer())

for i in 1:1000
for i in 1:200
batch = next_batch(loader, 50)
if i%100 == 1
train_accuracy = run(session, accuracy, Dict(x=>batch[1], y_=>batch[2], keep_prob=>1.0))
Expand All @@ -77,3 +80,5 @@ end
testx, testy = load_test_set()
test_accuracy = run(session, accuracy, Dict(x=>testx, y_=>testy, keep_prob=>1.0))
info("test accuracy $test_accuracy")

visualize()

0 comments on commit 28e4f5a

Please sign in to comment.