VB.NET中的日期时间转换

VB 获取不同的时间戳 (GMT、UTC 和 GMT+8) 阅读详情

http://blog.youkuaiyun.com/tyouvivi/article/details/1567731

刚刚学习VB.NET不久,在做几个小例子的时候发现经常要对日期时间和字符串之间进行转换,现整理如下,如有不正确的地方请大家指出。


 

1.字符型和日期型之间的转换

日期型转换为字符型是最简单的,可以直接转换。

Dim  dt  As   Date   =  Now
Debug.WriteLine(
" dt.ToLongDateString =  "   +  dt.ToLongDateString)
Debug.WriteLine(
" dt.ToLongTimeString =  "   +  dt.ToLongTimeString)
Debug.WriteLine(
" dt.ToShortDateString =  "   +  dt.ToShortDateString)
Debug.WriteLine(
" dt.ToShortTimeString =  "   +  dt.ToShortTimeString)
Debug.WriteLine(
" dt.ToFileTime =  "   +  dt.ToFileTime.ToString)
Debug.WriteLine(
" dt.ToFileTimeUtc =  "   +  dt.ToFileTimeUtc.ToString)
Debug.WriteLine(
" dt.ToLocalTime =  "   +  dt.ToLocalTime.ToString)
Debug.WriteLine(
" dt.ToOADate =  "   +  dt.ToOADate.ToString)
Debug.WriteLine(
" dt.ToUniversalTime =  "   +  dt.ToUniversalTime.ToString)
Debug.WriteLine(
" dt.ToString =  "   +  dt.ToString)
Debug.WriteLine(
" dt.ToString(yyyy/MM/dd hh:mm:ss:fff) =  "   +  dt.ToString( " yyyy/MM/dd hh:mm:ss:fff " ))

结果如下:
dt.ToLongDateString 
=  2007年4月16日
dt.ToLongTimeString 
=   15 : 10 : 21
dt.ToShortDateString 
=   2007 / 04 / 16
dt.ToShortTimeString 
=   15 : 10
dt.ToFileTime 
=   128211774217140929
dt.ToFileTimeUtc 
=   128211774217140929
dt.ToLocalTime 
=   2007 / 04 / 16   15 : 10 : 21
dt.ToOADate 
=   39188.6321957639
dt.ToUniversalTime 
=   2007 / 04 / 16   6 : 10 : 21
dt.ToString 
=   2007 / 04 / 16   15 : 10 : 21
dt.ToString(yyyy
/ MM / dd hh:mm:ss:fff)  =   2007 / 04 / 16   03 : 10 : 21 : 714  

用dt.ToString("yyyy/MM/dd hh:mm:ss:fff")是最方便的,可以转换为任意一种想要的字符串。
其中的毫秒部分最大有7位,【fff】表示只取前三位,【fffffff】表示取全部7位。 
上面的例子执行结果发现了一个奇怪的问题,执行结果中【dt.ToString = 2007/04/16 15:10:21】, 
而【dt.ToString(yyyy/MM/dd hh:mm:ss:fff) = 2007/04/16 03:10:21:714】,两者的小时部分相差了12,
网上好多帖子都说是因为计时开始时间的问题,我觉得肯定是转换时候的问题,不然为什么【ToString()】是正确的,
估计应该是时间表示的问题,试着把【ToString("yyyy/MM/dd HH:mm:ss:fff"))】中的【hh】改成大写的【HH】,执行OK。

dt.ToString(yyyy / MM / dd hh:mm:ss:fff)  =   2007 / 04 / 16   03 : 13 : 13 : 884  
dt.ToString(yyyy
/ MM / dd HH:mm:ss:fff)  =   2007 / 04 / 16   15 : 13 : 13 : 884

特别值得注意的是月【MM】和分【mm】,如果两者写错了很难发现。 大小写需要注意的地方:

年     Year            yyyy             小写固定
月    
Month          MM               大写固定
日    
Day             dd                小写固定
时    
Hour            HH或者hh       大小写均可
分    
Minute         mm               小写固定
秒    
Second        ss                小写固定
毫秒 Millisecond   ff或者FF         大小写均可(最大7位) 

