python - 有没有办法标准化一个参差不齐的张量?
问题描述
我试过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]]>
使用 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]]>
推荐阅读
- events - Sapper on:IE11 中的点击事件修饰符
- reactjs - 如何在 ReactJS 中隐藏或显示字段(使用 antd 组件)?
- ios - 使用 FCM 的 iOs 推送通知
- arduino - 如何获得 Arduino 机器人令牌?
- reactjs - 如何在一个语义 UI 手风琴下制作多个表单字段?
- swift - ipad 和 iphon xcode 上的尺寸等级问题
- swift - swift中的datePicker和validateDate条件
- oauth-2.0 - Google OAuth 2.0 中用户 ID 唯一性的范围
- ruby-on-rails - 从 HTTP.get('/my_pdf') 在浏览器中显示 pdf
- javascript - 我们可以在 CSS 中设置页码以仅打印特定页面吗?