python 实现一个简单的线性回归案例
作者:雾霾王者 时间:2023-05-08 23:40:25
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : 自实现一个线性回归.py
# @Author: 赵路仓
# @Date : 2020/4/12
# @Desc :
# @Contact : 398333404@qq.com
import os
import tensorflow as tf
def linear_regression():
"""
自实现一个线性回归
:return:
"""
# 命名空间
with tf.variable_scope("prepared_data"):
# 准备数据
x = tf.random_normal(shape=[100, 1], name="Feature")
y_true = tf.matmul(x, [[0.08]]) + 0.7
# x = tf.constant([[1.0], [2.0], [3.0]])
# y_true = tf.constant([[0.78], [0.86], [0.94]])
with tf.variable_scope("create_model"):
# 2.构造函数
# 定义模型变量参数
weights = tf.Variable(initial_value=tf.random_normal(shape=[1, 1], name="Weights"))
bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1], name="Bias"))
y_predit = tf.matmul(x, weights) + bias
with tf.variable_scope("loss_function"):
# 3.构造损失函数
error = tf.reduce_mean(tf.square(y_predit - y_true))
with tf.variable_scope("optimizer"):
# 4.优化损失
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(error)
# 收集变量
tf.summary.scalar("error", error)
tf.summary.histogram("weights", weights)
tf.summary.histogram("bias", bias)
# 合并变量
merged = tf.summary.merge_all()
# 创建saver对象
saver = tf.train.Saver()
# 显式的初始化变量
init = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
# 初始化变量
sess.run(init)
# 创建事件文件
file_writer = tf.summary.FileWriter("E:/tmp/linear", graph=sess.graph)
# print(x.eval())
# print(y_true.eval())
# 查看初始化变量模型参数之后的值
print("训练前模型参数为:权重%f,偏置%f" % (weights.eval(), bias.eval()))
# 开始训练
for i in range(1000):
sess.run(optimizer)
print("第%d次参数为:权重%f,偏置%f,损失%f" % (i + 1, weights.eval(), bias.eval(), error.eval()))
# 运行合并变量操作
summary = sess.run(merged)
# 将每次迭代后的变量写入事件
file_writer.add_summary(summary, i)
# 保存模型
if i == 999:
saver.save(sess, "./tmp/model/my_linear.ckpt")
# # 加载模型
# if os.path.exists("./tmp/model/checkpoint"):
# saver.restore(sess, "./tmp/model/my_linear.ckpt")
print("参数为:权重%f,偏置%f,损失%f" % (weights.eval(), bias.eval(), error.eval()))
pre = [[0.5]]
prediction = tf.matmul(pre, weights) + bias
sess.run(prediction)
print(prediction.eval())
return None
if __name__ == "__main__":
linear_regression()
来源:https://www.cnblogs.com/zlc364624/p/12686695.html
标签:python,线性回归


猜你喜欢
JavaScript的replace方法与正则表达式结合应用讲解
2008-03-06 21:37:00
怎样在不同版本SQL Server中存储数据
2009-01-20 13:11:00
HTML+JS实现猜拳游戏的示例代码
2024-04-16 09:31:25

python数据操作之lambda表达式详情
2022-08-19 21:21:32
Python Multiprocessing多进程 使用tqdm显示进度条的实现
2021-04-03 19:15:08

MySQL8.0.23安装超详细教程
2024-01-26 05:06:18

python re模块findall()函数实例解析
2022-07-07 13:38:40
详解Python读取配置文件模块ConfigParser
2022-02-25 09:05:23
使用百度云加速后网站打开速度慢、广告不显示的解决方法
2023-04-23 19:07:24

详解Python中contextlib上下文管理模块的用法
2022-03-10 22:32:51
Python中的 ansible 动态Inventory 脚本
2022-10-23 07:53:08

Java数据库连接池之c3p0简介_动力节点Java学院整理
2024-01-19 18:16:03

sql server关键字详解大全(图文)
2024-01-14 09:43:13

mysql中TIMESTAMPDIFF案例详解
2024-01-18 05:56:49

ASP开发网页牢记注意事项
2013-06-28 16:20:30
mysql 10w级别的mysql数据插入
2024-01-16 01:42:46
Python中使用多进程来实现并行处理的方法小结
2023-10-23 14:21:38
Python数据预处理之数据规范化(归一化)示例
2021-11-18 12:08:10

详解Vue2的diff算法
2024-04-28 09:30:48

Python+matplotlib实现堆叠图的绘制
2023-07-21 17:38:35
