Bitcoin hash-rateは1回目の攻撃を予測する


https://www.kaggle.com/uhhyunjoo/baseline-code-2f4c6a
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

データのロード

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[:,2:-1]
x_test = test.iloc[:,2:]
y_train = train.iloc[:,-1]


sc = MinMaxScaler()
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)

データ検証

# 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(6,512,bias=True)
        self.linear2 = nn.Linear(512,256,bias=True)
        self.linear3 = nn.Linear(256,64,bias=True)
        self.linear4 = nn.Linear(64,1,bias=True)
        self.relu = nn.ReLU()

        
    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)
        return out

model = NN().to(device)
model

学習パラメータの設定

optimizer = optim.Adam(model.parameters(), lr=0.001)
loss = nn.MSELoss().to(device)

epochs = 5000

学習モデル

model.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() 
    
    if epoch%100==0:
        print('Epoch : {}, Cost : {}'.format(epoch, cost.item())
Epoch : 0, Cost : 12024.15234375
Epoch : 100, Cost : 344.5037536621094
Epoch : 200, Cost : 133.16754150390625
Epoch : 300, Cost : 60.7478141784668
Epoch : 400, Cost : 55.92856979370117
Epoch : 500, Cost : 52.68528366088867
Epoch : 600, Cost : 50.02294158935547
Epoch : 700, Cost : 46.67107391357422
Epoch : 800, Cost : 44.24735641479492
Epoch : 900, Cost : 42.728973388671875
Epoch : 1000, Cost : 41.79881286621094
Epoch : 1100, Cost : 41.05110168457031
Epoch : 1200, Cost : 40.34303283691406
Epoch : 1300, Cost : 39.66261672973633
Epoch : 1400, Cost : 39.015777587890625
Epoch : 1500, Cost : 38.413230895996094
Epoch : 1600, Cost : 37.87659454345703
Epoch : 1700, Cost : 37.27472686767578
Epoch : 1800, Cost : 36.63091278076172
Epoch : 1900, Cost : 35.99745559692383
Epoch : 2000, Cost : 35.165828704833984
Epoch : 2100, Cost : 34.02845764160156
Epoch : 2200, Cost : 33.17118453979492
Epoch : 2300, Cost : 32.437374114990234
Epoch : 2400, Cost : 31.700515747070312
Epoch : 2500, Cost : 31.059764862060547
Epoch : 2600, Cost : 30.406587600708008
Epoch : 2700, Cost : 29.746143341064453
Epoch : 2800, Cost : 29.046707153320312
Epoch : 2900, Cost : 28.428150177001953
Epoch : 3000, Cost : 27.841066360473633
Epoch : 3100, Cost : 27.30438232421875
Epoch : 3200, Cost : 26.81180191040039
Epoch : 3300, Cost : 26.360044479370117
Epoch : 3400, Cost : 25.912120819091797
Epoch : 3500, Cost : 25.50192642211914
Epoch : 3600, Cost : 25.27527618408203
Epoch : 3700, Cost : 24.77205467224121
Epoch : 3800, Cost : 24.49279022216797
Epoch : 3900, Cost : 24.159841537475586
Epoch : 4000, Cost : 23.82038688659668
Epoch : 4100, Cost : 23.480161666870117
Epoch : 4200, Cost : 23.064599990844727
Epoch : 4300, Cost : 22.8005428314209
Epoch : 4400, Cost : 22.67483139038086
Epoch : 4500, Cost : 22.4219970703125
Epoch : 4600, Cost : 22.171236038208008
Epoch : 4700, Cost : 21.918193817138672
Epoch : 4800, Cost : 21.6683292388916
Epoch : 4900, Cost : 21.49550437927246
Epoch : 5000, Cost : 21.345048904418945

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

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