Sklearn_PyTorch for Random Forest works well when it runs in cpu. However, when we try to add the model to gpu, it fails and show the error:
AttributeError: 'TorchRandomForestClassifier' object has no attribute '_modules'
Our code:
env - movenet
Import of the model
from Sklearn_PyTorch import TorchRandomForestClassifier
import numpy as np
import preprocessing_original
import torchvision
import torch
torch.zeros(1).cuda()
print(torch.cuda.is_available())
if torch.cuda.is_available():
dev = "cuda:0"
else:
dev = "cpu:1"
device = torch.device(dev)
print(device)
df, classes_names = preprocessing_original.load_data_train(r"loaded train data")
df2, classes_names2 = preprocessing_original.load_data_test(r"loaded test data")
train_count = int(1 * len(df))
test_count = int(1 * len(df2))
feature_list = ["x0", "y0", "z0", "x1", "y1", "z1", "x2", "y2", "z2",
"x3", "y3", "z3", "x4", "y4", "z4", "x5", "y5", "z5",
"x6", "y6", "z6", "x7", "y7", "z7", "x11", "y11", "z11",
"x12", "y12", "z12", "x13", "y13", "z13", "x14", "y14", "z14",
"x18", "y18", "z18", "x19", "y19", "z19", "x20", "y20", "z20",
"x22", "y22", "z22", "x23", "y23", "z23", "x24", "y24", "z24", "x27", "y27", "z27"]
Definition of the input data
my_data = torch.Tensor(torch.tensor(df.loc[:train_count, feature_list].values.astype(np.float32)))
my_label = torch.Tensor(torch.tensor(df.loc[:train_count, 'Label'].values.astype(np.float32)))
my_data = my_data.to(device)
my_label = my_label.to(device)
print("Is data cuda: "+str(my_data.is_cuda))
print("Is label cuda: "+str(my_label.is_cuda))
Initialisation of the model
my_model = TorchRandomForestClassifier(nb_trees=100, nb_samples=3, max_depth=5, bootstrap=True)
my_model = my_model.to(device)
print("Is model cuda: "+str(my_model.is_cuda))
Fitting function
my_model.fit(my_data, my_label)
Prediction function
my_vector = torch.Tensor(torch.tensor(df.loc[:train_count, feature_list].values.astype(np.float32)))
my_result = []
for i in range(len(df)):
my_result.append(my_model.predict(my_vector[i]))
from sklearn import metrics
print(metrics.accuracy_score(df.loc[:train_count, 'Label'], my_result))
Sklearn_PyTorch for Random Forest works well when it runs in cpu. However, when we try to add the model to gpu, it fails and show the error:
AttributeError: 'TorchRandomForestClassifier' object has no attribute '_modules'
Our code:
env - movenet
Import of the model
from Sklearn_PyTorch import TorchRandomForestClassifier
import numpy as np
import preprocessing_original
import torchvision
import torch
torch.zeros(1).cuda()
print(torch.cuda.is_available())
if torch.cuda.is_available():
dev = "cuda:0"
else:
dev = "cpu:1"
device = torch.device(dev)
print(device)
df, classes_names = preprocessing_original.load_data_train(r"loaded train data")
df2, classes_names2 = preprocessing_original.load_data_test(r"loaded test data")
train_count = int(1 * len(df))
test_count = int(1 * len(df2))
feature_list = ["x0", "y0", "z0", "x1", "y1", "z1", "x2", "y2", "z2",
"x3", "y3", "z3", "x4", "y4", "z4", "x5", "y5", "z5",
"x6", "y6", "z6", "x7", "y7", "z7", "x11", "y11", "z11",
"x12", "y12", "z12", "x13", "y13", "z13", "x14", "y14", "z14",
"x18", "y18", "z18", "x19", "y19", "z19", "x20", "y20", "z20",
"x22", "y22", "z22", "x23", "y23", "z23", "x24", "y24", "z24", "x27", "y27", "z27"]
Definition of the input data
my_data = torch.Tensor(torch.tensor(df.loc[:train_count, feature_list].values.astype(np.float32)))
my_label = torch.Tensor(torch.tensor(df.loc[:train_count, 'Label'].values.astype(np.float32)))
my_data = my_data.to(device)
my_label = my_label.to(device)
print("Is data cuda: "+str(my_data.is_cuda))
print("Is label cuda: "+str(my_label.is_cuda))
Initialisation of the model
my_model = TorchRandomForestClassifier(nb_trees=100, nb_samples=3, max_depth=5, bootstrap=True)
my_model = my_model.to(device)
print("Is model cuda: "+str(my_model.is_cuda))
Fitting function
my_model.fit(my_data, my_label)
Prediction function
my_vector = torch.Tensor(torch.tensor(df.loc[:train_count, feature_list].values.astype(np.float32)))
my_result = []
for i in range(len(df)):
my_result.append(my_model.predict(my_vector[i]))
from sklearn import metrics
print(metrics.accuracy_score(df.loc[:train_count, 'Label'], my_result))