zoukankan      html  css  js  c++  java
  • tf.matmul()和tf.multipy()的区别

    首先我们分析一下下面的代码:

    import tensorflow as tf
    import numpy as np
    
    a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
    b=np.float32(np.random.randn(3,2))
    #c=tf.matmul(a,b)
    c=tf.multiply(a,b)
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        print(c.eval())

    问题是上面的代码编译正确吗?编译一下就知道,错误信息如下:

    ValueError: Dimensions must be equal, but are 2 and 3 for 'Mul' (op: 'Mul') with input shapes: [2,3], [3,2].

    显然,tf.multiply()表示点积,因此维度要一样。而tf.matmul()表示普通的矩阵乘法。

    而且tf.multiply(a,b)和tf.matmul(a,b)都要求a和b的类型必须一致。但是之间存在着细微的区别。

    在tf中所有返回的tensor,不管传进去是什么类型,传出来的都是numpy ndarray对象。

    看看官网API介绍:

    tf.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
    )
    tf.multiply(
        x,
        y,
        name=None
    )

    但是tf.matmul(a,b)函数不仅要求a和b的类型必须完全一致,同时返回的tensor类型同a和b一致;而tf.multiply(a,b)函数仅要求a和b的类型显式一致,同时返回的tensor类型与a一致,即在不声明类型的情况下,编译不报错。

    例如:

    #类型一致,可以运行
    import tensorflow as tf
    import numpy as np
    
    a=tf.constant([[1, 2, 3],[4, 5, 6]],dtype=np.float32)
    b=np.float32(np.random.randn(3,2))
    c=tf.matmul(a,b)
    #c=tf.multiply(a,b)
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        print (type(c.eval()),type(a.eval()),type(b))
    #类型不一致,不可以运行
    import tensorflow as tf
    import numpy as np
    
    a=tf.constant([[1, 2, 3],[4, 5, 6]])
    b=np.float32(np.random.randn(3,2))
    c=tf.matmul(a,b)
    #c=tf.multiply(a,b)
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        print (type(c.eval()),type(a.eval()),type(b))
    #类型不一致,可以运行,结果的类型和a一致
    import tensorflow as tf
    import numpy as np
    
    a=tf.constant([[1, 2, 3],[4, 5, 6]])
    b=np.float32(np.random.randn(2,3))
    #c=tf.matmul(a,b)
    c=tf.multiply(a,b)
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        print (c.eval())
        print (type(c.eval()),type(a.eval()),type(b))
    #类型不一致,不可以运行
    import tensorflow as tf
    import numpy as np
    
    a=tf.constant([[1, 2, 3],[4, 5, 6]], dtype=np.float32)
    b=tf.constant([[1, 2, 3],[4, 5, 6]], dtype=np.int32)
    #c=tf.matmul(a,b)
    c=tf.multiply(a,b)
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        print (c.eval())
        print (type(c.eval()),type(a.eval()),type(b))
  • 相关阅读:
    UIImageView的动画效果(左右移动、旋转、缩放)
    图片点击放大,再次点击回到原来状态(图片缩放)
    设置一个视图控制器为底部视图的方法
    Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The Swift Pods being used are: ReactiveCocoa, ReactiveSwift, and Resul
    cocoaPod的使用
    cocoaPod安装过程
    RAC(reactivecocoa)的使用方法
    百度地图定位
    <NET CLR via c# 第4版>笔记 第11章 事件
    <NET CLR via c# 第4版>笔记 第10章 属性
  • 原文地址:https://www.cnblogs.com/cvtoEyes/p/9035706.html
Copyright © 2011-2022 走看看