Tensorflow实现岭回归
来源:互联网 发布:学拳击怎么跑步 知乎 编辑:程序博客网 时间:2024/04/20 22:06
Tensorflow实现岭回归
岭回归可以看作是线性回归的一种增强版,实现的时候无非就是加入一个二范数正则化项,但是应当注意,这里只对于权重进行惩罚,偏置项是不加入正则化的,具体理论可以参见相关的论文和blog,在此不赘述。
这里还是使用tf实现了岭回归算法,并实际应用在了一个真实数据集而非人工数据集上,数据及规模相比于之前的数据也大了很多。
本次代码从数据读入,到建立计算图,到模型训练、模型评估,实际上是对前面几篇博客的小总结,至此,一般情况下的数据训练问题都可以套用这个模板来写。
我所使用的数据格式为最后一列是label,其余列是特征,label是二类,分别用1,-1表示。
目前版本只有岭回归,之后会加入logstic回归已经tensorboard显示的功能。
import tensorflow as tfimport numpy as npimport pandas as pdtrain = pd.read_csv("./data/dataset1-a9a-training.txt", encoding="utf-8")print(train.describe())columnNum = train.values.shape[1] # 获取数据的列数,其中最后一列是label# 使用pandas获取全部数据,评估效果用def getAll(filename): data = pd.read_csv(filename, dtype=np.float32) example = data.values[:,:-1] label = data.values[:,-1] return example, label# tf获取数据def read_data(filenameQueue): reader = tf.TextLineReader() key, value = reader.read(filenameQueue) recordDefaults = [] for i in range(columnNum): recordDefaults.append([0.0]) col = tf.decode_csv(value, record_defaults=recordDefaults) features = tf.squeeze(tf.reshape(tf.stack([col[:-1]]), [columnNum-1, 1]), squeeze_dims=1) # 统一格式为[n_samples,1] label = col[-1] return features, label# tf分批数据def input_batch(filename, batchSize, dequeue = 10000): fileNameQue = tf.train.string_input_producer([filename], shuffle=True) example, label = read_data(fileNameQue) min_after_dequeue = dequeue # 样本池调整的大一些随机效果好 capacity = min_after_dequeue + 3 * batchSize exampleBatch, labelBatch = tf.train.shuffle_batch([example, label], batch_size=batchSize, capacity=capacity, min_after_dequeue=min_after_dequeue) return exampleBatch, labelBatch# 岭回归计算图def ridge_regression(exampleBatch, labelBatch, lamda = 0.5, alpha = 0.1): with tf.name_scope("ridge"): W = tf.Variable(tf.random_normal([columnNum-1, 1]), name="W") b = tf.Variable(tf.random_normal([1]), name="b") logits = tf.matmul(exampleBatch, W)+b loss = tf.reduce_mean(tf.square(labelBatch-logits)) + lamda*tf.norm(W) train = tf.train.GradientDescentOptimizer(alpha).minimize(loss) return train, loss, W, b# 评价函数,计算准确率def evaluate(W, b, testData, testLabel): testExample = tf.Variable(testData, trainable=False) tfLabel = tf.Variable(testLabel, trainable=False) tfLabel = tf.equal(tfLabel, 1) tfLabel = tf.reshape(tfLabel, [-1, 1]) pred = tf.matmul(testExample, W)+b res = tf.equal(tf.greater(pred, 0.0), tfLabel) # 以0为分界点分类 acc = tf.reduce_mean((tf.cast(res, dtype=tf.float32))) # 转换成浮点型,整型计算会一直结果为0 return accif __name__ == "__main__": exampleBatch, labelBatch = input_batch("./data/dataset1-a9a-training.txt", batchSize=100) train, loss, W, b = ridge_regression(exampleBatch, labelBatch) testData, testLabel = getAll("./data/dataset1-a9a-training.txt") acc = evaluate(W, b, testData, testLabel) maxIter = 1000 # 最大迭代次数 with tf.Session() as sess: init = tf.global_variables_initializer() # 初始化放在计算图后 sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for i in range(maxIter): #example, label = sess.run([exampleBatch, labelBatch]) #print(example.shape) #print(label) _, lossArr, accArr = sess.run([train, loss, acc]) print(lossArr, accArr) #print(logits) coord.request_stop() coord.join(threads)
0 0
- Tensorflow实现岭回归
- Tensorflow实现softmax回归
- tensorflow 实现线性回归
- Tensorflow实现线性回归
- Tensorflow实现线性回归
- Tensorflow实现逻辑回归
- TensorFlow实现线性回归
- tensorflow实现非线性回归
- 逻辑回归之tensorflow实现
- TensorFlow实现对数几率回归
- Tensorflow实现逻辑回归模型
- tensorflow实现基本的回归
- TensorFlow实现逻辑回归分类器
- 基于TensorFLow实现MNIST和softmax回归
- TensorFlow上实现Softmax回归模型
- tensorflow实现线性回归的完整程序
- TensorFlow 实现一元线性回归模型
- Tensorflow学习之逻辑回归的实现
- 栈的弹出序列
- java集合架构____集合层次分析
- Oracle数据库连接问题排查思路
- 树状数组详解
- 校门外的树(NOIP2005复赛 普及组 第二题)
- Tensorflow实现岭回归
- 散列(hash table)
- 利用oracle官网提供的occi库在windows下操作oracle数据库
- Android 7.0 ProgressDialog 出现dismiss时背景灰色遮罩层无法关闭BUG
- 黑豆传说
- scala1:Debain操作系统里面安装scala
- CentOS 6.6下双网卡共享上网配置
- android图片引导页
- php实现验证码