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格式。