Dim  dt  As  DateTime  =  Now
Debug.WriteLine(
" dt.ToString(yyyy/MM/dd hh:mm:ss:fff) =  "   +  dt.ToString( " yyyy/MM/dd hh:mm:ss:fff " ))
Debug.WriteLine(
" dt.ToString(yyyy/MM/dd hh:mm:ss:fffff) =  "   +  dt.ToString( " yyyy/MM/dd hh:mm:ss:fffff " ))
Debug.WriteLine(
" dt.ToString(yyyy/MM/dd hh:mm:ss:fffffff) =  "   +  dt.ToString( " yyyy/MM/dd hh:mm:ss:fffffff " ))
Debug.WriteLine(
" dt.ToString(YYYY/MM/DD HH:MM:SS:FFF) =  "   +  dt.ToString( " YYYY/MM/DD HH:MM:SS:FFF " ))
Debug.WriteLine(
" dt.ToString(yyyy/mm/dd hh:mm:ss:fff) =  "   +  dt.ToString( " yyyy/mm/dd hh:mm:ss:fff " ))

结果如下:
dt.ToString(yyyy
/ MM / dd hh:mm:ss:fff)  =   2007 / 04 / 16   03 : 15 : 13 : 887
dt.ToString(yyyy
/ MM / dd hh:mm:ss:fffff)  =   2007 / 04 / 16   03 : 15 : 13 : 88783
dt.ToString(yyyy
/ MM / dd hh:mm:ss:fffffff)  =   2007 / 04 / 16   03 : 15 : 13 : 8878378
dt.ToString(YYYY
/ MM / DD HH:MM:SS:FFF)  =  YYYY / 04 / DD  15 : 04 :SS: 887
dt.ToString(yyyy
/ mm / dd hh:mm:ss:fff)  =   2007 / 15 / 16   03 : 15 : 13 : 887  

也可以用Format(Date,Format)进行转换 

Dim  dt  As  DateTime  =  Now
Debug.WriteLine(
Format (dt,  " yyyy/MM/dd HH:mm:ss:fff " ))

结果如下:
2007 / 04 / 16   16 : 24 : 09 : 758

其他的日期型转换为字符型都可以先转换为Date型或者DateTime型,然后再通过ToString转换为字符型。 

字符型转换为日期型最直接的方法就是解析日期字符串,取得年,月,日,时,分,秒,组织成日期形式【yyyy/MM/dd】或者【yyyy/MM/dd hh:mm:ss】,然后用CDate转换为日期型 

Dim  strDate  As   String   =   " 20050101 "
Dim  strTime  As   String   =   " 12:34:56 "
Dim   year   As   String   =  strDate.Substring( 0 ,  4 )
Dim   month   As   String   =  strDate.Substring( 4 ,  2 )
Dim   day   As   String   =  strDate.Substring( 6 ,  2 )
Dim  dt  As   Date   =   CDate ( year   +   " / "   +   month   +   " / "   +   day   +   "   "   +  strTime)    ' 组织成 yyyy/MM/dd hh:mm:ss 格式
Dim  dt2  As   Date   =   CDate ( year   +   " / "   +   month   +   " / "   +   day )
Debug.WriteLine(
" dt.ToString =  "   +  dt.ToString)
Debug.WriteLine(
" dt2.ToString =  "   +  dt2.ToString)

结果如下:
dt.ToString 
=   2005 / 01 / 01   12 : 34 : 56
dt2.ToString 
=   2005 / 01 / 01   0 : 00 : 00

   或者是用DateTime的构造函数,DateTime的构造函数有好多个

名称

说明

DateTime (Int64)

将 DateTime 结构的新实例初始化为指定的刻度数。

DateTime (Int64, DateTimeKind)

将 DateTime 结构的新实例初始化为指定的刻度数以及协调通用时间 (UTC) 或本地时间。  

DateTime (Int32, Int32, Int32)

将 DateTime 结构的新实例初始化为指定的年、月和日。

DateTime (Int32, Int32, Int32, Calendar)

将 DateTime 结构的新实例初始化为指定日历的指定年、月和日。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32)

将 DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

将 DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟和秒。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

将 DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调通用时间 (UTC) 或本地时间。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, Int32)

将 DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

将 DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

将 DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调通用时间 (UTC) 或本地时间。

