[Python] Is there an efficient approach in PyTorch for multi-output regression where inputs...

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for the fastest way to essentially predict a rotating n values using, effectively, the other n-1 features. I can conceptualize a way of doing this with a for loop and will explain it below. There very well may be a matrix operation I'm unaware of that does this and I just don't know the name for it.

    Say you have a toy dataset:

    # Training Data
    users, item1, item2, item3
    one , 1 , 2 , 1
    two , 1 , 4 , 3
    thr , 1 , 2 , 5

    # Prediction example
    pred , x , y , z


    In this example, I'm trying to predict n=3 classes; n being the number of features. One way I could approach this is by making a traditional feedforward NN with input_size = n and then use a mask.

    toy_data = ... # what you see up there
    model = model(input_size=num_of_cols) # traditional Torch NN

    for epoch in epochs:
    for col_index in num_of_cols:
    mask_toy_data = mask_col_at_idx(toy_data, col_index)

    # Basically, make prediction without nth col features
    pred = model.forward(mask_toy_data)

    # Loss is only on predictions on nth column
    mask_pred = mask_cols_at_all_other_idx(pred, col_index)
    mask_toy_data_for_loss = mask_cols_at_all_other_ix(toy_data, col_index)

    loss = mask_pred - mask_toy_data_for_loss
    ... # backprop, etc.

    def mask_col_at_idx(data, col_idx):
    # 0 out every value in the column idx

    def mask_cols_at_all_other_idx(data, col_idx):
    # 0 out every other col, besides the given one



    This approach would be totally fine if you only had three columns. But how could I approach this if I have hundreds or thousands? A for loop over each column does not seem feasible.

    Continue reading...

Compartilhe esta Página