{"id":994520,"date":"2024-12-27T08:56:11","date_gmt":"2024-12-27T00:56:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994520.html"},"modified":"2024-12-27T08:56:16","modified_gmt":"2024-12-27T00:56:16","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96minist-pkl","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994520.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6minist pkl"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071452\/0b6307f9-0343-4c1a-bf86-fc9f07635029.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6minist pkl\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6MNIST\u6570\u636e\u96c6\u7684.pkl\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7dpickle\u6587\u4ef6\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u6570\u636e\u3001\u5904\u7406\u6570\u636e\u4ee5\u63d0\u53d6\u56fe\u50cf\u548c\u6807\u7b7e\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/strong>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6b63\u786e\u52a0\u8f7d.pkl\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u5408\u9002\u7684\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u5e76\u63d0\u4f9b\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u52a0\u8f7d.pkl\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bfb\u53d6.pkl\u6587\u4ef6\u901a\u5e38\u4f7f\u7528pickle\u5e93\u3002pickle\u662fPython\u7684\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316Python\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528pickle\u5e93\u52a0\u8f7dMNIST\u7684.pkl\u6587\u4ef6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>def load_pickle_file(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        data = pickle.load(file)<\/p>\n<p>    return data<\/p>\n<p>mnist_data = load_pickle_file(&#39;mnist.pkl&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\uff0c\u6253\u5f00.pkl\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528pickle.load()\u65b9\u6cd5\u5c06\u5176\u5185\u5bb9\u52a0\u8f7d\u5230Python\u5bf9\u8c61\u4e2d\u3002\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14\u6587\u4ef6\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6570\u636e\u7ed3\u6784\u89e3\u6790\u4e0e\u5904\u7406<\/p>\n<\/p>\n<p><p>MNIST\u6570\u636e\u96c6\u901a\u5e38\u5305\u62ec\u8bad\u7ec3\u6570\u636e\u3001\u9a8c\u8bc1\u6570\u636e\u548c\u6d4b\u8bd5\u6570\u636e\uff0c\u6bcf\u4e2a\u90e8\u5206\u90fd\u5305\u542b\u56fe\u50cf\u548c\u6807\u7b7e\u3002\u5728\u52a0\u8f7d.pkl\u6587\u4ef6\u540e\uff0c\u6570\u636e\u901a\u5e38\u4f1a\u4ee5\u5b57\u5178\u6216\u5143\u7ec4\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u4e00\u6b65\u89e3\u6790\u4ee5\u63d0\u53d6\u56fe\u50cf\u548c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_mnist_data(data):<\/p>\n<p>    # \u5047\u8bbe\u6570\u636e\u4ee5\u5b57\u5178\u5f62\u5f0f\u5b58\u50a8<\/p>\n<p>    tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_images, train_labels = data[&#39;train&#39;]<\/p>\n<p>    validation_images, validation_labels = data[&#39;validation&#39;]<\/p>\n<p>    test_images, test_labels = data[&#39;test&#39;]<\/p>\n<p>    return train_images, train_labels, validation_images, validation_labels, test_images, test_labels<\/p>\n<p>train_images, train_labels, validation_images, validation_labels, test_images, test_labels = parse_mnist_data(mnist_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6570\u636e\u4ee5\u5b57\u5178\u5f62\u5f0f\u5b58\u50a8\uff0c\u5e76\u89e3\u6790\u51fa\u8bad\u7ec3\u3001\u9a8c\u8bc1\u548c\u6d4b\u8bd5\u6570\u636e\u7684\u56fe\u50cf\u548c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5728\u89e3\u6790\u51fa\u56fe\u50cf\u548c\u6807\u7b7e\u6570\u636e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362\u4ee5\u4fbf\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u3002\u901a\u5e38\uff0c\u56fe\u50cf\u6570\u636e\u9700\u8981\u5f52\u4e00\u5316\u5904\u7406\uff0c\u4ee5\u4fbf\u52a0\u5feb\u8bad\u7ec3\u901f\u5ea6\u5e76\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def normalize_images(images):<\/p>\n<p>    # \u5c06\u50cf\u7d20\u503c\u7f29\u653e\u52300\u52301\u4e4b\u95f4<\/p>\n<p>    return images \/ 255.0<\/p>\n<p>train_images = normalize_images(train_images)<\/p>\n<p>validation_images = normalize_images(validation_images)<\/p>\n<p>test_images = normalize_images(test_images)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u53ef\u89c6\u5316\u4e0e\u9a8c\u8bc1<\/p>\n<\/p>\n<p><p>\u5728\u51c6\u5907\u597d\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u7684\u53ef\u89c6\u5316\u548c\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u6b63\u786e\u52a0\u8f7d\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>def display_image(image, label):<\/p>\n<p>    plt.imshow(image.reshape(28, 28), cmap=&#39;gray&#39;)<\/p>\n<p>    plt.title(f&#39;Label: {label}&#39;)<\/p>\n<p>    plt.show()<\/p>\n<h2><strong>\u663e\u793a\u7b2c\u4e00\u4e2a\u8bad\u7ec3\u56fe\u50cf<\/strong><\/h2>\n<p>display_image(train_images[0], train_labels[0])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u53ef\u89c6\u5316\u56fe\u50cf\uff0c\u6211\u4eec\u53ef\u4ee5\u9a8c\u8bc1\u6570\u636e\u662f\u5426\u6b63\u786e\u52a0\u8f7d\uff0c\u5e76\u786e\u4fdd\u56fe\u50cf\u548c\u6807\u7b7e\u5339\u914d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6574\u5408\u4e0e\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u52a0\u8f7d\u548c\u5904\u7406\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6570\u636e\u7528\u4e8e\u8bad\u7ec3\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528TensorFlow\u6784\u5efa\u7b80\u5355\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u7684\u793a\u4f8b\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, Flatten<\/p>\n<p>def build_and_train_model(train_images, train_labels, validation_images, validation_labels):<\/p>\n<p>    model = Sequential([<\/p>\n<p>        Flatten(input_shape=(28, 28)),<\/p>\n<p>        Dense(128, activation=&#39;relu&#39;),<\/p>\n<p>        Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>    ])<\/p>\n<p>    model.compile(optimizer=&#39;adam&#39;,<\/p>\n<p>                  loss=&#39;sparse_categorical_crossentropy&#39;,<\/p>\n<p>                  metrics=[&#39;accuracy&#39;])<\/p>\n<p>    model.fit(train_images, train_labels, epochs=5, validation_data=(validation_images, validation_labels))<\/p>\n<p>    return model<\/p>\n<p>model = build_and_train_model(train_images, train_labels, validation_images, validation_labels)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528TensorFlow\u7684Keras\u63a5\u53e3\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5168\u8fde\u63a5\u795e\u7ecf\u7f51\u7edc\u3002\u6a21\u578b\u7531\u4e00\u4e2aFlatten\u5c42\u548c\u4e24\u4e2aDense\u5c42\u7ec4\u6210\uff0c\u9002\u7528\u4e8eMNIST\u624b\u5199\u6570\u5b57\u5206\u7c7b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u8bfb\u53d6\u548c\u5904\u7406MNIST\u7684.pkl\u6587\u4ef6\u5728Python\u4e2d\u76f8\u5bf9\u7b80\u5355\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u52a0\u8f7d.pkl\u6587\u4ef6\u3001\u89e3\u6790\u6570\u636e\u7ed3\u6784\u3001\u6570\u636e\u683c\u5f0f\u8f6c\u6362\u3001\u53ef\u89c6\u5316\u4e0e\u9a8c\u8bc1\u3001\u4ee5\u53ca\u6574\u5408\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u51c6\u5907\u6570\u636e\u5e76\u5e94\u7528\u4e8e\u4e0d\u540c\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e2d\u3002\u786e\u4fdd\u5728\u5904\u7406\u6570\u636e\u65f6\u5173\u6ce8\u7ec6\u8282\uff0c\u4ee5\u4fbf\u83b7\u53d6\u51c6\u786e\u7684\u7ed3\u679c\u548c\u9ad8\u6548\u7684\u6a21\u578b\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6MNIST\u6570\u636e\u96c6\u7684pkl\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6MNIST\u6570\u636e\u96c6\u7684pkl\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684pickle\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u4e86pickle\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u6253\u5f00\u548c\u8bfb\u53d6pkl\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import pickle\n\nwith open(&#39;mnist.pkl&#39;, &#39;rb&#39;) as f:\n    mnist_data = pickle.load(f)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u52a0\u8f7d\u6570\u636e\u96c6\u5e76\u5c06\u5176\u5b58\u50a8\u5728mnist_data\u53d8\u91cf\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbf\u95ee\u5176\u4e2d\u7684\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n<p><strong>MNIST\u6570\u636e\u96c6\u7684pkl\u6587\u4ef6\u901a\u5e38\u5305\u542b\u54ea\u4e9b\u4fe1\u606f\uff1f<\/strong><br \/>MNIST pkl\u6587\u4ef6\u4e00\u822c\u5305\u542b\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u7684\u56fe\u50cf\u548c\u6807\u7b7e\uff0c\u901a\u5e38\u4ee5\u5b57\u5178\u5f62\u5f0f\u5b58\u50a8\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u8bad\u7ec3\u6570\u636e\u3001\u8bad\u7ec3\u6807\u7b7e\u3001\u6d4b\u8bd5\u6570\u636e\u548c\u6d4b\u8bd5\u6807\u7b7e\u3002\u6570\u636e\u901a\u5e38\u4ee5numpy\u6570\u7ec4\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u5230\u7684MNIST\u6570\u636e\u96c6\uff1f<\/strong><br \/>\u8bfb\u53d6MNIST\u6570\u636e\u96c6\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528numpy\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u4f8b\u5982\u5f52\u4e00\u5316\u56fe\u50cf\u6570\u636e\u3001\u8fdb\u884c\u6570\u636e\u589e\u5f3a\u3001\u5206\u5272\u8bad\u7ec3\u96c6\u548c\u9a8c\u8bc1\u96c6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5bf9\u56fe\u50cf\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u5047\u8bbemnist_data\u662f\u4ecepkl\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u6570\u636e\ntrain_images = mnist_data[&#39;train_images&#39;] \/ 255.0  # \u5c06\u56fe\u50cf\u6570\u636e\u5f52\u4e00\u5316\u52300\u52301\u4e4b\u95f4\ntrain_labels = mnist_data[&#39;train_labels&#39;]\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u51c6\u5907\u597d\u6570\u636e\uff0c\u4ee5\u4fbf\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6MNIST\u6570\u636e\u96c6\u7684.pkl\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7dpickle\u6587\u4ef6\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u6570\u636e\u3001\u5904\u7406\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":994529,"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\/994520"}],"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=994520"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994520\/revisions"}],"predecessor-version":[{"id":994533,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994520\/revisions\/994533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994529"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}