<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>保存 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E4%BF%9D%E5%AD%98/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 17:12:21 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] PyTorch模型的两种保存方法</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e6%a8%a1%e5%9e%8b%e7%9a%84%e4%b8%a4%e7%a7%8d%e4%bf%9d%e5%ad%98%e6%96%b9%e6%b3%95/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e6%a8%a1%e5%9e%8b%e7%9a%84%e4%b8%a4%e7%a7%8d%e4%bf%9d%e5%ad%98%e6%96%b9%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 18 Nov 2019 17:19:03 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[PyTorch]]></category>
		<category><![CDATA[保存]]></category>
		<category><![CDATA[模型]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10953</guid>

					<description><![CDATA[<p>
根据<a href="https://pytorch.org/docs/master/notes/serialization.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">PyTorch文档</span></a>，在把PyTorch模型保存成文件的时候有两种方法，第一种是推荐的：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
torch.save(the_model.state_dict()<span style="color:#cc7832;">, </span>PATH)</pre>
<p>对应地，加载模型这样做：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
the_model = TheModelClass(*args<span style="color:#cc7832;">, </span>**kwargs)
the_model.load_state_dict(torch.load(PATH))</pre>
<p><span id="more-10953"></span><br />
另一种方法是<span style="color:#ff0000;">不推荐</span>的：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
torch.save(the_model<span style="color:#cc7832;">, </span>PATH)</pre>
<p>对应地，加载模型这样做：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
the_model = torch.load(PATH)</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
这两者的区别：第1种方法只保存了模型的参数，而第2种方法保存了整个模型(结构+参数)，所以<span style="color:#0000ff;">第2种方法保存出来的文件体积会比第1种方法大</span>。<br />
使用第2种方法的话，序列化的数据将绑定到所使用的特定类和确切的目录结构，因此在其他项目中使用或经过一些大的重构后，它可能会失效。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&#160;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" />&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e6%a8%a1%e5%9e%8b%e7%9a%84%e4%b8%a4%e7%a7%8d%e4%bf%9d%e5%ad%98%e6%96%b9%e6%b3%95/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
根据<a href="https://pytorch.org/docs/master/notes/serialization.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">PyTorch文档</span></a>，在把PyTorch模型保存成文件的时候有两种方法，第一种是推荐的：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
torch.save(the_model.state_dict()<span style="color:#cc7832;">, </span>PATH)</pre>
<p>对应地，加载模型这样做：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
the_model = TheModelClass(*args<span style="color:#cc7832;">, </span>**kwargs)
the_model.load_state_dict(torch.load(PATH))</pre>
<p><span id="more-10953"></span><br />
另一种方法是<span style="color:#ff0000;">不推荐</span>的：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
torch.save(the_model<span style="color:#cc7832;">, </span>PATH)</pre>
<p>对应地，加载模型这样做：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
the_model = torch.load(PATH)</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
这两者的区别：第1种方法只保存了模型的参数，而第2种方法保存了整个模型(结构+参数)，所以<span style="color:#0000ff;">第2种方法保存出来的文件体积会比第1种方法大</span>。<br />
使用第2种方法的话，序列化的数据将绑定到所使用的特定类和确切的目录结构，因此在其他项目中使用或经过一些大的重构后，它可能会失效。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e6%a8%a1%e5%9e%8b%e7%9a%84%e4%b8%a4%e7%a7%8d%e4%bf%9d%e5%ad%98%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
