#我从kaggle获取了这个csv文件(https://www.kaggle.com/datasets/keplersmachines/kepler-labelled-time-series-#data?resource=download)
train_df = pd.read_csv("../csvs/Space Travel/Exoplanet Hunting in Deep Space/1/exoTrain.csv")
test_df = pd.read_csv("../csvs/Space Travel/Exoplanet Hunting in Deep Space/1/exoTest.csv")
X_train_df = train_df.drop(["LABEL"], axis = 1).values
print(X_train_df.shape)
X_train_df
y_train_df = train_df["LABEL"].values.reshape(-1,1).squeeze()
print(y_train_df.shape)
y_train_df
X_train , X_test , y_train , y_test = train_test_split(X_train_df,
y_train_df,
test_size=0.2,
train_size=0.8,
shuffle=True,
random_state=123)
sc = MinMaxScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(f"max of train_data is {X_train.max()} \
and X_test max is {X_test.max()} \
and min is {X_train.min()} \
X_test min : {X_test.min()} \
same with y labels : {y_train.max()} \
y_test max : {y_test.max()} \
y_train min : {y_train.min()} \
and : {y_test.min()}")
X_train.shape[1]
print(type(X_train))
print(type(X_test))
print(type(y_train))
print(type(y_train))
#X_train = T.from_numpy(X_train).int()
#X_test = T.from_numpy(X_test).int()
#y_train = T.from_numpy(y_train).int()
#y_test = T.from_numpy(y_test).int()
X_train = T.from_numpy(X_train).float()
X_test = T.from_numpy(X_test).float()
y_train = T.from_numpy(y_train).float()
y_test = T.from_numpy(y_test).float()
#X_train = T.tensor(X_train , dtype = T.long)
#X_test =T.tensor(X_test , dtype = T.long)
#y_train = T.tensor(y_train , dtype = T.long)
#y_test =T.tensor(y_test , dtype = T.long)
print(type(X_train))
print(type(X_test))
print(type(y_train))
print(type(y_train))
class Exoplanet_AI(nn.Module):
def __init__(self, input_dims = X_train.shape[1] , hidden_units = 125 , output_dims = X_train.shape[1]):
super().__init__()
self.activation = nn.LeakyReLU()
self.droprate = (0.2)
self.dropout = nn.Dropout(p = self.droprate)
self.flaten = nn.Flatten()
self.ll1 = nn.Linear(in_features = input_dims , out_features = hidden_units)
self.ll2 = nn.Linear(in_features = hidden_units, out_features = hidden_units)
self.ll3 = nn.Linear(in_features = hidden_units , out_features = hidden_units)
self.ll4 = nn.Linear(in_features = hidden_units, out_features = output_dims)
self.compute_probability = nn.Softmax(dim = 1)
def forward(self , X):
X = self.flaten(X)
X = self.activation(self.ll1(X))
X = self.activation(self.ll2(X))
X = self.dropout(X)
X = self.activation(self.ll3(X))
X = self.activation(self.ll4(X))
X = self.compute_probability(X)
return X
class train_and_testing():
def __init__(self):
self.device = T.device("cuda:0" if T.cuda.is_available() else "cpu")
self.lr = 1e-3
self.epochs = 20
self.X_train = X_train
self.X_test = X_test
self.y_train = y_train
self.y_test = y_test
self.model = Exoplanet_AI().to(self.device)
self.criterion = opt.Adam(params = self.model.parameters() , lr = self.lr)
self.loss = nn.CrossEntropyLoss()
def parameters(self):
return self.model.state_dict()
def train_loop(self):
current_loss = 0.0
for i in range(self.epochs):
self.model.train()
forward_pass = self.model(self.X_train)
loss_fn = self.loss(forward_pass , self.y_train)
loss_fn.backward()
self.criterion.step()
self.criterion.zero_grad()
current_loss += loss_fn.item()
print(f"On epochs {i} the loss is {loss_fn}")
train_and_testing().parameters()
train_and_testing().train_loop()
我的错误是: 你可以在我的代码中看到我尝试了很多将数据类型更改为正确的数据类型,但每次我将另一个数据类型更改为另一个数据类型错误:(并且绝对不知道如何修复数据类型错误:(
and as error i get :
RuntimeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 train_and_testing().train_loop()
Cell In[26], line 57
53 self.model.train()
55 forward_pass = self.model(self.X_train)
---> 57 loss_fn = self.loss(forward_pass , self.y_train)
59 loss_fn.backward()
61 self.criterion.step()
packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
包orch n\modules\module.py:1520,在 Module._call_impl(self, *args, **kwargs) 第1515章 攀上漂亮女局长之后1516 第1517章 第1518章 攀上漂亮女局长之后1519 -> 1520 返回forward_call(*args, **kwargs) 第1522章 ... 第3057章 第3058章 - > 3059返回torch._C._nn.cross_entropy_loss(输入,目标,权重,_Reduction.get_enum(减少),ignore_index,label_smoothing)
RuntimeError: expected scalar type Long but found Float
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings
nn.CrossEntropyLoss
期望目标值具有 LongTensor
dtype。
您的代码调用
y_train = T.from_numpy(y_train).float()
,将目标值转换为浮点类型。
要转换为长类型,请使用
y_train = T.from_numpy(y_train).long()