张量流中的快速softmax回归实现

时间:2016-09-29 12:23:12

标签: tensorflow softmax

我正在尝试在tensorflow中实现softmax回归模型,以便与其他主流深度学习框架进行基准测试。由于张量流中的feed_dict issue,官方文档代码很慢。我试图将数据作为张量流常数提供,但我不知道最有效的方法。现在我只使用单批作为常量并通过该批次进行培训。制作该代码的小型解决方案的有效解决方案是什么?这是我的代码:

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)

1 个答案:

答案 0 :(得分:0)

如下。

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

像[None,784]这样的形状允许你提供任何形状的值[?,784]。

我没有测试过这段代码,但我希望它可以正常运行。