Skip to content

Instantly share code, notes, and snippets.

@junichiro
Last active January 13, 2017 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junichiro/91943b2545ed2bb450a771426d80b910 to your computer and use it in GitHub Desktop.
Save junichiro/91943b2545ed2bb450a771426d80b910 to your computer and use it in GitHub Desktop.
TensorFlow 入門 〜 MNIST for beginner のプチ応用 〜 ref: http://qiita.com/junichiro/items/6f0de9688e12535f93e8
h_\theta(x) = \frac{ 1 }{ 1 + e^{ -\theta^T x } }
J(\theta) = -\frac{ 1 }{ m }\sum_{ i = 1 }^{ m }\bigl( y^{ (i) } log h_\theta(x^{ (i) }) + (1 - y^{ (i) }) log (1 - h_\theta(x^{ (i) })) \bigr)
diff -u mnist_softmax.py mnist_sigmoid.py
--- mnist_softmax.py 2017-01-12 10:12:21.000000000 +0900
+++ mnist_sigmoid.py 2017-01-12 10:50:52.000000000 +0900
@@ -56,9 +56,9 @@
#
# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
# outputs of 'y', and then average across the batch.
- cross_entropy = tf.reduce_mean(
- tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
- train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
+ hx = tf.sigmoid(y)
+ loss = -tf.reduce_mean((y_ * tf.log(hx) + (1 - y_) * tf.log(1 - hx)), reduction_indices=[0])
+ train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
Diff finished. Thu Jan 12 10:51:33 2017
# Train
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
diff -u mnist_sigmoid.py mnist_sigmoid_all.py
--- mnist_sigmoid.py 2017-01-12 12:59:36.000000000 +0900
+++ mnist_sigmoid_all.py 2017-01-12 12:59:09.000000000 +0900
@@ -64,8 +64,7 @@
tf.global_variables_initializer().run()
# Train
for _ in range(1000):
- batch_xs, batch_ys = mnist.train.next_batch(100)
- sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
+ sess.run(train_step, feed_dict={x: mnist.train.images, y_: mnist.train.labels})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
Diff finished. Thu Jan 12 13:54:43 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment