解决Python Pandas UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 错误
The error message suggests that there is an issue with decoding the input files as UTF-8. This could be due to the encoding of the input files not being UTF-8 or the presence of non-UTF-8 characters in the files.
To resolve this issue, you can try the following steps:
-
Check the encoding of the input files: Verify that the encoding of the files is indeed UTF-8. If not, you can specify the correct encoding when reading the files using the
encodingparameter inpd.read_csv(). -
Handle non-UTF-8 characters: If there are non-UTF-8 characters in the files, you can try using a different encoding or remove those characters before reading the files.
Here's an updated version of your code with the suggested changes:
import os
import pandas as pd
import torch
import torch.nn as nn
from torch_geometric.data import Data, DataLoader
from torch_geometric.nn import GCNConv
import torch.nn.functional as F
from sklearn.model_selection import train_test_split
class MyDataset(torch.utils.data.Dataset):
def __init__(self, root, transform=None, pre_transform=None):
self.transform = transform
self.pre_transform = pre_transform
self.data_list = []
for i in range(1, 43): # 处理42张图片,编号从1到42
for j in range(37): # 每张图片37个节点
# 加载特征值数据
features_file = os.path.join(root, 'input', 'images_flatten', f'{i}.txt_{j}.txt')
features = pd.read_csv(features_file, header=None, sep=' ', encoding='utf-8')
x = torch.tensor(features.values, dtype=torch.float)
# 加载标签数据
labels_file = os.path.join(root, 'input', 'labels', f'{i}.txt_{j}.txt')
labels = pd.read_csv(labels_file, header=None, sep=' ', encoding='utf-8')
y = torch.tensor(labels.values, dtype=torch.long).squeeze()
# 加载边的关系数据
edges_file = os.path.join(root, 'input', 'edges_P.csv')
edges_df = pd.read_csv(edges_file, header=None, sep=',', encoding='utf-8')
# 构建 edge_index 张量
edge_index = torch.tensor(edges_df.values, dtype=torch.long).t().contiguous()
# 创建 train_mask 和 val_mask
train_mask = torch.zeros(y.size(0), dtype=torch.bool)
val_mask = torch.zeros(y.size(0), dtype=torch.bool)
train_mask[:30] = 1 # 前30个节点作为训练集
val_mask[30:] = 1 # 后7个节点作为验证集
data = Data(x=x, edge_index=edge_index, y=y, train_mask=train_mask, val_mask=val_mask)
if self.transform is not None:
data = self.transform(data)
self.data_list.append(data)
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
return self.data_list[idx]
class GCN(torch.nn.Module):
def __init__(self, num_node_features, num_classes):
super(GCN, self).__init__()
self.conv1 = GCNConv(num_node_features, 8)
self.conv2 = GCNConv(8, 16)
self.conv3 = GCNConv(16, num_classes)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv3(x, edge_index)
return x
def train_model(dataset, model, optimizer, device):
model.train()
total_loss = 0.0
for data in dataset:
data = data.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.cross_entropy(output[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
total_loss += loss.item()
return total_loss / len(dataset)
def validate_model(dataset, model, device):
model.eval()
correct = 0
total = 0
for data in dataset:
data = data.to(device)
output = model(data)
_, predicted = torch.max(output[data.val_mask], 1)
total += data.val_mask.sum().item()
correct += (predicted == data.y[data.val_mask]).sum().item()
return correct / total
if __name__ == '__main__':
root = r'C:\Users\jh\Desktop\data'
dataset = MyDataset(root=root)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GCN(num_node_features=1600, num_classes=8).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
train_dataset, val_dataset = train_test_split(dataset, test_size=0.1)
train_loader = DataLoader(train_dataset, batch_size=1, shuffle=False)
val_loader = DataLoader(val_dataset, batch_size=1, shuffle=False)
epochs = 10
for epoch in range(epochs):
train_loss = train_model(train_loader, model, optimizer, device)
print(f'Epoch {epoch+1}/{epochs}, Train Loss: {train_loss:.4f}')
val_accuracy = validate_model(val_loader, model, device)
print(f'Val_Acc: {val_accuracy:.4f}')
Make sure to replace 'C:\Users\jh\Desktop\data' with the actual path to your data directory.
原文地址: https://www.cveoy.top/t/topic/paeJ 著作权归作者所有。请勿转载和采集!