Spring Boot整合Apache Hop 2.3.0实战:从零搭建数据管道(附完整代码)

Apache Hop-使用介绍【持续完善中】 您在“创建管道”中学习了如何创建管道。您将在管道画布上执行许多操作,但是从管道编辑器的主工具栏中还可以获得许多额外的功能。还有另外两个重要的工具栏用于处理单元测试以及项目和环境。查看相关页面,了解有关管理项目和环境的更多信息,并了解如何为管道编写单元测试。让我们看看最上面的工具栏:管道是Hop项目的基本构建块。管道完成了繁重的工作:它们从各种来源读取数据,执行许多操作(合并、清理、完善、转换等),并将数据写入某些目标平台。管道以预定义的顺序并并行地执行所有这些操作。在下面的图片中,一个非常简单的管道从数据库 阅读详情

Spring Boot整合Apache Hop 2.3.0实战:从零搭建数据管道(附完整代码)

如果你已经熟悉了Spring Boot的Web开发,构建过几个微服务,但每当面对跨数据库的数据同步、定时ETL任务或者复杂的数据清洗流程时,总觉得用纯Java代码写起来既繁琐又难以维护,那么这篇文章就是为你准备的。数据集成是现代应用开发中绕不开的一环,但将其优雅地嵌入到我们熟悉的Spring生态中,却常常让人感到棘手。Apache Hop的出现,为这个痛点提供了一个非常“Spring化”的解决方案——它不是一个需要独立部署的重型调度系统,而是一个可以像普通库一样被引入、初始化和调用的引擎。

想象一下,你可以在一个标准的Spring Boot应用里,用几行代码就触发一个设计好的数据管道,这个管道可能连接着MySQL、Kafka、HDFS,进行着过滤、转换、聚合,而你无需关心线程池、连接管理和异常重试的底层细节。Apache Hop正是这样一个专注于可视化编排和嵌入式执行的数据集成框架。它脱胎于老牌的Kettle(Pentaho Data Integration),但架构完全重构,更轻量、更模块化,尤其适合云原生和微服务场景。本文将带你彻底走通这条路:从在一个全新的Spring Boot项目中引入Hop依赖开始,到解决令人头疼的插件加载和环境初始化,最后实现一个可通过HTTP API触发的、带参数注入的完整数据管道。我们会避开那些官方文档语焉不详的坑,分享我实际整合过程中踩过的雷和总结的最佳实践。

1. 项目初始化与环境搭建

万事开头难,整合的第一步往往卡在依赖和环境配置上。Apache Hop虽然核心清晰,但其依赖树和初始化方式与常见的Spring Boot Starter有些不同,需要一些手动配置才能和谐共处。

1.1 Maven依赖配置详解

创建一个标准的Spring Boot项目后,第一件事就是配置pom.xml。Apache Hop的组件是分模块发布的,这意味着我们可以按需引入,减少不必要的臃肿。对于大多数集成场景,我们需要以下几个核心模块:

<properties>
    <java.version>1.8</java.version>
    <!-- 明确指定Hop版本,便于统一管理 -->
    <hop.version>2.3.0</hop.version>
</properties>

<dependencies>
    <!-- Spring Boot基础依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Apache Hop 核心引擎 -->
    <dependency>
        <groupId>org.apache.hop</groupId>
        <artifactId>hop-engine</artifactId>
        <version>${hop.version}</version>
    </dependency>
    <!-- Beam执行引擎支持(用于本地执行) -->
    <dependency>
        <groupId>org.apache.hop</groupId>
        <artifactId>hop-engine-beam</artifactId>
        <version>${hop.version}</version>
    </dependency>
    <!-- Hop核心库,包含元数据、变量等基础功能 -->
    <dependency>
        <groupId>org.apache.hop</groupId>
        <artifactId>hop-core</artifactId>
        <version>${hop.version}</version>
    </dependency>

    <!-- 测试依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

这里有几个关键点需要注意:

  • hop-engine-beam:如果你计划主要在本地(local)环境运行管道或工作流,这个依赖是必须的。它提供了基于Apache Beam的本地执行器,性能不错。
  • 版本一致性:务必确保所有Hop组件的版本号完全一致,否则可能会引发难以排查的类加载或API不兼容错误。
  • 可选依赖:hop-ui模块通常用于桌面图形界面,在无头服务器环境中运行时不必要引入。数据库驱动(如MySQL Connector)应根据你管道中实际使用的数据源额外添加。

