牛骨文教育服务平台(让学习变的简单)
博文笔记

tf.subtract()

创建时间:2017-08-11 投稿人: 浏览次数:443

参考官方文档

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 = 
%s"%(z_v))
    print("z1 = 
%s"%(z1_v))
结果

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。