1. 基本类型 Tensor
Tensorflow的基本数据类型为tf.Tensor
支持
tf.add
tf.square
-
tf.reduce_sum
求和 -
tf.matmul
矩阵相乘
这些操作产生tf.Tensor
的对象, 可以使用.shape
和dtype
表示维数和尺度.
tf.Tensor
和NumPy的区别在于
- Tensor可以由GPU, TPU运算.
- Tensor 是不变的(immutable).
2. NumPy 和 Tensor
NumPy 和 Tensor两者之间的转换可以自动转换.
import numpy as np
ndarray = np.ones([3, 3])
print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.multiply(ndarray, 42)
print(tensor)
print("And NumPy operations convert Tensors to numpy arrays automatically")
print(np.add(tensor, 1))
print("The .numpy() method explicitly converts a Tensor to a numpy array")
print(tensor.numpy())
Dataset
tf.data.Dataset 构建将数据输送给模型的pipe.
网友评论