深度学习:利用随机数据更快地测试一个新的模型在自己数据格式很复杂的时候

技巧:

比如下面一个新的模型deeponet ,我自己的数据很复杂,这里在代码最后用用随机生成的数据,两分钟就完成了代码的测试成功。

import torch
import torch.nn as nn
import torch.optim as optim

# 带偏置项的 DeepONet 结构,包括 Branch 和 Trunk 网络
class DeepONet(nn.Module):
    def __init__(self, branch_input_dim, trunk_input_dim, hidden_dim):
        super(DeepONet, self).__init__()
        
        # Branch 网络,用于处理输入点云的特征(例如位移量、压强)
        self.branch_net = nn.Sequential(
            nn.Linear(branch_input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim)
        )
        
        # Trunk 网络,用于处理时间和空间坐标 [x, y, z, t]
        self.trunk_net = nn.Sequential(
            nn.Linear(trunk_input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim)
        )
        
        # 偏置项 bias
        self.bias = nn.Parameter(torch.zeros(1))  # 可训练的偏置项
        
        # 最终的输出层,预测位移或压强等物理状态
        self.fc_output = nn.Linear(hidden_dim, 3)
    
    def forward(self, point_features, coord_time):
        # Branch网络的输出
        branch_output = self.branch_net(point_features)
        
        # Trunk网络的输出
        trunk_output = self.trunk_net(coord_time)
        
        # 将 Branch 和 Trunk 的输出结合,计算最终的输出
        combined = branch_output * trunk_output
        output = self.fc_output(combined) + self.bias  # 加上偏置项
        
        return output

# 数据准备
# 输入的数据格式:
# point_features:3D点云的物理特征(例如位移量 pointDisplacement、压强 p)
# coord_time:空间位置和时间 [x, y, z, t]

# 示例数据的维度设置
branch_input_dim = 3  # 例如 [pointDisplacement, p, ...] 
trunk_input_dim = 4   # [x, y, z, t]
hidden_dim = 64       # 隐藏层维度,可根据需求调整

# 模型初始化
model = DeepONet(branch_input_dim, trunk_input_dim, hidden_dim)

# 损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 训练流程
def train(model, point_features, coord_time, target, epochs=1000):
    for epoch in range(epochs):
        optimizer.zero_grad()
        
        # 前向传播
        output = model(point_features, coord_time)
        
        # 计算损失
        loss = criterion(output, target)
        
        # 反向传播和优化
        loss.backward()
        optimizer.step()
        
        if epoch % 100 == 0:
            print(f"Epoch {epoch}, Loss: {loss.item()}")

# 示例数据,实际应用时需要替换为真实数据
N = 1000  # 样本数量
point_features = torch.randn(N, branch_input_dim)  # 3D点云的物理特征
coord_time = torch.randn(N, trunk_input_dim)       # [x, y, z, t]
target = torch.randn(N, 3)                         # 目标物理状态

# 训练模型
train(model, point_features, coord_time, target, epochs=1000)

# 推理:给定新的时空点,预测物理状态
def predict(model, point_features, coord_time):
    model.eval()
    with torch.no_grad():
        prediction = model(point_features, coord_time)
    return prediction

# 示例推理
new_point_features = torch.randn(1, branch_input_dim)
new_coord_time = torch.tensor([[0.5, 0.5, 0.5, 0.1]])  # 在 t=0.1 的 (0.5, 0.5, 0.5) 空间点
prediction = predict(model, new_point_features, new_coord_time)
print("Predicted state:", prediction)

输出如下:

Epoch 0, Loss: 1.0260347127914429
Epoch 100, Loss: 0.7669863104820251
Epoch 200, Loss: 0.5786211490631104
Epoch 300, Loss: 0.4749055504798889
Epoch 400, Loss: 0.41076529026031494
Epoch 500, Loss: 0.36538082361221313
Epoch 600, Loss: 0.39494913816452026
Epoch 700, Loss: 0.30206459760665894
Epoch 800, Loss: 0.2839098572731018
Epoch 900, Loss: 0.2648167908191681
Predicted state: tensor([[-0.2604,  0.2214,  0.5066]])

Process finished with exit code 0
相关推荐
Fuweizn11 分钟前
在工业生产中,物料搬运环节至关重要,搬运机器人开启新篇章
人工智能·智能机器人·复合机器人
AL.千灯学长2 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
LCG元2 小时前
大模型驱动的围术期质控系统全面解析与应用探索
人工智能
lihuayong2 小时前
计算机视觉:主流数据集整理
人工智能·计算机视觉·mnist数据集·coco数据集·图像数据集·cifar-10数据集·imagenet数据集
政安晨2 小时前
政安晨【零基础玩转各类开源AI项目】DeepSeek 多模态大模型Janus-Pro-7B,本地部署!支持图像识别和图像生成
人工智能·大模型·多模态·deepseek·janus-pro-7b
一ge科研小菜鸡3 小时前
DeepSeek 与后端开发:AI 赋能云端架构与智能化服务
人工智能·云原生
冰 河3 小时前
‌最新版DeepSeek保姆级安装教程:本地部署+避坑指南
人工智能·程序员·openai·deepseek·冰河大模型
维维180-3121-14553 小时前
AI赋能生态学暨“ChatGPT+”多技术融合在生态系统服务中的实践技术应用与论文撰写
人工智能·chatgpt
終不似少年遊*3 小时前
词向量与词嵌入
人工智能·深度学习·nlp·机器翻译·词嵌入
杜大哥3 小时前
如何在WPS打开的word、excel文件中,使用AI?
人工智能·word·excel·wps