import tensorflow as tf
import numpy as np
#生成2维的100个0-1的随机数
x_data = np.float32(np.random.rand(2,100))
计算内积,x_data一共100个值,每个值都是2维的向量,用[0.1,0.2]和每一个向量计算数量积,然后加起来
x_data=

[[ 0.27559635 0.35930911 0.77030689 0.71818703 0.49122271 0.43190494
0.644674 0.32078174 0.64300877 0.81156862 0.30409896 0.56623858
0.97026539 0.60468578 0.34658566 0.86189109 0.5117926 0.46588144
0.27591956 0.49946061 0.47744861 0.80954593 0.10624354 0.80820572
0.24388497 0.75275064 0.11153043 0.01847375 0.70894343 0.86511648
0.05999189 0.94242656 0.35399687 0.53131646 0.80690706 0.28856653
0.2685678 0.86655128 0.49340782 0.84330899 0.26634833 0.94808429
0.32813659 0.60548925 0.37914801 0.93819922 0.16300483 0.28346273
0.25481561 0.59326059 0.64435166 0.71002674 0.47835174 0.16478723
0.83618289 0.89197201 0.77212745 0.83254766 0.1176443 0.45999372
0.17508474 0.99125117 0.19204263 0.88548642 0.16025347 0.58622926
0.14167576 0.6784007 0.77483946 0.90998834 0.79064935 0.76124579
0.11255023 0.63665706 0.84133714 0.01388079 0.7478959 0.34365693
0.66228282 0.56429321 0.97419363 0.46427366 0.71639329 0.67420006
0.72603422 0.35240087 0.43481046 0.04478104 0.83910578 0.03852031
0.63502115 0.54327303 0.05116724 0.75744271 0.23107423 0.25379685
0.1543453 0.65174055 0.60880935 0.41448417]
[ 0.53852242 0.24170624 0.51843584 0.41295227 0.22256 0.05581184
0.42046902 0.49984431 0.83284378 0.44403863 0.43289256 0.04277489
0.97858369 0.32724616 0.69393569 0.80431139 0.20591183 0.10109164
0.07850602 0.61202133 0.04476574 0.99151891 0.1714984 0.83303201
0.1769124 0.97038633 0.71856993 0.97560126 0.80447757 0.48544171
0.95302963 0.21392477 0.72407377 0.32749009 0.87037027 0.23632777
0.09344739 0.49172315 0.27751547 0.3205907 0.42732051 0.0938397
0.65851027 0.75118226 0.30484736 0.69336241 0.96847987 0.08743072
0.5060789 0.128803 0.12509818 0.77400607 0.99729323 0.25656971
0.28877217 0.26310787 0.22661451 0.38361222 0.64689898 0.26246113
0.41836309 0.96913052 0.34863174 0.26865834 0.96321774 0.02932074
0.51096094 0.93037766 0.3862699 0.77660888 0.50103205 0.35242727
0.96469277 0.71796703 0.90261179 0.9502635 0.2554118 0.41087386
0.13807607 0.10848427 0.27238116 0.81126028 0.35296583 0.5478636
0.35726911 0.54948765 0.3683508 0.57419771 0.28177765 0.80673724
0.14792147 0.5277251 0.17725706 0.5937981 0.86571193 0.09969555
0.92383957 0.95939624 0.76108253 0.30095646]]

y_data = 

[ 0.43526412 0.38427216 0.48071786 0.45440916 0.39363427 0.35435286
0.4485612 0.43204704 0.53086963 0.46996459 0.41698841 0.36517884
0.59274328 0.42591781 0.4734457 0.54705139 0.39236163 0.36680647
0.34329316 0.47235033 0.35669801 0.57925838 0.34492403 0.54742697
0.35977098 0.56935233 0.45486703 0.49696763 0.53178986 0.48359999
0.49660512 0.43702761 0.48021444 0.41862966 0.55476476 0.37612221
0.34554626 0.48499976 0.40484388 0.44844904 0.41209894 0.41357637
0.46451571 0.51078538 0.39888427 0.53249241 0.50999646 0.34583242
0.42669734 0.38508666 0.3894548 0.52580389 0.54729382 0.36779267
0.44137272 0.44181877 0.42253565 0.45997721 0.44114423 0.3984916
0.40118109 0.59295122 0.38893061 0.44228031 0.50866889 0.36448708
0.41635976 0.5539156 0.45473793 0.54632061 0.47927135 0.44661003
0.50419358 0.50725911 0.56465607 0.49144078 0.42587195 0.41654046
0.3938435 0.37812617 0.45189559 0.50867942 0.4422325 0.47699273
0.44405724 0.44513762 0.41715121 0.41931765 0.44026611 0.46519948
0.39308641 0.45987232 0.34056814 0.49450389 0.49624981 0.34531879
0.50020244 0.5570533 0.51309744 0.40163971]

y_data = np.dot([0.100,0.200],x_data) + 0.300
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1,2],-1.0,1.0))
y = tf.matmul(W,x_data) + b
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in np.arange(0,201):
sess.run(train)
if step % 20 == 0:
print(step,sess.run(W),sess.run(b)) 结果如下所示

