Darius-H
commited on
Commit
·
d761eb1
1
Parent(s):
dad8f21
First model version
Browse files- __init__.py +0 -0
- __pycache__/test_model.cpython-39.pyc +0 -0
- custom-mymodel/config.json +13 -0
- custom-mymodel/pytorch_model.bin +3 -0
- custom-mymodel/test_model.py +44 -0
- pytorch_model.bin +3 -0
- register.py +14 -0
- test_model.py +44 -0
__init__.py
ADDED
|
File without changes
|
__pycache__/test_model.cpython-39.pyc
ADDED
|
Binary file (1.64 kB). View file
|
|
|
custom-mymodel/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 |
+
}
|
custom-mymodel/pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d3aaeadc87c87d041089e8ab0de07af2779cd0ff5952b2ecb477aa4301484f1f
|
| 3 |
+
size 7047
|
custom-mymodel/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') # 通常以此命名
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f82694bb48a4c787f32362fc2fc6dda47b02bd6c690846a729c0c25cf196e459
|
| 3 |
+
size 7175
|
register.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from test_model import MyModelConfig,MyModel
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
MyModelConfig.register_for_auto_class()
|
| 6 |
+
MyModel.register_for_auto_class("AutoModel")
|
| 7 |
+
save_config = MyModelConfig(input_dim=10,layers_num=3)
|
| 8 |
+
save_config.save_pretrained("custom-mymodel")
|
| 9 |
+
|
| 10 |
+
mymodel = MyModel(save_config)
|
| 11 |
+
mymodel.load_state_dict(torch.load('pytorch_model.bin'))
|
| 12 |
+
mymodel.save_pretrained("custom-mymodel")
|
| 13 |
+
|
| 14 |
+
mymodel.push_to_hub("custom-mymodel_v1")
|
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') # 通常以此命名
|