Bitcoin hash-rate予測1回Defense



https://www.kaggle.com/geunyeongju/defense-baseline-code

データのロード

train = pd.read_csv('/kaggle/input/sejongai-hashrate/train.csv')
test = pd.read_csv('/kaggle/input/sejongai-hashrate/test.csv')
sample = pd.read_csv('/kaggle/input/sejongai-hashrate/submit_sample.csv')

モジュールインポートとGPUの使用

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import random
from sklearn.preprocessing import MinMaxScaler

device ='cuda' if torch.cuda.is_available() else 'cpu'

random.seed(777)
torch.manual_seed(777)
if device == 'cuda' :
  torch.cuda.manual_seed_all(777)

device

データグループ

x_train = train.iloc[:,1:-1]
x_train['Timestamp'] = x_train['Timestamp'].str.split('/').str[0].astype(int)
x_test = test.iloc[:,1:]
x_test['Timestamp'] = x_test['Timestamp'].str.split('/').str[0].astype(int)
y_train = train.iloc[:,-1]


sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

x_train = torch.FloatTensor(x_train).to(device)
x_test = torch.FloatTensor(x_test).to(device)
y_train = torch.FloatTensor(y_train).reshape(-1,1).to(device)
「/」単位で年、月、日単位で月をTimestamp列に再抽出し、学習データを再加工します.

データ検証

# train data
print(x_train[:5])
print(x_train.shape)
print(y_train[:5])
print(y_train.shape)

# test data
print(x_test[:3])

モデル定義

class NN(torch.nn.Module):
    def __init__(self):
        super(NN,self).__init__()
        
        self.linear1 = nn.Linear(7,512,bias=True)
        self.linear2 = nn.Linear(512,256,bias=True)
        self.linear3 = nn.Linear(256,512,bias=True)
        self.linear4 = nn.Linear(512,64,bias=True)
        self.linear5 = nn.Linear(64,32,bias=True)
        self.linear6 = nn.Linear(32,1,bias=True)
        self.relu = nn.ReLU()
        
        torch.nn.init.orthogonal_(self.linear1.weight)
        torch.nn.init.orthogonal_(self.linear2.weight)
        torch.nn.init.orthogonal_(self.linear3.weight)
        torch.nn.init.orthogonal_(self.linear4.weight)
        torch.nn.init.orthogonal_(self.linear5.weight)
        torch.nn.init.orthogonal_(self.linear6.weight)
        
    def forward(self,x):
        out = self.linear1(x)
        out = self.relu(out)
        out = self.linear2(out)
        out = self.relu(out)
        out = self.linear3(out)
        out = self.relu(out)
        out = self.linear4(out)
        out = self.relu(out)
        out = self.linear5(out)
        out = self.relu(out)
        out = self.linear6(out)
        return out
カラムが追加され、入力が7に設定されます.

学習パラメータの設定

model = NN().to(device)
optimizer = optim.Adam(model.parameters(), lr=15e-5)
loss = nn.MSELoss().to(device)
epochs = 1500
model

マッチングを避けるために,epochを1500に減らすと性能が最も高いと決定した.
学習速度も変更しました.

学習モデル

model.train()
plt_los = []
train_total_batch = len(x_train)
for epoch in range(epochs+1) : 
    avg_cost = 0
    model.train()

    hypothesis = model(x_train) 
    cost = loss(hypothesis,y_train) 
    
    optimizer.zero_grad() 
    cost.backward()
    optimizer.step() 
    avg_cost += cost / train_total_batch
    plt_los.append([avg_cost.item()])
    if epoch%100==0:
        print('Epoch : {}, Cost : {}'.format(epoch, avg_cost.item()))

Plot

import matplotlib.pyplot as plt

def plot(loss_list: list, ylim=None, title=None) -> None:
    bn = [i[0] for i in loss_list]

    plt.figure(figsize=(10, 10))
    plt.plot(bn, label='avg_cost')
    if ylim:
        plt.ylim(ylim)

    if title:
        plt.title(title)
    plt.legend()
    plt.grid('on')
    plt.show()
    
plot(plt_los , [0.0, 1.0], title='Loss at Epoch')
print(avg_cost.item())

plot関数を定義してコスト値を可視化し,学習が良好であることを確認した.

予測のエクスポートとコミット

model.eval()
with torch.no_grad():
    y_pred = model(x_test)
sample['hash-rate'] = y_pred.cpu().numpy()
sample.to_csv('submit_sample.csv',index=False)
sample