23、Java NIO快速参考指南

Java NIO快速参考指南

1. 简介

Java NIO(New I/O)提供了一组强大的类和接口,用于高效的I/O操作。本指南将对Java NIO中的核心类和接口进行快速参考,涵盖 java.nio 和 java.nio.channels 包中的重要内容。

2. java.nio 包

java.nio 包包含了用于 java.nio.channels 和 java.nio.charset 子包中的 Buffer 类。

2.1 Buffer 类

Buffer 是所有其他缓冲区类的基类,包含了所有缓冲区类型通用的方法。以下是 Buffer 类的定义:

public abstract class Buffer {
    public final int capacity()
    public final Buffer clear()
    public final Buffer flip()
    public final boolean hasRemaining()
    public abstract boolean isReadOnly();
    public final int limit()
    public final Buffer limit (int newLimit)
    public final Buffer mark()
    public final int position()
    public final Buffer position (int newPosition)
    public final int remaining()
    public final Buffer reset()
    public final Buffer rewind()
}

2.2 异常类

  • BufferOverflowException :当缓冲区的位置等于其限制时,尝试进行简单的相对 put() 操作,或者批量 put() 操作会导致位置超过限制时,抛出此异常。
public class BufferOverflowException extends RuntimeException {
    public BufferOverflowException()
}
  • BufferUnderflowException :当缓冲区的位置等于其限制时,尝试进行简单的相对 get() 操作,或者批量 get() 操作会导致位置超过限制时,抛出此异常。
public class BufferUnderflowException extends RuntimeException {
    public BufferUnderflowException()
}

2.3 ByteBuffer 类

ByteBuffer 是所有缓冲区类中最复杂和通用的。只有字节缓冲区可以参与通道上的I/O操作以发送和接收数据。以下是 ByteBuffer 类的部分定义:

public abstract class ByteBuffer extends Buffer implements Comparable {
    public static ByteBuffer allocate (int capacity)
    public static ByteBuffer allocateDirect (int capacity)
    public final byte [] array()
    public final int arrayOffset()
    public abstract CharBuffer asCharBuffer();
    // 其他方法...
}

2.4 ByteOrder 类

ByteOrder 是一个类型安全的枚举,不能直接实例化。有两个公共可访问的 ByteOrder 实例作为静态类字段可见。提供了一个类方法来确定底层操作系统的本地字节顺序,这可能与Java平台默认的不同。

public final class ByteOrder {
    public static final ByteOrder BIG_ENDIAN
    public static final ByteOrder LITTLE_ENDIAN

    public static ByteOrder nativeOrder()
    public String toString()
}

2.5 其他缓冲区类

除了上述类, java.nio 包中还包含了 CharBuffer 、 DoubleBuffer 、 FloatBuffer 、 IntBuffer 、 LongBuffer 、 ShortBuffer 等类,它们分别管理不同类型的数据元素。以下是 CharBuffer 类的部分定义:

public abstract class CharBuffer extends Buffer implements Comparable, CharSequence {
    public static CharBuffer allocate (int capacity)
    public final char [] array()
    public final int arrayOffset()
    public abstract CharBuffer asReadOnlyBuffer();
    // 其他方法...
}

2.6 异常类

  • InvalidMarkException :当在没有设置标记的缓冲区上调用 reset() 方法时,抛出此异常。
public class InvalidMarkException extends IllegalStateException {
    public InvalidMarkException()
}
  • ReadOnlyBufferException :当在只读缓冲区上调用会修改缓冲区内容的方法(如 put() 或 compact() )时,抛出此异常。
public class ReadOnlyBufferException extends UnsupportedOperationException {
    public ReadOnlyBufferException()
}

2.7 MappedByteBuffer 类

MappedByteBuffer 是一种特殊类型的 ByteBuffer ,其数据元素是磁盘文件的内容。只能通过调用 FileChannel 对象的 map() 方法来创建 MappedByteBuffer 对象。

public abstract class MappedByteBuffer extends ByteBuffer {
    public final MappedByteBuffer force()
    public final boolean isLoaded()
    public final MappedByteBuffer load()
}

3. java.nio.channels 包

java.nio.channels 包包含了与通道和选择器相关的类和接口。

3.1 异常类

  • AlreadyConnectedException :当在已经连接的 SocketChannel 对象上调用 connect() 方法时,抛出此异常。
