如何解压缩pkl文件? [英] How to unpack pkl file?

查看:539
本文介绍了如何解压缩pkl文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自MNIST数据集的pkl文件,其中包含手写的数字图像.

I have a pkl file from MNIST dataset, which consists of handwritten digit images.

我想看一下每个数字图像,因此我需要解压缩pkl文件,除非我不知道怎么做.

I'd like to take a look at each of those digit images, so I need to unpack the pkl file, except I can't find out how.

是否可以解压缩pkl文件?

Is there a way to unpack/unzip pkl file?

推荐答案

通常

实际上,您的pkl文件是序列化的pickle文件,这意味着它已使用Python的

Generally

Your pkl file is, in fact, a serialized pickle file, which means it has been dumped using Python's pickle module.

要释放数据,您可以:

import pickle


with open('serialized.pkl', 'rb') as f:
    data = pickle.load(f)

对于MNIST数据集

注意gzip仅在文件被压缩时才需要:

For the MNIST data set

Note gzip is only needed if the file is compressed:

import gzip
import pickle


with gzip.open('mnist.pkl.gz', 'rb') as f:
    train_set, valid_set, test_set = pickle.load(f)

每组可以进一步划分的地方(即训练组):

Where each set can be further divided (i.e. for the training set):

train_x, train_y = train_set

这些将是集合的输入(数字)和输出(标签).

Those would be the inputs (digits) and outputs (labels) of your sets.

如果要显示数字:

import matplotlib.cm as cm
import matplotlib.pyplot as plt


plt.imshow(train_x[0].reshape((28, 28)), cmap=cm.Greys_r)
plt.show()

另一种选择是查看原始数据:

The other alternative would be to look at the original data:

http://yann.lecun.com/exdb/mnist/

但这会更困难,因为您需要创建一个程序来读取这些文件中的二进制数据.因此,我建议您使用Python,并使用pickle加载数据.如您所见,这非常容易. ;-)

But that will be harder, as you'll need to create a program to read the binary data in those files. So I recommend you to use Python, and load the data with pickle. As you've seen, it's very easy. ;-)

这篇关于如何解压缩pkl文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