Convert Pandas dataframe to PyTorch tensor?

前端 未结 4 1518
傲寒
傲寒 2020-12-29 18:59

I want to train a simple neural network on PyTorch using a personal database. This database is imported from an Excel file and stored in df.

One of the

4条回答
  •  难免孤独
    2020-12-29 19:40

    Simply convert the pandas dataframe -> numpy array -> pytorch tensor. An example of this is described below:

    import pandas as pd
    import numpy as np
    import torch
    
    df = pd.read_csv('train.csv')
    target = pd.DataFrame(df['target'])
    del df['target']
    train = data_utils.TensorDataset(torch.Tensor(np.array(df)), torch.Tensor(np.array(target)))
    train_loader = data_utils.DataLoader(train, batch_size = 10, shuffle = True)
    

    Hopefully, this will help you to create your own datasets using pytorch (Compatible with the latest version of pytorch).

提交回复
热议问题