{"id":9422,"date":"2025-06-10T19:57:34","date_gmt":"2025-06-10T11:57:34","guid":{"rendered":"\/?p=9422"},"modified":"2025-06-10T19:57:34","modified_gmt":"2025-06-10T11:57:34","slug":"pytorch%e6%a8%a1%e5%9e%8b%e4%bf%9d%e5%ad%98%e6%a0%bc%e5%bc%8f%e8%af%a6%e8%a7%a3%ef%bc%9a-pkl-vs-pth-pt","status":"publish","type":"post","link":"\/?p=9422","title":{"rendered":"PyTorch\u6a21\u578b\u4fdd\u5b58\u683c\u5f0f\u8be6\u89e3\uff1a.pkl vs .pth\/.pt"},"content":{"rendered":"<h3>\u6838\u5fc3\u7279\u6027<\/h3>\n<ul>\n<li>\n<p><strong>\u6587\u4ef6\u6269\u5c55\u540d<\/strong>\uff1a<code>.pkl<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u5e95\u5c42\u6280\u672f<\/strong>\uff1aPython Pickle\u6a21\u5757\u7684\u5e8f\u5217\u5316\u683c\u5f0f<\/p>\n<\/li>\n<li>\n<p><strong>\u5e8f\u5217\u5316\u539f\u7406<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\">import pickle\n# \u4fdd\u5b58\u6a21\u578b\nwith open('model.pkl', 'wb') as f:\n  pickle.dump(model, f)\n\n# \u52a0\u8f7d\u6a21\u578b\nwith open('model.pkl', 'rb') as f:\n  model = pickle.load(f)<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u4f18\u52bf\u4e0e\u5c40\u9650<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u4f18\u52bf<\/strong><\/th>\n<th><strong>\u5c40\u9650<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u2705 \u652f\u6301\u5b8c\u6574\u5bf9\u8c61\u4fdd\u5b58\uff08\u7ed3\u6784+\u53c2\u6570\uff09<\/td>\n<td>\u26a0\ufe0f \u5b58\u5728\u5b89\u5168\u98ce\u9669\uff08\u53cd\u5e8f\u5217\u5316\u6f0f\u6d1e\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u2705 \u517c\u5bb9\u590d\u6742\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u26a0\ufe0f Python\u7248\u672c\/\u4f9d\u8d56\u5e93\u517c\u5bb9\u6027\u95ee\u9898<\/td>\n<\/tr>\n<tr>\n<td>\u2705 \u5355\u6587\u4ef6\u6253\u5305\u591a\u7ec4\u4ef6<\/td>\n<td>\u274c \u4ec5\u9650Python\u73af\u5883\u4f7f\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u9002\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u5b9e\u9a8c\u6027\u4ee3\u7801\u5feb\u901f\u4fdd\u5b58<\/li>\n<li>\u591a\u7ec4\u4ef6\u6d41\u6c34\u7ebf\u6253\u5305\uff08\u5982\u9884\u5904\u7406+\u6a21\u578b\uff09<\/li>\n<li>\u77ed\u671f\u672c\u5730\u8c03\u8bd5<\/li>\n<\/ul>\n<pre><code class=\"language-python\">with open(model_export_path, &#039;wb&#039;) as file:\n    d = {\n        &quot;clf&quot;: clf,  # \u5206\u7c7b\u5668\uff08\u5982MultinomialNB\uff09\n        &quot;vectorizer&quot;: vectorizer,  # \u6587\u672c\u7279\u5f81\u63d0\u53d6\u5668\uff08\u5982CountVectorizer\uff09\n        &quot;tfidftransformer&quot;: tfidftransformer,  # TF-IDF\u6743\u91cd\u8f6c\u6362\u5668\n    }\n    pickle.dump(d, file)  # \u5e8f\u5217\u5316\u5230.pkl\u6587\u4ef6<\/code><\/pre>\n<h3>\u6838\u5fc3\u4f5c\u7528<\/h3>\n<ol>\n<li>\u5168\u72b6\u6001\u4fdd\u5b58\uff1a\u5b8c\u6574\u4fdd\u7559\u5bf9\u8c61\u7ed3\u6784\u3001\u53c2\u6570\u53ca\u4f9d\u8d56\u5173\u7cfb\uff0c\u52a0\u8f7d\u65f6\u65e0\u9700\u91cd\u65b0\u521d\u59cb\u5316<\/li>\n<li>\u591a\u5bf9\u8c61\u6253\u5305\uff1a\u652f\u6301\u5c06\u9884\u5904\u7406\u3001\u6a21\u578b\u3001\u8f6c\u6362\u5668\u7b49\u6d41\u6c34\u7ebf\u7ec4\u4ef6\u7edf\u4e00\u5b58\u50a8\uff0c\u786e\u4fdd\u63a8\u7406\u65f6\u903b\u8f91\u4e00\u81f4\u6027<\/li>\n<li>Python\u751f\u6001\u53cb\u597d\uff1a\u4e0eScikit-learn\u3001PyTorch\u7b49\u5e93\u65e0\u7f1d\u96c6\u6210\uff0c\u9002\u5408\u5feb\u901f\u5b9e\u9a8c\u548c\u672c\u5730\u8c03\u8bd5<\/li>\n<\/ol>\n<h2>\ud83d\udd10 .pth\/.pt \u6587\u4ef6\u683c\u5f0f<\/h2>\n<h3>\u6838\u5fc3\u7279\u6027<\/h3>\n<ul>\n<li>\n<p><strong>\u6587\u4ef6\u6269\u5c55\u540d<\/strong>\uff1a<code>.pth<\/code> \u6216 <code>.pt<\/code><\/p>\n<\/li>\n<li>\n<p>PyTorch\u6807\u51c6\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4fdd\u5b58\u72b6\u6001\u5b57\u5178\uff08\u63a8\u8350\uff09\ntorch.save(model.state_dict(), 'model.pth')\n\n# \u52a0\u8f7d\u72b6\u6001\u5b57\u5178\nmodel = MyModel()\nmodel.load_state_dict(torch.load('model.pth'))\n\n# \u5b8c\u6574\u6a21\u578b\u4fdd\u5b58\uff08\u4e0d\u63a8\u8350\uff09\ntorch.save(model, 'model_complete.pt')<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ol>\n<li>\n<p><strong>\u4f18\u5148\u4fdd\u5b58\u72b6\u6001\u5b57\u5178<\/strong>\uff08<code>state_dict()<\/code>\uff09<\/p>\n<\/li>\n<li>\n<p>\u914d\u5408\u6a21\u578b\u5b9a\u4e49\u4ee3\u7801\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">class MyModel(nn.Module):\n   def __init__(self):\n       super().__init__()\n       self.linear = nn.Linear(10, 2)\n\n   def forward(self, x):\n       return self.linear(x)<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u6838\u5fc3\u4f18\u52bf<\/h3>\n<ul>\n<li>\ud83d\udd12 \u66f4\u9ad8\u5b89\u5168\u6027\uff08\u65e0\u4ee3\u7801\u6267\u884c\u98ce\u9669\uff09<\/li>\n<li>\ud83d\udd04 \u66f4\u597d\u7684\u7248\u672c\u517c\u5bb9\u6027<\/li>\n<li>\ud83d\ude80 PyTorch\u751f\u6001\u539f\u751f\u652f\u6301<\/li>\n<li>\ud83d\udce6 \u66f4\u5c0f\u7684\u6587\u4ef6\u4f53\u79ef<\/li>\n<\/ul>\n<hr \/>\n<h2>\u2696\ufe0f \u683c\u5f0f\u5bf9\u6bd4\u4e0e\u63a8\u8350<\/h2>\n<table>\n<thead>\n<tr>\n<th><strong>\u7279\u6027<\/strong><\/th>\n<th><code>.pkl<\/code><\/th>\n<th><code>.pth\/.pt<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u5b89\u5168\u6027<\/strong><\/td>\n<td>\u9ad8\u98ce\u9669<\/td>\n<td>\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td><strong>\u517c\u5bb9\u6027<\/strong><\/td>\n<td>\u4f9d\u8d56Python\u73af\u5883<\/td>\n<td>\u6846\u67b6\u5185\u901a\u7528<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6587\u4ef6\u5185\u5bb9<\/strong><\/td>\n<td>\u5b8c\u6574\u5bf9\u8c61<\/td>\n<td>\u53c2\u6570\/\u5b8c\u6574\u6a21\u578b<\/td>\n<\/tr>\n<tr>\n<td><strong>\u751f\u4ea7\u63a8\u8350<\/strong><\/td>\n<td>\u274c \u4e0d\u63a8\u8350<\/td>\n<td>\u2705 \u9996\u9009<\/td>\n<\/tr>\n<tr>\n<td><strong>\u52a0\u8f7d\u8981\u6c42<\/strong><\/td>\n<td>\u65e0<\/td>\n<td>\u9700\u8981\u6a21\u578b\u5b9a\u4e49<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\ud83c\udfc6 \u884c\u4e1a\u63a8\u8350\u65b9\u6848<\/h3>\n<pre><code class=\"language-python\"># \u6807\u51c6\u4fdd\u5b58\u65b9\u5f0f\ntorch.save(model.state_dict(), &#039;model.pth&#039;)\n\n# \u6807\u51c6\u52a0\u8f7d\u65b9\u5f0f\nmodel = MyModel()  # \u9700\u9884\u5148\u5b9a\u4e49\u7ed3\u6784\nmodel.load_state_dict(torch.load(&#039;model.pth&#039;))<\/code><\/pre>\n<blockquote>\n<p><strong>\u5173\u952e\u7ed3\u8bba<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u751f\u4ea7\u4e2d\u7981\u7528<code>.pkl<\/code><\/strong>\uff1a\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\u548c\u517c\u5bb9\u6027\u95ee\u9898<\/li>\n<li><strong>\u59cb\u7ec8\u4f7f\u7528<code>.pth\/.pt<\/code><\/strong>\uff1a\u914d\u5408<code>state_dict()<\/code>\u5b9e\u73b0\u5b89\u5168\u53ef\u9760\u7684\u6a21\u578b\u6301\u4e45\u5316<\/li>\n<li><strong>\u5b8c\u6574\u6a21\u578b\u4fdd\u5b58\u4ec5\u7528\u4e8e\u8c03\u8bd5<\/strong>\uff1a\u907f\u514d\u751f\u4ea7\u73af\u5883\u4f7f\u7528<code>torch.save(model)<\/code><\/li>\n<\/ul>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6838\u5fc3\u7279\u6027 \u6587\u4ef6\u6269\u5c55\u540d\uff1a.pkl \u5e95\u5c42\u6280\u672f\uff1aPython Pickle\u6a21\u5757\u7684\u5e8f\u5217\u5316\u683c\u5f0f \u5e8f\u5217\u5316\u539f\u7406\uff1a import pickle # \u4fdd\u5b58   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[224],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9422"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9422"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9422\/revisions"}],"predecessor-version":[{"id":9423,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9422\/revisions\/9423"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9422"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}