tf1.基础结构

Tensorflow 是谷歌开发的深度学习系统,用它可以很快速地入门神经网络。
它可以做分类,也可以做拟合问题,就是要把这个模式给模拟出来。

这是一个基本的神经网络的结构,有输入层,隐藏层,和输出层。
每一层点开都有它相应的内容,函数和功能。

那我们要做的就是要建立一个这样的结构,然后把数据喂进去。
把数据放进去后它就可以自己运行,TensorFlow 翻译过来就是向量在里面飞。

这个动图的解释就是,在输入层输入数据,然后数据飞到隐藏层飞到输出层,用梯度下降处理,梯度下降会对几个参数进行更新和完善,更新后的参数再次跑到隐藏层去学习,这样一直循环直到结果收敛。

因为TensorFlow是采用数据流图(data flow graphs)来计算, 所以首先我们得创建一个数据流流图, 然后再将我们的数据(数据以张量(tensor)的形式存在)放在数据流图中计算. 节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组, 即张量(tensor). 训练模型时tensor会不断的从数据流图中的一个节点flow到另一节点, 这就是TensorFlow名字的由来.

张量(tensor):

张量有多种. 零阶张量为 纯量或标量 (scalar) 也就是一个数值. 比如 [1]
一阶张量为 向量 (vector), 比如 一维的 [1, 2, 3]
二阶张量为 矩阵 (matrix), 比如 二维的 [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
以此类推, 还有 三阶 三维的 …

简单线性预测例子

import sys
print(sys.version)
'''
3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
'''
import tensorflow as tf
import numpy as np

# shape=tuple ,size=100
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3 #实际值

Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))#[1],一个数,范围从-1.0到1.0
biases = tf.Variable(tf.zeros([1]))#初始值0

y = Weights*x_data + biases#框架
loss = tf.reduce_mean(tf.square(y-y_data))#定义误差(square平方)
#梯度下降法
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer()  # 替换成这样就好
sess = tf.Session()
sess.run(init)          # Very important
for step in range(201):#训练200次
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))#越来越接近真实w和b

输出