DateTime (Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

将 DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调通用时间 (UTC) 或本地时间。

 
 
   还有就是直接定义一个日期型的变量,不过要注意必须是一下格式【MM/dd/yyyy HH:mm:ss.fffffff】,其中【MM/dd/yyyy】是必须项目

Dim  d  As  DateTime  =   " 4/13/2007 12:34:56.123456 PM "
Debug.WriteLine(d.ToString(
" yyyy/MM/dd HH:mm:ss:fff " ))

执行结果:
2007 / 04 / 13   12 : 34 : 56 : 123   
2.Double型和日期型之间的转换

在一些数据库中保存日期的时候不是用日期型而是把日期转换成double型之后放入数据库,这样的情况下
就需要将Double转化为日期型。
    例如:39188.7805780093 = 2007/04/16 18:44:01:940
    39188表示天数,0.7805780093 表示 time.Second/(24*3600)的值。
    在DateTime的构造函数里有一个单一参数的构造函数【DateTime (Int64)】将 DateTime 结构的新实例初始化为指定的刻度数(Ticks)。
    日期零点值为1899年12月30日0点0分0秒0毫秒以来已经过的时间的以 100 毫微秒为间隔的间隔数。1秒钟是10,000,000 Ticks。
    从而可以将Double型先转化为Long型的Ticks,计算方法如下
    39188.7805780093 (天)*24(小时)*3600(秒)*10000000(100 毫微秒) = 33859106419400000

Dim  dblTicks  As   Double   =   39188.7805780093
Dim  dtime  As  DateTime  =   New  DateTime(dblTicks  *   24   *   3600   *   10000000 )
Debug.WriteLine(
" dtime.ToString(yyyy/MM/dd HH:mm:ss:fff) =  "   +  dtime.ToString( " yyyy/MM/dd HH:mm:ss:fff " ))

执行结果:
dtime.ToString(yyyy
/ MM / dd HH:mm:ss:fff)  =   0108 / 04 / 18   18 : 44 : 01 : 940

执行结果年日不正确,大概少了1900年,可能是因为从1899年12月30日开始计算的,要把计时开始的时间加上
1899年 12 月 30 日午夜 12:00:00开始的毫微秒数 = 599264352000000000

Dim  dt  As  DateTime  =   New  DateTime( " 1899 " ,  " 12 " ,  " 30 " )
Debug.WriteLine(dt.Ticks.ToString)

执行结果:
599264352000000000

将上面的例子改写为下面的样子,结果就正确了

Dim  dblTicks  As   Double   =   39188.7805780093
Dim  dtime  As  DateTime  =   New  DateTime(dblTicks  *   24   *   3600   *   10000000   +   New  DateTime( " 1899 " ,  " 12 " ,  " 30 " ).Ticks)
Debug.WriteLine(
" dtime.ToString(yyyy/MM/dd HH:mm:ss:fff) =  "   +  dtime.ToString( " yyyy/MM/dd HH:mm:ss:fff " ))

执行结果:
dtime.ToString(yyyy
/ MM / dd HH:mm:ss:fff)  =   2007 / 04 / 16   18 : 44 : 01 : 940

这样的方法用起来肯定很麻烦,DateTime还有另外一个从Double转换为Date型的方法,FromOADate(double),这样子就方便多了。当然,从日期型转换为Double型的话也很方便了,用Date.ToOaDate()就可以了.

Dim  dt  As  DateTime  =  DateTime.FromOADate( 39188.7805780093 )
Debug.WriteLine(
Format (dt,  " yyyy/MM/dd HH:mm:ss:fff " ))
Debug.WriteLine(
" dt.ToOADate.ToString =  "   +  dt.ToOADate.ToString)

执行结果:
2007 / 04 / 16   18 : 44 : 01 : 940
dt.ToOADate.ToString 
=   39188.7805780093
3.Long型和日期型之间的转换

Long型一般是表示为 Windows 文件时间的当前 DateTime 对象的值,它表示自协调通用时间 (UTC) 公元 (C.E.) 1601 年 1 月 1 日午夜 12:00 以来已经过的时间的以 100 毫微秒为间隔的间隔数。Windows 使用文件时间记录应用程序创建、访问或写入文件的时间。用Date.FromFileTime(long)方法。还有一个FromFileTimeUtc(long)方法,是将当前文件时间转换为同时的UTC(协调通用时间)

Dim  l  As   Long   =   128208176211955002
Dim  dt  As  DateTime  =  DateTime.FromFileTime( 128208176211955002 )
Dim  dt2  As  DateTime  =  DateTime.FromFileTimeUtc( 128208176211955002 )
Debug.WriteLine(dt.ToString)
Debug.WriteLine(dt2.ToString)

执行结果:
2007 / 04 / 12   11 : 13 : 41
2007 / 04 / 12   2 : 13 : 41
现在这里是东9区,刚好时间上差了9小时

同样,日期型转换为Long型用Date.ToFileTime()方法。另外还有一个ToFileTimeUtc()方法,两者在指定Kind的情况下是相同的,在没有指定Kind的情况下内容是不同的。
如下例中,DateTimeKind指定DateTime对象是表示本地时间、协调通用时间 (UTC),还是既不指定为本地时间,也不指定为 UTC。Local 表示的时间为本地时间。  Unspecified 表示的时间既未指定为本地时间,也未指定为协调通用时间 (UTC)。  Utc 表示的时间为 UTC。 

Dim  dt  As  DateTime  =  DateTime.FromFileTime( 128208176211955002 )
Debug.WriteLine(
" DateTimeKind = Null " )
Debug.WriteLine(dt.ToFileTime.ToString)
Debug.WriteLine(dt.ToFileTimeUtc().ToString)
dt 
=  DateTime.SpecifyKind(dt, DateTimeKind.Unspecified)
Debug.WriteLine(
" DateTimeKind = Unspecified " )
Debug.WriteLine(dt.ToFileTime.ToString)
Debug.WriteLine(dt.ToFileTimeUtc().ToString)
dt 
=  DateTime.SpecifyKind(dt, DateTimeKind.Local)
Debug.WriteLine(
" DateTimeKind = Local " )
Debug.WriteLine(dt.ToFileTime.ToString)
Debug.WriteLine(dt.ToFileTimeUtc().ToString)
dt 
=  DateTime.SpecifyKind(dt, DateTimeKind.Utc)
Debug.WriteLine(
" DateTimeKind = Utc " )
Debug.WriteLine(dt.ToFileTime.ToString)
Debug.WriteLine(dt.ToFileTimeUtc().ToString)

执行结果:
DateTimeKind 
=  Null
128208176211955002
128208176211955002
DateTimeKind 
=  Unspecified
128208176211955002
128208500211955002
DateTimeKind 
=  Local
128208176211955002
128208176211955002
DateTimeKind 
=  Utc
128208500211955002
128208500211955002

制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制) 实现的功能: l APP控制月球灯 l 本地月球灯控制 l APP控制“大白”颜色,实现各种颜色变身 l 门状态传感器状态APP显示 l 网络状态指示灯,连接服务器长亮,断开后闪烁 l 配网按键 l 云端定时功能 l 云端报警提示功能 l 云端历史数据查看 l 远程升级介绍 视频演示:【开源】制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制)_哔哩哔哩_bilibili 一、界面展示 ... 阅读详情

