【发布时间】:2018-04-26 04:17:01
【问题描述】:
我想找到具有最少非零元素数量的嵌入的最小数量,以限制模型中非零权重的数量。
我现在的架构是:
def build_model(max_len, num_words, num_classes):
I = layers.Input(shape=(max_len, ), name='Input')
E = layers.Embedding(input_dim=num_words,
output_dim=num_classes,
embeddings_initializer='he_uniform',
name='Embeddings')(I)
P = layers.GlobalAvgPool1D(name='Pool')(E)
O = layers.Softmax(name='Softmax')(P)
return models.Model(inputs=[I], outputs=[O])
模型看起来像:
Layer (type) Output Shape Param #
=================================================================
Input (InputLayer) (None, 271) 0
_________________________________________________________________
Embeddings (Embedding) (None, 271, 26) 1358786
_________________________________________________________________
Pool (GlobalAveragePooling1D (None, 26) 0
_________________________________________________________________
Softmax (Softmax) (None, 26) 0
=================================================================
我试图在一些迭代后将一些权重归零,以便只保留最大的权重
model = build_model(MAX_LEN, NUM_TOKENS, NUM_CLASSES)
model.summary()
model.compile(optimizers.Adam(lr=0.1), 'sparse_categorical_crossentropy', metrics=['accuracy'])
total_params = NUM_TOKENS * NUM_CLASSES
need_params = 200
num_iterations = 16
decay = (total_params / need_params) ** (1 / num_iterations)
for i in range(num_iterations):
wipe_out = int(total_params * (1 - 1 / decay ** (i + 1)))
model.fit(X, y, batch_size=512, epochs=i * 16, verbose=False)
weights = model.layers[1].get_weights()[0]
idx = np.argpartition(np.abs(weights), k, axis=None)
weights.flat[idx[:wipe_out]] = 0
model.layers[1].set_weights([weights])
但是在训练时,模型会不断将这个零权重更新为非零值。 有没有办法限制更新零值或某种只允许更改非零值(值方式,而不是嵌入方式)的掩码?
您能否帮助我使用嵌入或优化器或正则化器来构建一个嵌入矩阵中最多包含 200 个非零元素的模型,该模型具有相对较高的准确性?
问题是: 我有一个“原始职位”和“分类工作角色”的列表,我试图为每个单词分配一个与每个类别的距离,然后对它们进行平均并选择一个权重最高的类别。主要问题是最小化嵌入矩阵中非零元素的数量
【问题讨论】: