为了账号安全,请及时绑定邮箱和手机立即绑定

“ascii”编解码器无法解码位置 6 中的字节 0x8b:

“ascii”编解码器无法解码位置 6 中的字节 0x8b:

MYYA 2021-08-11 21:57:50
我正在尝试使用 tensorflow 编写 cnn 代码,但我不断出现此错误:UnicodeDecodeError                        Traceback (most recent call last)<ipython-input-20-a02172d91c0c> in <module>()     39 # Load all the data batches.     40 for i in range(5):---> 41     data_batch = unpickle( 'data_batch_' + str(i + 1))     42      43     train_data = np.append(train_data, data_batch[b'data'])<ipython-input-20-a02172d91c0c> in unpickle(file)     27     import pickle     28     with open(file, 'rb') as fo:---> 29         dict = pickle.load(fo)     30         dict = dict.encode('ascii', 'ignore')     31     return dictUnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)我不知道该怎么做我已经尝试了所有方法,但仍然遇到相同的错误。这是我的代码:# IMAGE RECOGNITION# Tensorflow and numpy to create the neural networkimport tensorflow as tfimport numpy as np# Matplotlib to plot info to show our resultsimport matplotlib.pyplot as plt# OS to load files and save checkpointsimport os# LOADING THE DATA:# LOADING CIFAR data from file:# Load cifar data from file# Load MNIST data from tf examples# Load cifar data from fileimage_height = 32image_width = 32color_channels = 3model_name = "cifar"def unpickle(file):    import pickle    with open(file, 'rb') as fo:        dict = pickle.load(fo)    return dicttrain_data = np.array([])train_labels = np.array([])# Load all the data batches.for i in range(5):    data_batch = unpickle( 'data_batch_' + str(i + 1))    train_data = np.append(train_data, data_batch[b'data'])    train_labels = np.append(train_labels, data_batch[b'labels'])# Load the eval batch.eval_batch = unpickle( 'test_batch')eval_data = eval_batch[b'data']eval_labels = eval_batch[b'labels']# Load the english category names.category_names_bytes = unpickle('batches.meta')[b'label_names']category_names = list(map(lambda x: x.decode("utf-8"), category_names_bytes))# TODO: Process Cifar data
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

尝试:

pickle.load(fo, encoding='latin1')

这可能是 Python 2/3 兼容性问题。顺便说一句,您应该尽量不要使用诸如dict变量名之类的东西,因为它会覆盖 Python 内置函数。


查看完整回答
反对 回复 2021-08-11
  • 1 回答
  • 0 关注
  • 367 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信