第三章 逻辑(Logistic)回归(7)

接下来使用TensorFlow 学习框架实现逻辑分类,TensorFlow的框架也在后面会有介绍,通过逻辑回归算法,对TensorFlow了解也会更加深刻, 在TensorFlow我们需要组建评价函数,不知道大家还记不记得逻辑回归的评价函数呢?在上一节中,如果忘了可以再去看一看。

逻辑回归的评价函数,也就是极大似然函数如下:

$$J(θ) = -\frac{1}{m} \left[ \sum_{i=1}^m y^{(i)}\log(h_\theta(x^{(i)})) + (1 - y^{(i)})\log(1 - h_\theta(x^{(i)})) \right]$$

TensorFlow评价函数进行描述,这里假设回归直线为:

$$Wx+b$$

评价函数 可以写成:

X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

W = tf.Variable(tf.zeros([feature_num, 1]))
b = tf.Variable([-.9])

db = tf.matmul(X, tf.reshape(W, [-1, 1])) + b
hyp = tf.sigmoid(db)

cost0 = y * tf.log(hyp)
cost1 = (1 - y) * tf.log(1 - hyp)
cost = (cost0 + cost1) / -sample_num
loss = tf.reduce_sum(cost)

使用梯度下降算法,求出最优值:

optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)

对数据进行训练:

sess.run(train, {X: train_X, y: train_y})

这是TensorFlow 求解逻辑回归的过程。

这个过程的代码如下:

import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv("ex2data1.txt", header=None)
train_data = df.values

train_X = train_data[:, :-1]
train_y = train_data[:, -1:]
feature_num = len(train_X[0])
sample_num = len(train_X)

X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

W = tf.Variable(tf.zeros([feature_num, 1]))
b = tf.Variable([-.9])

db = tf.matmul(X, tf.reshape(W, [-1, 1])) + b
hyp = tf.sigmoid(db)

cost0 = y * tf.log(hyp)
cost1 = (1 - y) * tf.log(1 - hyp)
cost = (cost0 + cost1) / -sample_num
loss = tf.reduce_sum(cost)

optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

feed_dict = {X: train_X, y: train_y}

for step in range(100000):
    sess.run(train, {X: train_X, y: train_y})
    if step % 100 == 0:
        print(step, sess.run(W).flatten(), sess.run(b).flatten())

最后显示训练的参数w和b如下:

(99900, array([ 0.0485824 ,  0.04162483], dtype=float32), array([-5.2481041], dtype=float32))

也可以绘制图形,如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


#!/usr/bin/python
#coding=utf-8

"""
start python 项目
"""

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rcParams['font.sans-serif'] = ['SimHei']
df = pd.read_csv("ex2data1.txt", header=None)

ex_data_0 = df[df[2] == 0].values
ex_data_1 = df[df[2] == 1].values


plt.figure()

plt.scatter(ex_data_0[:, 0], ex_data_0[:, 1], marker='x', c='r')
plt.scatter(ex_data_1[:, 0], ex_data_1[:, 1], marker='o', c='g')

plt.title(u"阿猫学编程 - tensorflow " )

x = np.arange(25, 110, 10)
y = (5.2481041-0.0485824*x)/0.04162483
plt.plot(x, y)
plt.savefig('tensorflow_logist.png')

图形如下:

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程


来自您的鼓励

如果您感觉这一系列的教程,读后有点收获,并对知识有点了解,不妨小额捐助我一下,让我有动力继续写出更多好文章;或者在评论区留下你的意见。

所有评论

写了这么多年博客,收到的优秀评论少之又少。在这个属于 SNS 的时代也并不缺少向作者反馈的渠道。

还没有评论

撰写评论