{"id":1180831,"date":"2025-01-15T18:43:23","date_gmt":"2025-01-15T10:43:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180831.html"},"modified":"2025-01-15T18:43:26","modified_gmt":"2025-01-15T10:43:26","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e5%bb%ba%e5%a5%bd%e7%9a%84%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1180831.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\u5efa\u597d\u7684\u6a21\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125400\/c928b382-7c58-40fb-99a6-6795905f47ad.webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u5efa\u597d\u7684\u6a21\u578b\" \/><\/p>\n<p><p> <strong>Python\u4fdd\u5b58\u5efa\u597d\u7684\u6a21\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528pickle\u3001joblib\u3001\u4ee5\u53ca\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u81ea\u5e26\u7684\u4fdd\u5b58\u65b9\u6cd5\u3002<\/strong> \u4f8b\u5982\uff0c\u5728\u4f7f\u7528Scikit-learn\u6784\u5efa\u6a21\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pickle\u6216joblib\u6765\u4fdd\u5b58\u6a21\u578b\uff1b\u5728\u4f7f\u7528Keras\u6216TensorFlow\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u63d0\u4f9b\u7684\u4fdd\u5b58\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528pickle\u4fdd\u5b58\u6a21\u578b\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528pickle\u4fdd\u5b58\u6a21\u578b\u975e\u5e38\u7b80\u5355\u4e14\u5e38\u7528\u3002\u9996\u5148\u9700\u8981\u5728\u8bad\u7ec3\u6a21\u578b\u540e\u4f7f\u7528pickle\u7684dump\u51fd\u6570\u5c06\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4e4b\u540e\u53ef\u4ee5\u4f7f\u7528load\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X, y = iris.data, iris.target<\/p>\n<h2><strong>\u5212\u5206\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier(n_estimators=100)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b<\/strong><\/h2>\n<p>with open(&#39;random_forest_model.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(model, file)<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b<\/strong><\/h2>\n<p>with open(&#39;random_forest_model.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    loaded_model = pickle.load(file)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>print(loaded_model.score(X_test, y_test))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7pickle\u4fdd\u5b58\u6a21\u578b\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6a21\u578b\u5e8f\u5217\u5316\u5230\u6587\u4ef6\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528PICKLE\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>Pickle\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Pickle\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002\u5e8f\u5217\u5316\u662f\u6307\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u800c\u53cd\u5e8f\u5217\u5316\u5219\u662f\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u56de\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002Pickle\u975e\u5e38\u9002\u5408\u4fdd\u5b58\u548c\u52a0\u8f7dScikit-learn\u6a21\u578b\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u4fdd\u5b58\u4efb\u4f55Python\u5bf9\u8c61\uff0c\u5305\u62ec\u590d\u6742\u7684\u6a21\u578b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>Pickle\u4fdd\u5b58\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pickle\u4fdd\u5b58\u6a21\u578b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8bad\u7ec3\u6a21\u578b\u3002<\/li>\n<li>\u4f7f\u7528pickle\u7684dump\u51fd\u6570\u5c06\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X, y = iris.data, iris.target<\/p>\n<h2><strong>\u5212\u5206\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier(n_estimators=100)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b<\/strong><\/h2>\n<p>with open(&#39;random_forest_model.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(model, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u52a0\u8f7d\u6570\u636e\u96c6\u5e76\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u7136\u540e\uff0c\u6211\u4eec\u8bad\u7ec3\u4e86\u4e00\u4e2a\u968f\u673a\u68ee\u6797\u5206\u7c7b\u5668\uff0c\u5e76\u4f7f\u7528pickle\u7684dump\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6<code>random_forest_model.pkl<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>Pickle\u52a0\u8f7d\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pickle\u52a0\u8f7d\u6a21\u578b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4fdd\u5b58\u6a21\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528pickle\u7684load\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6a21\u578b\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6a21\u578b<\/p>\n<p>with open(&#39;random_forest_model.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    loaded_model = pickle.load(file)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>print(loaded_model.score(X_test, y_test))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pickle\u7684load\u51fd\u6570\u4ece\u6587\u4ef6<code>random_forest_model.pkl<\/code>\u4e2d\u52a0\u8f7d\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528JOBLIB\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>Joblib\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Joblib\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5728Python\u4e2d\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u6a21\u578b\u4fdd\u5b58\u7684\u5e93\u3002\u4e0epickle\u76f8\u6bd4\uff0cjoblib\u5728\u5904\u7406\u5927\u578bnumpy\u6570\u7ec4\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u5b83\u975e\u5e38\u9002\u5408\u4fdd\u5b58\u548c\u52a0\u8f7dScikit-learn\u6a21\u578b\uff0c\u56e0\u4e3aScikit-learn\u6a21\u578b\u901a\u5e38\u5305\u542b\u5927\u91cf\u7684numpy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>Joblib\u4fdd\u5b58\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528joblib\u4fdd\u5b58\u6a21\u578b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8bad\u7ec3\u6a21\u578b\u3002<\/li>\n<li>\u4f7f\u7528joblib\u7684dump\u51fd\u6570\u5c06\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import joblib<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X, y = iris.data, iris.target<\/p>\n<h2><strong>\u5212\u5206\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier(n_estimators=100)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b<\/strong><\/h2>\n<p>joblib.dump(model, &#39;random_forest_model.joblib&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528joblib\u7684dump\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6<code>random_forest_model.joblib<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>Joblib\u52a0\u8f7d\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528joblib\u52a0\u8f7d\u6a21\u578b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528joblib\u7684load\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6a21\u578b\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6a21\u578b<\/p>\n<p>loaded_model = joblib.load(&#39;random_forest_model.joblib&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>print(loaded_model.score(X_test, y_test))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528joblib\u7684load\u51fd\u6570\u4ece\u6587\u4ef6<code>random_forest_model.joblib<\/code>\u4e2d\u52a0\u8f7d\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528KERAS\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>Keras\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Keras\u662f\u4e00\u4e2a\u9ad8\u5c42\u795e\u7ecf\u7f51\u7edcAPI\uff0c\u80fd\u591f\u5feb\u901f\u6784\u5efa\u548c\u8bad\u7ec3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002Keras\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\uff0c\u5305\u62ec\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u548c\u4ec5\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u5305\u62ec\u6a21\u578b\u7684\u7ed3\u6784\u548c\u6743\u91cd\uff0c\u4f7f\u7528Keras\u7684<code>model.save<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.models import Sequential<\/p>\n<p>from tensorflow.keras.layers import Dense<\/p>\n<h2><strong>\u6784\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential([<\/p>\n<p>    Dense(64, activation=&#39;relu&#39;, input_shape=(4,)),<\/p>\n<p>    Dense(3, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X_train, y_train, epochs=10, batch_size=32)<\/p>\n<h2><strong>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/strong><\/h2>\n<p>model.save(&#39;keras_model.h5&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Keras\u7684<code>model.save<\/code>\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6<code>keras_model.h5<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Keras\u7684<code>tf.keras.models.load_model<\/code>\u51fd\u6570\u53ef\u4ee5\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/p>\n<p>loaded_model = tf.keras.models.load_model(&#39;keras_model.h5&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>loaded_model.evaluate(X_test, y_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Keras\u7684<code>tf.keras.models.load_model<\/code>\u51fd\u6570\u4ece\u6587\u4ef6<code>keras_model.h5<\/code>\u4e2d\u52a0\u8f7d\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4ec5\u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\uff0c\u53ef\u4ee5\u4f7f\u7528Keras\u7684<code>model.save_weights<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/p>\n<p>model.save_weights(&#39;keras_weights.h5&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd\u4e4b\u524d\uff0c\u9700\u8981\u5148\u6784\u5efa\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528Keras\u7684<code>model.load_weights<\/code>\u51fd\u6570\u52a0\u8f7d\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6784\u5efa\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b<\/p>\n<p>model = Sequential([<\/p>\n<p>    Dense(64, activation=&#39;relu&#39;, input_shape=(4,)),<\/p>\n<p>    Dense(3, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/strong><\/h2>\n<p>model.load_weights(&#39;keras_weights.h5&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>model.evaluate(X_test, y_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6784\u5efa\u4e86\u4e00\u4e2a\u4e0e\u4fdd\u5b58\u6743\u91cd\u65f6\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>model.load_weights<\/code>\u51fd\u6570\u4ece\u6587\u4ef6<code>keras_weights.h5<\/code>\u4e2d\u52a0\u8f7d\u6743\u91cd\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u4f7f\u7528TENSORFLOW\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>TensorFlow\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u9886\u57df\u3002TensorFlow\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\uff0c\u5305\u62ec\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u548c\u4ec5\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u5305\u62ec\u6a21\u578b\u7684\u7ed3\u6784\u548c\u6743\u91cd\uff0c\u4f7f\u7528TensorFlow\u7684<code>tf.keras.Model.save<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u6784\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.Sequential([<\/p>\n<p>    tf.keras.layers.Dense(64, activation=&#39;relu&#39;, input_shape=(4,)),<\/p>\n<p>    tf.keras.layers.Dense(3, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X_train, y_train, epochs=10, batch_size=32)<\/p>\n<h2><strong>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/strong><\/h2>\n<p>model.save(&#39;tf_model&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528TensorFlow\u7684<code>tf.keras.Model.save<\/code>\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u5939<code>tf_model<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528TensorFlow\u7684<code>tf.keras.models.load_model<\/code>\u51fd\u6570\u53ef\u4ee5\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/p>\n<p>loaded_model = tf.keras.models.load_model(&#39;tf_model&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>loaded_model.evaluate(X_test, y_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528TensorFlow\u7684<code>tf.keras.models.load_model<\/code>\u51fd\u6570\u4ece\u6587\u4ef6\u5939<code>tf_model<\/code>\u4e2d\u52a0\u8f7d\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4ec5\u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\uff0c\u53ef\u4ee5\u4f7f\u7528TensorFlow\u7684<code>tf.keras.Model.save_weights<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/p>\n<p>model.save_weights(&#39;tf_weights&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd\u4e4b\u524d\uff0c\u9700\u8981\u5148\u6784\u5efa\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528TensorFlow\u7684<code>tf.keras.Model.load_weights<\/code>\u51fd\u6570\u52a0\u8f7d\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6784\u5efa\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b<\/p>\n<p>model = tf.keras.Sequential([<\/p>\n<p>    tf.keras.layers.Dense(64, activation=&#39;relu&#39;, input_shape=(4,)),<\/p>\n<p>    tf.keras.layers.Dense(3, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/strong><\/h2>\n<p>model.load_weights(&#39;tf_weights&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>model.evaluate(X_test, y_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6784\u5efa\u4e86\u4e00\u4e2a\u4e0e\u4fdd\u5b58\u6743\u91cd\u65f6\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>tf.keras.Model.load_weights<\/code>\u51fd\u6570\u4ece\u6587\u4ef6\u5939<code>tf_weights<\/code>\u4e2d\u52a0\u8f7d\u6743\u91cd\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u4f7f\u7528PYTORCH\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>PyTorch\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>PyTorch\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4ee5\u5176\u52a8\u6001\u8ba1\u7b97\u56fe\u548c\u7075\u6d3b\u6027\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce\u3002PyTorch\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\uff0c\u5305\u62ec\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u548c\u4ec5\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u5305\u62ec\u6a21\u578b\u7684\u7ed3\u6784\u548c\u6743\u91cd\uff0c\u4f7f\u7528PyTorch\u7684<code>torch.save<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import torch.nn as nn<\/p>\n<p>import torch.optim as optim<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b<\/strong><\/h2>\n<p>class Net(nn.Module):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super(Net, self).__init__()<\/p>\n<p>        self.fc1 = nn.Linear(4, 64)<\/p>\n<p>        self.fc2 = nn.Linear(64, 3)<\/p>\n<p>    def forward(self, x):<\/p>\n<p>        x = torch.relu(self.fc1(x))<\/p>\n<p>        x = torch.softmax(self.fc2(x), dim=1)<\/p>\n<p>        return x<\/p>\n<p>model = Net()<\/p>\n<h2><strong>\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/strong><\/h2>\n<p>criterion = nn.CrossEntropyLoss()<\/p>\n<p>optimizer = optim.Adam(model.parameters(), lr=0.001)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>for epoch in range(10):<\/p>\n<p>    optimizer.zero_grad()<\/p>\n<p>    outputs = model(torch.tensor(X_train, dtype=torch.float32))<\/p>\n<p>    loss = criterion(outputs, torch.tensor(y_train, dtype=torch.long))<\/p>\n<p>    loss.backward()<\/p>\n<p>    optimizer.step()<\/p>\n<h2><strong>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/strong><\/h2>\n<p>torch.save(model, &#39;pytorch_model.pth&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PyTorch\u7684<code>torch.save<\/code>\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6<code>pytorch_model.pth<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528PyTorch\u7684<code>torch.load<\/code>\u51fd\u6570\u53ef\u4ee5\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/p>\n<p>loaded_model = torch.load(&#39;pytorch_model.pth&#39;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>loaded_model.eval()<\/p>\n<p>with torch.no_grad():<\/p>\n<p>    outputs = loaded_model(torch.tensor(X_test, dtype=torch.float32))<\/p>\n<p>    _, predicted = torch.max(outputs, 1)<\/p>\n<p>    accuracy = (predicted == torch.tensor(y_test)).sum().item() \/ len(y_test)<\/p>\n<p>    print(&#39;Accuracy:&#39;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PyTorch\u7684<code>torch.load<\/code>\u51fd\u6570\u4ece\u6587\u4ef6<code>pytorch_model.pth<\/code>\u4e2d\u52a0\u8f7d\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4ec5\u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u4fdd\u5b58\u6a21\u578b\u7684\u6743\u91cd\uff0c\u53ef\u4ee5\u4f7f\u7528PyTorch\u7684<code>torch.save<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6a21\u578b\u6743\u91cd<\/p>\n<p>torch.save(model.state_dict(), &#39;pytorch_weights.pth&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/h3>\n<\/p>\n<p><p>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd\u4e4b\u524d\uff0c\u9700\u8981\u5148\u6784\u5efa\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528PyTorch\u7684<code>model.load_state_dict<\/code>\u51fd\u6570\u52a0\u8f7d\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b<\/p>\n<p>model = Net()<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b\u6743\u91cd<\/strong><\/h2>\n<p>model.load_state_dict(torch.save(&#39;pytorch_weights.pth&#39;))<\/p>\n<h2><strong>\u6d4b\u8bd5\u52a0\u8f7d\u7684\u6a21\u578b<\/strong><\/h2>\n<p>model.eval()<\/p>\n<p>with torch.no_grad():<\/p>\n<p>    outputs = model(torch.tensor(X_test, dtype=torch.float32))<\/p>\n<p>    _, predicted = torch.max(outputs, 1)<\/p>\n<p>    accuracy = (predicted == torch.tensor(y_test)).sum().item() \/ len(y_test)<\/p>\n<p>    print(&#39;Accuracy:&#39;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0e\u4fdd\u5b58\u6743\u91cd\u65f6\u76f8\u540c\u7ed3\u6784\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>model.load_state_dict<\/code>\u51fd\u6570\u4ece\u6587\u4ef6<code>pytorch_weights.pth<\/code>\u4e2d\u52a0\u8f7d\u6743\u91cd\uff0c\u5e76\u4f7f\u7528\u52a0\u8f7d\u7684\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u4f7f\u7528ONNX\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>ONNX\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>ONNX\uff08Open Neural Network Exchange\uff09\u662f\u4e00\u4e2a\u5f00\u653e\u7684\u795e\u7ecf\u7f51\u7edc\u4ea4\u6362\u683c\u5f0f\uff0c\u65e8\u5728\u4fc3\u8fdb\u4e0d\u540c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\u3002ONNX\u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u6a21\u578b\u4ece\u4e00\u4e2a\u6846\u67b6\u5bfc\u51fa\uff0c\u5e76\u5728\u53e6\u4e00\u4e2a\u6846\u67b6\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5bfc\u51fa\u6a21\u578b\u5230ONNX<\/h3>\n<\/p>\n<p><p>ONNX\u63d0\u4f9b\u4e86\u4e00\u79cd\u6807\u51c6\u5316\u7684\u65b9\u5f0f\u6765\u4fdd\u5b58\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06PyTorch\u6a21\u578b\u5bfc\u51fa\u4e3aONNX\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch.onnx<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u5165\u5f20\u91cf<\/strong><\/h2>\n<p>dummy_input = torch.tensor(X_train[0:1], dtype=torch.float32)<\/p>\n<h2><strong>\u5bfc\u51fa\u6a21\u578b\u5230ONNX<\/strong><\/h2>\n<p>torch.onnx.export(model, dummy_input, &#39;model.onnx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PyTorch\u7684<code>torch.onnx.export<\/code>\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5bfc\u51fa\u4e3aONNX\u683c\u5f0f\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6<code>model.onnx<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u52a0\u8f7dONNX\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528ONNX Runtime\u6765\u52a0\u8f7d\u548c\u8fd0\u884cONNX\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import onnxruntime as ort<\/p>\n<h2><strong>\u521b\u5efaONNX Runtime\u63a8\u7406\u4f1a\u8bdd<\/strong><\/h2>\n<p>ort_session = ort.InferenceSession(&#39;model.onnx&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u5165<\/strong><\/h2>\n<p>input_name = ort_session.get_inputs()[0].name<\/p>\n<p>input_data = {input_name: X_test.astype(np.float32)}<\/p>\n<h2><strong>\u63a8\u7406<\/strong><\/h2>\n<p>outputs = ort_session.run(None, input_data)<\/p>\n<h2><strong>\u83b7\u53d6\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>predicted = np.argmax(outputs[0], axis=1)<\/p>\n<p>accuracy = np.sum(predicted == y_test) \/ len(y_test)<\/p>\n<p>print(&#39;Accuracy:&#39;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ONNX Runtime\u7684<code>InferenceSession<\/code>\u7c7b\u52a0\u8f7dONNX\u6a21\u578b\uff0c\u5e76\u8fdb\u884c\u63a8\u7406\u3002\u6700\u540e\uff0c\u8ba1\u7b97\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u4f7f\u7528MLFLOW\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u6a21\u578b<\/h2>\n<\/p>\n<p><h3>MLflow\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>MLflow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5e73\u53f0\uff0c\u65e8\u5728\u7ba1\u7406\u6574\u4e2a\u673a\u5668\u5b66\u4e60\u751f\u547d\u5468\u671f\uff0c\u5305\u62ec\u5b9e\u9a8c\u8ddf\u8e2a\u3001<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/23316.html\" target=\"_blank\">\u9879\u76ee\u7ba1\u7406<\/a>\u3001\u6a21\u578b\u90e8\u7f72\u548c\u6ce8\u518c\u8868\u3002MLflow\u63d0\u4f9b\u4e86\u4e00\u79cd\u6807\u51c6\u5316\u7684\u65b9\u5f0f\u6765\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4fdd\u5b58\u6a21\u578b\u5230MLflow<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6a21\u578b\u4fdd\u5b58\u5230MLflow\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mlflow<\/p>\n<p>import mlflow.sklearn<\/p>\n<h2><strong>\u542f\u52a8MLflow\u8ddf\u8e2a<\/strong><\/h2>\n<p>mlflow.start_run()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier(n_estimators=100)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b\u5230MLflow<\/strong><\/h2>\n<p>mlflow.sklearn.log_model(model, &#39;model&#39;)<\/p>\n<h2><strong>\u7ed3\u675fMLflow\u8ddf\u8e2a<\/strong><\/h2>\n<p>mlflow.end_run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528MLflow\u7684<code>mlflow.sklearn.log_model<\/code>\u51fd\u6570\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230MLflow\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u52a0\u8f7dMLflow\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528MLflow\u7684<code>mlflow.sklearn.load_model<\/code>\u51fd\u6570\u52a0\u8f7d\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4fdd\u5b58\u673a\u5668\u5b66\u4e60\u6a21\u578b\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528<code>pickle<\/code>\u6216<code>joblib<\/code>\u5e93\u3002<code>pickle<\/code>\u662fPython\u5185\u7f6e\u7684\u5e8f\u5217\u5316\u5e93\uff0c\u53ef\u4ee5\u5c06\u6a21\u578b\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<code>joblib<\/code>\u66f4\u9002\u5408\u4e8e\u5927\u578bnumpy\u6570\u7ec4\u7684\u9ad8\u6548\u5e8f\u5217\u5316\u3002\u4f7f\u7528<code>joblib.dump(model, &#39;model.pkl&#39;)<\/code>\u53ef\u4ee5\u8f7b\u677e\u4fdd\u5b58\u6a21\u578b\uff0c\u800c\u4f7f\u7528<code>joblib.load(&#39;model.pkl&#39;)<\/code>\u5219\u53ef\u91cd\u65b0\u52a0\u8f7d\u6a21\u578b\u3002<\/p>\n<p><strong>\u4fdd\u5b58\u6a21\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u6a21\u578b\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u683c\u5f0f\u548c\u540e\u7f00\u540d\u3002\u901a\u5e38\uff0c<code>.pkl<\/code>\u548c<code>.joblib<\/code>\u662f\u5e38\u89c1\u7684\u540e\u7f00\u540d\uff0c\u80fd\u591f\u660e\u786e\u6307\u793a\u6587\u4ef6\u5185\u5bb9\u3002\u8fd8\u9700\u8981\u6ce8\u610f\u6a21\u578b\u7684\u7248\u672c\u517c\u5bb9\u6027\uff0c\u786e\u4fdd\u5728\u52a0\u8f7d\u6a21\u578b\u65f6\u4f7f\u7528\u4e0e\u4fdd\u5b58\u65f6\u76f8\u540c\u7684\u5e93\u7248\u672c\u3002\u6b64\u5916\uff0c\u8003\u8651\u5230\u6a21\u578b\u7684\u53ef\u590d\u73b0\u6027\uff0c\u5efa\u8bae\u540c\u65f6\u4fdd\u5b58\u6a21\u578b\u7684\u8bad\u7ec3\u53c2\u6570\u548c\u6570\u636e\u9884\u5904\u7406\u6b65\u9aa4\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u4fdd\u5b58\u7684\u6a21\u578b\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\uff1f<\/strong><br \/>\u9a8c\u8bc1\u4fdd\u5b58\u7684\u6a21\u578b\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u52a0\u8f7d\u6a21\u578b\u5e76\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u6765\u5b9e\u73b0\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u8ba1\u7b97\u6a21\u578b\u7684\u51c6\u786e\u7387\u6216\u5176\u4ed6\u6027\u80fd\u6307\u6807\uff0c\u4ee5\u786e\u4fdd\u5176\u6027\u80fd\u672a\u53d7\u5230\u5f71\u54cd\u3002\u4f7f\u7528\u8bad\u7ec3\u96c6\u4e4b\u5916\u7684\u6570\u636e\u8fdb\u884c\u9a8c\u8bc1\u53ef\u4ee5\u63d0\u4f9b\u66f4\u53ef\u9760\u7684\u6027\u80fd\u53cd\u9988\u3002\u6b64\u5916\uff0c\u5c06\u6a21\u578b\u7684\u9884\u6d4b\u7ed3\u679c\u4e0e\u9884\u671f\u7ed3\u679c\u8fdb\u884c\u5bf9\u6bd4\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u9a8c\u8bc1\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4fdd\u5b58\u5efa\u597d\u7684\u6a21\u578b\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528pickle\u3001joblib\u3001\u4ee5\u53ca\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u81ea\u5e26\u7684\u4fdd\u5b58\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1180838,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180831"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1180831"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180831\/revisions"}],"predecessor-version":[{"id":1180841,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180831\/revisions\/1180841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1180838"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}