TensorFlow2.0教程-张量及其操作
本文地址:https://blog.csdn.net/qq_31456593/article/details/90178004
完整tensorflow2.0教程代码请看tensorflow2.0:中文教程tensorflow2_tutorials_chinese(欢迎star)
更多TensorFlow2.0 入门教程请持续关注本博客:https://blog.csdn.net/qq_31456593/article/details/88606284
导入TensorFlow
运行tensorflow程序,需要导入tensorflow模块。
从TensorFlow 2.0开始,默认情况下会启用eager模式执行。 这为TensorFlow提供了一个更加互动的前端节。
from __future__ import absolute_import, division, print_function
import tensorflow as tf
1 Tensors
张量是一个多维数组。 与NumPy ndarray对象类似,tf.Tensor对象具有数据类型和形状。 此外,tf.Tensors可以驻留在加速器内存中(如GPU)。 TensorFlow提供了丰富的操作库(tf.add,tf.matmul,tf.linalg.inv等),它们使用和生成tf.Tensors。 这些操作会自动转换原生Python类型,例如:
print(tf.add(1,2))
print(tf.add([3,8], [2,5]))
print(tf.square(6))
print(tf.reduce_sum([7,8,9]))
print(tf.square(3)+tf.square(4))
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([ 5 13], shape=(2,), dtype=int32)
tf.Tensor(36, shape=(), dtype=int32)