Tensorflow - Tutorial (1) : 线性回归
来源:互联网 发布:如何清除软件注册表 编辑:程序博客网 时间:2024/05/18 13:24
使用tensorflow做线性回归
程序运行结果如下图:
实验代码:
#!/usr/bin/env pythonimport tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# 生成101组二维训练数据 trX = np.linspace(-1, 1, 101) #创建-1到1之间的一维等差数组trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # 目标函数为y=2x,并施加一个高斯分布的噪音X = tf.placeholder("float") #创建占位符Y = tf.placeholder("float")def model(X, w): return tf.mul(X, w) # 线性回归模型:x与w的内积w = tf.Variable(0.0, name="weights") # 为模型权重W创建共享变量y_model = model(X, w) cost = tf.square(Y - y_model) # 定义平方损失函数train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) #使用步长为0.01的梯度下降算法,最小化损失函数cost# 创建会话with tf.Session() as sess: # 初始化变量W tf.initialize_all_variables().run() for i in range(100): for (x, y) in zip(trX, trY): sess.run(train_op, feed_dict={X: x, Y: y}) weight = sess.run(w) print(weight) #输出训练完成的权重w,结果应接近2#画图fig = plt.figure()ax1 = fig.add_subplot(111)xx = np.linspace(-1, 1, 101) yy = xx*weightax1.scatter(trX ,trY)plt.plot(xx,yy,"r")plt.show()
5 0
- Tensorflow - Tutorial (1) : 线性回归
- UFLDL Tutorial 线性回归
- tensorflow 实现线性回归
- tensorflow线性回归
- Tensorflow实现线性回归
- TensorFlow之线性回归
- tensorflow之线性回归
- Tensorflow实现线性回归
- TensorFlow实现线性回归
- tensorflow线性回归例子
- tensorflow线性回归测试
- TensorFlow之线性回归
- Tensorflow-线性回归
- Tensorflow-线性回归
- tensorflow 线性回归
- TensorFlow训练线性回归
- Tensorflow-note-线性回归
- tensorflow 一元线性回归
- 【华为OJ】简单密码破解
- Java规范
- JSONP是如何工作的?
- echarts自定义showlading()样式和文本
- Angular2 gotchas: Double binding within Form
- Tensorflow - Tutorial (1) : 线性回归
- ubuntu、centos、debian镜像的下载地址
- Windows Mysql乱码
- java 相关环境变量的配置(jdk、tomcat、maven)
- nodeJS+express+CKeditor 上传图
- 【mybatis】mybatis的逆向工程使用步骤
- 分布式系统的CAP
- 客户端到服务器端的通信过程及原理
- STL容器