validation

Prepare training and test data

X = np.array([item["Image"] for item in data]) / 255.0 # Normalize to [0, 1]
y = np.array([item["Label"] for item in data])

Split into training and validation sets

split_idx = int(0.8 * len(X))
X_train, X_val = X[:split_idx], X[split_idx:]
y_train, y_val = y[:split_idx], y[split_idx:]

One-hot encode labels

y_train_df = pd.DataFrame(y_train, columns=["Label"])
y_val_df = pd.DataFrame(y_val, columns=["Label"])
y_train_one_hot = pd.get_dummies(y_train_df["Label"]).values
y_val_one_hot = pd.get_dummies(y_val_df["Label"]).values

train_losses = []
val_losses = []

def accuracy(y_true, y_pred):
y_true_labels = np.argmax(y_true, axis=1)
y_pred_labels = np.argmax(y_pred, axis=1)
return np.mean(y_true_labels == y_pred_labels)

posted @ 2024-10-31 23:03  無糖花生醬  阅读(1)  评论(0编辑  收藏  举报