Skip to content

Commit

Permalink
Add otto
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuchizu committed Mar 25, 2021
1 parent 2812e48 commit 1c21871
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ run-mnist_1 | ===========================================
...
```

### Fashion MNIST
```bash
docker-compose up run-fashion-mnist
```

### EMNIST
```bash
docker-compose up run-emnist
```

### otto
下に書いた方法でデータをダウンロードしてから
```bash
docker-compose up run-otto
```

## データセットについて

#### MNIST
Expand All @@ -88,6 +104,23 @@ run-mnist_1 | ===========================================
`data/fashion_train_400.csv`ですが、作成方法は上と同じです。
元データは100MBを超えるのでGitHubにアップロードできませんでした。

#### EMNIST
[EMNIST (Extended MNIST)](https://www.kaggle.com/crawford/emnist) のデータセットを使っています。

`data/emnist_train_400.csv`は上のデータセットの`emnist-letters-test.csv`をMNISTと同様に処理したものです。

> Cohen, G., Afshar, S., Tapson, J., & van Schaik, A. (2017). EMNIST: an extension of MNIST to handwritten letters. Retrieved from http://arxiv.org/abs/1702.05373
#### otto
[Otto Group Product Classification Challenge](https://www.kaggle.com/c/otto-group-product-classification-challenge) というKaggleのコンペのデータです。
入力は93個の特徴量、出力は9クラスの多クラス分類のタスクです。

[このリンクの先で](https://www.kaggle.com/c/otto-group-product-classification-challenge/data) Kaggleにログインし、`Join Competition`
を押してから下の画面のtrain.csvをダウンロードしてください。
![](img/1-join.png)

ダウンロードしたファイルを解凍したら、`train.csv`をこのディレクトリの`data/otto-train.csv` に移動することでデータが利用できます。

## エラーについて

たまに学習時、以下のようなエラーが発生します。~~原因はよくわかってないので、実行し直す、分割して実行するなどして対処してください。~~
Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ services:
volumes:
- .:/src
- ~/.amplify:/src/src/token
run-emnist:
command:
- "python"
- "-u"
- "emnist.py"
build: .
volumes:
- .:/src
- ~/.amplify:/src/src/token
run-otto:
command:
- "python"
- "-u"
- "otto.py"
build: .
volumes:
- .:/src
- ~/.amplify:/src/src/token
120 changes: 120 additions & 0 deletions src/otto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score, accuracy_score
from omegaconf import OmegaConf
from torch.utils.data import Dataset, DataLoader

from utils import *

conf = """
base:
api_key_path: "token/token.json"
train_path: "../data/otto-train.csv"
seed: 67
dataset:
img_size: 20
features: 93 # 特徴量の数
val_size: 300 # validationに使うデータ数
target: 0 # ラベル
model:
timeout: 3000 # ms 計算時間
# 訓練に使うデータは(batch_size * n_iter)個
batch_size: 30 # バッチサイズ
n_iter: 5 # ループ数
l: 3 # 正則化項
each_weight: 2.5 # 重み係数
length_weight: 5 # 重みの層の数
multiprocessing: true
"""
target_dict = {
"Class_1": 0,
"Class_2": 1,
"Class_3": 2,
"Class_4": 3,
"Class_5": 4,
"Class_6": 5,
"Class_7": 6,
"Class_8": 7,
"Class_9": 8
}

cfg = OmegaConf.create(conf)

init_client(cfg)


# PyTorch形式
class MyDataset(Dataset):
def __init__(self, data, label):
self.label = label
self.data = np.apply_along_axis(minmax, 1, data)

self.label = np.where(self.label == cfg.dataset.target, 1, 0)
self.data[self.data == 0] = -1

def __len__(self):
return self.data.shape[0]

def __getitem__(self, idx):
return self.data[idx, :], self.label[idx]


def get_ds(n, seed):
true_ = dataset.loc[dataset.iloc[:, -1] == cfg.dataset.target, :].sample(n // 2)
false_ = dataset.loc[dataset.iloc[:, -1] != cfg.dataset.target, :].sample(n // 2)
return pd.concat(
[
true_,
false_
]
).sample(
frac=1,
random_state=seed
).values


dataset = pd.read_csv(cfg.base.train_path).iloc[:, 1:]
dataset["target"] = dataset["target"].map(target_dict)
for i in range(9):
init_client(cfg)
cfg.dataset.target = i
train = get_ds(
int(
cfg.model.n_iter * cfg.model.batch_size
),
cfg.base.seed
)

val = get_ds(
cfg.dataset.val_size,
cfg.base.seed + 1
)

train_ds = MyDataset(train[:, :-1], train[:, -1])
valid_ds = MyDataset(val[:, :-1], val[:, -1])
train_dl = DataLoader(
train_ds,
batch_size=cfg.model.batch_size,
shuffle=True
)
valid_dl = DataLoader(
valid_ds,
batch_size=64,
shuffle=False
)
weight = np.zeros(
(
cfg.dataset.features,
cfg.model.length_weight
)
)

weight = run_model(cfg, train_dl, weight, cfg.model.multiprocessing)
pred, label = eval_model(cfg, valid_dl, weight)

print("=" * 20, i, "=" * 20)
print("AUC:", roc_auc_score(label, pred))
print("ACC:", accuracy_score(label, np.round(pred)))
print("=" * 43)

0 comments on commit 1c21871

Please sign in to comment.