|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "import sys\n",
- "\n",
- "sys.path.append(\"../../\")\n",
- "\n",
- "import torch.nn as nn\n",
- "import torch\n",
- "\n",
- "from abl.abducer.abducer_base import HED_Abducer\n",
- "from abl.abducer.kb import HED_prolog_KB\n",
- "\n",
- "from abl.utils.plog import logger\n",
- "from abl.models.basic_model import BasicModel\n",
- "from abl.models.wabl_models import WABLBasicModel\n",
- "\n",
- "from models.nn import SymbolNet\n",
- "from datasets.get_hed import get_hed, split_equation\n",
- "import framework_hed"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialize logger\n",
- "recorder = logger()"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Logic Part"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialize knowledge base and abducer\n",
- "kb = HED_prolog_KB(pseudo_label_list=[1, 0, '+', '='], pl_file='./datasets/learn_add.pl')\n",
- "abducer = HED_Abducer(kb)"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Machine Learning Part"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialize necessary component for machine learning part\n",
- "cls = SymbolNet(\n",
- " num_classes=len(kb.pseudo_label_list),\n",
- " image_size=(28, 28, 1),\n",
- ")\n",
- "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
- "criterion = nn.CrossEntropyLoss()\n",
- "optimizer = torch.optim.RMSprop(cls.parameters(), lr=0.001, weight_decay=1e-6)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Pretrain NN classifier\n",
- "framework_hed.hed_pretrain(kb, cls, recorder)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialize BasicModel\n",
- "# The function of BasicModel is to wrap NN models into the form of an sklearn estimator\n",
- "base_model = BasicModel(\n",
- " cls,\n",
- " criterion,\n",
- " optimizer,\n",
- " device,\n",
- " save_interval=1,\n",
- " save_dir=recorder.save_dir,\n",
- " batch_size=32,\n",
- " num_epochs=1,\n",
- " recorder=recorder,\n",
- ")"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Use WABL model to join two parts"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model = WABLBasicModel(base_model, kb.pseudo_label_list)"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Dataset"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "total_train_data = get_hed(train=True)\n",
- "train_data, val_data = split_equation(total_train_data, 3, 1)\n",
- "test_data = get_hed(train=False)"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Train and save"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model, mapping = framework_hed.train_with_rule(model, abducer, train_data, val_data, select_num=10, min_len=5, max_len=8)\n",
- "framework_hed.hed_test(model, abducer, mapping, train_data, test_data, min_len=5, max_len=8)\n",
- "\n",
- "recorder.dump()"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "ABL",
- "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.16"
- },
- "orig_nbformat": 4
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|