tensorflow 示例解析及结果展示

Python 程序生成了一些三维数据, 然后用一个平面拟合它.
import tensorflow as tf
import numpy as np
x_data = np.float32(np.random.rand(2,100)) #create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
y_data = np.dot([0.100, 0.200], x_data) + 0.300 # Dot product of two arrays


b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) #return an array of the given shape and the value is between -0.1 and 1.0
y= tf.matmul(W, x_data) + b 


loss = tf.reduce_mean(tf.square( y - y_data)) # get the value of mean
print loss
optimizer = tf.train.GradientDescentOptimizer(0.5)  #0.5 is a learning rate
train = optimizer.minimize(loss)




init = tf.initialize_all_variables()
sess = tf.Session() #Launch the graph in a session.
sess.run(init)  #Evaluate the tensor `c`


for step in xrange(0, 401):
    sess.run(train)
    if step%20 == 0:
        print step, sess.run(W), sess.run(b)


0 [[ 0.26362666  0.36850226]] [ 0.27107275]
20 [[ 0.14066522  0.23561597]] [ 0.25837913]
40 [[ 0.11415388  0.21148048]] [ 0.28603336]
60 [[ 0.10485236  0.20377406]] [ 0.29530376]
80 [[ 0.10165044  0.20125468]] [ 0.29841915]
100 [[ 0.10055901  0.20041972]] [ 0.29946756]
120 [[ 0.1001889   0.20014086]] [ 0.29982063]
140 [[ 0.10006375  0.20004736]] [ 0.29993957]
160 [[ 0.1000215   0.20001593]] [ 0.29997963]
180 [[ 0.10000724  0.20000535]] [ 0.29999316]
200 [[ 0.10000244  0.20000181]] [ 0.29999769]
220 [[ 0.10000083  0.20000061]] [ 0.29999921]
240 [[ 0.10000027  0.20000021]] [ 0.29999974]
260 [[ 0.1000001   0.20000009]] [ 0.29999989]
280 [[ 0.1000001   0.20000009]] [ 0.29999989]
300 [[ 0.1000001   0.20000009]] [ 0.29999989]
320 [[ 0.1000001   0.20000009]] [ 0.29999989]
340 [[ 0.1000001   0.20000009]] [ 0.29999989]
360 [[ 0.1000001   0.20000009]] [ 0.29999989]
380 [[ 0.1000001   0.20000009]] [ 0.29999989]
400 [[ 0.1000001   0.20000009]] [ 0.29999989]


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值