aitech-eks-pub-22/cw/08_regresja_logistyczna_ODPOWIEDZI.ipynb

1242 lines
28 KiB
Plaintext
Raw Normal View History

2022-05-03 20:46:10 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Logo 1](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech1.jpg)\n",
"<div class=\"alert alert-block alert-info\">\n",
"<h1> Ekstrakcja informacji </h1>\n",
"<h2> 8. <i>Regresja logistyczna</i> [ćwiczenia]</h2> \n",
"<h3> Jakub Pokrywka (2021)</h3>\n",
"</div>\n",
"\n",
"![Logo 2](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech2.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regresja logistyczna"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## import bibliotek"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"from sklearn.datasets import fetch_20newsgroups\n",
"# https://scikit-learn.org/0.19/datasets/twenty_newsgroups.html\n",
"\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.metrics import accuracy_score"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"CATEGORIES = ['soc.religion.christian', 'alt.atheism']"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"newsgroups_train_dev = fetch_20newsgroups(subset = 'train', categories=CATEGORIES)\n",
"newsgroups_test = fetch_20newsgroups(subset = 'test', categories=CATEGORIES)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"newsgroups_train_dev_text = newsgroups_train_dev['data']\n",
"newsgroups_test_text = newsgroups_test['data']"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"Y_train_dev = newsgroups_train_dev['target']\n",
"Y_test = newsgroups_test['target']"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"newsgroups_train_text, newsgroups_dev_text, Y_train, Y_dev = train_test_split(newsgroups_train_dev_text, Y_train_dev, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"Y_names = newsgroups_train_dev['target_names']"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['alt.atheism', 'soc.religion.christian']"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y_names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## baseline"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1,\n",
" 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0,\n",
" 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,\n",
" 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1,\n",
" 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1,\n",
" 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0,\n",
" 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0,\n",
" 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 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, 0])"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y_train"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 450\n",
"0 359\n",
"dtype: int64"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.value_counts(Y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### train"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5562422744128553"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"accuracy_score(np.ones_like(Y_train) * 1, Y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dev"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5518518518518518"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"accuracy_score(np.ones_like(Y_dev) * 1, Y_dev)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### test"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5550906555090656"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"accuracy_score(np.ones_like(Y_test) * 1, Y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PYTANIE: co jest nie tak z regresją liniową?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Regresja logistyczna"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### wektoryzacja"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"FEAUTERES = 10_000"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"vectorizer = TfidfVectorizer(max_features=10_000)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"X_train = vectorizer.fit_transform(newsgroups_train_text)\n",
"X_dev = vectorizer.transform(newsgroups_dev_text)\n",
"X_test = vectorizer.transform(newsgroups_test_text)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<717x10000 sparse matrix of type '<class 'numpy.float64'>'\n",
"\twith 120739 stored elements in Compressed Sparse Row format>"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### model - inicjalizacja "
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"class LogisticRegressionModel(torch.nn.Module):\n",
"\n",
" def __init__(self):\n",
" super(LogisticRegressionModel, self).__init__()\n",
" self.fc = torch.nn.Linear(FEAUTERES,1)\n",
"\n",
" def forward(self, x):\n",
" x = self.fc(x)\n",
" x = torch.sigmoid(x)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"lr_model = LogisticRegressionModel()"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.4983],\n",
" [0.4978],\n",
" [0.5004],\n",
" [0.4991],\n",
" [0.5014]], grad_fn=<SigmoidBackward0>)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr_model(torch.Tensor(X_train[0:5].astype(np.float32).todense()))"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LogisticRegressionModel(\n",
" (fc): Linear(in_features=10000, out_features=1, bias=True)\n",
")"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr_model"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Parameter containing:\n",
" tensor([[-0.0022, 0.0024, 0.0013, ..., 0.0090, 0.0095, 0.0065]],\n",
" requires_grad=True),\n",
" Parameter containing:\n",
" tensor([0.0043], requires_grad=True)]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(lr_model.parameters())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## model - trenowanie"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"BATCH_SIZE = 5"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"criterion = torch.nn.BCELoss()"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"optimizer = torch.optim.SGD(lr_model.parameters(), lr = 0.1)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"809"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y_train.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"loss_score = 0\n",
"acc_score = 0\n",
"items_total = 0\n",
"lr_model.train()\n",
"for i in range(0, Y_train.shape[0], BATCH_SIZE):\n",
" X = X_train[i:i+BATCH_SIZE]\n",
" X = torch.tensor(X.astype(np.float32).todense())\n",
" Y = Y_train[i:i+BATCH_SIZE]\n",
" Y = torch.tensor(Y.astype(np.float32)).reshape(-1,1)\n",
" Y_predictions = lr_model(X)\n",
" acc_score += torch.sum((Y_predictions > 0.5) == Y).item()\n",
" items_total += Y.shape[0] \n",
" \n",
" optimizer.zero_grad()\n",
" loss = criterion(Y_predictions, Y)\n",
" loss.backward()\n",
" optimizer.step()\n",
" \n",
"\n",
" loss_score += loss.item() * Y.shape[0] "
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.5029],\n",
" [0.6063],\n",
" [0.5796],\n",
" [0.4821]], grad_fn=<SigmoidBackward0>)"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y_predictions"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.],\n",
" [1.],\n",
" [1.],\n",
" [0.]])"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"673"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"acc_score"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"809"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"items_total"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"accuracy: 0.8318912237330037\n"
]
}
],
"source": [
"print(f'accuracy: {acc_score / items_total}')"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BCE loss: 0.551247839174695\n"
]
}
],
"source": [
"print(f'BCE loss: {loss_score / items_total}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### model - ewaluacja"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
"def get_loss_acc(model, X_dataset, Y_dataset):\n",
" loss_score = 0\n",
" acc_score = 0\n",
" items_total = 0\n",
" model.eval()\n",
" for i in range(0, Y_dataset.shape[0], BATCH_SIZE):\n",
" X = X_dataset[i:i+BATCH_SIZE]\n",
" X = torch.tensor(X.astype(np.float32).todense())\n",
" Y = Y_dataset[i:i+BATCH_SIZE]\n",
" Y = torch.tensor(Y.astype(np.float32)).reshape(-1,1)\n",
" Y_predictions = model(X)\n",
" acc_score += torch.sum((Y_predictions > 0.5) == Y).item()\n",
" items_total += Y.shape[0] \n",
"\n",
" loss = criterion(Y_predictions, Y)\n",
"\n",
" loss_score += loss.item() * Y.shape[0] \n",
" return (loss_score / items_total), (acc_score / items_total)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.5396295055765451, 0.7935723114956736)"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_loss_acc(lr_model, X_train, Y_train)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.5654726171935046, 0.7407407407407407)"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_loss_acc(lr_model, X_dev, Y_dev)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.5901291338386562, 0.6847977684797768)"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_loss_acc(lr_model, X_test, Y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### wagi modelu"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Parameter containing:\n",
" tensor([[ 0.0749, -0.0687, 0.0117, ..., 0.0045, 0.0223, -0.0058]],\n",
" requires_grad=True),\n",
" Parameter containing:\n",
" tensor([0.1239], requires_grad=True)]"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(lr_model.parameters())"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([ 0.0749, -0.0687, 0.0117, ..., 0.0045, 0.0223, -0.0058],\n",
" grad_fn=<SelectBackward0>)"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(lr_model.parameters())[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.return_types.topk(\n",
"values=tensor([0.6079, 0.4051, 0.3739, 0.3648, 0.3574, 0.3527, 0.3471, 0.3414, 0.3330,\n",
" 0.3024, 0.2906, 0.2766, 0.2705, 0.2418, 0.2389, 0.2333, 0.2230, 0.2156,\n",
" 0.2151, 0.2129], grad_fn=<TopkBackward0>),\n",
"indices=tensor([8942, 6336, 4039, 1857, 9709, 9056, 1852, 5002, 1865, 7820, 803, 3558,\n",
" 4306, 4259, 8208, 1046, 1855, 4285, 6481, 130]))"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.topk(list(lr_model.parameters())[0][0], 20)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the\n",
"of\n",
"god\n",
"christians\n",
"we\n",
"to\n",
"christ\n",
"jesus\n",
"church\n",
"rutgers\n",
"and\n",
"faith\n",
"hell\n",
"he\n",
"sin\n",
"athos\n",
"christian\n",
"heaven\n",
"our\n",
"1993\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/kuba/anaconda3/envs/zajeciaei/lib/python3.10/site-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function get_feature_names is deprecated; get_feature_names is deprecated in 1.0 and will be removed in 1.2. Please use get_feature_names_out instead.\n",
" warnings.warn(msg, category=FutureWarning)\n"
]
}
],
"source": [
"for i in torch.topk(list(lr_model.parameters())[0][0], 20)[1]:\n",
" print(vectorizer.get_feature_names()[i])"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.return_types.topk(\n",
"values=tensor([-0.7723, -0.5291, -0.4631, -0.4499, -0.4225, -0.4144, -0.4041, -0.4019,\n",
" -0.3622, -0.3604, -0.3442, -0.3228, -0.3218, -0.3179, -0.3162, -0.3127,\n",
" -0.3034, -0.3027, -0.2983, -0.2750], grad_fn=<TopkBackward0>),\n",
"indices=tensor([5119, 1627, 8096, 5420, 6194, 5946, 4436, 6901, 1991, 4925, 3116, 4926,\n",
" 9906, 1036, 8329, 7869, 4959, 8800, 6289, 7921]))"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.topk(list(lr_model.parameters())[0][0], 20, largest = False)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"keith\n",
"caltech\n",
"sgi\n",
"livesey\n",
"nntp\n",
"morality\n",
"host\n",
"posting\n",
"com\n",
"islam\n",
"edu\n",
"islamic\n",
"wpd\n",
"atheism\n",
"solntze\n",
"sandvik\n",
"jaeger\n",
"system\n",
"objective\n",
"schneider\n"
]
}
],
"source": [
"for i in torch.topk(list(lr_model.parameters())[0][0], 20, largest = False)[1]:\n",
" print(vectorizer.get_feature_names()[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### sieć neuronowa"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"class NeuralNetworkModel(torch.nn.Module):\n",
"\n",
" def __init__(self):\n",
" super(NeuralNetworkModel, self).__init__()\n",
" self.fc1 = torch.nn.Linear(FEAUTERES,500)\n",
" self.fc2 = torch.nn.Linear(500,1)\n",
"\n",
" def forward(self, x):\n",
" x = self.fc1(x)\n",
" x = torch.relu(x)\n",
" x = self.fc2(x)\n",
" x = torch.sigmoid(x)\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"nn_model = NeuralNetworkModel()"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
"BATCH_SIZE = 5"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"criterion = torch.nn.BCELoss()"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"optimizer = torch.optim.SGD(nn_model.parameters(), lr = 0.1)"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.6796266682657824, 0.5562422744128553)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.6829625014905576, 0.5518518518518518)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.6543819982056565, 0.5562422744128553)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.662480209712629, 0.5518518518518518)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.5808140672328888, 0.7132262051915945)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.6008473800288306, 0.6555555555555556)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.4458613999657637, 0.9048207663782447)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.48269164175898943, 0.8481481481481481)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"4"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.3061209664080287, 0.9567367119901112)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(0.3538406518874345, 0.9074074074074074)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for epoch in range(5):\n",
" loss_score = 0\n",
" acc_score = 0\n",
" items_total = 0\n",
" nn_model.train()\n",
" for i in range(0, Y_train.shape[0], BATCH_SIZE):\n",
" X = X_train[i:i+BATCH_SIZE]\n",
" X = torch.tensor(X.astype(np.float32).todense())\n",
" Y = Y_train[i:i+BATCH_SIZE]\n",
" Y = torch.tensor(Y.astype(np.float32)).reshape(-1,1)\n",
" Y_predictions = nn_model(X)\n",
" acc_score += torch.sum((Y_predictions > 0.5) == Y).item()\n",
" items_total += Y.shape[0] \n",
"\n",
" optimizer.zero_grad()\n",
" loss = criterion(Y_predictions, Y)\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
"\n",
" loss_score += loss.item() * Y.shape[0] \n",
"\n",
" display(epoch)\n",
" display(get_loss_acc(nn_model, X_train, Y_train))\n",
" display(get_loss_acc(nn_model, X_dev, Y_dev))"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.4221827702666925, 0.8619246861924686)"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_loss_acc(nn_model, X_test, Y_test)"
]
}
],
"metadata": {
"author": "Jakub Pokrywka",
"email": "kubapok@wmi.amu.edu.pl",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"lang": "pl",
"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.10.4"
},
"subtitle": "8.Regresja logistyczna[ćwiczenia]",
"title": "Ekstrakcja informacji",
"year": "2021"
},
"nbformat": 4,
"nbformat_minor": 4
}