美文网首页
Tensor使用笔记

Tensor使用笔记

作者: 独行的卡卡 | 来源:发表于2020-03-05 23:09 被阅读0次

Tensor长度扩张

在进行模型结构设计的时,我们时常需要将一个变长的Tensor通过扩张来与另一个Tensor维度对齐,进而方便下一步的计算。这个时候就可以使用tf.tile()来进行Tensor的复制性扩张。

import tensorflow as tf

x = tf.constant(['a'], name='x')
y = tf.tile(x, [3], name='y')
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(y.eval())

Output:

[b'a' b'a' b'a']

Tensor打印

直接打印Tensor

在初学TensorFlow时,我们通常需要频繁的编写Demo以及打印Tensor来促使我们快速了解TensorFlow。但是与普通编程框架不同,TensorFlow大体上属于声明式编程,它的基础数据单元Tensor无法直接通过print()进行打印。如下代码将会输出该Tensor的结构而非内容:

import tensorflow as tf

a = tf.constant(["Hello World"])

print(a)

Output:

Tensor("Const:0", shape=(1,), dtype=string)

在TensorFlow中,如果我们希望简单的打印某个常量的内容,我们可以在Session初始化完毕后通过Tensor的eval()函数来进行获取。

import tensorflow as tf

a = tf.constant(["Hello World"])

with tf.Session() as sess:
    print(a.eval())

Output:

[b'Hello World']

更进一步,当我们试图采用上述方式试图打印某个变量的内容时:

import tensorflow as tf

a = tf.get_variable(name='a', dtype=tf.string, initializer=["Hello World"])

with tf.Session() as sess:
    print(a.eval())

将会产生如下异常:

Instructions for updating:
Colocations handled automatically by placer.
2020-03-05 23:29:28.889473: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "/Users/tan/anaconda2/envs/tf36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/Users/tan/anaconda2/envs/tf36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/Users/tan/anaconda2/envs/tf36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value a
     [[{{node _retval_a_0_0}}]]
...

正如上文所述,TensorFlow大体上来说属于声明式编程框架,对于Tensor变量,虽然我们设置类初始值,我们仍应当在其在Session中初始化之后才能进行各类操作:

import tensorflow as tf

a = tf.get_variable(name='a', dtype=tf.string, initializer=["Hello World"])
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(a.eval())

Output:

[b'Hello World']

获取Tensor的Shape

在使用TensorFlow进行建模的过程中,我们经常会需要获取一个Tensor的Shape以用于比如构建一个新的Tensor等逻辑。通常,我们有以下两种方式获取一个Tensor的Shape:

import tensorflow as tf

x = tf.constant(['a,b,c,d,e'], name='x')

x_shape = x.get_shape()
print(x_shape)
x_shape = tf.shape(x)
print(x_shape)

Output:

(1,)
Tensor("Shape:0", shape=(1,), dtype=int32)

从Output中我们不难发现,两种形式返回的数据是截然不同的,当我们希望使用Shape处理普通实现逻辑时,我们应当采用第一种方式;当我们希望使用Shape进行TensorFlow计算时,我们应当采用第二种方式。毕竟TensorFlow中的数据计算,大都是Tensor格式。

相关文章

  • Tensor使用笔记

    Tensor长度扩张 在进行模型结构设计的时,我们时常需要将一个变长的Tensor通过扩张来与另一个Tensor维...

  • keras, pytorch,tf 的学习笔记

    学习笔记 assert 加judge 判断式 tensor.item() 取一个元素 tensor.i...

  • PyTorch 学习笔记

    Tensor 在 PyTorch 中 Tensor 代替了 NumPy 中的 Array,且可以使用 GPU ...

  • tf中tensor和numpy

    tensor和numpy 使用tf.*定义的都是tensor,包括variable和placeholder以及tf...

  • Tensor2Tensor使用入门

    1. 简介 Tensor2Tensor是google出品的一个神仙级工具包,能大大简化类似模型的开发调试时间。在众...

  • 张量变换

    注意:使用Tensor参数的函数也可以接受tf.convert_to_tensor接受的任何内容。 接口列表整理 ...

  • torch.Tensor常用函数

    torch.Tensor 本笔记引用自PyTorch中文文档 torch.Tensor是一种包含单一数据类型元素的...

  • #Tensor 阅读笔记

    Tensor 阅读笔记 @(编程笔记)[TensonFlow] TensorFlow是一个非常强大的用来做大规模数...

  • tensorflow:理解 rank, shape, type

    tensorflow 使用一种叫 tensor 的数据结构去展示所有的数据,我们可以把 tensor 看成是 n ...

  • TF2 基础 (2) : Tensor 介绍

    本文是对官方文档[https://www.tensorflow.org/guide/tensor] 的学习笔记。 ...

网友评论

      本文标题:Tensor使用笔记

      本文链接:https://www.haomeiwen.com/subject/pgjrrhtx.html