public class AlreadyConnectedException extends IllegalStateException {
    public AlreadyConnectedException()
}
  • AsynchronousCloseException :当一个线程在通道操作(如 read() 或 write() )上被阻塞,而另一个线程关闭了该通道时,抛出此异常。
public class AsynchronousCloseException extends ClosedChannelException {
    public AsynchronousCloseException()
}

3.2 接口类

  • ByteChannel :这是一个空的聚合接口,将 ReadableByteChannel 和 WritableByteChannel 组合成一个单一的接口,但不定义任何新方法。
public interface ByteChannel extends ReadableByteChannel, WritableByteChannel {
}
  • Channel :是所有其他通道接口的超接口,定义了所有具体通道类通用的方法。
public interface Channel {
    public void close() throws java.io.IOException;
    public boolean isOpen();
}

3.3 Channels 工具类

Channels 是一个实用类,使通道能够与传统的字节和字符流进行互操作。工厂方法返回包装对象,使通道适应流,反之亦然。返回的通道对象可能不可选择或不可中断。

public final class Channels {
    public static ReadableByteChannel newChannel (java.io.InputStream in)
    public static WritableByteChannel newChannel (java.io.OutputStream out)
    public static java.io.InputStream newInputStream (ReadableByteChannel ch)
    public static java.io.OutputStream newOutputStream (WritableByteChannel ch)
    public static java.io.Reader newReader (ReadableByteChannel ch, String csName)
    public static java.io.Reader newReader (ReadableByteChannel ch, java.nio.charset.CharsetDecoder dec, int minBufferCap)
    public static java.io.Writer newWriter (WritableByteChannel ch, String csName)
    public static java.io.Writer newWriter (WritableByteChannel ch, java.nio.charset.CharsetEncoder enc, int minBufferCap)
}

3.4 其他异常类

  • CancelledKeyException :当尝试使用已失效的 SelectionKey 对象时,抛出此异常。
public class CancelledKeyException extends IllegalStateException {
    public CancelledKeyException()
}
  • ClosedByInterruptException :当一个线程在通道操作(如 read() 或 write() )上被阻塞,而另一个线程中断了该线程时,抛出此异常。作为副作用,该线程正在休眠的通道将被关闭。
public class ClosedByInterruptException extends AsynchronousCloseException {
    public ClosedByInterruptException()
}
  • ClosedChannelException :当尝试对已关闭的通道进行操作时,抛出此异常。
public class ClosedChannelException extends java.io.IOException {
    public ClosedChannelException()
}
  • ClosedSelectorException :当尝试使用已关闭的 Selector 时,抛出此异常。
public class ClosedSelectorException extends IllegalStateException {
    public ClosedSelectorException()
}
  • ConnectionPendingException :当在非阻塞模式的 SocketChannel 对象上调用 connect() 方法,而该对象已经有一个并发连接正在进行时,抛出此异常。
public class ConnectionPendingException extends IllegalStateException {
    public ConnectionPendingException()
}

3.5 DatagramChannel 类

DatagramChannel 类提供了从 ByteBuffer 对象发送和接收数据报包的方法。

public abstract class DatagramChannel extends java.nio.channels.spi.AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel {
    public abstract DatagramChannel connect (java.net.SocketAddress remote) throws java.io.IOException;
    public abstract DatagramChannel disconnect() throws java.io.IOException;
    public abstract boolean isConnected();
    public static DatagramChannel open() throws java.io.IOException
    public abstract int read (java.nio.ByteBuffer dst) throws java.io.IOException;
    public final long read (java.nio.ByteBuffer [] dsts) throws java.io.IOException
    public abstract long read (java.nio.ByteBuffer [] dsts, int offset, int length) throws java.io.IOException;
    public abstract java.net.SocketAddress receive (java.nio.ByteBuffer dst) throws java.io.IOException;
    public abstract int send (java.nio.ByteBuffer src, java.net.SocketAddress target) throws java.io.IOException;
    public abstract java.net.DatagramSocket socket();
    public final int validOps()
    public abstract int write (java.nio.ByteBuffer src) throws java.io.IOException;
    public final long write (java.nio.ByteBuffer [] srcs) throws java.io.IOException
    public abstract long write (java.nio.ByteBuffer [] srcs, int offset, int length) throws java.io.IOException;
}

