Есть ли способ нормализовать рваный тензор? - PullRequest
0 голосов
/ 30 марта 2020

Я попытался tf.linalg.normalize, и он дал мне ошибку значения:

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

Также я не смог заставить метод tf.keras.layers.experimental.preprocessing.Normalization() работать. Спасибо,

1 Ответ

0 голосов
/ 30 марта 2020

Я воссоздал ошибку, с которой вы столкнулись, и нашел решение, чтобы исправить ее. Ниже описано, как можно нормализовать рваный тензор.

Использование 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]]>

Использование math.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]]>
...