>>> import testTensorflow
0 [[ 0.61865866 0.54848659]] [-0.35512698]
20 [[ 0.27226886 0.31722128]] [ 0.14856057]
40 [[ 0.15020847 0.23340638]] [ 0.25623968]
60 [[ 0.11460328 0.20955895]] [ 0.28735051]
80 [[ 0.10424114 0.20274341]] [ 0.29634258]
100 [[ 0.10123044 0.20078911]] [ 0.2989423]
120 [[ 0.10035671 0.20022736]] [ 0.29969406]
140 [[ 0.10010336 0.20006558]] [ 0.2999115]
160 [[ 0.10002995 0.20001893]] [ 0.29997438]
180 [[ 0.10000868 0.20000547]] [ 0.29999259]
200 [[ 0.10000249 0.20000155]] [ 0.29999787]
>>>

48、tensorflow入门二,线性模型的拟合的更多相关文章

  1. TensorFlow 入门之手写识别(MNIST) softmax算法 二

    TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...

  2. (转)TensorFlow 入门

        TensorFlow 入门 本文转自:http://www.jianshu.com/p/6766fbcd43b9 字数3303 阅读904 评论3 喜欢5 CS224d-Day 2: 在 Da ...

  3. #tensorflow入门(1)

    tensorflow入门(1) 关于 TensorFlow TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操 ...

  4. tensorflow入门(1):构造线性回归模型

    今天让我们一起来学习如何用TF实现线性回归模型.所谓线性回归模型就是y = W * x + b的形式的表达式拟合的模型. 我们先假设一条直线为 y = 0.1x + 0.3,即W = 0.1,b = ...

  5. TensorFlow入门:线性回归

    随机.mini-batch.batch(见最后解释) 在每个 epoch 送入单个数据点.这被称为随机梯度下降(stochastic gradient descent).我们也可以在每个 epoch ...

  6. TensorFlow入门(五)多层 LSTM 通俗易懂版

    欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-09 前言: 根据我本人学习 TensorFlow 实现 LSTM 的经 ...

  7. [译]TensorFlow入门

    TensorFlow入门 张量(tensor) Tensorflow中的主要数据单元是张量(tensor), 一个张量包含了一组基本数据,可以是列多维数据.一个张量的"等级"(ra ...

  8. 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)

    http://blog.csdn.net/Jerr__y/article/details/70471066 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @cr ...

  9. 1 TensorFlow入门笔记之基础架构

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

随机推荐

  1. 返回变量的类型VarType函数

    'vbEmpty 0 Empty(未初始化)'vbNull 1 Null(无有效数据)'vbInteger 2 整数'vbLong 3 长整数'vbSingle 4 单精度浮点数'vbDouble 5 ...

  2. xshell 注册码

    Xshell 5 注册码: 101210-450789-147200Xftp 5 注册码:101210-450789-147200 Xmanager 5 注册码:101210-450789-14720 ...

  3. spring+ibatis问题1—— 程序报错:java.sql.SQLException: Io 异常: Connection reset by peer, socket write error; ”或“java.sql.SQLException 关闭的连接”异常

    转自:http://blog.sina.com.cn/s/blog_1549fb0710102whz2.html spring+ibatis程序测试时报错:java.sql.SQLException: ...

  4. centos7下用yum安装mysql5.7

    1.安装mysql源 下载地址:http://dev.mysql.com/downloads/repo/yum/ 下载之后用yum安装:yum localinstall -y xx.noarch.rp ...

  5. MySQL-Front 出现“程序注册时间到期 程序将被限制模式下运行”解决方式

    MySQL-Front 出现“程序注册时间到期 程序将被限制模式下运行”解决方式 在用mysql-front的时候遇到显示:程序注册时间到期程序将被限制模式下运行.可以在“帮助”菜单下的点“登记”-- ...

  6. 第一次使用cnblogs

    第一次使用!!!!!留名纪念下!!!!!!!!!!!

  7. 博客word测试

    博客word测试 博客word测试 from __future__ import division, print_functionDOCLINES = (__doc__ or '').split(&q ...

  8. hadoop中,combine、partition、shuffle作用分别是什么?

    combine和partition都是函数,中间的步骤应该只有shuffle! combine分为map端和reduce端,作用是把同一个key的键值对合并在一起,可以自定义的.combine函数把一 ...

  9. 抢红包算法 java

    抢红包的需求分析 抢红包的场景有点像秒杀,但是要比秒杀简单点.因为秒杀通常要和库存相关.而抢红包则可以允许有些红包没有被抢到,因为发红包的人不会有损失,没抢完的钱再退回给发红包的人即可.另外像小米这样 ...

  10. ubuntu环境下jdk安装及jenkins安装

    本文内容参考http://jingyan.baidu.com/article/c33e3f48a3365dea15cbb5c9.html 1 jdk下载 安装 http://www.oracle.co ...