博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
博客存档TensorFlow入门一 1.4编程练习
阅读量:4679 次
发布时间:2019-06-09

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

 

 

1 import tensorflow as tf 2 import numpy 3 import matplotlib.pyplot as plt 4 #from sklearn.model_selection import train_test_split 5 rng = numpy.random 6  7 # Parameters 8 learning_rate = 0.01 9 training_epochs = 200010 display_step = 5011 12 # Training Data13 train_X = numpy.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])14 train_Y = numpy.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])15 n_samples = train_X.shape[0]16 17 # tf Graph Input18 X = tf.placeholder("float")19 Y = tf.placeholder("float")20 21 # Create Model22 23 # Set model weights24 W = tf.Variable(rng.randn(), name="weight")25 b = tf.Variable(rng.randn(), name="bias")26 27 # Construct a linear model28 activation = tf.add(tf.mul(X, W), b)29 30 # Minimize the squared errors31 cost = tf.reduce_sum(tf.pow(activation-Y, 2))/(2*n_samples)   #L2 loss32 33  #reduce_sum:把里面的平方求和34  # pow(x,y):这个是表示x的y次幂。35 36 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)37 38 #Gradient descent39 40 # Initializing the variables41 init = tf.initialize_all_variables()42 43 # Launch the graph44 with tf.Session() as sess:45     sess.run(init)46 47     # Fit all training data48     for epoch in range(training_epochs):49         for (x, y) in zip(train_X, train_Y):50             sess.run(optimizer, feed_dict={X: x, Y: y})51               #zip:对应的元素打包成一个个元组52         #Display logs per epoch step53         if epoch % display_step == 0:54             print("Epoch:", '%04d' % (epoch+1), "cost=", \55                 "{:.9f}".format(sess.run(cost, feed_dict={X: train_X, Y:train_Y})), \56                 "W=", sess.run(W), "b=", sess.run(b))57 58     print("Optimization Finished!")59     print("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), \60           "W=", sess.run(W), "b=", sess.run(b))61 62     #Graphic display63     plt.plot(train_X, train_Y, 'ro', label='Original data')64     plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')65     plt.legend()66     plt.show()

 

转载于:https://www.cnblogs.com/captain-dl/p/9270926.html

你可能感兴趣的文章
python文件操作
查看>>
java虚拟机的运行原理
查看>>
配置Oracle10g即时客户端plsql的配置
查看>>
关于设计:Actionscript 有关鼠标事件笔记2
查看>>
【LOJ】#2538. 「PKUWC2018」Slay the Spire
查看>>
Helper
查看>>
架构设计系列-前端模式的后端(BFF)翻译PhilCalçado
查看>>
常用dos命令
查看>>
Redis学习第四课:Redis List类型及操作
查看>>
满血复活前的记录(持续更新ing)
查看>>
vs2008使用过AnkhSVN后不能绑定到vss的问题解决
查看>>
在vue中使用sass
查看>>
IPv4组播通信原理
查看>>
Sql Server 新的日期类型
查看>>
“我爱淘”冲刺阶段Scrum站立会议8
查看>>
js获取元素class的几种方法
查看>>
delphi 枚举类型与字符串的转换
查看>>
UVA-10689 Yet another Number Sequence (矩阵二分幂模板)
查看>>
element自定义表单验证
查看>>
Mysql 存储引擎的区别和比较
查看>>