相关推荐

服务器能效测试工具BenchSEE使用方法简介

BenchSEE是由中国标准化研究院开发的一款针对服务器产品能效测试的基准软件。BenchSEE基准的设计参考了全球众多服务器厂家、芯片厂家、能效认证机构和IT节能领域科研机构的意见,旨在满足服务器市场应用对能效测评的需求。BenchSEE基准负载工具支持的服务器类型包括:机架服务器、刀片服务器多节点服务器和机柜服务器等,服务器可以配的最大外理器个数包括:1P,2P和4P等。测试基准涵盖服务器关键部件:CPU,内存和存储介质三大基础部件。

MQWYY3的博客 4224

在vb.net中取日期取时间的有用方法,格式化时间 Format(Now()

VB.net 中 取系统时间 Dim datestr As String = "" datestr = Format(Now(), "yyyy/MM/dd H:mm:ss ffff") 用户定义的日期/时间格式(Format 函数) 下表显示可用于创建用户定义的日期/时间格式的字符。与 Visual Basic 的早期版本不同,这些格式字符区分大小写。 字符 说明 (:) 时间分隔符。在某些

plumlee的专栏 8278

【自然语言处理】【多模态】多模态综述:视觉语言预训练模型

多模态预训练模型综述、模型结构、损失函数、下游任务、CLIP

bqw的博客 7747

vb.net 日期时间格式转换(Format函数)

VB.net 中 取系统时间 Dim datestr As String = "" datestr = Format(Now(), "yyyy/MM/dd H:mm:ss ffff") 用户定义的日期/时间格式(Format 函数) 下表显示可用于创建用户定义的日期/时间格式的字符。与 Visual Basic 的早期版本不同,这些格式字符区分大小写。

bigheadsheep的专栏 6万+

STM32 伺服电机 指令脉冲+方向位置控制模式 主从定时器 实现PWM脉冲数精确控制

实验室有个项目涉及到多个步进电机以及伺服电机的控制,需要电机得到一个触发信号后精确移动一段距离。 下面以单个伺服电机控制为例,利用伺服电机的指令脉冲加方向位置控制模式实现jing'que ...

phcdd的博客 2万+

通俗解析:霍尔编码器、光电编码器、电位计、磁编码器和旋转变压器的实战选型指南

本文深入解析了霍尔编码器、光电编码器、电位计、磁编码器和旋转变压器五大主流编码器的工作原理与核心特性。通过对比精度、环境耐受性、成本及寿命等关键维度,并结合消费电子、工业自动化、汽车电子及极端环境等典型应用场景,提供了一份清晰的实战选型指南,旨在帮助工程师根据具体需求,在性能、可靠性与成本间做出最优决策。

weixin_29216353的博客 256

VB之format 格式化日期

VB之format 格式化时间

qq_34504731的博客 2097

用Python+ArcGIS分析厦门共享单车数据:5分钟搞定订单时长与距离分布的可视化

本文详细介绍了如何利用Python和ArcGIS Pro 3.x快速分析厦门共享单车数据,实现订单时长与距离分布的可视化。通过数据清洗、预处理和高级可视化技巧,揭示用户行为特征,为运营优化提供数据支持。文章还提供了环境配置、代码示例和业务洞察,帮助开发者高效完成数据分析任务。

weixin_28327051的博客 342

vb.net字符串日期型转换

前言: 如何把字符串转为日期型呢? 例如:“20120814”如何转为日期型。 关键:先将字符串形成日期型的“形式” 比如“20120814”     形成: “2012-08-14”    或"2012/08/14" 故:举例如下: Dim a As Date   Dim s As String = "20080814"   a = CDate(Format(CInt(s

pplsunny----舞动青春 6371

【RT-DETR涨点改进】全网独家-特征融合改进篇 | AAAI 2025 顶会| 高效涨点,RT-DETR利用CDFA对比度特征聚合模块,适用于目标检查(全网独家创新)

本文提出了一种基于CDFA模块的多级特征融合优化RT-DETR模型,用于解决医学图像分割中的软边界模糊、小目标和共现现象等问题。CDFA模块通过对比度特征聚合、局部特征增强和多级注意力机制,有效提升模型性能。研究提供了三种改进方案(rtdetr-r18/y50/l-CDFA),并在多个医学数据集上验证了其有效性。核心创新包括语义信息解耦、对比驱动特征融合和尺寸感知解码器。实验结果表明,该方法在分割精度和鲁棒性方面达到SOTA水平。

Ai缝合怪、助力小伙伴高效发论文 510

C#版OpenCV:OpenCVSharp 最详细最全面教程(万字详细总结)

OpenCVSharp 是一个用于 .NET 环境的 OpenCV 包装库,它提供了一种简便的方法来利用 OpenCV 的功能进行图像和视频处理。OpenCV(Open Source Computer Vision Library)是一个开源计算机视觉和机器学习软件库,最初由 Intel 研发并发布。OpenCVSharp 让 C# 和其他 .NET 语言的开发者可以方便地访问 OpenCV 的强大功能。// 打印 OpenCV 版本信息Cv2。

仰望星空的博客 2万+

VB.NET常用的函数

这通常是 .NET Framework 的一部分,但如果你使用的是 .NET Core 或 .NET 5/6/7+ 等更现代的版本,你可能需要通过 NuGet 安装 System.Data.SqlClient 包。在 VB.NET 中连接数据库,需要使用一个数据库连接对象,这个对象负责建立和管理与数据库的连接。ConnectionString 是一个字符串,包含了连接到数据库所需的所有信息,如服务器名称、数据库名称、用户名和密码等。请确保正确处理数据库连接和错误,并在完成数据库操作后关闭连接。

niaixiao1987的博客 1372

23、电生理与CSD分析:探索大脑活动的前沿技术

本文介绍了电生理与电流源密度(CSD)分析在探索大脑活动中的前沿应用。从电生理技术的发展现状,到局部场电位(LFPs)的起源与解释,重点阐述了CSD分析的原理、实验方法及其在绘制皮层兴奋流、解析突触输入和研究自发振荡中的广泛应用。文章还总结了该领域的优势与挑战,并展望了技术改进、多模态融合、数据分析创新及临床拓展的未来方向,展示了这些技术在揭示大脑功能机制中的关键作用。

Apple的专栏 124

VB.NET语言基础(显式转换函数、日期时间函数)

1、类型转换函数名(显式转换) 函数名    返回数据类型  expression 参数范围 CBool Boolean 数据类型 (Visual Basic) 任何有效的 Char、String 或数值表达式。 CByte Byte 数据类型 (Visual Basic) 0 到 255(无符号);舍入小数部分。 CChar Char 数据类型 (Visual Basic) 任何有效的 Char 或 String 表达式;只转换 Strin

黄爱岗廊坊师范学院信息技术提高班 第七期 3148

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

深度学习工具包 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")

VB6.0与VB.NET函数的区别

VB6.0与VB.NET函数的区别 VB6.0 VB.NET AddItem

ygzh_123的专栏 4354

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【VB.NET】常用函数详解 (VS2010)

以上是VB.NET在VS2010中常用的一些函数及其详细说明和示例。这些函数涵盖了字符串处理、类型转换、数学计算、日期时间处理、数组操作和文件处理等多个方面,是VB.NET编程的基础。

weixin_43910833的博客 354

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

VB.Net 常用函数

VB.NET常用函数速查指南 本文系统介绍了VB.NET的主要内置函数,包括六大核心类别: 类型转换:CInt、CDbl等C开头函数 数学计算:Math.Abs、Sin、Sqrt等 字符串处理:Len、Left/Right/Mid、Replace等 日期时间:Now、DateAdd、DateDiff等 文件操作:FileLen、FileDateTime等 数组集合:Filter、Join/Split、UBound等 文中包含丰富的代码示例和Mermaid函数关系图,并附有专业术语表,帮助开发者快速掌握VB.

小猫三两只。 502

机房重构——数据类型很重要

数据类型很是中要,在以前的VB6.0里面却一直没有注意到,到了VB.NET的世界才发现数据类型可以出这么多的问题,数据类型不匹配,不可以转换的例子层出不穷的,随着Format函数还有类型转换函数的改变,这里的类型的转换也有很多的变化,下面详细说明。 一、日期的类型和使用         在VB.NET中,日期和时间在类型上是放在一起的,然而我们在敲机房的时候却需要把他们来分开放

Big Smile 1171

VB.NET学习资料:

学习资料:物件(Object)属性(Peroperty)方法(Method)事件(Event)数据型态数据型态型态宣告字符Dim intIncome%Single !Double #Integer %Long &String $数据型态前缀数据型态前缀Char chr Double dblString str Boolean blnShort sht Object objInt

Brssye的专栏 4086

VB.NET2005函數大全(转自网络)

VB.NET2005函數大全VB.NET 2005 2009-09-21 15:53:16 阅读283 评论0 字号:大中小 一、数学函数 函数 说明 Abs (num) 取绝对值。 Exp (num) 返回以e为底、以num为指数的值,如Exp(2)返回e^2值。 Log (num) 返回参数num的自然对数值,为Double类型,即以e为底的对数。 Hex (num)` 将参数num转

yjqmaster的专栏 1019

VB.NET2005函數大全

VB.NET2005函數大全 一、数学函数 函数 说明 Abs (num) 取绝对值。 Exp (num) 返回以e为底、以num为指数的值,如Exp(2)返回e^2值。 Log (num) 返回参数num的自然对数值,为Double类型,即以e为底的对数。 Hex (num)` 将参数num转换为16进制。 Oct (num) 将参数num转换为8进制 Sign (num) 返...

weixin_30340775的博客 149
上一篇: 金旭亮:软件天才与技术民工
下一篇: GDI资源,用完需释放
Code_My_Life
博客等级 码龄12年 43粉丝 108原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值