1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
''' # @Time : 2022/11/18 23:41:11 # @Author: wd-2711 ''' import pandas as pd import numpy as np import paddle import time
class PUBGRegressor(paddle.nn.Layer): """数据量很大,建议尝试深层神经网络""" def __init__(self): super(PUBGRegressor, self).__init__() self.fc1 = paddle.nn.Linear(in_features=11, out_features=64) self.fc2 = paddle.nn.Linear(in_features=64, out_features=128) self.fc3 = paddle.nn.Linear(in_features=128, out_features=256) self.fc4 = paddle.nn.Linear(in_features=256, out_features=512) self.fc5 = paddle.nn.Linear(in_features=512, out_features=1024) self.fc6 = paddle.nn.Linear(in_features=1024, out_features=2048) self.fc7 = paddle.nn.Linear(in_features=2048, out_features=2048) self.fc8 = paddle.nn.Linear(in_features=2048, out_features=2048) self.fc9 = paddle.nn.Linear(in_features=2048, out_features=1024) self.fc10 = paddle.nn.Linear(in_features=1024, out_features=512) self.fc11 = paddle.nn.Linear(in_features=512, out_features=256) self.fc12 = paddle.nn.Linear(in_features=256, out_features=128) self.fc13 = paddle.nn.Linear(in_features=128, out_features=64) self.fc14 = paddle.nn.Linear(in_features=64, out_features=1) self.relu = paddle.nn.ReLU()
def forward(self, inputs): x = self.relu(self.fc1(inputs)) x = self.relu(self.fc2(x)) x = self.relu(self.fc3(x)) x = self.relu(self.fc4(x)) x = self.relu(self.fc5(x)) x = self.relu(self.fc6(x)) x = self.relu(self.fc7(x)) x = self.relu(self.fc8(x)) x = self.relu(self.fc9(x)) x = self.relu(self.fc10(x)) x = self.relu(self.fc11(x)) x = self.relu(self.fc12(x)) x = self.relu(self.fc13(x)) x = self.fc14(x) return x
print("[+] final v2") st = time.time() train_df = pd.read_csv('data/data137263/pubg_train.csv.zip') test_df = pd.read_csv('data/data137263/pubg_test.csv.zip')
train_df = train_df.fillna(0) test_df = test_df.fillna(0)
train_df = train_df.drop(['player_name'], axis = 1) test_df = test_df.drop(['player_name'], axis = 1)
new_train_df = train_df.groupby(['match_id', 'team_id'], as_index = False).agg(np.mean) new_train_df['team_placement'] = new_train_df['team_placement'] / new_train_df['game_size'] train_df = pd.merge(train_df, new_train_df, on = ['match_id', 'team_id'], how = "outer") train_df = train_df.drop(train_df.columns[2:15], axis = 1)
new_test_df = test_df.groupby(['match_id', 'team_id'], as_index = False).agg(np.mean) test_df = pd.merge(test_df, new_test_df, on = ['match_id', 'team_id'], how = "outer") test_df = test_df.drop(test_df.columns[2:14], axis = 1)
for col in train_df.columns[3:-1]: train_df[col] /= train_df[col].max() for col in test_df.columns[3:]: test_df[col] /= test_df[col].max() print("[+] data processing cost {:.2f}s".format(time.time() - st))
model = PUBGRegressor()
model.train()
scheduler = paddle.optimizer.lr.CosineAnnealingDecay(learning_rate = 0.01, T_max = 200) opt = paddle.optimizer.Lamb(learning_rate = scheduler, lamb_weight_decay = 0.01,beta1 = 0.9, beta2 = 0.999, epsilon = 1e-06,parameters = model.parameters(), grad_clip = None, name = None)
EPOCH_NUM = 200 BATCH_SIZE = 1000 print(f"[+] EPOCH {EPOCH_NUM}, BATCH_SIZE {BATCH_SIZE}")
training_data = train_df.iloc[:-1 * int(train_df.shape[0] * 0.2)].values.astype(np.float32) val_data = train_df.iloc[-1 * int(train_df.shape[0] * 0.2):].values.astype(np.float32) min_loss = 100
for epoch_id in range(EPOCH_NUM): st = time.time() np.random.shuffle(training_data) mini_batches = [training_data[k:min(k+BATCH_SIZE, len(training_data))] for k in range(0, len(training_data), BATCH_SIZE)][:-1]
train_loss = [] for iter_id, mini_batch in enumerate(mini_batches): opt.clear_grad()
x = np.array(mini_batch[:, 3:-1]) y = np.array(mini_batch[:, -1])
features = paddle.to_tensor(x) y = paddle.to_tensor(y) predicts = model(features) loss = paddle.nn.functional.l1_loss(predicts, label = y) avg_loss = paddle.mean(loss) train_loss.append(avg_loss.numpy()) avg_loss.backward()
opt.step() mini_batches = [val_data[k:min(k+BATCH_SIZE, len(training_data))] for k in range(0, len(val_data), BATCH_SIZE)][:-1] val_loss = [] for iter_id, mini_batch in enumerate(mini_batches): x = np.array(mini_batch[:, 3:-1]) y = np.array(mini_batch[:, -1])
features = paddle.to_tensor(x) y = paddle.to_tensor(y) predicts = model(features) loss = paddle.nn.functional.l1_loss(predicts, label = y) avg_loss = paddle.mean(loss) val_loss.append(avg_loss.numpy())
print(f'Epoch {epoch_id}, train MAE {np.mean(train_loss) * 50:.3f}, val MAE {np.mean(val_loss) * 50:.3f}, timecost {time.time() - st:.2f}s') if min_loss > np.mean(val_loss): min_loss = np.mean(val_loss) paddle.save(model.state_dict(), 'best-pubg.model') print("min loss: ", min_loss * 50)
model.eval() test_data = paddle.to_tensor(test_df.iloc[:, 3:].values.astype(np.float32)) test_predict = model(test_data) test_predict = test_predict.numpy().squeeze()*test_df.iloc[:, 2].to_numpy() test_predict = test_predict.round().astype(int)
pd.DataFrame({ 'team_placement': test_predict }).to_csv('submission_PUBG_final.csv', index=None)
!zip submission_PUBG_final.zip submission_PUBG_final.csv
|