How do I train a simple neural network with PyTorch on a pandas dataframe df
?
The column df["Target"]
is the target (e.g. labels) of the network. This doesn’t work:
JavaScript
x
7
1
import pandas as pd
2
import torch.utils.data as data_utils
3
4
target = pd.DataFrame(df['Target'])
5
train = data_utils.TensorDataset(df, target)
6
train_loader = data_utils.DataLoader(train, batch_size=10, shuffle=True)
7
Advertisement
Answer
I’m referring to the question in the title as you haven’t really specified anything else in the text, so just converting the DataFrame into a PyTorch tensor.
Without information about your data, I’m just taking float values as example targets here.
Convert Pandas dataframe to PyTorch tensor?
JavaScript
1
17
17
1
import pandas as pd
2
import torch
3
import random
4
5
# creating dummy targets (float values)
6
targets_data = [random.random() for i in range(10)]
7
8
# creating DataFrame from targets_data
9
targets_df = pd.DataFrame(data=targets_data)
10
targets_df.columns = ['targets']
11
12
# creating tensor from targets_df
13
torch_tensor = torch.tensor(targets_df['targets'].values)
14
15
# printing out result
16
print(torch_tensor)
17
Output:
JavaScript
1
3
1
tensor([ 0.5827, 0.5881, 0.1543, 0.6815, 0.9400, 0.8683, 0.4289,
2
0.5940, 0.6438, 0.7514], dtype=torch.float64)
3
Tested with Pytorch 0.4.0.
I hope this helps, if you have any further questions – just ask. :)