除了AutoGraph还有 ...... 这两天TensorFlow真是会搞事情

15 篇文章 1 订阅
1 篇文章 0 订阅

640?wx_fmt=jpeg

编辑 | Jane

出品 | 人工智能头条(公众号ID:AI_Thinker)

 

【人工智能头条导读】昨天谷歌发布了 TensorFlow 的一个新工具 —— AutoGraph,可以将 Python 代码快速转化到 TensorFlow 的体系里。目前测试来看,这个工具简化了构图流程,加强了 TensorFlow 调用 Python 时的性能。昨天晚上 TensorFlow 又宣布了下一代移动视觉应用支持的新版本 —— MobileNetV2。TensorFlow 官方教程中又不断新增V 1.9 的 eager execution 实例。这几天 TensorFlow 可真是一点没闲着呢。

 

 

AutoGraph

 

我们在 TensorFlow 中构建的计算图比较难理解,尤其是涉及复杂模型的场景,使用 Python 的一些语句,如 if 、while 或接受结构化输入的 print ( ) s时都会有一种无力感。而新工具 AutoGraph 的作用就是自动解决这个问题。

 

使用 autograph.convert() 装饰器来装饰函数,AutoGraph 将自动生成图可用的代码。

 

 

@autograph.convert()
def fizzbuzz(num):
  if num % 3 == 0 and num % 5 == 0:
      print('FizzBuzz')
  elif num % 3 == 0:
      print('Fizz')
  elif num % 5 == 0:
      print('Buzz')
  else:
      print(num)
  return num


with tf.Graph().as_default():
  # The result works like a regular op: takes tensors in, returns tensors.
  # You can inspect the graph using tf.get_default_graph().as_graph_def()
  num = tf.placeholder(tf.int32)
  result = fizzbuzz(num)
  with tf.Session() as sess:
    for n in range(10,16):
      sess.run(result, feed_dict={num:n})

 

源码中也是给出了 6 个示例,比如:

 

Assert

 

 

@autograph.convert()
def f(x):
  assert x != 0'Do not pass zero!'
  return x * x

with tf.Graph().as_default():  
  with tf.Session():
    try:
      print(f(tf.constant(0)).eval())
    except tf.errors.InvalidArgumentError as e:
      print('Got error message:\n    %s' % e.message)

 

Print

 

 

@autograph.convert()
def f(n):
  if n >= 0:
    while n < 5:
      n += 1
      print(n)
  return n

with tf.Graph().as_default():
  with tf.Session():
    f(tf.constant(0)).eval()

 

Lists

 

 

@autograph.convert()
def f(n):
  z = []
  # We ask you to tell us the element dtype of the list
  autograph.set_element_type(z, tf.int32)

  for i in range(n):
    z.append(i)
  # when you're done with the list, stack it
  # (this is just like np.stack)
  return autograph.stack(z) 

#tf_f = autograph.to_graph(f)

with tf.Graph().as_default():  
  with tf.Session():
    print(f(tf.constant(3)).eval())

 

If

 

 

@autograph.convert()
def nearest_odd_square(x):
  if x > 0:
    x = x * x
    if x % 2 == 0:
      x = x + 1
  return x

with tf.Graph().as_default():  
  with tf.Session() as sess:
    print(sess.run(nearest_odd_square(tf.constant(4))))
    print(sess.run(nearest_odd_square(tf.constant(5))))
    print(sess.run(nearest_odd_square(tf.constant(6))))

 

While Loop

 

 

@autograph.convert()
def square_until_stop(x, y):
  while x < y:
    x = x * x
  return x

with tf.Graph().as_default():  
  with tf.Session() as sess:
    print(sess.run(square_until_stop(tf.constant(4), tf.constant(100))))

 

Break

 

 

@autograph.convert()
def argwhere_cumsum(x, threshold):
  current_sum = 0.0
  idx = 0
  for i in range(len(x)):
    idx = i
    if current_sum >= threshold:
      break
    current_sum += x[i]
  return idx

