简单的tensorflow:1_logistic regression

关于softmax_cross_entropy_with_logits与sigmoid_cross_entropy_with_logits

loss = -tf.reduce_mean(tf_Y*tf.log(tf.clip_by_value(pred,1e-11,1.0)))
# 也可以直接使用tensorflow的版本:
# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_Y,logits=pred))
loss = tf.reduce_mean(- Y*  tf.log(tf.clip_by_value(H,1e-11,1.0)) - (1 - Y) * tf.log(1 - tf.clip_by_value(H,1e-11,1.0)))
# 也可以使用tensorflow的版本:
#loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_Y,logits=pred))


源码来自:
https://github.com/nlintz/TensorFlow-Tutorials
感谢作者无私提供

以下是很简单的logistic regression代码,初学者可以先看懂代码,然后推荐在ipython或者jupyter上边独立实现。

#!/usr/bin/env python

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data


def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def model(X, w):
    return tf.matmul(X, w) 
# notice we use the same model as linear regression, this is because there is a baked in cost function which performs softmax and cross entropy


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder("float", [None, 784]) # create symbolic variables
Y = tf.placeholder("float", [None, 10])

w = init_weights([784, 10]) 
# like in linear regression, we need a shared variable weight matrix for logistic regression

py_x = model(X, w)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
 # compute mean cross entropy (softmax is applied internally)
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) 
# construct optimizer
predict_op = tf.argmax(py_x, 1) 
# at predict time, evaluate the argmax of the logistic regression

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize all variables
    tf.global_variables_initializer().run()

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
        print(i, np.mean(np.argmax(teY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX})))
def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

这是初始化模型的参数w,tensorflow中使用tf.Variable保存参数,这个参数是由tf.random_normal(shape, stddev=0.01)形成的,具体的tf.random_normal可以参考coding小记:np.random.randn与tf.random_normal

def model(X, w):
    return tf.matmul(X, w) 

定义一个非常简单的模型,tf.matmul实现了线性代数中矩阵的相乘。其中X和w的形状要符合矩阵相乘的规则。

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

获取真实的数据集,这里使用的是著名的mnist手写数字数据集,input_data.read_data_sets是获取数据集数据的标准写法。trX, trY, teX, teY = ... 则是分别得到训练集的数据和标签与测试集的数据和标签。

X = tf.placeholder("float", [None, 784]) # create symbolic variables
Y = tf.placeholder("float", [None, 10])

tensorflow中使用的是先创建图,再填入数据的原则,因此再创建图的时候就需要有一个容器来占住数据的位置,就是tf.placeholder,X的placeholder定义了X是float类型,并且拥有[None, 784]的shape。其中None代表了我们一次性要输入未知数量的图片,有可能一次输入64张,有可能是128张等等,因此用None表示。784 = 28 * 28,mnist数据集中每张图片都是28 * 28的规格,因此一张图片包含784个像素点。Y同样是float类型,因为手写数字是从0到9一共10种,所以shape是10。

w = ([784, 10]) 
# like in linear regression, we need a shared variable weight matrix for logistic regression

py_x = model(X, w)

调用自己定义的init_weightsmodel,来初始化参数w和构建模型py_x。

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
 # compute mean cross entropy (softmax is applied internally)
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) 
# construct optimizer
predict_op = tf.argmax(py_x, 1) 
# at predict time, evaluate the argmax of the logistic regression

定义cost,也就是我们要优化的东西,通过优化cost来优化我们的模型。这是一个分类问题,因此使用softmax作为激活函数,cost用交叉熵来定义tf.nn.softmax_cross_entropy_with_logits,最后一次性输入的n张图片的所有的cost再求一个平均值tf.reduce_mean
train_op:然后定义训练过程,我们使用tf.train.GradientDescentOptimizer 梯度下降优化器,定义学习率为0.05,来最小化cost。

with tf.Session() as sess:
    # you need to initialize all variables
    tf.global_variables_initializer().run()

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
        print(i, np.mean(np.argmax(teY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX})))

最后开始训练,开启一个session,所有的训练内容都在这个session中进行。
首先初始化所有参数,这是必需步骤。tf.global_variables_initializer().run()
然后开始训练100轮。sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]}),使用session来run我们之前定义的train_op,从而来使用梯度下降最小化cost,在这里我们要为之前构建的图填入数据,也就是feed_dict
最后打印出训练准确率,如下,可以看出准确率随着训练在一点点上升:

0 0.8845
1 0.897
2 0.9031
3 0.9072
4 0.9102
5 0.9106
6 0.9121
7 0.913
8 0.9146
9 0.916
10 0.9161
11 0.9166
12 0.9166
13 0.9171
14 0.9171
15 0.918
16 0.9186
17 0.919
18 0.9196
19 0.9201
20 0.9199
21 0.92
22 0.9203
23 0.92
24 0.9204
25 0.9202
26 0.9205
27 0.9209
28 0.9212
29 0.9213
30 0.9216
31 0.9215
32 0.9214
33 0.9214
34 0.9211
35 0.9213
36 0.9213
37 0.9215
38 0.9217
39 0.9219
40 0.9221
41 0.922
42 0.9221
43 0.922
44 0.922
45 0.9219
46 0.922
47 0.922
48 0.9219
49 0.9219
50 0.922
51 0.9222
52 0.9222
53 0.9224
54 0.9223
55 0.9223
56 0.9224
57 0.9223
58 0.9224
59 0.9225
60 0.9228
61 0.923
62 0.9231
63 0.923
64 0.9231
65 0.9232
66 0.9233
67 0.9236
68 0.9235
69 0.9235
70 0.9234
71 0.9234
72 0.9236
73 0.9236
74 0.9237
75 0.9237
76 0.9236
77 0.9236
78 0.9236
79 0.9236
80 0.9235
81 0.9234
82 0.9234
83 0.9235
84 0.9234
85 0.9233
86 0.9233
87 0.9233
88 0.9233
89 0.9231
90 0.9231
91 0.9233
92 0.9233
93 0.9233
94 0.9233
95 0.9232
96 0.9233
97 0.9235
98 0.9235
99 0.9235

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值