zoukankan      html  css  js  c++  java
  • tf.matmul函数和tf.multiply函数

    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) 

    参数:

    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的维数必须相同

    import numpy as np
    import tensorflow as tf

    X = np.array([[1,2],
            [2,2],
            [3,2]])
    W = np.array([[3,1,1,4],
            [3,1,1,4]])

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        Input = tf.matmul(X,W)
        result = sess.run(Input)
        print(result)

    [[ 9 3 3 12]
     [12 4 4 16]
     [15 5 5 20]]

    b在神经网络中一般用作权重矩阵,shape=[x,y],x为前一层神经网络的神经元数量,y为后一层神经网络的神经元数量

    下面演示加上偏置bias,偏置采用以为数组,长度为后一层神经网络的神经元数量

    import numpy as np

    import tensorflow as tf

    X = np.array([[1,2],[2,2],[3,2]]) 

    W = np.array([[3,1,1,4],[3,1,1,4]]) 

    bias = np.array([1,2,3,4]) 

    with tf.Session() as sess:

      sess.run(tf.global_variables_initializer())

      Input = tf.matmul(X,W) + bias

      result = sess.run(Input)

      print(result)

    [[10 5 6 16]
    [13 6 7 20]
    [16 7 8 24]]

     tf.multiply(x, y, name=None) ,两个矩阵中对应元素各自相乘,最后返回数据的维数以最多维数据的维数相同

    import numpy as np
    import tensorflow as tf
    
    a = np.array([[1,2],
                  [2,3],
                  [3,4]])
    b= np.array([2,3])
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        Input = tf.multiply(a,b)
        result = sess.run(Input)    
        print(result)

    [[ 2 6]
     [ 4 9]
     [ 6 12]]

     

  • 相关阅读:
    河工大玲珑校赛重现の 饶学妹的比赛
    河工大玲珑杯校赛随笔
    河南省第四届ACM省赛(T1) 序号互换
    河南省第四届ACM省赛(T3) 表达式求值
    debian系统下安装ssh
    戴尔poweredge r730服务器配置以及系统安装
    win10环境下安装Ubantu双系统(超详解)
    debian服务器解决中文安装后出现乱码的问题
    debian系统下安装ssh
    如何在ubuntu上搭建hustoj?
  • 原文地址:https://www.cnblogs.com/wzdLY/p/9748988.html
Copyright © 2011-2022 走看看