Keras Tensorflow中的嵌入层索引顺序含义
embedding layer index order meaning in Keras Tensorflow
我正在使用 Keras Tensorflow 在混合数据输入(数字和分类)上拟合深度学习模型。分类协变量已使用 scikit-learn LabelEncoder
编码为数字,并使用 Input
层(一维)引入深度学习。然后,我使用例如具有 6 个不同级别的变量设置 Embedding
层:
variable_emb = Embedding(input_dim=6,output_dim=1,input_shape=(1,),name='variable_emb_emb')(variable_tensor_input)
我的问题是嵌入矩阵行遵循的顺序是什么。第 i 行是遵循 LabelEncoder 索引还是根据它们在输入数据集中出现的顺序分配给不同的原始变量级别?
我相信输出行只是从 0 到 input_dim-1 进行索引,并且您的 LabelEncoder 为每个分类变量生成的任何整数都将对应于适当的行。例如,如果您有 3 个分类变量:"apple"、"pear"、"orange" 并且 LabelEncoder 指定 pear=2、orange=0、apple=1,则嵌入矩阵的第 0 行将与 "orange" 相关,第 1 行将是 "apple",第 2 行将是 "pear."
下面的工作代码片段展示了这个想法。希望对您有所帮助。
import numpy as np
from keras import Sequential
from keras.layers import Embedding
model = Sequential()
model.add(Embedding(5, 2))
model.compile('rmsprop', 'mse')
#model.summary()
input_array = np.array([[1, 1, 1],
[1, 2, 2]])
output_array = model.predict(input_array)
print('model.get_weights():')
print(model.get_weights())
print('output_array:')
print(output_array)
受到@ad2004 的启发,我在尝试使设置 crystal 清晰时做了一个类似的实验。要说明的结果是:嵌入层的索引直接对应于您的分类特征的整数表示。
首先设计一个带有嵌入层的简单模型
from numpy.random import seed
seed(42)
import tensorflow
tensorflow.random.set_seed(42)
import numpy as np
from keras import Sequential
from keras.layers import Embedding
model = Sequential()
model.add(Embedding(input_dim=3, output_dim=2, input_length=1, name='embedding'))
model.add(Flatten())
model.add(Dense(2, activation="relu"))
model.add(Dense(1))
model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics=['accuracy'])
model.summary()
然后通过定义不同的 x_arrays 但相同的 y:
normal_order = 1 # or 0
if normal_order:
x_array = np.array([[0],
[1],
[2],
[0],
[1],
[2]])
if not normal_order:
x_array = np.array([[2],
[1],
[0],
[2],
[1],
[0]])
y = np.array([1,1,0,1,1,0])
我们期望 normal_order=1 个案例的 嵌入向量应该有 0 和 1 彼此接近 ,同时,normal_order=0 情况下的嵌入向量应该有 2 和 1 彼此接近。 运行 以下代码:
model.fit(x = x_array, y, epochs=2000 )
emb_df = pd.DataFrame( model.get_layer('embedding').get_weights()[0]).reset_index()
normal_order=1 个案例的 emb_df 是:
emb_df
# index 0 1
#0 0 -0.169505 -0.242161
#1 1 -0.246347 -0.211345
#2 2 0.067879 0.062597
而 normal_order=0 的情况是:
emb_df
# index 0 1
#0 0 0.104817 0.014432
#1 1 -0.264942 -0.227623
#2 2 -0.179805 -0.162726
我正在使用 Keras Tensorflow 在混合数据输入(数字和分类)上拟合深度学习模型。分类协变量已使用 scikit-learn LabelEncoder
编码为数字,并使用 Input
层(一维)引入深度学习。然后,我使用例如具有 6 个不同级别的变量设置 Embedding
层:
variable_emb = Embedding(input_dim=6,output_dim=1,input_shape=(1,),name='variable_emb_emb')(variable_tensor_input)
我的问题是嵌入矩阵行遵循的顺序是什么。第 i 行是遵循 LabelEncoder 索引还是根据它们在输入数据集中出现的顺序分配给不同的原始变量级别?
我相信输出行只是从 0 到 input_dim-1 进行索引,并且您的 LabelEncoder 为每个分类变量生成的任何整数都将对应于适当的行。例如,如果您有 3 个分类变量:"apple"、"pear"、"orange" 并且 LabelEncoder 指定 pear=2、orange=0、apple=1,则嵌入矩阵的第 0 行将与 "orange" 相关,第 1 行将是 "apple",第 2 行将是 "pear."
下面的工作代码片段展示了这个想法。希望对您有所帮助。
import numpy as np
from keras import Sequential
from keras.layers import Embedding
model = Sequential()
model.add(Embedding(5, 2))
model.compile('rmsprop', 'mse')
#model.summary()
input_array = np.array([[1, 1, 1],
[1, 2, 2]])
output_array = model.predict(input_array)
print('model.get_weights():')
print(model.get_weights())
print('output_array:')
print(output_array)
受到@ad2004 的启发,我在尝试使设置 crystal 清晰时做了一个类似的实验。要说明的结果是:嵌入层的索引直接对应于您的分类特征的整数表示。
首先设计一个带有嵌入层的简单模型
from numpy.random import seed
seed(42)
import tensorflow
tensorflow.random.set_seed(42)
import numpy as np
from keras import Sequential
from keras.layers import Embedding
model = Sequential()
model.add(Embedding(input_dim=3, output_dim=2, input_length=1, name='embedding'))
model.add(Flatten())
model.add(Dense(2, activation="relu"))
model.add(Dense(1))
model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics=['accuracy'])
model.summary()
然后通过定义不同的 x_arrays 但相同的 y:
normal_order = 1 # or 0
if normal_order:
x_array = np.array([[0],
[1],
[2],
[0],
[1],
[2]])
if not normal_order:
x_array = np.array([[2],
[1],
[0],
[2],
[1],
[0]])
y = np.array([1,1,0,1,1,0])
我们期望 normal_order=1 个案例的 嵌入向量应该有 0 和 1 彼此接近 ,同时,normal_order=0 情况下的嵌入向量应该有 2 和 1 彼此接近。 运行 以下代码:
model.fit(x = x_array, y, epochs=2000 )
emb_df = pd.DataFrame( model.get_layer('embedding').get_weights()[0]).reset_index()
normal_order=1 个案例的 emb_df 是:
emb_df
# index 0 1
#0 0 -0.169505 -0.242161
#1 1 -0.246347 -0.211345
#2 2 0.067879 0.062597
而 normal_order=0 的情况是:
emb_df
# index 0 1
#0 0 0.104817 0.014432
#1 1 -0.264942 -0.227623
#2 2 -0.179805 -0.162726