使用tensorflow实现逻辑回归算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xckkcxxck/article/details/79367565

本文参考《tensorflow实战机器学习指南》

 
# -*- coding: utf-8 -*- """ Created on Sat Feb 24 20:55:02 2018 @author: www """ #用tensorflow实现逻辑回归算法 #使用鸢尾花数据集 import pandas as pd import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import requests from tensorflow.python.framework import ops ops.reset_default_graph() birthdata_url = 'https://www.umass.edu/statdata/statdata/data/lowbwt.dat' birth_file = requests.get(birthdata_url) birth_data = birth_file.text.split('\r\n')[5:] birth_header = [x for x in birth_data[0].split( '') if len(x) >= 1] birth_data = [[float(x) for x in y.split(' ') if len(x) >= 1] for y in birth_data[1:] if len(y) >= 1] y_vals = np.array([x[1] for x in birth_data]) x_vals = np.array([x[2:9] for x in birth_data]) train_indices = np.random.choice(len(x_vals), round(len(x_vals) * 0.8), replace=False) test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices))) X_train = x_vals[train_indices] X_test = x_vals[test_indices] y_train = y_vals[train_indices] y_test = y_vals[test_indices] #特征缩放 def normalize_cols(m): col_max = m.max(axis=0) col_min = m.min(axis=0) return (m - col_min) / (col_max - m) X_train = normalize_cols(X_train) X_test = normalize_cols(X_test) #声明批量大小,占位符,变量和逻辑模型,这步不需要用sigmoid函数封装输出结果,因为sigmoid操作是包含在 #内建损失函数之中的 batch_size = 25 x_data = tf.placeholder(shape = [None, 7], dtype=tf.float32) y_target = tf.placeholder(shape = [None,1], dtype=tf.float32) A = tf.Variable(tf.random_normal(shape=[7,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) mdel_output = tf.add(tf.matmul(x_data, A), b) #声明损失函数,其包括sigmoid函数,初始化变量,声明优化器, loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_target, logits=mdel_output)) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) my_opt = tf.train.GradientDescentOptimizer(0.01) train_step = my_opt.minimize(loss) #除记录损失函数外,也需要记录分类器在训练集和测试集的准确度,所以创建一个返回准确度的预测函数 prediction = tf.round(tf.sigmoid(mdel_output)) prediction_correct = tf.cast(tf.equal(prediction, y_target), tf.float32) accuracy = tf.reduce_mean(prediction_correct) #开始遍历迭代训练,记录损失值和准确度, loss_vec = [] train_acc = [] test_acc = [] for i in range(1500): rand_index = np.random.choice(len(X_train), size=batch_size) rand_x = X_train[rand_index] rand_y = np.transpose([y_train[rand_index]]) sess.run(train_step, feed_dict={x_data:rand_x, y_target:rand_y}) temp_loss = sess.run(loss, feed_dict={x_data:rand_x, y_target:rand_y}) loss_vec.append(temp_loss) temp_acc_train = sess.run(accuracy, feed_dict={x_data:X_train, y_target:np.transpose([y_train])}) train_acc.append(temp_acc_train) #============================================================================== # temp_acc_test = sess.run(accuracy, feed_dict={x_data:X_test, y_target:np.transpose([y_test])}) # test_acc.append(temp_acc_test) #============================================================================== #绘制损失和准确度 plt.plot(loss_vec, 'k-') plt.title('Cross Entropy Loss per Generation') plt.xlabel('Generation') plt.ylabel('Cross Entropy Loss') plt.show() plt.plot(train_acc, 'k-', label='Train Set Accuracy') plt.plot(test_acc, 'k-', label='Test Set Accuracy') plt.title('Train and Test Accuracy') plt.xlabel('Generation') plt.ylabel('Accuracy') plt.legend(loc='lower right') plt.show() 
 

猜你喜欢

转载自blog.csdn.net/xckkcxxck/article/details/79367565