package com.omega.engine.optimizer;
import com.omega.common.data.Tensor;
import com.omega.common.data.utils.DataTransforms;
import com.omega.common.utils.*;
import com.omega.engine.check.BaseCheck;
import com.omega.engine.gpu.CUDAModules;
import com.omega.engine.nn.data.BaseData;
import com.omega.engine.nn.grad.GradClipping;
import com.omega.engine.nn.layer.Layer;
import com.omega.engine.nn.network.*;
import com.omega.engine.nn.network.vae.*;
import com.omega.engine.nn.network.vqgan.LPIPS;
import com.omega.engine.nn.network.vqgan.PatchGANDiscriminator;
import com.omega.engine.optimizer.lr.LearnRateUpdate;
import com.omega.example.diffusion.utils.DiffusionImageDataLoader;
import com.omega.example.rnn.data.OneHotDataLoader;
import com.omega.example.rnn.data.RNNDataLoader;
import com.omega.example.sd.utils.SDImageDataLoader;
import com.omega.example.sd.utils.SDImageDataLoaderEN;
import com.omega.example.transformer.utils.ModelUtils;
import com.omega.example.yolo.data.BaseDataLoader;
import com.omega.example.yolo.data.DetectionDataLoader;
import com.omega.example.yolo.utils.YoloLabelUtils;
import jcuda.driver.JCudaDriver;
import jcuda.runtime.JCuda;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Mini Batch Stochastic Gradient Descent
*
* @author Administrator
*/
public class MBSGDOptimizer extends Optimizer {
private YoloLabelUtils u;
public MBSGDOptimizer(Network network, int trainTime, float error, int batchSize, boolean warmUp) throws Exception {
super(network, batchSize, trainTime, error, warmUp);
// TODO Auto-generated constructor stub
this.batchSize = batchSize;
this.loss = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.lossDiff = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
}
public MBSGDOptimizer(String sid, Network network, int trainTime, float error, int batchSize, boolean warmUp) throws Exception {
super(network, batchSize, trainTime, error, warmUp);
// TODO Auto-generated constructor stub
this.setSid(sid);
this.batchSize = batchSize;
this.loss = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.lossDiff = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
}
public MBSGDOptimizer(Network network, int trainTime, float error, int batchSize, LearnRateUpdate learnRateUpdate, boolean warmUp) throws Exception {
super(network, batchSize, trainTime, error, warmUp);
// TODO Auto-generated constructor stub
this.batchSize = batchSize;
this.loss = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.lossDiff = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.learnRateUpdate = learnRateUpdate;
}
public MBSGDOptimizer(Network network, int trainTime, float error, int batchSize, LearnRateUpdate learnRateUpdate, boolean warmUp, BaseCheck check) throws Exception {
super(network, batchSize, trainTime, error, warmUp);
// TODO Auto-generated constructor stub
this.batchSize = batchSize;
this.loss = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.lossDiff = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.learnRateUpdate = learnRateUpdate;
this.check = check;
}
public MBSGDOptimizer(String sid, Network network, int trainTime, float error, int batchSize, LearnRateUpdate learnRateUpdate, boolean warmUp) throws Exception {
super(network, batchSize, trainTime, error, warmUp);
// TODO Auto-generated constructor stub
this.setSid(sid);
this.batchSize = batchSize;
this.loss = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.lossDiff = new Tensor(batchSize, this.network.oChannel, this.network.oHeight, this.network.oWidth);
this.learnRateUpdate = learnRateUpdate;
}
public static void q_mean_variance(Tensor x_0, Tensor x_t, Tensor t, float[] posterior_mean_coef1, float[] posterior_mean_coef2, float[] posterior_mean) {
for (int b = 0; b < x_t.number; b++) {
for (int i = 0; i < x_t.getOnceSize(); i++) {
int idx = b * x_t.getOnceSize() + i;
posterior_mean[idx] = posterior_mean_coef1[b] * x_0.data[idx] - posterior_mean_coef2[b] * x_t.data[idx];
}
}
}
public static void sample_prev_timestep(DiffusionUNetCond2 network, Tensor condInput, Tensor xt, Tensor t, Tensor x0, int timestep, float[] a, float[] b, float[] betas, float[] alphas, float[] alphas_bar) {
for (int i = 0; i < xt.number; i++) {
t.data[i] = timestep;
}
t.hostToDevice();
Tensor noisePred = null;
if (condInput != null) {
noisePred = network.forward(xt, t, condInput);
} else {
noisePred = network.forward(xt, t);
}
noisePred.syncHost();
JCuda.cudaDeviceSynchronize();
if (timestep > 0) {
System.err.println("timestep:" + timestep);
float var = (1.0f - alphas_bar[timestep - 1]) / (1.0f - alphas_bar[timestep]) * betas[timestep];
float sigma = (float) Math.pow(var, 0.5);
float[] noise = RandomUtils.gaussianRandom(noisePred.dataLength, 1.0f);
for (int i = 0; i < xt.dataLength; i++) {
xt.data[i] = (float) ((xt.data[i] - (betas[timestep] * noisePred.data[i]) / b[timestep]) / Math.sqrt(alphas[timestep])) + sigma * noise[i];
}
} else {
/**
* mean
*/
for (int i = 0; i < xt.dataLength; i++) {
xt.data[i] = (float) ((xt.data[i] - (betas[timestep] * noisePred.data[i]) / b[timestep]) / Math.sqrt(alphas[timestep]));
}
}
xt.hostToDevice();
}
public static void showImgs(String outputPath, Tensor input) {
ImageUtils utils = new ImageUtils();
if (input.isHasGPU()) {
input.syncHost();
}
for (int b = 0; b < input.number; b++) {
float[] once = input.getByNumber(b);
// once = MatrixOperation.add(once, 0.5f);
utils.createRGBImage(outputPath + b + ".png", "png", ImageUtils.color2rgb2(once, input.channel, input.height, input.width, true), input.height, input.width, null, null);
}
}
public static void showImgs(String outputPath, Tensor input, String it, float[] mean, float[] std) {
ImageUtils utils = new ImageUtils();
for (int b = 0; b < input.number; b++) {
float[] once = input.getByNumber(b);
utils.createRGBImage(outputPath + it + "_" + b + ".png", "png", ImageUtils.color2rgb2(once, input.channel, input.height, input.width, true, mean, std), input.height, input.width, null, null);
}
}
public static void showImgs(String outputPath, Tensor input, String it, float[] mean, float[] std, String[] labels) {
ImageUtils utils = new ImageUtils();
if (labels != null) {
for (int b = 0; b < input.number; b++) {
float[] once = input.getByNumber(b);
String title = labels[b];
if (title.length() > 30) {
title = title.substring(0, 30);
}
utils.createRGBImage(outputPath + it + "_[" + title + "]" + b + ".png", "png", ImageUtils.color2rgb2(once, input.channel, input.height, input.width, true, mean, std), input.height, input.width, null, null);
}
} else {
for (int b = 0; b < input.number; b++) {
float[] o
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于java打造的深度学习框架,帮助你快速搭建神经网络,实现训练或测试模型,支持多GPU训练。框架目前支持BP神经网络、卷积神经网络、循环神经网络、vgg16、resnet、yolo、lstm、transformer、gpt、llama、diffusion、stable diffusion等模型的构建,目前引擎最新版本支持CUDA和CUDNN两种GPU加速方式,关于GPU加速的环境配置与jcuda版本jar包的对应依赖,引擎中所实现的模型和算法除了使用cuda和cudnn相关依赖包之外均不使用任何api和第三方依赖包。
资源推荐
资源详情
资源评论





























收起资源包目录





































































































共 1501 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论


Java程序员-张凯
- 粉丝: 1w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 培训学习中小学办公软件Office2010word学习笔记.pdf
- 恩网络品牌营销服务说明书--遇见.doc
- 证券交易所综合业务平台市场参与者接口规格说明书.doc
- 基于单片机的模拟电梯系统毕业设计.doc
- 电子商务专业教学指导方案模板.doc
- 通信工程职业生涯规划.doc
- 浅海石油作业无线电通信安全管理规定.doc
- 网络营销广告.pptx
- 国家开放大学电大专科《网络多媒体素材加工》填空题题库.docx
- 调整《AutoCAD》教材内容的授课顺序获奖科研报告论文.docx
- 智能家居之智能照明方案.docx
- 连锁餐饮信息化应用构想(业务部分).pptx
- 流水施工和网络图讲解.pdf
- 天文观测系统工程项目管理总结.doc
- 使用查账-评估软件核查账务有技巧那些?【2017至2018最新会计实务】.doc
- (源码)基于C语言uCOSII框架的乒乓球收集项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
