有没有办法将参差不齐的张量归一化?

Is there a way to normalize a ragged tensor?

我已经试过 tf.linalg.normalize 但它给了我一个值错误:

ValueError: TypeError: object of type 'RaggedTensor' has no len().

我也无法使 tf.keras.layers.experimental.preprocessing.Normalization() 方法起作用。 谢谢,

我已经重新创建了您遇到的错误并找到了修复它的解决方案。下面是如何标准化参差不齐的张量。

使用tf.linalg.normalize:

import tensorflow as tf
import keras
import numpy as np

# Create a Ragged Tensor
rt = tf.ragged.constant([[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]])
print("Ragged Tensor:","\n",rt,"\n")

# Convert to Tensor to have same length
rt = rt.to_tensor()
print("Tensor of same length:","\n",rt,"\n")

# Normalize
rt = tf.linalg.normalize(rt, axis = None)
print("Normalized and Norm Tensor:","\n",rt,"\n")
# Get the normalized part
rt = tf.convert_to_tensor(rt[0])
print("Normalized Tensor:","\n",rt,"\n")

# Convert to Ragged Tensor
rt = tf.RaggedTensor.from_tensor(rt, padding=0.0)
print("Normalized Ragged Tensor:","\n",rt)

输出-

Ragged Tensor: 
 <tf.RaggedTensor [[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]]> 

Tensor of same length: 
 tf.Tensor(
[[9. 8. 7.]
 [0. 0. 0.]
 [6. 5. 0.]
 [4. 0. 0.]], shape=(4, 3), dtype=float32) 

Normalized and Norm Tensor: 
 (<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
array([[0.546711  , 0.48596537, 0.4252197 ],
       [0.        , 0.        , 0.        ],
       [0.36447403, 0.30372837, 0.        ],
       [0.24298269, 0.        , 0.        ]], dtype=float32)>, <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[16.462078]], dtype=float32)>) 

Normalized Tensor: 
 tf.Tensor(
[[0.546711   0.48596537 0.4252197 ]
 [0.         0.         0.        ]
 [0.36447403 0.30372837 0.        ]
 [0.24298269 0.         0.        ]], shape=(4, 3), dtype=float32) 

Normalized Ragged Tensor: 
 <tf.RaggedTensor [[0.5467110276222229, 0.485965371131897, 0.42521971464157104], [], [0.36447402834892273, 0.3037283718585968], [0.2429826855659485]]>

使用数学。l2_normalize:

import tensorflow as tf
import keras
import numpy as np

# Create a Ragged Tensor
rt = tf.ragged.constant([[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]])
print("Ragged Tensor:","\n",rt,"\n")

# Convert to Tensor to have same length
rt = rt.to_tensor()
print("Tensor of same length:","\n",rt,"\n")

# Normalize
rt = tf.math.l2_normalize(rt, axis = None)
print("Normalized Tensor:","\n",rt,"\n")

# Convert to Ragged Tensor
rt = tf.RaggedTensor.from_tensor(rt, padding=0.0)
print("Normalized Ragged Tensor:","\n",rt)

输出-

Ragged Tensor: 
 <tf.RaggedTensor [[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]]> 

Tensor of same length: 
 tf.Tensor(
[[9. 8. 7.]
 [0. 0. 0.]
 [6. 5. 0.]
 [4. 0. 0.]], shape=(4, 3), dtype=float32) 

Normalized Tensor: 
 tf.Tensor(
[[0.546711   0.48596537 0.4252197 ]
 [0.         0.         0.        ]
 [0.36447403 0.30372834 0.        ]
 [0.24298269 0.         0.        ]], shape=(4, 3), dtype=float32) 

Normalized Ragged Tensor: 
 <tf.RaggedTensor [[0.5467110276222229, 0.485965371131897, 0.42521971464157104], [], [0.36447402834892273, 0.3037283420562744], [0.2429826855659485]]>