将正整数(索引)转换为固定大小的密集向量。此层只能用作模型中的第一层
import tensorflow as tf
import numpy as np
embedding_layer = tf.keras.layers.Embedding(
1000,
4,
)
print(embedding_layer)
<keras.layers.embeddings.Embedding object at 0x000001CE579C0BB0>
input_array = np.random.randint(10, size=(2, 3))
print(input_array.shape)
(2, 3)
print(input_array)
[[8 2 5]
[4 0 1]]
output_array = embedding_layer(input_array)
print(output_array)
tf.Tensor(
[[[-0.01356944 -0.03771597 -0.00765408 -0.0108991 ]
[-0.01145458 0.00670235 -0.04148499 0.03742183]
[ 0.02257642 -0.01279204 0.0417194 -0.01290895]]
[[ 0.02775535 -0.02057298 -0.01002337 -0.00311716]
[-0.01408575 -0.00358304 -0.04300636 0.01298282]
[-0.04962901 -0.02087239 -0.00043805 -0.01269226]]], shape=(2, 3, 4), dtype=float32)