SHMT / multi_modal_model.py
zeroMN's picture
Upload 9 files
cceec1f verified
raw
history blame
7.4 kB
import os
import torch
import torch.nn as nn
import torch.optim as optim
from transformers import (
BartForConditionalGeneration,
AutoModelForCausalLM,
BertModel,
Wav2Vec2Model,
CLIPModel,
AutoTokenizer
)
import numpy as np
import random
import copy
class MultiModalModel(nn.Module):
def __init__(self):
super(MultiModalModel, self).__init__()
# 初始化子模型
self.text_generator = BartForConditionalGeneration.from_pretrained('facebook/bart-base')
self.code_generator = AutoModelForCausalLM.from_pretrained('gpt2')
self.nlp_encoder = BertModel.from_pretrained('bert-base-uncased')
self.speech_encoder = Wav2Vec2Model.from_pretrained('facebook/wav2vec2-base-960h')
self.vision_encoder = CLIPModel.from_pretrained('openai/clip-vit-base-patch32')
# 初始化分词器和处理器
self.text_tokenizer = AutoTokenizer.from_pretrained('facebook/bart-base')
self.code_tokenizer = AutoTokenizer.from_pretrained('gpt2')
self.nlp_tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
self.speech_processor = AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h')
self.vision_processor = AutoTokenizer.from_pretrained('openai/clip-vit-base-patch32')
def forward(self, task, inputs):
if task == 'text_generation':
attention_mask = inputs.attention_mask
outputs = self.text_generator.generate(
inputs.input_ids,
max_new_tokens=50,
pad_token_id=self.text_tokenizer.eos_token_id,
attention_mask=attention_mask,
top_p=0.95,
top_k=50,
temperature=1.2,
do_sample=True
)
return self.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
elif task == 'code_generation':
attention_mask = inputs.attention_mask
outputs = self.code_generator.generate(
inputs.input_ids,
max_new_tokens=50,
pad_token_id=self.code_tokenizer.eos_token_id,
attention_mask=attention_mask,
top_p=0.95,
top_k=50,
temperature=1.2,
do_sample=True
)
return self.code_tokenizer.decode(outputs[0], skip_special_tokens=True)
elif task == 'text_understanding':
outputs = self.nlp_encoder(**inputs)
return outputs.last_hidden_state
elif task == 'speech_recognition':
outputs = self.speech_encoder(**inputs).logits
predicted_ids = torch.argmax(outputs, dim=-1)
transcription = self.speech_processor.batch_decode(predicted_ids)[0]
return transcription
elif task == 'vision_understanding':
outputs = self.vision_encoder.get_image_features(**inputs)
return outputs
def save_model(self, save_directory):
os.makedirs(save_directory, exist_ok=True)
torch.save(self.state_dict(), os.path.join(save_directory, 'multi_modal_model_state_dict.pth'))
self.text_tokenizer.save_pretrained(os.path.join(save_directory, 'text_generator'))
self.code_tokenizer.save_pretrained(os.path.join(save_directory, 'code_generator'))
self.nlp_tokenizer.save_pretrained(os.path.join(save_directory, 'nlp_encoder'))
self.speech_processor.save_pretrained(os.path.join(save_directory, 'speech_encoder'))
self.vision_processor.save_pretrained(os.path.join(save_directory, 'vision_encoder'))
def load_model(self, load_directory):
self.load_state_dict(torch.load(os.path.join(load_directory, 'multi_modal_model_state_dict.pth')))
self.text_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'text_generator'))
self.code_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'code_generator'))
self.nlp_tokenizer = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'nlp_encoder'))
self.speech_processor = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'speech_encoder'))
self.vision_processor = AutoTokenizer.from_pretrained(os.path.join(load_directory, 'vision_encoder'))
class EvolutionaryMultiModalNetwork(nn.Module):
def __init__(self, device='cuda' if torch.cuda.is_available() else 'cpu'):
super(EvolutionaryMultiModalNetwork, self).__init__()
self.device = device
self.multi_modal_model = MultiModalModel().to(self.device)
self.mutation_params = {
'mutation_rate': 0.2, # 增加变异率
'mutation_scale': 0.05 # 增加变异幅度
}
def mutate_model(self, model):
"""
模型参数变异
"""
for param in model.parameters():
if param.requires_grad:
noise = torch.normal(
mean=torch.zeros_like(param.data),
std=self.mutation_params['mutation_scale']
).to(self.device)
if random.random() < self.mutation_params['mutation_rate']:
param.data.add_(noise)
return model
def evaluate_model(self, model, test_input):
"""
模型评估
"""
try:
with torch.no_grad():
output = model('text_generation', test_input)
complexity = sum(p.numel() for p in model.parameters())
performance = len(output) # 示例性能评估指标
return complexity, performance
except Exception as e:
print(f"模型评估错误: {e}")
return 0, 0
def save_models(self, save_dir='./model_checkpoints'):
"""
保存模型
"""
os.makedirs(save_dir, exist_ok=True)
self.multi_modal_model.save_model(os.path.join(save_dir, 'multi_modal_model'))
print(f"模型已保存到 {save_dir}")
def evolutionary_training(self, epochs=5):
"""
进化训练
"""
print("🧬 开始进化训练...")
for epoch in range(epochs):
print(f"\n🌟 第 {epoch+1} 代:")
# 模型变异
self.multi_modal_model = self.mutate_model(self.multi_modal_model)
# 模型评估
test_input = self.multi_modal_model.text_tokenizer("Sample input for evaluation.", return_tensors='pt').to(self.device)
complexity, performance = self.evaluate_model(self.multi_modal_model, test_input)
print(f"多模态模型 - 复杂度: {complexity}, 性能: {performance:.4f}")
def main():
# 设置随机种子
torch.manual_seed(42)
np.random.seed(42)
random.seed(42)
# 创建进化多模态神经网络
evo_network = EvolutionaryMultiModalNetwork()
# 打印模型信息
evo_network.multi_modal_model.text_generator.config # 打印模型配置示例
# 进化训练
evo_network.evolutionary_training(epochs=5)
# 保存模型
evo_network.save_models()
if __name__ == "__main__":
main()