tensorflow: Shapes and Shaping 探究

定义

Tensor Transformations - Shapes and Shaping: TensorFlow provides several operations that you can use to determine the shape of a tensor and change the shape of a tensor.


tensorflow提供了一些操作,让用户可以定义和修改tensor的形状


常用API

tf.shape

  以tensor形式,返回tensor形状。

tf.shape(input, name=None, out_type=tf.int32)

import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
[2 2 3]

  另一种方法也可以的到类似答案:

import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
print (t.shape)
(2, 2, 3)

tf.size

  以tensor形式,返回tensor元素总数。

tf.size(input, name=None, out_type=tf.int32)

import tensorflow as tf

t = tf.ones(shape=[2, 5, 10], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.size(t)))
100

tf.rank

  以tensor形式,返回tensor阶数。

tf.rank(input, name=None)

import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(tf.rank(t)))
3

tf.reshape

  以tensor形式,返回重新被塑形的tensor。

tf.reshape(tensor, shape, name=None)

import tensorflow as tf

t = tf.constant(value=[1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(t))
    print
    print (sess.run(tf.reshape(t, [3, 3])))
[1 2 3 4 5 6 7 8 9]

[[1 2 3]
 [4 5 6]
 [7 8 9]]

tf.squeeze

  以tensor形式,返回移除指定维后的tensor。

tf.squeeze(input, axis=None, name=None, squeeze_dims=None)

  • axis=None 时:
    Removes dimensions of size 1 from the shape of a tensor.
    将tensor中 维度为1所有维 全部移除
  • axis=[2, 4] 时:
    将tensor中 维度为1第2维第4维 移除
import tensorflow as tf

t = tf.ones(shape=[1, 2, 1, 3, 1, 1], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
    print
    print (sess.run(tf.shape(tf.squeeze(t))))
    print
    print (sess.run(tf.shape(tf.squeeze(t, axis=[2, 4]))))
[1 2 1 3 1 1]

[2 3]

[1 2 3 1]

tf.expand_dims

  以tensor形式,返回插入指定维后的tensor。

tf.expand_dims(input, axis=None, name=None, dim=None)

import tensorflow as tf

t = tf.ones(shape=[2, 3, 5], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 0))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 1))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 2))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 3))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, -1))))
[2 3 5]

[1 2 3 5]

[2 1 3 5]

[2 3 1 5]

[2 3 5 1]

[2 3 5 1]


"JavaScript engine fundamentals: Shapes and Inline Caches" 是指JavaScript引擎的基本概念:Shapes(形状)和Inline Caches(内联缓存)。 1. Shapes(形状):在JavaScript引擎中,每个对象都有一个形状,用于描述对象的结构和属性。形状包含对象的属性名称、偏移量和其他相关信息。当创建一个新对象时,引擎会根据对象的属性来确定其形状,并将该形状与对象关联起来。这样,在后续对同一形状的对象进行操作时,引擎可以更高效地执行。 形状对于优化JavaScript代码的执行非常重要。引擎可以根据对象的形状来进行内存布局和属性访问的优化,提高代码的执行效率。如果对象的形状发生变化(例如添加或删除属性),引擎会为该对象创建一个新的形状,并相应地调整相关操作。 2. Inline Caches(内联缓存):内联缓存是一种用于加速函数调用和属性访问的技术。当使用JavaScript代码调用函数或访问对象属性时,引擎会在编译阶段生成内联缓存,将函数调用或属性访问与特定的对象关联起来。 内联缓存可以避免每次函数调用或属性访问时都进行动态查找和解析。引擎会根据对象的形状和属性的特征,将函数调用或属性访问的地址直接嵌入到代码中。这样,在后续对同一对象进行函数调用或属性访问时,引擎可以直接使用内联缓存中的地址,避免了额外的查找和解析开销。 通过使用Shapes和Inline Caches,JavaScript引擎可以提高代码的执行效率和性能,加快函数调用和属性访问的速度。这些基本概念是JavaScript引擎优化和执行的关键部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值