---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-29-beb34359c07a> in <cell line: 3>()
      1 #Train the model on the dataset:
      2 
----> 3 train_losses, train_accuracies, test_losses, test_accuracies = train_model(
      4     model=alexnet,
      5     X_train=X_train,

<ipython-input-28-4d6627dffdc3> in train_model(model, X_train, y_train, X_test, y_test, epochs, batch_size, learning_rate, optimization_technique, patience, scheduler_patience, **kwargs)
     25         for i in range(0, len(X_train), batch_size):
     26             X_batch = torch.tensor(X_train[i:i + batch_size], dtype=torch.float).permute(0, 3, 1, 2)
---> 27             y_batch = torch.tensor(y_train[i:i + batch_size].values, dtype=torch.long).view(-1)
     28 
     29             optimizer.zero_grad()

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead. def process_image(file_path, size=(64, 64)):
    img = cv2.imread(file_path)
    img = cv2.resize(img, size)
    return np.array(img, dtype=np.float32) / 255.0

def load_and_preprocess(folder_names):
    folder_paths = [os.path.join(folder_name, file) for folder_name in folder_names for file in os.listdir(folder_name)]
    labels = [folder_name for folder_name in folder_names for _ in os.listdir(folder_name)]

    with ThreadPoolExecutor() as executor:
        processed_images = list(executor.map(process_image, folder_paths))

    return np.array(processed_images), pd.get_dummies(labels), labels


X, y, label_list = load_and_preprocess(folder_names)# Create train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify = y)


#  input dimensions for train_model
y_train_values = y_train.idxmax(1).values
X_train = X_train.astype(np.float32)
y_train_values = np.array([list(y_train.columns).index(v) for v in y_train_values], dtype=np.int64)
X_test = X_test.astype(np.float32)
y_test_values = y_test.idxmax(1).values
y_test_values = np.array([list(y_test.columns).index(v) for v in y_test_values], dtype=np.int64)
from sklearn.metrics import accuracy_score

def train_model(model, X_train, y_train, X_test, y_test, epochs = None, batch_size = None, learning_rate = None, optimization_technique = None,  patience=None, scheduler_patience=None, **kwargs):
    # Define loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=learning_rate)

    if optimization_technique == 'learning_rate_scheduler' and scheduler_patience:
        scheduler = ReduceLROnPlateau(optimizer, 'min', patience=scheduler_patience, verbose=True)

    # Training segment
    train_losses = []
    train_accuracies = []
    test_losses = []
    test_accuracies = []

    best_loss = float('inf')
    stopping_counter = 0

    for epoch in range(epochs):
        epoch_train_losses = []
        epoch_y_true = []
        epoch_y_pred = []

        for i in range(0, len(X_train), batch_size):
            X_batch = torch.tensor(X_train[i:i + batch_size], dtype=torch.float).permute(0, 3, 1, 2)
            y_batch = torch.tensor(y_train[i:i + batch_size].values, dtype=torch.long).view(-1)

            optimizer.zero_grad()
            y_pred = model(X_batch)
            loss = criterion(y_pred, y_batch)
            loss.backward()
            optimizer.step()

            epoch_train_losses.append(loss.item())
            epoch_y_true.extend(y_batch.numpy().flatten().tolist())
            epoch_y_pred.extend(y_pred.argmax(dim=1).numpy().flatten().tolist())

        train_losses.append(sum(epoch_train_losses) / len(epoch_train_losses))
        train_accuracies.append(accuracy_score(epoch_y_true, epoch_y_pred))

        # Testing segment
        with torch.no_grad():
            X_test_tensor = torch.tensor(X_test, dtype=torch.float32).permute(0, 3, 1, 2)
            y_test_tensor = torch.tensor(y_test.values, dtype=torch.long).view(-1)

            test_pred = model(X_test_tensor)
            _, preds_indices = torch.max(test_pred, 1)
            test_loss = criterion(test_pred, y_test_tensor)
            test_accuracy = accuracy_score(y_test_tensor, preds_indices)

        test_losses.append(test_loss.item())
        test_accuracies.append(test_accuracy)

        # Early stopping
        if optimization_technique == 'early_stopping' and patience:
            if test_loss < best_loss:
                best_loss = test_loss
                stopping_counter = 0
            else:
                stopping_counter += 1
            if stopping_counter > patience:
                print(f"Early stopping at epoch {epoch+1}/{epochs}")
                break

        # Learning rate scheduler
        if optimization_technique == 'learning_rate_scheduler' and scheduler_patience and scheduler:
            scheduler.step(test_loss)

        if epoch % 100 == 0:
            print(f"Epoch: {epoch+1}/{epochs}, Training Loss: {train_losses[-1]}, Test Loss: {test_loss.item()}, Training Accuracy: {train_accuracies[-1]}, Test Accuracy: {test_accuracy}")

    return train_losses, train_accuracies, test_losses, test_accuracies. Fix the error.  Check train_model() function thoroughly.