首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Tensor,与Numpy比较,Constant

本教程参考stanford.edu-cs20si

01

Operations分类预览

02

Tensor

10-d tensor, or "scalar"

t_0 = 19

tf.zeros_like(t_0)# ==> 0

tf.ones_like(t_0)# ==> 1

21-d tensor, or "vector"

t_1 = ['apple', 'peach', 'banana']#==>['' '' '']

32x2 tensor, or "matrix"

t_2 = [[True, False, False], [False, False, True], [False, True, False]]

tf.zeros_like(t_2)# ==> 2x2 tensor, all elements are False

tf.ones_like(t_2)# ==> 2x2 tensor, all elements are True

03

TensorFlow和Numpy

TensorFlow 和 Numpy能做到无缝衔接,例如:

tf.int32 == np.int32# True

但是,将来tensorflow和numpy可能兼容性没有现在这么好。

可以传递Numpy对象给TensorFlow ops

tf.ones([2, 2], np.float32) # [[1.0 1.0], [1.0 1.0]]

不要传递python的原生对象给Tensorflow,因为Tensorflow必须推断python的类型。

04

Constant

tf.constant(value, dtype=None, shape=None,name='Const', verify_shape=False)

b = tf.constant([[0, 1], [2, 3]], name="b")

tf.ones(shape, dtype=tf.float32, name=None)

tf.linspace(10.0, 13.0, 4)#==> [10.0 11.0 12.0 13.0]

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype= tf.float32, seed=None, name=None)

tf.set_random_seed(seed)

a = tf.constant([3, 6])

b = tf.constant([2, 2])

tf.add(a, b)# >> [5 8]

05

What's wrong with constant?

Constants 存储在 graph definition中,当 constants很大时,加载图就会变得非常昂贵。

什么场合下用constant?

仅仅用primitive types为constant,用variables or readers 来缓存占用内存更大的数据。

明天推送tf.Variable等,为什么是tf.Variable? tf.constant,为什么不写为 tf.Constant? 关于这个问题,接下来回答。

《实例》阐述算法,通俗易懂,助您对算法的理解达到一个新高度。包含但不限于:基本算法,机器学习,深度学习,Kaggle实战,Spark和Tensorflow等。期待您的到来!

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180201G00Z7D00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券