#定义简单的矩阵 constant是定义常量
improt tensorflow as tf
tensorA = tf.constant([[1,2],[3,4]],dtype = tf.float32)
tensorB = tf.constant([[5,6],[7,8]],dtype = tf.float32)
#矩阵相乘
tensorMatmul = tf.matmul(tensorA,tensorB)
等价于tensorA*tensorB
#矩阵相加
tensorAdd = tf.add(tensorA,tensorB)
等价于tensorA+tensorB
#矩阵相除
tensorDivide = tf.divide(tensorA,tensorB)
等价于tensorA/tensorB
#矩阵相减
tensorSubtract = tf.subtract(tensorA,tensorB)
等价于tensorA-tensorB
TensorFlow 在进行四则运算时,一定要求各个 Tensor 数据类型、维度必须一致。
该文介绍了如何使用TensorFlow进行矩阵运算,包括通过tf.constant定义常量,然后展示了矩阵相乘(tf.matmul)、相加(tf.add)、相除(tf.divide)和相减(tf.subtract)的操作,并强调了操作中数据类型和维度的一致性要求。
2306

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



