Upload model
Browse files- config.json +13 -0
- pytorch_model.bin +3 -0
- test_model.py +44 -0
config.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"MyModel"
|
4 |
+
],
|
5 |
+
"auto_map": {
|
6 |
+
"AutoConfig": "test_model.MyModelConfig",
|
7 |
+
"AutoModel": "test_model.MyModel"
|
8 |
+
},
|
9 |
+
"input_dim": 10,
|
10 |
+
"layers_num": 3,
|
11 |
+
"torch_dtype": "float32",
|
12 |
+
"transformers_version": "4.24.0"
|
13 |
+
}
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d3aaeadc87c87d041089e8ab0de07af2779cd0ff5952b2ecb477aa4301484f1f
|
3 |
+
size 7047
|
test_model.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PreTrainedModel
|
2 |
+
from transformers import PretrainedConfig
|
3 |
+
from typing import List
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
class MyModelConfig(PretrainedConfig):
|
9 |
+
|
10 |
+
def __init__(# 每个参数都必须带有默认值,否则会报错
|
11 |
+
self,
|
12 |
+
input_dim=100,
|
13 |
+
layers_num=5,
|
14 |
+
**kwargs,
|
15 |
+
):
|
16 |
+
self.input_dim = input_dim
|
17 |
+
self.layers_num = layers_num
|
18 |
+
super().__init__(**kwargs)
|
19 |
+
|
20 |
+
class MyModel(PreTrainedModel):
|
21 |
+
config_class = MyModelConfig
|
22 |
+
|
23 |
+
def __init__(self, config):
|
24 |
+
super().__init__(config)
|
25 |
+
modules = []
|
26 |
+
assert config.layers_num >= 1
|
27 |
+
if config.layers_num == 1:
|
28 |
+
modules.append(nn.Linear(config.input_dim,1))
|
29 |
+
else:
|
30 |
+
modules.append(nn.Linear(config.input_dim,30))
|
31 |
+
for i in range(config.layers_num-2):
|
32 |
+
modules.append(nn.Linear(30,30))
|
33 |
+
modules.append(nn.Linear(30,1))
|
34 |
+
self.model = nn.ModuleList(modules)
|
35 |
+
|
36 |
+
|
37 |
+
def forward(self, tensor):
|
38 |
+
return self.model(tensor)
|
39 |
+
|
40 |
+
if __name__ == '__main__':
|
41 |
+
save_config = MyModelConfig(input_dim=10,layers_num=3)
|
42 |
+
save_config.save_pretrained("custom-mymodel")
|
43 |
+
mymodel = MyModel(save_config)
|
44 |
+
torch.save(mymodel.state_dict(),'pytorch_model.bin') # 通常以此命名
|