注意:由于Apache Hop的依赖可能从Apache官方仓库下载,国内网络环境有时不稳定。建议在Maven的settings.xml中配置阿里云等国内镜像仓库以加速下载。

1.2 配置文件与Hop环境初始化

依赖搞定后,接下来是配置。Hop需要知道它的一些基础目录在哪里,比如插件目录、项目元数据目录等。最优雅的方式是将这些路径外部化,放在Spring Boot的application.yml或application.properties中。

# application.yml
spring:
  application:
    name: hop-spring-demo

server:
  port: 8080

# Apache Hop 核心配置
hop:
  # 插件安装的基础目录,包含各种数据库插件、转换步骤等
  plugin-base-folders: /opt/apache-hop/plugins
  # 项目文件夹路径,里面存放你的管道(.hpl)、工作流(.hwf)文件
  project-folder: /opt/apache-hop/projects/my-project
  # 元数据文件夹路径,存放数据库连接、变量等配置信息
  metadata-folder: ${hop.project-folder}/metadata

配置写好了,但Hop环境不会自动启动。我们需要一个地方在Spring应用启动后,立即初始化Hop。实现ApplicationRunner或CommandLineRunner接口是一个完美选择。

package com.example.hopdemo.config;

import org.apache.hop.core.HopEnvironment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class HopEnvInitializer implements ApplicationRunner {

    @Value("${hop.plugin-base-folders}")
    private String pluginBaseFolders;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("正在初始化Apache Hop环境...");
        
        // 1. 设置插件目录系统属性(必须在HopEnvironment.init()之前)
        if (pluginBaseFolders != null && !pluginBaseFolders.isEmpty()) {
            System.setProperty("HOP_PLUGIN_BASE_FOLDERS", pluginBaseFolders);
        } else {
            // 提供一个默认的、相对路径的插件目录,方便开发
            String defaultPluginPath = new File("hop/plugins").getAbsolutePath();
            System.setProperty("HOP_PLUGIN_BASE_FOLDERS", defaultPluginPath);
            System.out.println("未配置插件目录,使用默认路径: " + defaultPluginPath);
        }

        // 2. 初始化Hop环境
        try {
            HopEnvironment.init();
            System.out.println("Apache Hop环境初始化成功。");
        } catch (Exception e) {
            System.err.println("Apache Hop环境初始化失败!");
            e.printStackTrace();
            // 根据你的策略决定是否要终止应用启动
            // throw new RuntimeException("Hop初始化失败", e);
        }
    }
}

这段代码干了三件重要的事:

  1. 设置系统属性:HOP_PLUGIN_BASE_FOLDERS
Apache Hop使用 hop 阅读详情

相关推荐

Spring Boot整合Apache Hop 2.3.0实战:从零搭建ETL数据管道(附完整代码)

本文详细介绍了如何在Spring Boot应用中深度集成Apache Hop 2.3.0,以工程化方式构建企业级ETL数据管道。内容涵盖从依赖管理、环境配置、运行时初始化到核心执行引擎的封装,并提供了异步执行、动态变量注入等高级集成模式与完整代码实践,帮助开发者将数据处理能力无缝内化到Java应用中。

bb456的博客 776

Spring Boot集成Apache Hop:Java API构建数据管道实战

数据同步与ETL是后端开发中的常见需求,MySQL到PostgreSQL的跨库数据迁移往往伴随着字段清洗和异常过滤。传统硬编码方式在规则复杂时维护成本高,可视化编排工具则提供了更灵活的解决方案。Apache Hop作为一款开源数据集成引擎,以Transform和Hop为核心模型,支持通过Java API在应用内构建Pipeline。本文将Apache Hop嵌入Spring Boot,利用其Java API实现数据管道的动态装配与执行,在不依赖独立服务的前提下完成同步任务。文章从环境配置、核心API解析到实

weixin_27051161的博客 169

Apache Hop嵌入Spring Boot:构建可控数据管道的工程实践

