zoukankan      html  css  js  c++  java
  • Tensorflow中multiply()函数与matmul()函数的用法区别

    1.tf.multiply()函数:矩阵对应元素相乘

    官网定义:

    multiply(x,y,name=None)

    参数:

    x: 一个类型为:half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128的张量。

    y: 一个类型跟张量x相同的张量。

    注意:

      (1)该函数实现的是元素级别的相乘,也就是两个相乘的数元素各自相乘,而不是矩阵乘法

      (2)两个相乘的数必须是相同的类型,否则会报错。

    举例说明:

     1 import tensorflow as tf
     2 
     3 # 1.数和矩阵相乘
     4 x = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
     5 y1 = tf.constant(2)
     6 xy1 = tf.multiply(x,y1)
     7 
     8 # 2.两个矩阵对应元素相乘
     9 x = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
    10 y2 = tf.constant([[1,1,1],[2,2,2],[3,3,3]])
    11 xy2 = tf.multiply(x,y2)
    12 
    13 # 3.元素类型不一样,报错
    14 # x = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
    15 # y3 = tf.constant([[1.0,1.0,1.0],[2.0,2.0,2.0],[3.0,3.0,3.0]])
    16 # xy3 = tf.multiply(x,y3)
    17 
    18 with tf.Session() as sess:
    19     sess.run(tf.global_variables_initializer())
    20 
    21     print('xy1 =
    ',sess.run(xy1))
    22     print('xy2 =
    ',sess.run(xy2))
    23     # print('xy3 =
    ',sess.run(xy3))

    运行结果:

    xy1 =
     [[ 2  4  6]
      [ 4  6  8]
      [ 6  8 10]]
    xy2 =
     [[ 1  2  3]
      [ 4  6  8]
      [ 9 12 15]]

    2.tf.matmul()函数:矩阵乘法

    官网定义:

    matmul(a,
           b,
           transpose_a=False,
           transpose_b=False,
           adjoint_a=False,
           adjoint_b=False,
           a_is_sparse=False,
           b_is_sparse=False,
           name=None)

    参数:

    a: 一个类型为 float16, float32, float64, int32, complex64, complex128 且张量秩 > 1 的张量。 
    b: 一个类型跟张量a相同的张量。 
    transpose_a: 如果为真, a则在进行乘法计算前进行转置。 
    transpose_b: 如果为真, b则在进行乘法计算前进行转置。 
    adjoint_a: 如果为真, a则在进行乘法计算前进行共轭和转置。 
    adjoint_b: 如果为真, b则在进行乘法计算前进行共轭和转置。 
    a_is_sparse: 如果为真, a会被处理为稀疏矩阵。 
    b_is_sparse: 如果为真, b会被处理为稀疏矩阵。

    返回值: 一个跟张量a和张量b类型一样的张量且最内部矩阵是a和b中的相应矩阵的乘积。

    举例说明:

     1 import tensorflow as tf
     2 
     3 x = tf.constant([[1,2,3],
     4                  [2,3,4],
     5                  [3,4,5]])
     6 y = tf.constant([[1,2,3],
     7                  [1,2,3],
     8                  [1,2,3]])
     9 xy = tf.matmul(x,y)
    10 
    11 with tf.Session() as sess:
    12     sess.run(tf.global_variables_initializer())
    13     print('xy1 =
    ',sess.run(xy))

    运行结果:

    xy =
     [[ 6 12 18]
      [ 9 18 27]
      [12 24 36]]

    3.multiply()和matmul()对比

    举例说明:

     1 import tensorflow as tf
     2 
     3 x = tf.constant([[1,2,3],
     4                  [2,3,4],
     5                  [3,4,5]])
     6 y1 = tf.constant([[1,2,3],
     7                   [1,2,3],
     8                   [1,2,3]])
     9 y2 = tf.constant([[1,2,3],
    10                   [1,2,3],
    11                   [1,2,3]])
    12 z1 = tf.multiply(x,y1)      # 对应元素相乘
    13 z2 = tf.matmul(x,y2)        # 矩阵相乘
    14         
    15 with tf.Session() as sess:
    16     sess.run(tf.global_variables_initializer())
    17     print('z1 =
    ',sess.run(z1))
    18     print('z2 =
    ',sess.run(z2))

    运行结果:

    z1 =
     [[ 1  4  9]
      [ 2  6 12]
      [ 3  8 15]]
    z2 =
     [[ 6 12 18]
      [ 9 18 27]
      [12 24 36]]
  • 相关阅读:
    解说asp.net core MVC 过滤器的执行顺序
    asp.net core 2.0 Microsoft.Extensions.Logging 文本文件日志扩展
    【技术累积】【点】【java】【30】代理模式
    【技术累积】【点】【java】【29】MapUtils
    【技术累积】【点】【java】【28】Map遍历
    【技术累积】【点】【java】【27】@JSONField
    【技术累积】【点】【java】【26】@Value默认值
    【技术累积】【点】【java】【25】Orderd
    【技术累积】【点】【java】【23】super以及重写重载
    【技术累积】【线】【java】【2】AOP
  • 原文地址:https://www.cnblogs.com/muzidaitou/p/11275075.html
Copyright © 2011-2022 走看看