{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b44bab5d-a11d-4003-96ff-4f71c083fef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "\n",
    "from datasets.dataset import transform_dataset, kfold_dataset\n",
    "from DRNet import train, DRNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f49dd5dd-aca4-46a7-8bbe-781f1a057ed3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read datasets\n",
    "name = 'adult'\n",
    "X, Y, X_headers, Y_headers = transform_dataset(name, method='onehot-compare', negations=False, labels='binary')\n",
    "datasets = kfold_dataset(X, Y, shuffle=1)\n",
    "X_train, X_test, Y_train, Y_test = datasets[0]\n",
    "\n",
    "train_set = torch.utils.data.TensorDataset(torch.Tensor(X_train.to_numpy()), torch.Tensor(Y_train))\n",
    "test_set = torch.utils.data.TensorDataset(torch.Tensor(X_test.to_numpy()), torch.Tensor(Y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b60c1c84-8267-4a3a-9e86-89363f8ec696",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Epoch: 100%|██████████| 2000/2000 [07:36<00:00,  4.38it/s, loss=0.544, epoch accu=0.831, test accu=0.836, num rules=11, sparsity=0.907]                                                                                                                                                 \n"
     ]
    }
   ],
   "source": [
    "# Train DR-Net\n",
    "# Default learning rate (1e-2), and_lam (1e-2), and and_lam (1e-5) usually work the best. A large epochs number is necessary for a sparse rule set i.e 10000 epochs.\n",
    "net = DRNet(train_set[:][0].size(1), 50, 1)\n",
    "train(net, train_set, test_set=test_set, device='cuda', lr=1e-2, epochs=2000, batch_size=400,\n",
    "      and_lam=1e-2, or_lam=1e-5, num_alter=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f208a470-759a-4499-a3a8-e4cf004c8663",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.836399801093983, num rules: 11, num conditions: 131\n"
     ]
    }
   ],
   "source": [
    "# Get accuracy and the rule net\n",
    "accu = (net.predict(np.array(X_test)) == Y_test).mean()\n",
    "rules = net.get_rules(X_headers)\n",
    "print(f'Accuracy: {accu}, num rules: {len(rules)}, num conditions: {sum(map(len, rules))}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1260593-6f2b-44e2-9a11-27ffdea0d14b",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
