tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)
生活随笔
收集整理的這篇文章主要介紹了
tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@tf_export("matmul")
def 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):"""Multiplies matrix `a` by matrix `b`, producing `a` * `b`.將矩陣a與矩陣b相乘,得出a * b。The inputs must, following any transpositions, be tensors of rank >= 2where the inner 2 dimensions specify valid matrix multiplication arguments,and any further outer dimensions match.在進行任何換位后,輸入必須為(秩?)> = 2的張量,其中內部2維指定有效的矩陣乘法自變量,并且任何其他外部維匹配。Both matrices must be of the same type. The supported types are:`float16`, `float32`, `float64`, `int32`, `complex64`, `complex128`.兩種矩陣必須屬于同一類型。 支持的類型有:`float16`,`float32`,`float64`,`int32`,`complex64`,`complex128`。Either matrix can be transposed or adjointed (conjugated and transposed) onthe fly by setting one of the corresponding flag to `True`. These are `False`by default.通過將相應標志之一設置為“ True”,可以即時對矩陣進行轉置或連接(共軛和轉置)。 這些默認為False。If one or both of the matrices contain a lot of zeros, a more efficientmultiplication algorithm can be used by setting the corresponding`a_is_sparse` or `b_is_sparse` flag to `True`. These are `False` by default.This optimization is only available for plain matrices (rank-2 tensors) withdatatypes `bfloat16` or `float32`.如果一個或兩個矩陣都包含大量零,則可以通過將相應的“ a_is_sparse”或“ b_is_sparse”標志,設置為“ True”來使用更有效的乘法算法。 這些默認為False。此優化僅適用于數據類型為bfloat16或float32的普通矩陣(秩2張量)。For example:```python# 2-D tensor `a`# [[1, 2, 3],# [4, 5, 6]]a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])# 2-D tensor `b`# [[ 7, 8],# [ 9, 10],# [11, 12]]b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])# `a` * `b`# [[ 58, 64],# [139, 154]]c = tf.matmul(a, b)# 3-D tensor `a`# [[[ 1, 2, 3],# [ 4, 5, 6]],# [[ 7, 8, 9],# [10, 11, 12]]]a = tf.constant(np.arange(1, 13, dtype=np.int32),shape=[2, 2, 3])# 3-D tensor `b`# [[[13, 14],# [15, 16],# [17, 18]],# [[19, 20],# [21, 22],# [23, 24]]]b = tf.constant(np.arange(13, 25, dtype=np.int32),shape=[2, 3, 2])# `a` * `b`# [[[ 94, 100],# [229, 244]],# [[508, 532],# [697, 730]]]c = tf.matmul(a, b)# Since python >= 3.5 the @ operator is supported (see PEP 465).# In TensorFlow, it simply calls the `tf.matmul()` function, so the# following lines are equivalent:由于python> = 3.5,因此支持@運算符(請參閱PEP 465)。在TensorFlow中,它僅調用`tf.matmul()`函數,因此以下幾行是等效的:d = a @ b @ [[10.], [11.]]d = tf.matmul(tf.matmul(a, b), [[10.], [11.]])Args:a: `Tensor` of type `float16`, `float32`, `float64`, `int32`, `complex64`,`complex128` and rank > 1.類型為`float16`,`float32`,`float64`,`int32,`complex64`,`complex128`和秩> 1的`Tensor`。b: `Tensor` with same type and rank as `a`.具有與a相同類型和秩的Tensor。transpose_a: If `True`, `a` is transposed before multiplication.如果為True,則在相乘之前對a進行轉置。transpose_b: If `True`, `b` is transposed before multiplication.如果為True,則在相乘之前將b換位。adjoint_a: If `True`, `a` is conjugated and transposed beforemultiplication.如果為True,則在相乘之前對a進行共軛和轉置。adjoint_b: If `True`, `b` is conjugated and transposed beforemultiplication.如果為True,則在相乘之前對b進行共軛和轉置。a_is_sparse: If `True`, `a` is treated as a sparse matrix.如果為True,則將a視為稀疏矩陣。b_is_sparse: If `True`, `b` is treated as a sparse matrix.如果為True,則將b視為稀疏矩陣。name: Name for the operation (optional).操作名稱(可選)。Returns:A `Tensor` of the same type as `a` and `b` where each inner-most matrix isthe product of the corresponding matrices in `a` and `b`, e.g. if alltranspose or adjoint attributes are `False`:與`a`和`b`具有相同類型的`張量`,其中每個最里面的矩陣是`a`和`b`中對應矩陣的乘積,例如 如果所有轉置或伴隨屬性均為False:`output`[..., i, j] = sum_k (`a`[..., i, k] * `b`[..., k, j]),for all indices i, j.Note: This is matrix product, not element-wise product.這是矩陣乘積,而不是元素乘積。Raises:ValueError: If transpose_a and adjoint_a, or transpose_b and adjoint_bare both set to True."""with ops.name_scope(name, "MatMul", [a, b]) as name:if transpose_a and adjoint_a:raise ValueError("Only one of transpose_a and adjoint_a can be True.")if transpose_b and adjoint_b:raise ValueError("Only one of transpose_b and adjoint_b can be True.")if context.executing_eagerly():if not isinstance(a, (ops.EagerTensor, _resource_variable_type)):a = ops.convert_to_tensor(a, name="a")if not isinstance(b, (ops.EagerTensor, _resource_variable_type)):b = ops.convert_to_tensor(b, name="b")else:a = ops.convert_to_tensor(a, name="a")b = ops.convert_to_tensor(b, name="b")# TODO(apassos) remove _shape_tuple here when it is not needed.a_shape = a._shape_tuple() # pylint: disable=protected-accessb_shape = b._shape_tuple() # pylint: disable=protected-accessif (not a_is_sparse andnot b_is_sparse) and ((a_shape is None or len(a_shape) > 2) and(b_shape is None or len(b_shape) > 2)):# BatchMatmul does not support transpose, so we conjugate the matrix and# use adjoint instead. Conj() is a noop for real matrices.if transpose_a:a = conj(a)adjoint_a = Trueif transpose_b:b = conj(b)adjoint_b = Truereturn gen_math_ops.batch_mat_mul(a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)# Neither matmul nor sparse_matmul support adjoint, so we conjugate# the matrix and use transpose instead. Conj() is a noop for real# matrices.if adjoint_a:a = conj(a)transpose_a = Trueif adjoint_b:b = conj(b)transpose_b = Trueuse_sparse_matmul = Falseif a_is_sparse or b_is_sparse:sparse_matmul_types = [dtypes.bfloat16, dtypes.float32]use_sparse_matmul = (a.dtype in sparse_matmul_types and b.dtype in sparse_matmul_types)if ((a.dtype == dtypes.bfloat16 or b.dtype == dtypes.bfloat16) anda.dtype != b.dtype):# matmul currently doesn't handle mixed-precision inputs.use_sparse_matmul = Trueif use_sparse_matmul:ret = sparse_matmul(a,b,transpose_a=transpose_a,transpose_b=transpose_b,a_is_sparse=a_is_sparse,b_is_sparse=b_is_sparse,name=name)# sparse_matmul always returns float32, even with# bfloat16 inputs. This prevents us from configuring bfloat16 training.# casting to bfloat16 also matches non-sparse matmul behavior better.if a.dtype == dtypes.bfloat16 and b.dtype == dtypes.bfloat16:ret = cast(ret, dtypes.bfloat16)return retelse:return gen_math_ops.mat_mul(a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
通過結果可以看出,兩個多維矩陣再相乘時,它們除了后兩維之外的維度必須相同,否則怎能做到一一對應呢?
比如a的維度是(2,2,3),b的維度是(2,3,2),它們除了最后兩維之外的維度2必須相同,而最后兩維需要滿足矩陣乘法要求,一個是(i,j),另一個必須是(j,k)。
相乘后,除后兩維之外的維度不變,后兩維變成(i,k),如(…,i,j)*(…,j,k)= (…,i,k)
參考文章:[tensorflow] 多維矩陣的乘法
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的tensorflow tf.matmul() (多维)矩阵相乘(多维矩阵乘法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tensorflow tf.encode
- 下一篇: tensorflow教程 学习笔记 之