博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tensorflow 梯度下降以及summary
阅读量:4315 次
发布时间:2019-06-06

本文共 4148 字,大约阅读时间需要 13 分钟。

# 保证脚本与Python3兼容from __future__ import print_functionimport osimport tensorflow as tfimport numpy as npfrom utils import createSummaryWriter, generateLinearData, createLinearModel    #导入utilsdef gradientDescent(X, Y, model, learningRate=0.01, maxIter=10000, tol=1.e-6):    """    利用梯度下降法训练模型。    参数    ----    X : np.array, 自变量数据    Y : np.array, 因变量数据    model : dict, 里面包含模型的参数,损失函数,自变量,应变量。    """    # 确定最优化算法    method = tf.train.GradientDescentOptimizer(learning_rate=learningRate)    optimizer = method.minimize(model["loss_function"])    # 增加日志    tf.summary.scalar("loss_function", model["loss_function"])    tf.summary.histogram("params", model["model_params"])    tf.summary.scalar("first_param", tf.reduce_mean(model["model_params"][0]))    tf.summary.scalar("last_param", tf.reduce_mean(model["model_params"][-1]))    summary = tf.summary.merge_all()    # 在程序运行结束之后,运行如下命令,查看日志    # tensorboard --logdir logs/    # Windows下的存储路径与Linux并不相同    if os.name == "nt":        summaryWriter = createSummaryWriter("logs\\gradient_descent")    else:        summaryWriter = createSummaryWriter("logs/gradient_descent")    # tensorflow开始运行    sess = tf.Session()    # 产生初始参数    init = tf.global_variables_initializer()    # 用之前产生的初始参数初始化模型    sess.run(init)    # 迭代梯度下降法    step = 0    prevLoss = np.inf    diff = np.inf    # 当损失函数的变动小于阈值或达到最大循环次数,则停止迭代    while (step < maxIter) & (diff > tol):        _, summaryStr, loss = sess.run(            [optimizer, summary, model["loss_function"]],            feed_dict={model["independent_variable"]: X,                       model["dependent_variable"]: Y})        # 将运行细节写入目录        summaryWriter.add_summary(summaryStr, step)        # 计算损失函数的变动        diff = abs(prevLoss - loss)        prevLoss = loss        step += 1    summaryWriter.close()    # 在Windows下运行此脚本需确保Windows下的命令提示符(cmd)能显示中文    # 输出最终结果    print("模型参数:\n%s" % sess.run(model["model_params"]))    print("迭代次数:%s" % step)    print("损失函数值:%s" % loss)def run():    """    程序入口    """    # dimension表示自变量的个数,num表示数据集里数据的个数。    dimension = 30    num = 10000    # 随机产生模型数据    X, Y = generateLinearData(dimension, num)    # 定义模型    model = createLinearModel(dimension)    # 使用梯度下降法,估计模型参数    gradientDescent(X, Y, model)if __name__ == "__main__":    run()

utils:

"""此脚本用于随机生成线性模型数据、定义模型以及其他工具"""import numpy as npimport tensorflow as tfdef generateLinearData(dimension, num):    """    随机产生线性模型数据    参数    ----    dimension :int,自变量个数    num :int,数据个数    返回    ----    x :np.array,自变量    y :np.array,因变量    """    np.random.seed(1024)    beta = np.array(range(dimension)) + 1    x = np.random.random((num, dimension))    epsilon = np.random.random((num, 1))    # 将被预测值写成矩阵形式,会极大加快速度    y = x.dot(beta).reshape((-1, 1)) + epsilon    return x, ydef createLinearModel(dimension):    """    搭建模型,包括数据中的自变量,应变量和损失函数    参数    ----    dimension : int,自变量的个数    返回    ----    model :dict,里面包含模型的参数,损失函数,自变量,应变量    """    np.random.seed(1024)    # 定义自变量和应变量    x = tf.placeholder(tf.float64, shape=[None, dimension], name='x')    ## 将被预测值写成矩阵形式,会极大加快速度    y = tf.placeholder(tf.float64, shape=[None, 1], name="y")    # 定义参数估计值和预测值    betaPred = tf.Variable(np.random.random([dimension, 1]))    yPred = tf.matmul(x, betaPred, name="y_pred")    # 定义损失函数    loss = tf.reduce_mean(tf.square(yPred - y))    model = {
"loss_function": loss, "independent_variable": x, "dependent_variable": y, "prediction": yPred, "model_params": betaPred} return modeldef createSummaryWriter(logPath): """ 检查所给路径是否已存在,如果存在删除原有日志。并创建日志写入对象 参数 ---- logPath :string,日志存储路径 返回 ---- summaryWriter :FileWriter,日志写入器 """ if tf.gfile.Exists(logPath): tf.gfile.DeleteRecursively(logPath) summaryWriter = tf.summary.FileWriter(logPath, graph=tf.get_default_graph()) return summaryWriter

 

(1)对于单个数值型的变量,比如所示函数值,使用scalar函数记录它的信息

(2)对于矩阵型变量比如模型参数使用histogram函数记录信息。 如果想追踪某个特定参数,需要先将特定参数转化为标量,再用scalar函数记录信息

(3)summary类定义的操作和其他tensorflow操作一样,只有在Session()对象调用它们时才会真正运行,手工调用效率显然不高,所以使用merge_all函数将他们合并为一个操作。

(4)为了记录产生的日志,需要创建tf.summary.FileWriter对象,。然后在每次迭代梯度下降法的使用时,将相应的日志写入文件。

转载于:https://www.cnblogs.com/francischeng/p/9736777.html

你可能感兴趣的文章
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>