Reputation: 17
I am trying to implement the softmax regression model in tensorflow in order to make a benchmark with other mainstream deep-learning frameworks. The official documentation code is slow because of the feed_dict issue in tensorflow. I am trying to serve the data as tensorflow constant but I don't know the most efficient way to do that. For now I just use the single batch as constant and trained through that batch. What are the efficient solutions of making minibatched solution of that code? Here is my code:
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
batch_xs, batch_ys = mnist.train.next_batch(100)
x = tf.constant(batch_xs, name="x")
W = tf.Variable(0.1*tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
logits = tf.matmul(x, W) + b
batch_y = batch_ys.astype(np.float32)
y_ = tf.constant(batch_y, name="y_")
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, y_))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
....
# Minitbatch is never updated during that for loop
for i in range(5500):
sess.run(train_step)
Upvotes: 0
Views: 500
Reputation: 3159
Just as follows.
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
batch_size = 32 #any size you want
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
x = tf.placeholder(tf.float32, shape = [None, 784])
y = tf.placeholder(tf.float32, shape = [None, 10])
W = tf.Variable(0.1*tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
logits = tf.matmul(x, W) + b
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
....
# Minitbatch is never updated during that for loop
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
l, _ = sess.run([loss, train_step], feed_dict = {x: batch_x, y: batch_ys})
print l #loss for every minibatch
Shape like [None, 784] allows you to feed any value of shape [?, 784].
I haven't tested this code, but I hope it would work.
Upvotes: 1