tensorflow 学习笔记(2)-basic_example
来源:互联网 发布:苹果mac爱奇艺视频 编辑:程序博客网 时间:2024/06/06 16:26
tensorflow 学习笔记(2)-basic_example
- linear_regression
import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as nptrain_epochs = 1000 # the step of epochdiaplay_step = 50learn_rate = 0.01 # the learning rate of GradientDescentOptimizer# the data set of trainingtrain_x=np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1])train_y=np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3])# tf graph inputX = tf.placeholder("float")Y = tf.placeholder("float")n_sample = train_x.shape[0]# set model weightsW = tf.Variable(np.random.randn(), name = "weight")b = tf.Variable(np.random.randn(), name = "bias")# construct a linear modelpred = tf.add(tf.multiply(X, W), b)# MSEcost = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_sample)optimizer=tf.train.GradientDescentOptimizer(learn_rate).minimize(cost)init = tf.global_variables_initializer()# Initializing the variableswith tf.Session() as sess: sess.run(init) for epoch in range(train_epochs): for (x, y) in zip(train_x, train_y): sess.run(optimizer, feed_dict = {X: x, Y: y}) if (epoch + 1) % 50 == 0: c = sess.run(cost, feed_dict = {X: train_x, Y: train_y}) print("Epoch: ", epoch + 1, "cost = ", c , "W = ", sess.run(W), "b = ", sess.run(b)) print("Optimization Finished!") train_cost = sess.run(cost, feed_dict = {X: train_x, Y: train_y}) print("Training cost = ", train_cost, "W = ", sess.run(W), "b = ", sess.run(b),'\n') # Graphic display plt.plot(train_x, train_y, 'ro', label = "Original data") plt.plot(train_x, train_x * sess.run(W) + sess.run(b), label = "Fitted line") plt.legend() plt.show() # Testing example, test_x = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1]) test_y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03]) print("Testing... (Mean square loss Comparison)") test_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2))/(2 * test_x.shape[0]), feed_dict = {X:test_x,Y:test_y}) print("Testing cost: ", test_cost) print("Absolute mean square loss difference: ",abs( test_cost - train_cost)) # Graphic display plt.plot(test_x, test_y, "bo", label = "Testing data") plt.plot(test_x, test_x * sess.run(W) + sess.run(b),label = "Fitted line") plt.legend() plt.show()
阅读全文
0 0
- tensorflow 学习笔记(2)-basic_example
- tensorflow 学习笔记(3)-basic_example
- tensorflow 学习笔记(4)-basic_example
- Tensorflow学习笔记(2)
- Tensorflow学习笔记2
- TensorFlow学习笔记2
- tensorflow 学习笔记 2
- Tensorflow学习笔记(2)
- TensorFlow实战学习笔记2
- TensorFlow学习笔记:2、TensorFlow超简单入门程序
- TensorFlow学习笔记(2)--运行MNIST例子
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记2:构建CNN模型
- tensorflow 学习笔记2 session与Variable
- Tensorflow学习笔记(2)-基本运算
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记2:基本运算
- Application.ProcessMessages
- 手机管理工具类 → AppPhoneMgr
- 设计模式--命令模式
- Androdid中ExecutorService内存泄露原因分析
- 【实战】嵌入式linux修改内核启动的LOGO界面
- tensorflow 学习笔记(2)-basic_example
- iOS 微信登录集成让人抓不着头脑的问题
- 每日一学(八) Android动画---Tween动画的xml实现
- Python bug整理
- 信息熵、信息增益与信息增益率
- 你真的了解html代码的事件,离开焦点和聚焦焦点的动作的意思吗?onblur、focus?它们是点击才可以触发,而不是鼠标悬停,鼠标悬停是hover
- 【0006】安装谷歌浏览器时,一直处于联网状态
- Ubuntu常见问题解决方法
- C++入门基础知识