创建张量
import tensorflow as tf
# 从列表创建
data = [[1, 2], [3, 4]]
x = tf.constant(data)
print(x)
# 特殊张量
zeros = tf.zeros([2, 3]) # 全0张量
ones = tf.ones([2, 3]) # 全1张量
rand = tf.random.normal([2, 3]) # 随机张量
# 变量(可训练参数)
w = tf.Variable(tf.random.normal([3, 2]))
张量操作
# 基本运算
x = tf.constant([1, 2, 3])
y = tf.constant([4, 5, 6])
add = tf.add(x, y) # 加法
sub = tf.subtract(x, y) # 减法
mul = tf.multiply(x, y) # 乘法
div = tf.divide(x, y) # 除法
# 矩阵乘法
A = tf.random.normal([3, 4])
B = tf.random.normal([4, 5])
C = tf.matmul(A, B) # 矩阵乘法