我正在尝试为重整变压器实现分类头。分类头工作正常,但是当我尝试更改配置参数之一config.axial_pos_shape,即模型的序列长度参数时,会引发错误;
reformer.embeddings.position_embeddings.weights.0的大小不匹配:从检查点复制形状为torch.Size([512,1,64])的参数,当前模型中的形状为torch.Size([64,1] ,64])。 reformer.embeddings.position_embeddings.weights.1的大小不匹配:从检查点复制形状为torch.Size([1,1024,192])的参数,当前模型中的形状为torch.Size([1,128,192] )。
配置:
{
"architectures": [
"ReformerForSequenceClassification"
],
"attention_head_size": 64,
"attention_probs_dropout_prob": 0.1,
"attn_layers": [
"local",
"lsh",
"local",
"lsh",
"local",
"lsh"
],
"axial_norm_std": 1.0,
"axial_pos_embds": true,
"axial_pos_embds_dim": [
64,
192
],
"axial_pos_shape": [
64,
256
],
"chunk_size_feed_forward": 0,
"chunk_size_lm_head": 0,
"eos_token_id": 2,
"feed_forward_size": 512,
"hash_seed": null,
"hidden_act": "relu",
"hidden_dropout_prob": 0.05,
"hidden_size": 256,
"initializer_range": 0.02,
"intermediate_size": 3072,
"is_decoder": true,
"layer_norm_eps": 1e-12,
"local_attention_probs_dropout_prob": 0.05,
"local_attn_chunk_length": 64,
"local_num_chunks_after": 0,
"local_num_chunks_before": 1,
"lsh_attention_probs_dropout_prob": 0.0,
"lsh_attn_chunk_length": 64,
"lsh_num_chunks_after": 0,
"lsh_num_chunks_before": 1,
"max_position_embeddings": 8192,
"model_type": "reformer",
"num_attention_heads": 2,
"num_buckets": [
64,
128
],
"num_chunks_after": 0,
"num_chunks_before": 1,
"num_hashes": 1,
"num_hidden_layers": 6,
"output_past": true,
"pad_token_id": 0,
"task_specific_params": {
"text-generation": {
"do_sample": true,
"max_length": 100
}
},
"vocab_size": 320
}
Python代码:
config = ReformerConfig()
config.max_position_embeddings = 8192
config.axial_pos_shape=[64, 128]
#config = ReformerConfig.from_pretrained('./cnp/config.json', output_attention=True)
model = ReformerForSequenceClassification(config)
model.load_state_dict(torch.load("./cnp/pytorch_model.bin"))
答案 0 :(得分:0)
我遇到了同样的问题,试图将重组器预训练中使用的默认最大序列长度的65536(128 * 512)的大小减半。
如@cronoik所述,您必须:
那些不必要的权重是“位置嵌入”层中的权重。在Reformer模型中,使用“轴向位置编码”策略来学习位置嵌入(而不是使用诸如BERT这样的固定嵌入)。轴向位置编码以内存有效的方式存储位置嵌入,使用两个小的张量而不是一个大的张量。
但是,位置嵌入的概念仍然完全相同,即每个位置获得不同的嵌入。
也就是说,从理论上讲(如果我在某个地方误解了,请纠正我),删除最后的位置嵌入以匹配您的自定义最大序列长度不会影响性能。您可以参考此post from HuggingFace来查看“轴向位置编码”的更详细说明,并了解在何处截断位置嵌入张量。
我设法通过以下代码调整并使用了自定义最大长度为32768(128 * 256)的Reformer:
# Load intial pretrained model
model = ReformerForSequenceClassification.from_pretrained('google/reformer-enwik8', num_labels=2)
# Reshape Axial Position Embeddings layer to match desired max seq length
model.reformer.embeddings.position_embeddings.weights[1] = torch.nn.Parameter(model.reformer.embeddings.position_embeddings.weights[1][0][:256])
# Update the config file to match custom max seq length
model.config.axial_pos_shape = 128, 256
model.config.max_position_embeddings = 128*256 # 32768
# Save model with custom max length
output_model_path = "path/to/model"
model.save_pretrained(output_model_path)