Upload 9 files
Browse files- main.py +67 -0
- multi_modal_model.py +172 -0
- requirements.txt +9 -0
main.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import numpy as np
|
| 5 |
+
import random
|
| 6 |
+
from transformers import (
|
| 7 |
+
BartForConditionalGeneration,
|
| 8 |
+
AutoModelForCausalLM,
|
| 9 |
+
BertModel,
|
| 10 |
+
Wav2Vec2Model,
|
| 11 |
+
CLIPModel,
|
| 12 |
+
AutoTokenizer
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
class MultiModalModel(nn.Module):
|
| 16 |
+
def __init__(self):
|
| 17 |
+
super(MultiModalModel, self).__init__()
|
| 18 |
+
# 初始化子模型
|
| 19 |
+
self.text_generator = BartForConditionalGeneration.from_pretrained('facebook/bart-base')
|
| 20 |
+
self.code_generator = AutoModelForCausalLM.from_pretrained('gpt2')
|
| 21 |
+
self.nlp_encoder = BertModel.from_pretrained('bert-base-uncased')
|
| 22 |
+
self.speech_encoder = Wav2Vec2Model.from_pretrained('facebook/wav2vec2-base-960h')
|
| 23 |
+
self.vision_encoder = CLIPModel.from_pretrained('openai/clip-vit-base-patch32')
|
| 24 |
+
|
| 25 |
+
# 初始化分词器和处理器
|
| 26 |
+
self.text_tokenizer = AutoTokenizer.from_pretrained('facebook/bart-base')
|
| 27 |
+
self.code_tokenizer = AutoTokenizer.from_pretrained('gpt2')
|
| 28 |
+
self.nlp_tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 29 |
+
self.speech_processor = AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h')
|
| 30 |
+
self.vision_processor = AutoTokenizer.from_pretrained('openai/clip-vit-base-patch32')
|
| 31 |
+
|
| 32 |
+
def forward(self, task, inputs):
|
| 33 |
+
if task == 'text_generation':
|
| 34 |
+
# 确保 attention_mask 在 inputs 中
|
| 35 |
+
attention_mask = inputs.get('attention_mask')
|
| 36 |
+
print("输入数据:", inputs)
|
| 37 |
+
outputs = self.text_generator.generate(
|
| 38 |
+
inputs['input_ids'],
|
| 39 |
+
max_new_tokens=100, # 增加生成的最大新令牌数
|
| 40 |
+
pad_token_id=self.text_tokenizer.eos_token_id,
|
| 41 |
+
attention_mask=attention_mask,
|
| 42 |
+
top_p=0.9, # 调整 top_p 值
|
| 43 |
+
top_k=50, # 保持 top_k 值
|
| 44 |
+
temperature=0.8, # 调整 temperature 值
|
| 45 |
+
do_sample=True
|
| 46 |
+
)
|
| 47 |
+
print("生成的输出:", outputs)
|
| 48 |
+
return self.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 49 |
+
# 根据需要添加其他任务的逻辑...
|
| 50 |
+
|
| 51 |
+
# 主函数
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
# 初始化模型
|
| 54 |
+
model = MultiModalModel()
|
| 55 |
+
|
| 56 |
+
# 示例任务和输入数据
|
| 57 |
+
task = "text_generation"
|
| 58 |
+
input_text = "This is a sample input."
|
| 59 |
+
tokenizer = model.text_tokenizer
|
| 60 |
+
inputs = tokenizer(input_text, return_tensors='pt')
|
| 61 |
+
|
| 62 |
+
# 添加 attention_mask 键值对
|
| 63 |
+
inputs['attention_mask'] = torch.ones_like(inputs['input_ids'])
|
| 64 |
+
|
| 65 |
+
# 模型推理
|
| 66 |
+
result = model(task, inputs)
|
| 67 |
+
print("最终输出结果:", result)
|
multi_modal_model.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.optim as optim
|
| 5 |
+
from transformers import (
|
| 6 |
+
BartForConditionalGeneration,
|
| 7 |
+
AutoModelForCausalLM,
|
| 8 |
+
BertModel,
|
| 9 |
+
Wav2Vec2Model,
|
| 10 |
+
CLIPModel,
|
| 11 |
+
AutoTokenizer
|
| 12 |
+
)
|
| 13 |
+
import numpy as np
|
| 14 |
+
import random
|
| 15 |
+
import copy
|
| 16 |
+
|
| 17 |
+
class MultiModalModel(nn.Module):
|
| 18 |
+
def __init__(self):
|
| 19 |
+
super(MultiModalModel, self).__init__()
|
| 20 |
+
# 初始化子模型
|
| 21 |
+
self.text_generator = BartForConditionalGeneration.from_pretrained('facebook/bart-base')
|
| 22 |
+
self.code_generator = AutoModelForCausalLM.from_pretrained('gpt2')
|
| 23 |
+
self.nlp_encoder = BertModel.from_pretrained('bert-base-uncased')
|
| 24 |
+
self.speech_encoder = Wav2Vec2Model.from_pretrained('facebook/wav2vec2-base-960h')
|
| 25 |
+
self.vision_encoder = CLIPModel.from_pretrained('openai/clip-vit-base-patch32')
|
| 26 |
+
|
| 27 |
+
# 初始化分词器和处理器
|
| 28 |
+
self.text_tokenizer = AutoTokenizer.from_pretrained('facebook/bart-base')
|
| 29 |
+
self.code_tokenizer = AutoTokenizer.from_pretrained('gpt2')
|
| 30 |
+
self.nlp_tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 31 |
+
self.speech_processor = AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h')
|
| 32 |
+
self.vision_processor = AutoTokenizer.from_pretrained('openai/clip-vit-base-patch32')
|
| 33 |
+
|
| 34 |
+
def forward(self, task, inputs):
|
| 35 |
+
if task == 'text_generation':
|
| 36 |
+
attention_mask = inputs.attention_mask
|
| 37 |
+
outputs = self.text_generator.generate(
|
| 38 |
+
inputs.input_ids,
|
| 39 |
+
max_new_tokens=50,
|
| 40 |
+
pad_token_id=self.text_tokenizer.eos_token_id,
|
| 41 |
+
attention_mask=attention_mask,
|
| 42 |
+
top_p=0.95,
|
| 43 |
+
top_k=50,
|
| 44 |
+
temperature=1.2,
|
| 45 |
+
do_sample=True
|
| 46 |
+
)
|
| 47 |
+
return self.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
+
elif task == 'code_generation':
|
| 49 |
+
attention_mask = inputs.attention_mask
|
| 50 |
+
outputs = self.code_generator.generate(
|
| 51 |
+
inputs.input_ids,
|
| 52 |
+
max_new_tokens=50,
|
| 53 |
+
pad_token_id=self.code_tokenizer.eos_token_id,
|
| 54 |
+
attention_mask=attention_mask,
|
| 55 |
+
top_p=0.95,
|
| 56 |
+
top_k=50,
|
| 57 |
+
temperature=1.2,
|
| 58 |
+
do_sample=True
|
| 59 |
+
)
|
| 60 |
+
return self.code_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 61 |
+
elif task == 'text_understanding':
|
| 62 |
+
outputs = self.nlp_encoder(**inputs)
|
| 63 |
+
return outputs.last_hidden_state
|
| 64 |
+
elif task == 'speech_recognition':
|
| 65 |
+
outputs = self.speech_encoder(**inputs).logits
|
| 66 |
+
predicted_ids = torch.argmax(outputs, dim=-1)
|
| 67 |
+
transcription = self.speech_processor.batch_decode(predicted_ids)[0]
|
| 68 |
+
return transcription
|
| 69 |
+
elif task == 'vision_understanding':
|
| 70 |
+
outputs = self.vision_encoder.get_image_features(**inputs)
|
| 71 |
+
return outputs
|
| 72 |
+
|
| 73 |
+
def save_model(self, save_directory):
|
| 74 |
+
os.makedirs(save_directory, exist_ok=True)
|
| 75 |
+
torch.save(self.state_dict(), os.path.join(save_directory, 'multi_modal_model_state_dict.pth'))
|
| 76 |
+
self.text_tokenizer.save_pretrained(os.path.join(save_directory, 'text_generator'))
|
| 77 |
+
self.code_tokenizer.save_pretrained(os.path.join(save_directory, 'code_generator'))
|
| 78 |
+
self.nlp_tokenizer.save_pretrained(os.path.join(save_directory, 'nlp_encoder'))
|
| 79 |
+
self.speech_processor.save_pretrained(os.path.join(save_directory, 'speech_encoder'))
|
| 80 |
+
self.vision_processor.save_pretrained(os.path.join(save_directory, 'vision_encoder'))
|
| 81 |
+
|
| 82 |
+
def load_model(self, load_directory):
|
| 83 |
+
self.load_state_dict(torch.load(os.path.join(load_directory, 'multi_modal_model_state_dict.pth')))
|
| 84 |
+
self.text_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'text_generator'))
|
| 85 |
+
self.code_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'code_generator'))
|
| 86 |
+
self.nlp_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'nlp_encoder'))
|
| 87 |
+
self.speech_processor = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'speech_encoder'))
|
| 88 |
+
self.vision_processor = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'vision_encoder'))
|
| 89 |
+
|
| 90 |
+
class EvolutionaryMultiModalNetwork(nn.Module):
|
| 91 |
+
def __init__(self, device='cuda' if torch.cuda.is_available() else 'cpu'):
|
| 92 |
+
super(EvolutionaryMultiModalNetwork, self).__init__()
|
| 93 |
+
self.device = device
|
| 94 |
+
self.multi_modal_model = MultiModalModel().to(self.device)
|
| 95 |
+
self.mutation_params = {
|
| 96 |
+
'mutation_rate': 0.2, # 增加变异率
|
| 97 |
+
'mutation_scale': 0.05 # 增加变异幅度
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def mutate_model(self, model):
|
| 101 |
+
"""
|
| 102 |
+
模型参数变异
|
| 103 |
+
"""
|
| 104 |
+
for param in model.parameters():
|
| 105 |
+
if param.requires_grad:
|
| 106 |
+
noise = torch.normal(
|
| 107 |
+
mean=torch.zeros_like(param.data),
|
| 108 |
+
std=self.mutation_params['mutation_scale']
|
| 109 |
+
).to(self.device)
|
| 110 |
+
if random.random() < self.mutation_params['mutation_rate']:
|
| 111 |
+
param.data.add_(noise)
|
| 112 |
+
return model
|
| 113 |
+
|
| 114 |
+
def evaluate_model(self, model, test_input):
|
| 115 |
+
"""
|
| 116 |
+
模型评估
|
| 117 |
+
"""
|
| 118 |
+
try:
|
| 119 |
+
with torch.no_grad():
|
| 120 |
+
output = model('text_generation', test_input)
|
| 121 |
+
complexity = sum(p.numel() for p in model.parameters())
|
| 122 |
+
performance = len(output) # 示例性能评估指标
|
| 123 |
+
return complexity, performance
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"模型评估错误: {e}")
|
| 126 |
+
return 0, 0
|
| 127 |
+
|
| 128 |
+
def save_models(self, save_dir='./model_checkpoints'):
|
| 129 |
+
"""
|
| 130 |
+
保存模型
|
| 131 |
+
"""
|
| 132 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 133 |
+
self.multi_modal_model.save_model(os.path.join(save_dir, 'multi_modal_model'))
|
| 134 |
+
print(f"模型已保存到 {save_dir}")
|
| 135 |
+
|
| 136 |
+
def evolutionary_training(self, epochs=5):
|
| 137 |
+
"""
|
| 138 |
+
进化训练
|
| 139 |
+
"""
|
| 140 |
+
print("🧬 开始进化训练...")
|
| 141 |
+
|
| 142 |
+
for epoch in range(epochs):
|
| 143 |
+
print(f"\n🌟 第 {epoch+1} 代:")
|
| 144 |
+
|
| 145 |
+
# 模型变异
|
| 146 |
+
self.multi_modal_model = self.mutate_model(self.multi_modal_model)
|
| 147 |
+
|
| 148 |
+
# 模型评估
|
| 149 |
+
test_input = self.multi_modal_model.text_tokenizer("Sample input for evaluation.", return_tensors='pt').to(self.device)
|
| 150 |
+
complexity, performance = self.evaluate_model(self.multi_modal_model, test_input)
|
| 151 |
+
print(f"多模态模型 - 复杂度: {complexity}, 性能: {performance:.4f}")
|
| 152 |
+
|
| 153 |
+
def main():
|
| 154 |
+
# 设置随机种子
|
| 155 |
+
torch.manual_seed(42)
|
| 156 |
+
np.random.seed(42)
|
| 157 |
+
random.seed(42)
|
| 158 |
+
|
| 159 |
+
# 创建进化多模态神经网络
|
| 160 |
+
evo_network = EvolutionaryMultiModalNetwork()
|
| 161 |
+
|
| 162 |
+
# 打印模型信息
|
| 163 |
+
evo_network.multi_modal_model.text_generator.config # 打印模型配置示例
|
| 164 |
+
|
| 165 |
+
# 进化训练
|
| 166 |
+
evo_network.evolutionary_training(epochs=5)
|
| 167 |
+
|
| 168 |
+
# 保存模型
|
| 169 |
+
evo_network.save_models()
|
| 170 |
+
|
| 171 |
+
if __name__ == "__main__":
|
| 172 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
numpy
|
| 3 |
+
pandas
|
| 4 |
+
scikit-learn
|
| 5 |
+
code_generator
|
| 6 |
+
nlp_encoder
|
| 7 |
+
speech_encoder
|
| 8 |
+
text_generator
|
| 9 |
+
vision_encoder
|