当前位置: 代码迷 >> 综合 >> tf.linalg.svd踩坑
  详细解决方案

tf.linalg.svd踩坑

热度:68   发布时间:2023-12-17 00:24:41.0

np.linalg.svd具体形式。注意u,s,v的位置 

>>> import numpy as np>>> M = np.mat([[1,2,3,4],[5,6,7,8],[2,3,4,5]])
>>> u, s, v = np.linalg.svd(M)>>> print(M.shape)
(3, 4)
>>> print(u.shape)
(3, 3)
>>> print(s.shape)
(3,)   # 根据矩阵乘法,这里实际表示的对角阵shape是(3, 4)
>>> print(v.shape)
(4, 4)# 验证一下公式
>>> print(u.dot(np.column_stack((diag(s), np.zeros(3))).dot(v)))
[[ 1.  2.  3.  4.][ 5.  6.  7.  8.][ 2.  3.  4.  5.]]

 首先请注意它返回值中的u、s、v矩阵顺序,这是新手很容易被坑的地方。

>>> import tensorflow as tf
>>> tf.InteractiveSession()
>>> M = tf.constant([[1,2,3,4],[5,6,7,8],[2,3,4,5]], dtype=tf.float32)
>>> s, u, v = tf.svd(M)>>> print(M.shape)
(3, 4)
>>> print(u.shape)
(3, 3)
>>> print(s.shape)
(3,)   # 根据矩阵乘法,这里实际表示的对角阵shape是(3, 3)
>>> print(v.shape)
(4, 3)

排查了一下午,才找到这个问题,因为tensorflow的某些向量是不能直接显示出来的。所以只能一点一点排查。希望对新手有所帮助。