3.6 FileChannel 类

FileChannel 类提供了丰富的文件操作。只能通过在 RandomAccessFile 、 FileInputStream 或 FileOutputStream 对象上调用 getChannel() 方法来获取 FileChannel 对象。

public abstract class FileChannel extends java.nio.channels.spi.AbstractInterruptibleChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel {
    public abstract void force (boolean metaData) throws java.io.IOException;
    public final FileLock lock() throws java.io.IOException
    public abstract FileLock lock (long position, long size, boolean shared) throws java.io.IOException;
    public abstract java.nio.MappedByteBuffer map (FileChannel.MapMode mode, long position, long size) throws java.io.IOException;
    public abstract long position() throws java.io.IOException;
    public abstract FileChannel position (long newPosition) throws java.io.IOException;
    public abstract int read (java.nio.ByteBuffer dst) throws java.io.IOException;
    public final long read (java.nio.ByteBuffer [] dsts) throws java.io.IOException
    public abstract int read (java.nio.ByteBuffer dst, long position) throws java.io.IOException;
    public abstract long read (java.nio.ByteBuffer [] dsts, int offset, int length) throws java.io.IOException;
    public abstract long size() throws java.io.IOException;
    public abstract long transferFrom (ReadableByteChannel src, long position, long count) throws java.io.IOException;
    public abstract long transferTo (long position, long count, WritableByteChannel target) throws java.io.IOException;
    public abstract FileChannel truncate (long size) throws java.io.IOException;
    public final FileLock tryLock() throws java.io.IOException
    public abstract FileLock tryLock (long position, long size, boolean shared) throws java.io.IOException;
    public abstract int write (java.nio.ByteBuffer src) throws java.io.IOException;
    public final long write (java.nio.ByteBuffer [] srcs) throws java.io.IOException
    public abstract int write (java.nio.ByteBuffer src, long position) throws java.io.IOException;
    public abstract long write (java.nio.ByteBuffer [] srcs, int offset, int length) throws java.io.IOException;

    public static class FileChannel.MapMode {
        public static final FileChannel.MapMode PRIVATE
        public static final FileChannel.MapMode READ_ONLY
        public static final FileChannel.MapMode READ_WRITE

        public String toString()
    }
}

3.7 FileLock 类

FileLock 类封装了与 FileChannel 对象关联的锁区域。

public abstract class FileLock {
    public final FileChannel channel()
    public final boolean isShared()
    public abstract boolean isValid();
    public final boolean overlaps (long position, long size)
    public final long position()
    public abstract void release() throws java.io.IOException;
    public final long size()
    public final String toString()
}

总结

本部分介绍了 java.nio 和 java.nio.channels 包中的核心类和接口,包括缓冲区类、通道类、异常类等。这些类和接口为Java中的高效I/O操作提供了强大的支持。通过合理使用这些类和接口,可以实现更高效、更灵活的I/O编程。

流程图

graph TD;
    A[java.nio包] --> B[Buffer类];
    A --> C[ByteBuffer类];
    A --> D[ByteOrder类];
    A --> E[其他缓冲区类];
    A --> F[异常类];
    A --> G[MappedByteBuffer类];
    H[java.nio.channels包] --> I[异常类];
    H --> J[接口类];
    H --> K[Channels工具类];
    H --> L[其他异常类];
    H --> M[DatagramChannel类];
    H --> N[FileChannel类];
    H --> O[FileLock类];

表格

包名 类名 描述
java.nio Buffer 所有缓冲区类的基类
java.nio ByteBuffer 最复杂和通用的缓冲区类,可参与I/O操作
java.nio ByteOrder 类型安全的枚举,用于确定字节顺序
java.nio.channels DatagramChannel 用于发送和接收数据报包
java.nio.channels FileChannel 提供丰富的文件操作

3.8 各异常类及相关类的关系总结

为了更清晰地理解 java.nio.channels 包中各异常类以及相关类之间的关系,我们可以通过以下表格和流程图进行总结。

