没有内置优化器的Tensorflow中的多元Logistic回归

时间:2017-03-04 17:57:33

标签: python tensorflow

我试图通过实现L2正则化的随机梯度下降来构建25112 28x28图像的多级逻辑回归量,这些图像是0-4手写的,但不使用传统的张量流函数(我正在尝试从头开始构建它。我是新手,所以我可能犯了一些错误。感谢您抽出宝贵时间!

我相信我:

正确阅读培训和测试图像

正确设置了tf.Session()

我正在尝试打印出学习重量和相应偏差的结果,但我不确定我是否正确这样做。

我收到以下错误:

Traceback (most recent call last):
  File "Program2edited.py", line 87, in <module>
    test_lr()
  File "Program2edited.py", line 83, in test_lr
    print(classifier.predict((y1)))
  File "Program2edited.py", line 72, in predict
    return softmax(tf.matmul(x, self.W) + self.b)
  File "C:\Users\Person\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\o
    a = ops.convert_to_tensor(a, name="a")
  File "C:\Users\Person\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\f
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\Person\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\f
    return constant(v, dtype=dtype, name=name)
  File "C:\Users\Person\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\f
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "C:\Users\Person\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\f
    _GetDenseDimensions(values)))
ValueError: Argument must be a dense tensor: [array([[[0, 0, 0, ..., 0, 0, 0]],

       [[1, 0, 1, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0]],

       ...,
       [[0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0]]], dtype=uint8)] - got shape [1, 4983, 1, 784], but wanted [1].

我的代码如下:

import numpy as np

import tensorflow as tf

filenames = tf.train.match_filenames_once("C:/train_data/*.jpg")
filename_queue = tf.train.string_input_producer(filenames)

image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
images = tf.image.decode_jpeg(image_file)
image = tf.image.resize_image_with_crop_or_pad(images, 28, 28)
image.set_shape((28, 28, 1))
images = tf.reshape(image, [-1, 784])
# `image_batch` contains 25112 consecutive images, packed into a single tensor.
image_batch = tf.train.batch((images,), 25112)

test_filenames = tf.train.match_filenames_once("C:/test_data/*.jpg")
test_filename_queue = tf.train.string_input_producer(test_filenames)

image_reader = tf.WholeFileReader()
_, test_image_file = image_reader.read(test_filename_queue)
test_images = tf.image.decode_jpeg(test_image_file)
test_image = tf.image.resize_image_with_crop_or_pad(test_images, 28, 28)
test_image.set_shape((28, 28, 1))
test_images = tf.reshape(test_image, [-1, 784])
# `test image_batch` contains 4983 consecutive images, packed into a single tensor.
test_image_batch = tf.train.batch((test_images,), 4983) 

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    image_tensor2 = sess.run([image_batch]) 
    test_image_tensor2 = sess.run([test_image_batch])
    coord.request_stop()
    coord.join(threads)

    def sigmoid(x):
        return 1./(1+ tf.exp(-x))

    def shape(tensor):
        s = tensor.get_shape()
        return tuple([s[i].value for i in range(0, len(s))])

    def softmax(x):
        e = tf.exp(x - tf.reduce_max(x))
        return e 

    class LogisticRegression(object):
        def __init__(self, input, label):
            self.x = tf.placeholder(tf.float32, [25112, 784]) #784=28*28
            self.y = tf.placeholder(tf.float32, [25112, 5])
            self.W = tf.Variable(tf.zeros([784, 5])) 
            self.b  = tf.Variable(tf.zeros([5]))  

    def train(self, lr=0.1, input=None, L2_reg=0.01):
        if input is not None:
            self.x = input 
        p_y_given_x = softmax(tf.matmul(self.x, self.W) + self.b)
        d_y = self.y - p_y_given_x
        x_transpose = self.x
        self.W += lr * tf.matmul(tf.transpose(self.x), d_y) - lr * L2_reg * self.W
        self.b += lr * tf.reduce_mean(d_y, axis=0)

    def negative_log_likelihood(self):
        sigmoid_activation = softmax(tf.matmul(self.x, self.W) + self.b)
        cross_entropy = - tf.reduce_mean(tf.add((self.y * tf.log(sigmoid_activation)), (1 - self.y) * tf.log(1 - sigmoid_activation)), axis=1)
        return cross_entropy


    def predict(self, x):
        return softmax(tf.matmul(x, self.W) + self.b)

def test_lr(image_tensor2 = image_tensor2, test_image_tensor2 = test_image_tensor2, learning_rate=0.01, n_epochs=200):
    x1 = image_tensor2
    y1 = test_image_tensor2

    classifier = LogisticRegression(input=x1, label=y1)
    for epoch in range(0, n_epochs):
        learning_rate *= 0.95
        classifier.train(lr=learning_rate)
        cost = classifier.negative_log_likelihood()

    print(classifier.predict((y1)))


if __name__ == "__main__":
    test_lr()

1 个答案:

答案 0 :(得分:0)

这段代码中有很多奇怪的东西。例如,classifier.train实际上并不训练分类器,它只是构建ops来执行此操作。您需要创建一个会话以在tensorflow中运行ops。

我建议从basic tutorials in the tensorflow website开始。