我正在尝试使用 Pytorch 定义 NN 模型,但是当我想将 y-train 转换为 y-train-tensor 时出现类型错误,我应该如何修复它?

问题描述 投票:0回答:1

这是我的代码。我将威斯康星州乳腺癌数据库从 kaggle 导入到 vscode。所有数据集变量均为数字和浮点数 64。存在此类型错误(发生异常:TypeError 无法转换 numpy.object_ 类型的 np.ndarray。唯一支持的类型有:float64、float32、float16、complex64、complex128、int64、int32、int16、int8、uint64、uint32、uint16、uint8 和 bool。 文件“C:\Users\User\Desktop\Machine Learning\Sharif\WBCD.py”,第 26 行,位于 y_train_tensor = torch.tensor(y_train.values, dtype=torch.float32).view(-1, 1) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 类型错误:无法转换 numpy.object_ 类型的 np.ndarray。唯一支持的类型是:float64、float32、float16、complex64、complex128、int64、int32、int16、int8、uint64、uint32、uint16、uint8 和 bool。) on this(y_train_tensor = torch.tensor(y_train.values, dtype) =torch.float32).view(-1, 1)) 行。我使用 y_train.values.flatten 但没有帮助。

import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from ucimlrepo import fetch_ucirepo  
breast_cancer_wisconsin_diagnostic = fetch_ucirepo(id=17) 
print(type(breast_cancer_wisconsin_diagnostic.data.features))
print(type(breast_cancer_wisconsin_diagnostic.data.targets)) 
X = pd.DataFrame(breast_cancer_wisconsin_diagnostic.data.features)
y = pd.DataFrame(breast_cancer_wisconsin_diagnostic.data.targets)
print(X.dtypes)
print(y.dtypes)
print(X.shape)
print(y.shape) 
df = pd.concat([X, y], axis=1)
print(df.head(10))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
y_train = y_train.dropna()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
X_test_tensor = torch.tensor(X_test, dtype=torch.float32) 
y_train_tensor = torch.tensor(y_train.values, dtype=torch.float32).view(-1, 1)
y_test_tensor = torch.tensor(y_test.values, dtype=torch.float32).view(-1, 1)
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.fc1 = nn.Linear(X_train.shape[1], 16)  # Input layer to hidden layer
        self.fc2 = nn.Linear(16, 8)                  # Hidden layer to hidden layer
        self.fc3 = nn.Linear(8, 1)                   # Hidden layer to output layer
        self.relu = nn.ReLU()                         # Activation function

    def forward(self, x):
        x = self.relu(self.fc1(x))                   # First layer with ReLU activation
        x = self.relu(self.fc2(x))                   # Second layer with ReLU activation
        x = torch.sigmoid(self.fc3(x))               # Output layer with sigmoid activation for binary classification
        return x

# Instantiate the model, define loss function and optimizer
model = NeuralNetwork()
loss_fn = nn.BCELoss()                           # Binary Cross Entropy Loss for binary classification
optimizer = optim.Adam(model.parameters(), lr=0.001)  # Adam optimizer

# Training the model
n_epochs = 100
batch_size = 10

for epoch in range(n_epochs):
    model.train()                                 # Set the model to training mode
    for i in range(0, len(X_train_tensor), batch_size):
        X_batch = X_train_tensor[i:i+batch_size]
        y_batch = y_train_tensor[i:i+batch_size]

        optimizer.zero_grad()                     # Zero the gradients
        y_pred = model(X_batch)                   # Forward pass
        loss = loss_fn(y_pred, y_batch)          # Compute loss
        loss.backward()                           # Backward pass (compute gradients)
        optimizer.step()                          # Update weights

    print(f'Finished epoch {epoch+1}, latest loss: {loss.item():.4f}')

# Evaluating the model on the test set
model.eval()                                     # Set the model to evaluation mode
with torch.no_grad():
    y_test_pred = model(X_test_tensor)           # Forward pass on test set
    y_test_pred_classes = (y_test_pred > 0.5).float()  # Convert probabilities to binary predictions

# Calculate accuracy
accuracy = (y_test_pred_classes.eq(y_test_tensor).sum().item()) / len(y_test_tensor)
print(f'Accuracy on test set: {accuracy * 100:.2f}%') 


pytorch neural-network typeerror numpy-ndarray
1个回答
0
投票

发生错误是因为

y_train
包含非数字数据,与
torch.tensor
不兼容。

检查

y_train
的内容并确保只是数值,以便您可以安全地转换为张量。

此外,根据错误,

y_train
中的错误类型似乎是一个numpy对象。

© www.soinside.com 2019 - 2024. All rights reserved.