异常类/相关类 父类/接口 触发条件
AlreadyConnectedException IllegalStateException 在已连接的 SocketChannel 对象上调用 connect() 方法
AsynchronousCloseException ClosedChannelException 线程在通道操作被阻塞时,通道被其他线程关闭
CancelledKeyException IllegalStateException 尝试使用已失效的 SelectionKey 对象
ClosedByInterruptException AsynchronousCloseException 线程在通道操作被阻塞时,被其他线程中断
ClosedChannelException IOException 尝试对已关闭的通道进行操作
ClosedSelectorException IllegalStateException 尝试使用已关闭的 Selector
ConnectionPendingException IllegalStateException 在非阻塞模式的 SocketChannel 对象上调用 connect() ,且已有并发连接在进行
graph TD;
    A[IOException] --> B[ClosedChannelException];
    B --> C[AsynchronousCloseException];
    C --> D[ClosedByInterruptException];
    E[IllegalStateException] --> F[AlreadyConnectedException];
    E --> G[CancelledKeyException];
    E --> H[ClosedSelectorException];
    E --> I[ConnectionPendingException];
    J[Channel] --> K[SocketChannel];
    K --> F;
    K --> I;
    L[SelectionKey] --> G;
    M[Selector] --> H;
    N[AbstractSelectableChannel] --> O[DatagramChannel];
    P[AbstractInterruptibleChannel] --> Q[FileChannel];

4. 操作示例

4.1 ByteBuffer 的使用示例

以下是一个简单的 ByteBuffer 使用示例,展示了如何分配缓冲区、写入数据和读取数据。

import java.nio.ByteBuffer;

public class ByteBufferExample {
    public static void main(String[] args) {
        // 分配一个容量为10的ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(10);

        // 写入数据
        buffer.put((byte) 1);
        buffer.put((byte) 2);
        buffer.put((byte) 3);

        // 切换到读模式
        buffer.flip();

        // 读取数据
        while (buffer.hasRemaining()) {
            System.out.println(buffer.get());
        }
    }
}
4.2 FileChannel 的使用示例

以下示例展示了如何使用 FileChannel 读取文件内容。

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("test.txt");
             FileChannel channel = fis.getChannel()) {
            // 分配一个容量为1024的ByteBuffer
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            // 读取数据到缓冲区
            int bytesRead = channel.read(buffer);
            while (bytesRead != -1) {
                // 切换到读模式
                buffer.flip();

                // 处理缓冲区中的数据
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());
                }

                // 清空缓冲区
                buffer.clear();
                bytesRead = channel.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5. 总结与展望

Java NIO提供的类和接口为高效的I/O操作提供了强大的支持。 java.nio 包中的缓冲区类可以帮助我们更好地管理数据,而 java.nio.channels 包中的通道类和选择器类则为非阻塞I/O操作提供了可能。

通过合理使用这些类和接口,我们可以实现更高效、更灵活的I/O编程。例如,在网络编程中,可以使用 DatagramChannel 和 SocketChannel 进行数据的发送和接收;在文件操作中,可以使用 FileChannel 进行文件的读写和锁定。

未来,随着技术的不断发展,Java NIO可能会进一步优化和扩展,以满足更多的应用场景需求。例如,可能会提供更高效的缓冲区管理机制,或者支持更多类型的通道操作。开发者可以持续关注Java NIO的发展,以便在实际项目中更好地应用这些技术。

流程图

graph TD;
    A[操作示例] --> B[ByteBuffer示例];
    A --> C[FileChannel示例];
    B --> D[分配缓冲区];
    B --> E[写入数据];
    B --> F[切换到读模式];
    B --> G[读取数据];
    C --> H[创建FileInputStream];
    H --> I[获取FileChannel];
    I --> J[分配ByteBuffer];
    J --> K[读取数据到缓冲区];
    K --> L[切换到读模式];
    L --> M[处理数据];
    M --> N[清空缓冲区];
    N --> K;

表格

操作示例 步骤 代码片段
ByteBuffer示例 分配缓冲区 ByteBuffer buffer = ByteBuffer.allocate(10);
ByteBuffer示例 写入数据 buffer.put((byte) 1);
ByteBuffer示例 切换到读模式 buffer.flip();
ByteBuffer示例 读取数据 buffer.get();
FileChannel示例 创建FileInputStream FileInputStream fis = new FileInputStream("test.txt");
FileChannel示例 获取FileChannel FileChannel channel = fis.getChannel();
FileChannel示例 分配ByteBuffer ByteBuffer buffer = ByteBuffer.allocate(1024);
FileChannel示例 读取数据到缓冲区 int bytesRead = channel.read(buffer);
FileChannel示例 切换到读模式 buffer.flip();
FileChannel示例 处理数据 System.out.print((char) buffer.get());
FileChannel示例 清空缓冲区 buffer.clear();

