當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.linalg.LinearOperatorDiag用法及代碼示例

LinearOperator 的作用類似於 [batch] 方對角矩陣。

繼承自:LinearOperatorModule

用法

tf.linalg.LinearOperatorDiag(
    diag, is_non_singular=None, is_self_adjoint=None, is_positive_definite=None,
    is_square=None, name='LinearOperatorDiag'
)

參數

  • diag 形狀 [B1,...,Bb, N] Tensorb >= 0 N >= 0 。運算符的對角線。允許的數據類型:float16 , float32float64 , complex64 , complex128
  • is_non_singular 期望這個運算符是非奇異的。
  • is_self_adjoint 期望這個算子等於它的厄米轉置。如果 diag.dtype 是真實的,則這是 auto-set 到 True
  • is_positive_definite 期望這個算子是正定的,意思是二次形式x^H A x對所有非零具有正實部x.請注意,我們不要求算子自伴是正定的。看:https://en.wikipedia.org/wiki/Positive-definite_matrix#Extension_for_non-symmetric_matrices
  • is_square 期望此運算符的行為類似於方形 [batch] 矩陣。
  • name LinearOperator 的名稱。

拋出

  • TypeError 如果 diag.dtype 不是允許的類型。
  • ValueError 如果 diag.dtype 是真實的,而 is_self_adjoint 不是 True

屬性

  • H 返回當前的伴隨LinearOperator.

    給定 A 表示此 LinearOperator ,返回 A* 。請注意,調用self.adjoint()self.H 是等效的。

  • batch_shape TensorShape這批尺寸的LinearOperator.

    如果此運算符的作用類似於帶有 A.shape = [B1,...,Bb, M, N] 的批處理矩陣 A,則返回 TensorShape([B1,...,Bb]) ,相當於 A.shape[:-2]

  • diag
  • domain_dimension 此運算符的域的維度(在向量空間的意義上)。

    如果此運算符的作用類似於帶有 A.shape = [B1,...,Bb, M, N] 的批處理矩陣 A ,則返回 N

  • dtype TensorDType 由此 LinearOperator 處理。
  • graph_parents 這個的圖依賴列表LinearOperator. (已棄用)

    警告:此函數已棄用。它將在未來的版本中刪除。更新說明:請勿調用 graph_parents

  • is_non_singular
  • is_positive_definite
  • is_self_adjoint
  • is_square 返回 True/False 取決於此運算符是否為正方形。
  • parameters 用於實例化此 LinearOperator 的參數字典。
  • range_dimension 此運算符範圍的維度(在向量空間的意義上)。

    如果此運算符的作用類似於帶有 A.shape = [B1,...,Bb, M, N] 的批處理矩陣 A ,則返回 M

  • shape TensorShape這個的LinearOperator.

    如果此運算符的作用類似於帶有 A.shape = [B1,...,Bb, M, N] 的批處理矩陣 A ,則返回 TensorShape([B1,...,Bb, M, N]) ,等效於 A.shape

  • tensor_rank 與此運算符對應的矩陣的秩(在張量的意義上)。

    如果此運算符的作用類似於帶有 A.shape = [B1,...,Bb, M, N] 的批處理矩陣 A ,則返回 b + 2

此運算符的作用類似於 [batch] 對角矩陣 A ,對於某些 b >= 0 ,其形狀為 [B1,...,Bb, N, N] 。第一個 b 索引索引批處理成員。對於每個批次索引 (i1,...,ib) , A[i1,...,ib,::] 是一個 N x N 矩陣。此矩陣A 未具體化,但為了廣播此形狀將是相關的。

LinearOperatorDiag 使用(批量)向量進行初始化。

# Create a 2 x 2 diagonal linear operator.
diag = [1., -1.]
operator = LinearOperatorDiag(diag)

operator.to_dense()
==> [[1.,  0.]
     [0., -1.]]

operator.shape
==> [2, 2]

operator.log_abs_determinant()
==> scalar Tensor

x = ... Shape [2, 4] Tensor
operator.matmul(x)
==> Shape [2, 4] Tensor

# Create a [2, 3] batch of 4 x 4 linear operators.
diag = tf.random.normal(shape=[2, 3, 4])
operator = LinearOperatorDiag(diag)

# Create a shape [2, 1, 4, 2] vector.  Note that this shape is compatible
# since the batch dimensions, [2, 1], are broadcast to
# operator.batch_shape = [2, 3].
y = tf.random.normal(shape=[2, 1, 4, 2])
x = operator.solve(y)
==> operator.matmul(x) = y

形狀兼容性

該運算符作用於具有兼容形狀的 [batch] 矩陣。 x 是與 matmulsolve 的形狀兼容的批處理矩陣,如果

operator.shape = [B1,...,Bb] + [N, N],  with b >= 0
x.shape =   [C1,...,Cc] + [N, R],
and [C1,...,Cc] broadcasts with [B1,...,Bb] to [D1,...,Dd]

性能

假設 operator 是形狀為 [N, N]x.shape = [N, R]LinearOperatorDiag。然後

  • operator.matmul(x) 涉及N * R 乘法。
  • operator.solve(x) 涉及N 除法和N * R 乘法。
  • operator.determinant() 涉及大小 N reduce_prod

如果相反 operatorx 具有形狀 [B1,...,Bb, N, N][B1,...,Bb, N, R] ,則每個操作的複雜性都會增加 B1*...*Bb

矩陣屬性提示

LinearOperator 使用 is_X 形式的布爾標誌初始化,用於 X = non_singular, self_adjoint, positive_definite, square 。它們具有以下含義:

  • 如果 is_X == True ,調用者應該期望操作符具有屬性 X 。這是一個應該實現的承諾,但不是運行時斷言。例如,有限的浮點精度可能會導致違反這些承諾。
  • 如果 is_X == False ,調用者應該期望操作符沒有 X
  • 如果is_X == None(默認),調用者應該沒有任何期望。

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.linalg.LinearOperatorDiag。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。