接下来使用 TensorFlow
学习框架实现逻辑分类, TensorFlow
的框架也在后面会有介绍,通过逻辑回归算法,对 TensorFlow
了解也会更加深刻, 在 TensorFlow
我们需要组建评价函数,不知道大家还记不记得逻辑回归的评价函数呢?在上一节中,如果忘了可以再去看一看。
逻辑回归的 评价函数
,也就是 极大似然函数
如下:
J ( θ ) = − 1 m [ ∑ i = 1 m y ( i ) log ( h θ ( x ( i ) ) ) + ( 1 − y ( i ) ) log ( 1 − h θ ( x ( i ) ) ) ] 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]J (θ)=−m 1 [i =1 ∑m y (i )lo g (h θ(x (i )))+(1 −y (i ))lo g (1 −h θ(x (i )))]
用 TensorFlow
对 评价函数
进行描述,这里假设回归直线为:
W x + b Wx+b W x +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')
图形如下:
Original: https://blog.csdn.net/weixin_40425640/article/details/124166512
Author: go2coding
Title: 机器学习入门:第三章 逻辑(Logistic)回归 TensorFlow 回归计算(7)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/630738/
转载文章受原作者版权保护。转载请注明原作者出处!