在数据驱动的业务场景中,ETL(抽取、转换、加载)是连接原始数据与分析价值的核心桥梁。传统的数据处理往往依赖硬编码脚本或重量级工具,前者难以应对频繁变更的清洗与校验规则,后者则因集成复杂度高而难以融入现有Java服务体系。Apache Hop作为一款模块化、可嵌入的数据编排平台,通过Pipeline与Workflow将数据来源、转换逻辑与目标存储抽象为可视化资产,并基于Java API支持与Spring Boot深度整合。其轻量级架构和丰富的转换组件,为CSV文件解析、数据清洗、定时同步、SQLite落库等

weixin_29063681的博客 232

Apache Hop集成Spring Boot实战:从配置到执行HPL文件

本文详细介绍了将Apache Hop集成到Spring Boot应用中的完整实战方案。通过配置核心依赖、初始化Hop环境、编写服务层逻辑,实现了在Java应用中程序化执行HPL管道文件,从而将可视化ETL能力无缝嵌入微服务架构,提升数据处理的自动化与可维护性。

blue42的博客 478

Apache Hop与Spring Boot集成实战指南

本文详细介绍了如何在Spring Boot项目中集成Apache Hop,实现高效的数据集成与处理。通过实战案例展示了依赖配置、环境初始化、管道执行等关键步骤,并提供了性能优化和常见问题解决方案,帮助开发者快速掌握这一轻量级ETL工具的集成技巧。

weixin_28826521的博客 317

Apache给AI工程的四堂课:模块化、依赖管理与数据管道实践

软件工程中的基础设施与治理机制,往往决定了系统的长期生命力。Apache软件基金会通过二十余年实践,在依赖管理、构建可重复性、模块化架构和兼容性演进方面积累了成熟方法论。这些原理不仅适用于传统后端,也是AI应用落地时规避风险的关键。当模型选型与提示词优化被过度聚焦时,依赖冲突、数据管道不可重复、部署环境不一致等问题反而成为生产环境的绊脚石。通过Maven集中管理依赖、Spark Pipeline固化特征计算、接口隔离实现模型适配层抽象,团队可以构建稳定且可演进的AI系统。本文结合Spring AI演示模块化

weixin_33853827的博客 337

Kafka Event Streaming 本质:事件日志与流式架构原理

Event Streaming 是一种以不可变、带时间戳的事件事实为核心的数据处理范式,区别于传统消息队列的瞬时指令模型;其底层依赖分布式日志(Log)、分区(Partition)、副本(Replica)、消费者组(Consumer Group)和位移(Offset)五大机制协同实现高吞吐、可重放、多视角分析能力;该范式支撑实时风控、用户行为分析、CDC同步等强时效性场景,也是 Flink 消费 Kafka、Spring Boot 集成、Kafka Connect 数据管道等工程实践的理论根基;理解 Kaf

weixin_30235225的博客 364

【信息科学与工程学】计算机科学与自动化——第一百三十五篇 开发框架篇 101开发框架对比

EJB 事务是“容器替你管到底”的重量级契约,Spring 事务是“你定义规则、容器帮你执行”的轻量级抽象。对比点数学表达含义EJB 事务成本重但标准Spring 本地事务成本轻量Spring JTA 事务成本介于两者之间代理开销Spring AOP 每次调用额外成本NESTED 保存点仅部分回滚时支付XA 2PC分布式强一致代价自调用失效(无代理)Spring AOP 陷阱事务同步回调Spring 特有扩展点对比维度MyBatisVue 3React 18FastAPI。

weixin_49199313的博客 219

【信息科学与工程学】【数据中心】第三十五篇 云计算数据中心的学科知识04

编号学科(课程)核心知识点在云计算/云存储/云网络/云安全/云MaaS中的作用代表教材/资料/论文 + 数学方程式列表工业界应用D1421​云原生数据库:TiDB​HTAP(混合事务/分析处理)、分布式SQL、水平扩展、强一致(Raft)、自动故障恢复、与MySQL兼容、TiFlash列式引擎提供弹性扩展的分布式数据库;支撑高并发在线交易与实时分析教材:《TiDB in Action》PingCAP(2020) 论文:《TiDB: A Raft-based HTAP Database》(2017)