0 [-0.01233229] [ 0.479366]
20 [ 0.04969411] [ 0.32581243]
40 [ 0.08435673] [ 0.30802673]
60 [ 0.09513554] [ 0.30249602]
80 [ 0.09848735] [ 0.30077615]
100 [ 0.09952962] [ 0.30024135]
120 [ 0.09985375] [ 0.30007505]
140 [ 0.09995452] [ 0.30002335]
160 [ 0.09998586] [ 0.30000728]
180 [ 0.09999558] [ 0.30000228]
200 [ 0.09999862] [ 0.30000073]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Baa 一个简单高效的Go web开发框架。主要有路由、中间件,依赖注入和HTTP上下文构成。Baa 不使用 ``反射``和``正则``,没有魔法的实现。快速上手安装:go get -u gopkg.in/baa.v1示例:package main import (     "gopkg.in/baa.v1" ) func main() {     app := baa.New()     app.Get("/", func(c *baa.Context) {         c.String(200, "Hello World!")     })     app.Run(":1323") }特性支持静态路由、参数路由、组路由(前缀路由/命名空间)和路由命名路由支持链式操作路由支持文件/目录服务支持中间件和链式操作支持依赖注入*支持JSON/JSONP/XML/HTML格式输出统一的HTTP错误处理统一的日志处理支持任意更换模板引擎(实现baa.Renderer接口即可)中间件[gzip](https://github.com/baa-middleware/gzip)[logger](https://github.com/baa-middleware/logger)[recovery](https://github.com/baa-middleware/recovery)[session](https://github.com/baa-middleware/session)组件(DI)[cache](https://github.com/go-baa/cache)[render](https://github.com/go-baa/render)性能测试和快速的Echo框架对比 [Echo](https://github.com/labstack/echo)> 注意:[Echo](https://github.com/labstack/echo) 在V2版本中使用了fasthttp,我们这里使用 [Echo V1](https://github.com/labstack/echo/releases/tag/v1.4) 测试。路由测试使用 [go-http-routing-benchmark] (https://github.com/safeie/go-http-routing-benchmark) 测试, 2016-02-27 更新.[GitHub API](http://developer.github.com/v3)> Baa的路由性能非常接近 Echo.BenchmarkBaa_GithubAll             30000     50984 ns/op       0 B/op       0 allocs/op BenchmarkBeego_GithubAll            3000    478556 ns/op    6496 B/op     203 allocs/op BenchmarkEcho_GithubAll           30000     47121 ns/op       0 B/op       0 allocs/op BenchmarkGin_GithubAll             30000     41004 ns/op       0 B/op       0 allocs/op BenchmarkGocraftWeb_GithubAll      3000    450709 ns/op  131656 B/op    1686 allocs/op BenchmarkGorillaMux_GithubAll       200   6591485 ns/op  154880 B/op    2469 allocs/op BenchmarkMacaron_GithubAll          2000    679559 ns/op  201140 B/op    1803 allocs/op BenchmarkMartini_GithubAll           300   5680389 ns/op  228216 B/op    2483 allocs/op BenchmarkRevel_GithubAll            1000   1413894 ns/op  337424 B/op    5512 allocs/opHTTP测试Baa:package main import ( "github.com/baa-middleware/logger" "github.com/baa-middleware/recovery" "gopkg.in/baa.v1" ) func hello(c *baa.Context) { c.String(200, "Hello, World!\n") } func main() { b := baa.New() b.Use(logger.Logger()) b.Use(recovery.Recovery()) b.Get("/", hello) b.Run(":8001") }Echo:package main import ( "github.com/labstack/echo" mw "github.com/labstack/echo/middleware" ) // Handler func hello(c *echo.Context) error { return c.String(200, "Hello, World!\n") } func main() { // Echo instance e := echo.New() // Middleware e.Use(mw.Logger()) // Routes e.Get("/", hello) // Start server e.Run(":8001") }测试结果:> Baa 在http中的表现还稍稍比 Echo 好一些。Baa:$ wrk -t 10 -c 100 -d 30 http://127.0.0.1:8001/ Running 30s test @ http://127.0.0.1:8001/   10 threads and 100 connections   Thread Stats   Avg      Stdev     Max    /- Stdev     Latency     1.92ms    1.43ms  55.26ms   90.86%     Req/Sec     5.46k   257.26     6.08k    88.30%   1629324 requests in 30.00s, 203.55MB read Requests/sec:  54304.14 Transfer/sec:      6.78MB Echo:$ wrk -t 10 -c 100 -d 30 http://127.0.0.1:8001/ Running 30s test @ http://127.0.0.1:8001/   10 threads and 100 connections   Thread Stats   Avg      Stdev     Max    /- Stdev     Latency     2.83ms    3.76ms  98.38ms   90.20%     Req/Sec     4.79k     0.88k   45.22k    96.27%   1431144 requests in 30.10s, 178.79MB read Requests/sec:  47548.11 Transfer/sec:      5.94MB案例目前使用在 健康一线 的私有项目中。手册[godoc](http://godoc.org/github.com/go-baa/baa)贡献Baa的灵感来自 [beego](https://github.com/astaxie/beego) [echo](https://github.com/labstack/echo) [macaron](https://github.com/go-macaron/macaron)- [safeie](https://github.com/safeie)、[micate](https://github.com/micate) - Author- [betty](https://github.com/betty3039) - Language Consultant- [Contributors](https://github.com/go-baa/baa/graphs/contributors)LicenseThis project is under the MIT License (MIT) See the [LICENSE](https://raw.githubusercontent.com/go-baa/baa/master/LICENSE) file for the full license text. 标签:Web框架

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值