相关推荐

关于自制的《航空学报》latex模板(非官网模板)

《航空学报》的官网只提供了word模板,并且该期刊投稿只需要pdf版本不需要源文件。为方便latex爱好者撰写论文,提供该模板及说明给广大科研人士~

qq_37105055的博客 4695

HDLBits答案汇总

前言 该博客为本人做HDLBits习题时的心得记录总结,欢迎大家一起交流进步。 HDLBits网站链接 Verilog Language Basics Vectors Modules:Hierarchy Procedures More Verilog Features Circuits Combinational Logic Basic gates Multiplexers Arithmetic Circuits Karnaugh Map to Circuit Sequential Logic Latche

谦豫 9万+

GD32F103RCT6/GD32F303RCT6(6.2)USART串口实战:从寄存器配置到DMA高效传输

本文详细介绍了GD32F103RCT6和GD32F303RCT6的USART串口通信实战,从寄存器配置到DMA高效传输的全过程。通过具体的代码示例和优化建议,帮助开发者快速掌握串口通信的关键技术,提升嵌入式开发效率。特别强调了DMA在高速数据传输中的优势和应用技巧。

weixin_33725239的博客 308

[特殊字符] 智能补货(AI-driven Replenishment)完整实施方案

补货量=预测需求+安全库存−当前库存补货量 = 预测需求 + 安全库存 - 当前库存补货量=预测需求+安全库存−当前库存。:基于 LSTM/Transformer 预测未来 30/60/90 天的销量。

nbsaas-boot基于Request-Response的企业级快速开发框架 967

Apple Store 上架过包:iOS审核4.3被拒原因分析与解决修改方案

Apple Store 上架过包:iOS审核4.3被拒原因分析与解决修改方案

专业解决各种上架过包问题,专业团队专业处理 751

【四足机器人项目实战】四足机器人嵌入式硬件设计

系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 TODO:写完再整理 文章目录系列文章目录前言一、电源管理板设计及制作二、通讯中继板设计及制作三、力控无刷电机驱动板设计及制作1.磁场定向控制FOC控制原理2.电机转子位置、速度检测原理3.硬件CAN通讯原理 前言 认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长! 本文先对四足机器人–嵌入式硬件设计做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章 提示:以下是本篇文章正文内容 一、电

qq_35635374的博客 1万+

解决Windows 11文件右键菜单卡死问题的方法

当你右键点击一个文件或者文件夹时,系统会加载与该文件类型相关的上下文菜单项,如果其中某个菜单项的代码执行时间过长或者出现错误,就会导致整个右键菜单卡死。综上所述,当你在Windows 11中遇到右键菜单卡死的问题时,你可以尝试禁用上下文菜单项、重置文件关联、排除第三方软件干扰、修复系统文件以及优化代码等方法来解决问题。如果你是一个开发者,并且你自己编写了一个上下文菜单项,那么问题可能出在你的代码中。例如,如果你遇到了图片文件的右键菜单卡死问题,你可以找到".jpg"或".png"等相关的子项。

HnrzLinux的博客 7536

部署OpenClaw时设置环境变量提示export: not valid in this context的完整解决方案

OpenClaw环境变量设置错误解决方案 问题概述 在部署OpenClaw时设置环境变量出现"export: not valid in this context"错误,通常由以下原因导致: 在非交互式shell中使用export命令 在命令替换或管道中使用export 脚本文件包含Windows换行符(CRLF) 解决方案 修正脚本执行方式:确保export命令在正确的shell上下文中使用 使用source命令:通过. setup_openclaw.sh或source setup_op

博客 458

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

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

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

深度学习工具包 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'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('DeepLearnToolbox'));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('state',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'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',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 = 'sigm';%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, 'Too big error');```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('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';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)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';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, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% 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'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 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, 'Too big error');```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('state',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, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',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, 'Too big error');%% ex3 neural net with dropoutrand('state',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, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % 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, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % 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, 'Too big error');%% 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('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % 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, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 3、Django 微服务架构:从基础到实践
下一篇: 25、Linux网络基础与应用详解
杠精协会主席
博客等级 码龄3年 75粉丝 1287原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值