weixin_49199313的博客 442

C语言 函数

一、什么是函数 二、函数的相关语法 三、函数编码规范

m0_57105760的博客 1892

南邮-软件设计实验(C++版)

南邮软件设计 A档 一、分数统计 课题内容:   设计一个分数统计程序。包括学生信息的输入输出以及排序。通过该课题全面熟悉数组、字符串、文件的使用,掌握程序设计的基本方法及友好界面的设计。 课题要求: (1)输入某班级学生的姓名、分数; (2)对(1)的分数进行降幂排列并输出; (3)具有输入输出界面。 示例代码: #include<bits/stdc++.h> using...

Dainelcw 1万+

5分钟搞定!用FunASR在本地快速部署达摩院Paraformer语音识别模型

本文提供了一份快速部署指南,教你如何在5分钟内利用FunASR工具包在本地电脑上启动达摩院Paraformer语音识别模型。Paraformer作为一款非自回归端到端模型,以其推理速度快、识别精度高的特点,结合FunASR的一键式模型加载和统一API,让开发者能轻松构建本地语音识别系统,极大降低了前沿AI技术的应用门槛。

q9w8e7r6t5的博客 610

在VS2019里把JSBSim飞行模型集成到你的C++项目里(附完整配置流程和常见错误解决)

本文详细介绍了如何在Visual Studio 2019中将JSBSim飞行模型集成到C++项目中,包括环境准备、源码编译、项目配置和常见错误解决方案。通过实战技巧和优化建议,帮助开发者高效实现高精度飞行仿真功能,提升开发效率。

weixin_42620202的博客 416

Altium Designer 16来自原理图/PCB的各种报错 (持续更新):

NO1.Nets with only one pin: 根本原因: 在BGA的原理图绘制时,我一般习惯拉出该元件的所有IO的网络,这方便利于我开发其他款板卡。同样也会因为不同板卡的需求不同而造成有些IO并没有使用到,也就是整个原理图中only one pin。 解决方案: 鼠标右键工程文件>工程参数>下图圈中的选项将“错误”更改为“警告/不报告” ...

CSDN@猿谷攻城狮 1万+

CH582低功耗实战:从1.2mA降到5uA,我的蓝牙广播功耗优化笔记

本文详细记录了沁恒CH582蓝牙芯片从1.2mA降至5μA的功耗优化全过程。通过建立基准测试环境、分层排查软件配置与IO状态、精细调优射频参数以及硬件层面的电源管理,实现了极致的低功耗表现。文章还提供了实用的功耗测量方法和典型问题排查指南,为蓝牙设备开发者提供了宝贵的参考经验。

weixin_30368405的博客 578

MyBatis-Plus 实战:敏感字段自动加解密支持模糊查询

本文基于MyBatis-Plus框架提出了一种全链路敏感字段加密方案。通过自定义TypeHandler实现数据入库自动加密和查询自动解密,结合重写LambdaQueryWrapper处理查询条件加密,并适配多种查询方式。方案核心包括:1)AESUtil工具类提供加解密基础能力;2)EncryptTypeHandler实现字段类型转换;3)实体类通过注解配置加密字段。该方案在保证数据安全的同时,对业务代码侵入性小,支持姓名、手机号等敏感信息的全流程加密处理,满足系统开发的安全需求。

蔡定努 597

代谢组数据分析(四):多种差异分析方法筛选代谢物标记物

本文详细介绍了代谢组学中常用的差异分析方法,包括倍数变化法、T检验、VIP值在OPLS-DA中的应用。通过这些方法,研究者能够筛选出在不同组别间具有显著差异的代谢物,为疾病诊断和治疗提供线索。文章还提供了一个整合的R函数run_metabolomeDA,便于研究人员进行一站式差异分析,并展示了如何生成火山图来可视化结果。

专注生信领域 6788

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 如何利用VS Code高效调试AutoDock4-GPU项目?Ubuntu20.04.2LTS实战教程
下一篇: 从GROUP BY到OVER:用MySQL窗口函数重构电商报表的完整指南
web99
博客等级 码龄18年 89粉丝 1303原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址