gpt4 book ai didi

python - “ascii”编解码器无法解码位置 6 : ordinal not in range(128) 中的字节 0x8b

转载 作者:行者123 更新时间:2023-11-30 22:04:41 25 4
gpt4 key购买 nike

我正在尝试使用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 dict

UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)

我不知道该怎么办,我已经尝试了所有方法,但仍然遇到相同的错误。这是我的代码:

# IMAGE RECOGNITION

# Tensorflow and numpy to create the neural network
import tensorflow as tf
import numpy as np

# Matplotlib to plot info to show our results
import matplotlib.pyplot as plt

# OS to load files and save checkpoints
import os

# LOADING THE DATA:

# LOADING CIFAR data from file:
# Load cifar data from file
# Load MNIST data from tf examples

# Load cifar data from file

image_height = 32
image_width = 32

color_channels = 3

model_name = "cifar"


def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo)

return dict




train_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

def process_data(data):
float_data = np.array(data, dtype=float) / 255.0

reshaped_data = np.reshape(float_data, (-1, color_channels, image_height, image_width))

# The incorrect image

transposed_data = np.transpose(reshaped_data, [0, 2, 3, 1])

return transposed_data


train_data = process_data(train_data)

eval_data = process_data(eval_data)

# TODO: The Neural Network
# CONVOLUTIONAL NEURAL NETWORK CLASS:

谢谢!

最佳答案

尝试:

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

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

关于python - “ascii”编解码器无法解码位置 6 : ordinal not in range(128) 中的字节 0x8b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com