N = 10
with tf.Graph().as_default():  
  with tf.Session() as sess:
    idx = argwhere_cumsum(tf.ones(N), tf.constant(float(N/2)))
    print(sess.run(idx))

 

大家知道这个消息后也纷纷跃跃欲试,不仅尝试了官方示例,还有自己的新发现。

 

640?wx_fmt=png

via:微博用户@王咏刚

(https://weibo.com/ygwang?refer_flag=1001030103_)

 

640?wx_fmt=png

via:微博用户@tobe-陈迪豪

(https://weibo.com/tobegit3hub?profile_ftype=1&is_ori=1#_0)

 

AutoGraph 打开了构建和训练模型的新思路,虽然现在还是实验工具,不过,官方表示会尽快转移到核心的 TensorFlow 中,建议未来可以尝试添加更多的功能到 AutoGraph 中,如果广大的开发爱好者们有自己的心得与建议也可以加入进来,不断完善。

 

GitHub 地址:

https://github.com/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb

 

Colab 链接:

https://colab.research.google.com/github/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb

 

 

MobileNetV2

 

昨天,TensorFlow 很高兴地宣布——发布 MobileNetV2 ,它将为下一代移动视觉应用提供支持。

 

去年,TensorFlow 引入了面向移动设备设计的通用型计算机视觉神经网络 ——MobileNetV1,可支持分类和检测等功能。在个人移动设备上运行深度网络可以提升用户体验,并允许随时随地访问,并且在安全性、隐私和能耗方面同样具有优势。随着让用户与现实世界实时交互的新应用的不断出现,对更高效神经网络的需求也逐渐增加。

 

MobileNetV2 在 MobileNetV1 的基础上进行了重大改进,并推动了移动视觉识别技术的发展,包括分类、对象检测和语义分割。

 

640?wx_fmt=jpeg

MobileNetV2 架构概览

蓝色块表示上面所示的复合卷积构建块

   

640?wx_fmt=jpeg

MobileNetV2 提高了速度(缩短了延迟时间)并提高了 ImageNet Top 1 的准确度

 

640?wx_fmt=png

在检测方面,与新引入的 SSDLite 搭配使用时,在实现相同准确性的情况下,新模型的速度要比 MobileNetV1 快大约 35%。

 

640?wx_fmt=png

为实现设备上语义分割,在近期宣布的 DeepLabv3 简化版中采用 MobileNetV2 作为特征提取器。与使用 MobileNetV1 作为特征提取器的性能相似,但前者的参数数量减少 5.3 倍,乘加运算数量减少 5.2 倍。

 

 更多内容大家可以参考:

1.MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Applications, Howard AG, Zhu M, Chen B, Kalenichenko D, Wang W, Weyand T, Andreetto M, Adam H, arXiv:1704.04861, 2017.

 

2.MobileNetV2:Inverted Residuals and Linear Bottlenecks, Sandler M, Howard A, Zhu M, Zhmoginov A, Chen LC. arXiv preprint. arXiv:1801.04381, 2018.

 

3.Rethinking Atrous Convolution for Semantic Image Segmentation, Chen LC, Papandreou G, Schroff F, Adam H. arXiv:1706.05587, 2017.

 

4.Speed/accuracy trade-offs for modern convolutional object detectors, Huang J, Rathod V, Sun C, Zhu M, Korattikara A, Fathi A, Fischer I, Wojna Z, Song Y, Guadarrama S, Murphy K, CVPR 2017.

 

5.Deep Residual Learning for Image Recognition, He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. arXiv:1512.03385,2015

 

 

TensorFlow官方教程:

新增1.9版eager execution实例

 

自上周新版本 V 1.9 发布以来,TensorFlow 官方教程中又不断新增 eager execution 实例,主要包括:注意力机器翻译、文本生成、图像自动描述等教程。大家又可以在周末开始一波操作了!

 

640?wx_fmt=png

640?wx_fmt=png

 

官方链接:

https://www.tensorflow.org/tutorials/

 

—【完】—

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值