像使用NumPy一样使用TensorFlow
1. TensorFlow基础
TensorFlow的API围绕张量(tensors)展开,张量在不同操作之间流动,这也是TensorFlow名字的由来。张量与NumPy的 ndarray 非常相似,通常是多维数组,也可以是标量(如42)。在创建自定义成本函数、自定义指标、自定义层等时,张量非常重要。
1.1 创建和操作张量
可以使用 tf.constant() 创建张量。例如,创建一个2行3列的浮点矩阵:
import tensorflow as tf
t = tf.constant([[1., 2., 3.], [4., 5., 6.]]) # matrix
print(t)
输出:
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)>
和 ndarray 一样, tf.Tensor 有形状和数据类型:
print(t.shape)
print(t.dtype)
输出:
TensorShape([2
TensorFlow与NumPy的对比使用
超级会员免费看
订阅专栏 解锁全文
1013

被折叠的 条评论
为什么被折叠?



