Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
d937867cb5 | |||
26b05adf74 | |||
1dc429b554 | |||
f02d3abb2f |
110
.ipynb_checkpoints/Bayes-checkpoint.ipynb
Normal file
110
.ipynb_checkpoints/Bayes-checkpoint.ipynb
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from sklearn.preprocessing import LabelEncoder\n",
|
||||||
|
"from sklearn.naive_bayes import MultinomialNB\n",
|
||||||
|
"from sklearn.pipeline import make_pipeline\n",
|
||||||
|
"from sklearn.feature_extraction.text import TfidfVectorizer"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"train/in.tsv\") as f:\n",
|
||||||
|
" x_train = f.readlines()\n",
|
||||||
|
"\n",
|
||||||
|
"with open(\"train/expected.tsv\") as f:\n",
|
||||||
|
" y_train = f.readlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"array([1, 0, 0, ..., 0, 0, 1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"y_train = LabelEncoder().fit_transform(y_train)\n",
|
||||||
|
"y_train"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pipeline = make_pipeline(TfidfVectorizer(),MultinomialNB())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 22,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model = pipeline.fit(x_train, y_train)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 23,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"dev-0/in.tsv\") as f:\n",
|
||||||
|
" x_dev = f.readlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 24,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"prediction = model.predict(x_dev)\n",
|
||||||
|
"np.savetxt(\"dev-0/out.tsv\", prediction, fmt='%d')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"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.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
269
.ipynb_checkpoints/LogisticRegression-checkpoint.ipynb
Normal file
269
.ipynb_checkpoints/LogisticRegression-checkpoint.ipynb
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import torch\n",
|
||||||
|
"from nltk.tokenize import word_tokenize\n",
|
||||||
|
"import gensim.downloader"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#wczytywanie danych\n",
|
||||||
|
"x_train = pd.read_table('train/in.tsv', sep='\\t', error_bad_lines=False, quoting=3, header=None, names=['content', 'id'], usecols=['content'])\n",
|
||||||
|
"y_train = pd.read_table('train/expected.tsv', sep='\\t', error_bad_lines=False, quoting=3, header=None, names=['label'])\n",
|
||||||
|
"x_dev = pd.read_table('dev-0/in.tsv', sep='\\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])\n",
|
||||||
|
"x_test = pd.read_table('test-A/in.tsv', sep='\\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = x_train.content.str.lower()\n",
|
||||||
|
"x_dev = x_dev.content.str.lower()\n",
|
||||||
|
"x_test = x_test.content.str.lower()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[nltk_data] Downloading package punkt to /home/tomasz/nltk_data...\n",
|
||||||
|
"[nltk_data] Unzipping tokenizers/punkt.zip.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"True"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"import nltk\n",
|
||||||
|
"nltk.download('punkt')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = [word_tokenize(content) for content in x_train]\n",
|
||||||
|
"x_dev = [word_tokenize(content) for content in x_dev]\n",
|
||||||
|
"x_test = [word_tokenize(content) for content in x_test]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"word2vec = gensim.downloader.load(\"word2vec-google-news-300\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def document_vector(doc):\n",
|
||||||
|
" \"\"\"Create document vectors by averaging word vectors. Remove out-of-vocabulary words.\"\"\"\n",
|
||||||
|
" return np.mean([word2vec[w] for w in doc if w in word2vec] or [np.zeros(300)], axis=0)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = [document_vector(doc) for doc in x_train]\n",
|
||||||
|
"x_dev = [document_vector(doc) for doc in x_dev]\n",
|
||||||
|
"x_test = [document_vector(doc) for doc in x_test]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class NeuralNetwork(torch.nn.Module): \n",
|
||||||
|
" def __init__(self, hidden_size):\n",
|
||||||
|
" super(NeuralNetwork, self).__init__()\n",
|
||||||
|
" self.l1 = torch.nn.Linear(300, hidden_size)\n",
|
||||||
|
" self.l2 = torch.nn.Linear(hidden_size, 1)\n",
|
||||||
|
"\n",
|
||||||
|
" def forward(self, x):\n",
|
||||||
|
" x = self.l1(x)\n",
|
||||||
|
" x = torch.relu(x)\n",
|
||||||
|
" x = self.l2(x)\n",
|
||||||
|
" x = torch.sigmoid(x)\n",
|
||||||
|
" return x"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hidden_size = 600\n",
|
||||||
|
"epochs = 5\n",
|
||||||
|
"batch_size = 15\n",
|
||||||
|
"model = NeuralNetwork(hidden_size)\n",
|
||||||
|
"criterion = torch.nn.BCELoss()\n",
|
||||||
|
"optimizer = torch.optim.SGD(model.parameters(), lr=0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"/home/tomasz/.local/lib/python3.8/site-packages/torch/autograd/__init__.py:130: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)\n",
|
||||||
|
" Variable._execution_engine.run_backward(\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for epoch in range(epochs):\n",
|
||||||
|
" 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)\n",
|
||||||
|
" y = y_train[i:i+batch_size]\n",
|
||||||
|
" y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1, 1)\n",
|
||||||
|
" \n",
|
||||||
|
" outputs = model(X.float())\n",
|
||||||
|
" loss = criterion(outputs, y)\n",
|
||||||
|
" \n",
|
||||||
|
" optimizer.zero_grad()\n",
|
||||||
|
" loss.backward()\n",
|
||||||
|
" optimizer.step()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y_dev = []\n",
|
||||||
|
"y_test = []"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"NeuralNetwork(\n",
|
||||||
|
" (l1): Linear(in_features=300, out_features=600, bias=True)\n",
|
||||||
|
" (l2): Linear(in_features=600, out_features=1, bias=True)\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"model.eval()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with torch.no_grad():\n",
|
||||||
|
" for i in range(0, len(x_dev), batch_size):\n",
|
||||||
|
" X = x_dev[i:i+batch_size]\n",
|
||||||
|
" X = torch.tensor(X)\n",
|
||||||
|
" outputs = model(X.float()) \n",
|
||||||
|
" prediction = (outputs > 0.5)\n",
|
||||||
|
" y_dev += prediction.tolist()\n",
|
||||||
|
"\n",
|
||||||
|
" for i in range(0, len(x_test), batch_size):\n",
|
||||||
|
" X = x_test[i:i+batch_size]\n",
|
||||||
|
" X = torch.tensor(X)\n",
|
||||||
|
" outputs = model(X.float())\n",
|
||||||
|
" y = (outputs > 0.5)\n",
|
||||||
|
" y_test += prediction.tolist()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y_dev = np.asarray(y_dev, dtype=np.int32)\n",
|
||||||
|
"y_test = np.asarray(y_test, dtype=np.int32)\n",
|
||||||
|
"\n",
|
||||||
|
"y_dev = pd.DataFrame({'label':y_dev})\n",
|
||||||
|
"y_test = pd.DataFrame({'label':y_test})\n",
|
||||||
|
"\n",
|
||||||
|
"y_dev.to_csv(r'dev-0/out.tsv', sep='\\t', index=False, header=False)\n",
|
||||||
|
"y_test.to_csv(r'test-A/out.tsv', sep='\\t', index=False, header=False)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"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.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
BIN
Raport.docx
Normal file
BIN
Raport.docx
Normal file
Binary file not shown.
130
bayes.ipynb
Normal file
130
bayes.ipynb
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from sklearn.preprocessing import LabelEncoder\n",
|
||||||
|
"from sklearn.naive_bayes import MultinomialNB\n",
|
||||||
|
"from sklearn.pipeline import make_pipeline\n",
|
||||||
|
"from sklearn.feature_extraction.text import TfidfVectorizer"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"train/in.tsv\") as f:\n",
|
||||||
|
" x_train = f.readlines()\n",
|
||||||
|
"\n",
|
||||||
|
"with open(\"train/expected.tsv\") as f:\n",
|
||||||
|
" y_train = f.readlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"array([1, 0, 0, ..., 0, 0, 1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"y_train = LabelEncoder().fit_transform(y_train)\n",
|
||||||
|
"y_train"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"pipeline = make_pipeline(TfidfVectorizer(),MultinomialNB())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 22,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"model = pipeline.fit(x_train, y_train)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 23,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"dev-0/in.tsv\") as f:\n",
|
||||||
|
" x_dev = f.readlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 25,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"prediction = model.predict(x_dev)\n",
|
||||||
|
"np.savetxt(\"dev-0/out.tsv\", prediction, fmt='%d')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 26,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"test-A/in.tsv\") as f:\n",
|
||||||
|
" x_test = f.readlines()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 27,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"prediction = model.predict(x_test)\n",
|
||||||
|
"np.savetxt(\"test-A/out.tsv\", prediction, fmt='%d')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"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.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
29
bayes.py
Executable file
29
bayes.py
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
import numpy as np
|
||||||
|
from sklearn.preprocessing import LabelEncoder
|
||||||
|
from sklearn.naive_bayes import MultinomialNB
|
||||||
|
from sklearn.pipeline import make_pipeline
|
||||||
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
||||||
|
|
||||||
|
with open("train/in.tsv") as f:
|
||||||
|
x_train = f.readlines()
|
||||||
|
|
||||||
|
with open("train/expected.tsv") as f:
|
||||||
|
y_train = f.readlines()
|
||||||
|
|
||||||
|
y_train = LabelEncoder().fit_transform(y_train)
|
||||||
|
|
||||||
|
pipeline = make_pipeline(TfidfVectorizer(),MultinomialNB())
|
||||||
|
|
||||||
|
model = pipeline.fit(x_train, y_train)
|
||||||
|
|
||||||
|
with open("dev-0/in.tsv") as f:
|
||||||
|
x_dev = f.readlines()
|
||||||
|
|
||||||
|
prediction = model.predict(x_dev)
|
||||||
|
np.savetxt("dev-0/out.tsv", prediction, fmt='%d')
|
||||||
|
|
||||||
|
with open("test-A/in.tsv") as f:
|
||||||
|
x_test = f.readlines()
|
||||||
|
|
||||||
|
prediction = model.predict(x_test)
|
||||||
|
np.savetxt("test-A/out.tsv", prediction, fmt='%d')
|
5272
dev-0/in.tsv
Normal file
5272
dev-0/in.tsv
Normal file
File diff suppressed because one or more lines are too long
BIN
dev-0/in.tsv.xz
BIN
dev-0/in.tsv.xz
Binary file not shown.
5272
dev-0/out.tsv
Normal file
5272
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
269
neural_network.ipynb
Normal file
269
neural_network.ipynb
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import torch\n",
|
||||||
|
"from nltk.tokenize import word_tokenize\n",
|
||||||
|
"import gensim.downloader"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#wczytywanie danych\n",
|
||||||
|
"x_train = pd.read_table('train/in.tsv', sep='\\t', error_bad_lines=False, quoting=3, header=None, names=['content', 'id'], usecols=['content'])\n",
|
||||||
|
"y_train = pd.read_table('train/expected.tsv', sep='\\t', error_bad_lines=False, quoting=3, header=None, names=['label'])\n",
|
||||||
|
"x_dev = pd.read_table('dev-0/in.tsv', sep='\\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])\n",
|
||||||
|
"x_test = pd.read_table('test-A/in.tsv', sep='\\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = x_train.content.str.lower()\n",
|
||||||
|
"x_dev = x_dev.content.str.lower()\n",
|
||||||
|
"x_test = x_test.content.str.lower()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[nltk_data] Downloading package punkt to /home/tomasz/nltk_data...\n",
|
||||||
|
"[nltk_data] Unzipping tokenizers/punkt.zip.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"True"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"import nltk\n",
|
||||||
|
"nltk.download('punkt')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = [word_tokenize(content) for content in x_train]\n",
|
||||||
|
"x_dev = [word_tokenize(content) for content in x_dev]\n",
|
||||||
|
"x_test = [word_tokenize(content) for content in x_test]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"word2vec = gensim.downloader.load(\"word2vec-google-news-300\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def document_vector(doc):\n",
|
||||||
|
" \"\"\"Create document vectors by averaging word vectors. Remove out-of-vocabulary words.\"\"\"\n",
|
||||||
|
" return np.mean([word2vec[w] for w in doc if w in word2vec] or [np.zeros(300)], axis=0)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x_train = [document_vector(doc) for doc in x_train]\n",
|
||||||
|
"x_dev = [document_vector(doc) for doc in x_dev]\n",
|
||||||
|
"x_test = [document_vector(doc) for doc in x_test]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class NeuralNetwork(torch.nn.Module): \n",
|
||||||
|
" def __init__(self, hidden_size):\n",
|
||||||
|
" super(NeuralNetwork, self).__init__()\n",
|
||||||
|
" self.l1 = torch.nn.Linear(300, hidden_size)\n",
|
||||||
|
" self.l2 = torch.nn.Linear(hidden_size, 1)\n",
|
||||||
|
"\n",
|
||||||
|
" def forward(self, x):\n",
|
||||||
|
" x = self.l1(x)\n",
|
||||||
|
" x = torch.relu(x)\n",
|
||||||
|
" x = self.l2(x)\n",
|
||||||
|
" x = torch.sigmoid(x)\n",
|
||||||
|
" return x"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"hidden_size = 600\n",
|
||||||
|
"epochs = 5\n",
|
||||||
|
"batch_size = 15\n",
|
||||||
|
"model = NeuralNetwork(hidden_size)\n",
|
||||||
|
"criterion = torch.nn.BCELoss()\n",
|
||||||
|
"optimizer = torch.optim.SGD(model.parameters(), lr=0.01)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"/home/tomasz/.local/lib/python3.8/site-packages/torch/autograd/__init__.py:130: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)\n",
|
||||||
|
" Variable._execution_engine.run_backward(\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for epoch in range(epochs):\n",
|
||||||
|
" 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)\n",
|
||||||
|
" y = y_train[i:i+batch_size]\n",
|
||||||
|
" y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1, 1)\n",
|
||||||
|
" \n",
|
||||||
|
" outputs = model(X.float())\n",
|
||||||
|
" loss = criterion(outputs, y)\n",
|
||||||
|
" \n",
|
||||||
|
" optimizer.zero_grad()\n",
|
||||||
|
" loss.backward()\n",
|
||||||
|
" optimizer.step()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y_dev = []\n",
|
||||||
|
"y_test = []"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"NeuralNetwork(\n",
|
||||||
|
" (l1): Linear(in_features=300, out_features=600, bias=True)\n",
|
||||||
|
" (l2): Linear(in_features=600, out_features=1, bias=True)\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 20,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"model.eval()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with torch.no_grad():\n",
|
||||||
|
" for i in range(0, len(x_dev), batch_size):\n",
|
||||||
|
" X = x_dev[i:i+batch_size]\n",
|
||||||
|
" X = torch.tensor(X)\n",
|
||||||
|
" outputs = model(X.float()) \n",
|
||||||
|
" prediction = (outputs > 0.5)\n",
|
||||||
|
" y_dev.extend(prediction)\n",
|
||||||
|
"\n",
|
||||||
|
" for i in range(0, len(x_test), batch_size):\n",
|
||||||
|
" X = x_test[i:i+batch_size]\n",
|
||||||
|
" X = torch.tensor(X)\n",
|
||||||
|
" outputs = model(X.float())\n",
|
||||||
|
" y = (outputs > 0.5)\n",
|
||||||
|
" y_test.extend(prediction)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 22,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"y_dev = np.asarray(y_dev, dtype=np.int32)\n",
|
||||||
|
"y_test = np.asarray(y_test, dtype=np.int32)\n",
|
||||||
|
"\n",
|
||||||
|
"y_dev = pd.DataFrame({'label':y_dev})\n",
|
||||||
|
"y_test = pd.DataFrame({'label':y_test})\n",
|
||||||
|
"\n",
|
||||||
|
"y_dev.to_csv(r'dev-0/out.tsv', sep='\\t', index=False, header=False)\n",
|
||||||
|
"y_test.to_csv(r'test-A/out.tsv', sep='\\t', index=False, header=False)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"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.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
95
nural_network.py
Executable file
95
nural_network.py
Executable file
@ -0,0 +1,95 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
from nltk.tokenize import word_tokenize
|
||||||
|
import gensim.downloader
|
||||||
|
|
||||||
|
|
||||||
|
x_train = pd.read_table('train/in.tsv', sep='\t', error_bad_lines=False, quoting=3, header=None, names=['content', 'id'], usecols=['content'])
|
||||||
|
y_train = pd.read_table('train/expected.tsv', sep='\t', error_bad_lines=False, quoting=3, header=None, names=['label'])
|
||||||
|
x_dev = pd.read_table('dev-0/in.tsv', sep='\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])
|
||||||
|
x_test = pd.read_table('test-A/in.tsv', sep='\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])
|
||||||
|
|
||||||
|
x_train = x_train.content.str.lower()
|
||||||
|
x_dev = x_dev.content.str.lower()
|
||||||
|
x_test = x_test.content.str.lower()
|
||||||
|
|
||||||
|
x_train = [word_tokenize(content) for content in x_train]
|
||||||
|
x_dev = [word_tokenize(content) for content in x_dev]
|
||||||
|
x_test = [word_tokenize(content) for content in x_test]
|
||||||
|
|
||||||
|
word2vec = gensim.downloader.load("word2vec-google-news-300")
|
||||||
|
|
||||||
|
def document_vector(doc):
|
||||||
|
"""Create document vectors by averaging word vectors. Remove out-of-vocabulary words."""
|
||||||
|
return np.mean([word2vec[w] for w in doc if w in word2vec] or [np.zeros(300)], axis=0)
|
||||||
|
|
||||||
|
x_train = [document_vector(doc) for doc in x_train]
|
||||||
|
x_dev = [document_vector(doc) for doc in x_dev]
|
||||||
|
x_test = [document_vector(doc) for doc in x_test]
|
||||||
|
|
||||||
|
class NeuralNetwork(torch.nn.Module):
|
||||||
|
def __init__(self, hidden_size):
|
||||||
|
super(NeuralNetwork, self).__init__()
|
||||||
|
self.l1 = torch.nn.Linear(300, hidden_size)
|
||||||
|
self.l2 = torch.nn.Linear(hidden_size, 1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.l1(x)
|
||||||
|
x = torch.relu(x)
|
||||||
|
x = self.l2(x)
|
||||||
|
x = torch.sigmoid(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
hidden_size = 600
|
||||||
|
epochs = 5
|
||||||
|
batch_size = 15
|
||||||
|
model = NeuralNetwork(hidden_size)
|
||||||
|
criterion = torch.nn.BCELoss()
|
||||||
|
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
||||||
|
|
||||||
|
for epoch in range(epochs):
|
||||||
|
model.train()
|
||||||
|
for i in range(0, y_train.shape[0], batch_size):
|
||||||
|
X = x_train[i:i+batch_size]
|
||||||
|
X = torch.tensor(X)
|
||||||
|
y = y_train[i:i+batch_size]
|
||||||
|
y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1, 1)
|
||||||
|
|
||||||
|
outputs = model(X.float())
|
||||||
|
loss = criterion(outputs, y)
|
||||||
|
|
||||||
|
optimizer.zero_grad()
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
y_dev = []
|
||||||
|
y_test = []
|
||||||
|
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
for i in range(0, len(x_dev), batch_size):
|
||||||
|
X = x_dev[i:i+batch_size]
|
||||||
|
X = torch.tensor(X)
|
||||||
|
outputs = model(X.float())
|
||||||
|
prediction = (outputs > 0.5)
|
||||||
|
y_dev.extend(prediction)
|
||||||
|
|
||||||
|
for i in range(0, len(x_test), batch_size):
|
||||||
|
X = x_test[i:i+batch_size]
|
||||||
|
X = torch.tensor(X)
|
||||||
|
outputs = model(X.float())
|
||||||
|
y = (outputs > 0.5)
|
||||||
|
y_test.extend(prediction)
|
||||||
|
|
||||||
|
|
||||||
|
y_dev = np.asarray(y_dev, dtype=np.int32)
|
||||||
|
y_test = np.asarray(y_test, dtype=np.int32)
|
||||||
|
|
||||||
|
y_dev = pd.DataFrame({'label':y_dev})
|
||||||
|
y_test = pd.DataFrame({'label':y_test})
|
||||||
|
|
||||||
|
y_dev.to_csv(r'dev-0/out.tsv', sep='\t', index=False, header=False)
|
||||||
|
y_test.to_csv(r'test-A/out.tsv', sep='\t', index=False, header=False)
|
5152
test-A/in.tsv
Normal file
5152
test-A/in.tsv
Normal file
File diff suppressed because one or more lines are too long
BIN
test-A/in.tsv.xz
BIN
test-A/in.tsv.xz
Binary file not shown.
2408
test-A/out.tsv
Normal file
2408
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
289579
train/in.tsv
Normal file
289579
train/in.tsv
Normal file
File diff suppressed because one or more lines are too long
BIN
train/in.tsv.xz
BIN
train/in.tsv.xz
Binary file not shown.
14
wyniki.txt
Normal file
14
wyniki.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
Bayes:
|
||||||
|
Likelihood 0.0000
|
||||||
|
Accuracy 0.7367
|
||||||
|
F1.0 0.4367
|
||||||
|
Precision 0.8997
|
||||||
|
Recall 0.2883
|
||||||
|
|
||||||
|
Logistic Regression:
|
||||||
|
Likelihood 0.0000
|
||||||
|
Accuracy 0.7523
|
||||||
|
F1.0 0.6143
|
||||||
|
Precision 0.6842
|
||||||
|
Recall 0.5573
|
||||||
|
|
Loading…
Reference in New Issue
Block a user