对象的序列化

.net中对象的序列化是指将对象的状态存储起来,先将对象的字段和属性以及类名转换为字节流,然后再把字节流写入数据流。通过对对象反序列化,得到原对象完全相同的副本。

对象的序列化主要的目的是将对象持久化,经过持久化的对象可以从一个地方传输到另一个地方。


在.net中, IFormatter接口提供了对象序列化的功能。他有两个公有的方法:

反序列化对象方法
Deserialize : Deserializes the data on the provided stream and reconstitutes the graph of objects。

序列化对象方法
Serialize:Serializes an object, or graph of objects with the given root to the provided stream。

我们可以将对象序列化成两种格式:

BinaryFormatter :将对象序列化为二进制格式
SoapFormatter:将对象序列化为Soap格式

代码:

//要进行序列化的类
 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4
 5 namespace  SerializeDemos
 6 {
 7    [Serializable]
 8    public class Car
 9    {
10        private string _Price;
11        private string _Owner;
12        public string Price
13        {
14            get return this._Price; }
15            set this._Price = value; }
16        }

17        public string Owner
18        {
19            get return this._Owner; }
20            set this._Owner = value; }
21        }

22        public Car(string o, string p)
23        {
24            this.Price = p;
25            this.Owner = o;
26        }

27    }

28}

29

//序列化以及反序列化对象
 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.ComponentModel;
 4 using  System.Data;
 5 using  System.Drawing;
 6 using  System.Text;
 7 using  System.Windows.Forms;
 8 using  System.IO;
 9 using  System.Runtime.Serialization.Formatters.Binary;
10 using  System.Runtime.Serialization.Formatters.Soap;
11 using  System.Xml.Serialization;
12
13
14 namespace  SerializeDemos
15 {
16
17    public partial class Form1 : Form
18    {
19        public Form1()
20        {
21            InitializeComponent();
22        }

23        private void Form1_Load(object sender, EventArgs e)
24        {
25
26        }

27
28        private void button1_Click(object sender, EventArgs e)
29        {
30            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
31            FileStream fileStream = new FileStream("SerializedCar.bin", FileMode.Create);
32            // 用二进制格式序列化
33            BinaryFormatter binaryFormatter = new BinaryFormatter();
34            binaryFormatter.Serialize(fileStream, car);
35            fileStream.Close();
36            MessageBox.Show("Successful");
37        }

38
39        private void button2_Click(object sender, EventArgs e)
40        {
41            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
42            FileStream fileStream = new FileStream("SerializedCar.Soap", FileMode.Create);
43            // 序列化为Soap
44            SoapFormatter formatter = new SoapFormatter();
45            formatter.Serialize(fileStream, car);
46            fileStream.Close();
47            MessageBox.Show("Successful");
48        }

49
50        private void button3_Click(object sender, EventArgs e)
51        {
52            Car car = new Car(this.txtOwner.Text, this.txtPrice.Text);
53            FileStream fileStream = new FileStream("SerializedCar.xml", FileMode.Create);
54            // 序列化为xml
55            SoapFormatter formatter = new SoapFormatter();
56            formatter.Serialize(fileStream, car);
57            fileStream.Close();
58            MessageBox.Show("Successful");
59        }

60
61        private void button4_Click(object sender, EventArgs e)
62        {  
63            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
64            //二进制格式反序列化
65            Stream stream = new FileStream("SerializedCar.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
66            Car mycar = (Car)formatter.Deserialize(stream);
67            stream.Close();
68            MessageBox.Show(string.Format("Price of Car: {0},/n Owner:{1}", mycar.Price, mycar.Owner));
69        }

70
71        private void button5_Click(object sender, EventArgs e)
72        {
73            System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
74            //Soap格式反序列化
75            Stream stream = new FileStream("SerializedCar.Soap", FileMode.Open, FileAccess.Read, FileShare.Read);
76            Car mycar = (Car)formatter.Deserialize(stream);
77            stream.Close();
78            MessageBox.Show(string.Format("Price of Car: {0},/n Owner:{1}", mycar.Price, mycar.Owner));
79        }

80
81        private void button6_Click(object sender, EventArgs e)
82        {
83            System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
84            //Xml格式反序列化
85            Stream stream = new FileStream("SerializedCar.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
86            Car mycar = (Car)formatter.Deserialize(stream);
87            stream.Close();
88            MessageBox.Show(string.Format("Price of Car: {0},/n Owner:{1}", mycar.Price, mycar.Owner));
89        }

90    }

91}
 
内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值