参考官方文档
format:subtract(x, y, name=None)
Args:
x: A `Tensor`. Must be one of the following types: `half`, `float32`, `float64`, `int32`, `int64`, `complex64`, `complex128`.
y: A `Tensor`. Must have the same type as `x`.(也是强调x,y两个参数类型必须相同)
name: A name for the operation (optional).
Returns:
A `Tensor`. Has the same type as `x`.x - y element-wise.(返回的数据类型与x相同,且是x-y的操作是元素级别的,具体看栗子)
栗子
import tensorflow as tf
#两个矩阵相减
x=tf.constant([[1,2],[2,1]])
y=tf.constant([[1,1],[1,2]])
z=tf.subtract(x,y)
#一个矩阵减一个数
x1=tf.constant([[1,2],[2,1]])
y1=tf.constant(2)
z1=tf.subtract(x1,y1)
#一个数减一个矩阵
x2=tf.constant(2)
y2=tf.constant([[1,2],[2,1]])
z2=tf.subtract(x2,y2)
with tf.Session() as sess:
z_v,z1_v=sess.run([z,z1])
print('z = \n%s'%(z_v))
print('z1 = \n%s'%(z1_v))
结果