Test
This commit is contained in:
parent
d735a85b01
commit
62f9576eb8
155
Untitled.ipynb
Normal file
155
Untitled.ipynb
Normal file
@ -0,0 +1,155 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def getInput(path):\n",
|
||||
" with open(path,encoding='utf-8') as f:\n",
|
||||
" return f.readlines()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import gensim.downloader as gensim\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import torch\n",
|
||||
"from nltk.tokenize import word_tokenize"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"word2vec = gensim.load('word2vec-google-news-300')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# train_in=getInput('./train/in.tsv')\n",
|
||||
"# train_expected=getInput('./train/expected.tsv')\n",
|
||||
"# test_in=getInput('./test-A/in.tsv')\n",
|
||||
"# dev_in=getInput('./dev-0/in.tsv')\n",
|
||||
"# dev_expected=getInput('./dev-0/expected.tsv')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class NeuralNetworkModel(torch.nn.Module):\n",
|
||||
" def __init__(self):\n",
|
||||
" super(NeuralNetworkModel, self).__init__()\n",
|
||||
" self.l01 = torch.nn.Linear(300, 300)\n",
|
||||
" self.l02 = torch.nn.Linear(300, 1)\n",
|
||||
"\n",
|
||||
" def forward(self, x):\n",
|
||||
" x = self.l01(x)\n",
|
||||
" x = torch.relu(x)\n",
|
||||
" x = self.l02(x)\n",
|
||||
" x = torch.sigmoid(x)\n",
|
||||
" return x\n",
|
||||
"\n",
|
||||
"def d2v(doc):\n",
|
||||
" return np.mean([word2vec[word] for word in doc if word in word2vec] or [np.zeros(300)], axis=0)\n",
|
||||
"x_train = pd.read_table('train/in.tsv.xz', compression='xz', sep='\\t', header=None, error_bad_lines=False, quoting=3)\n",
|
||||
"x_train = x_train[0].str.lower()\n",
|
||||
"x_dev = pd.read_table('dev-0/in.tsv.xz', compression='xz', sep='\\t', header=None, quoting=3)\n",
|
||||
"x_dev = x_dev[0].str.lower()\n",
|
||||
"x_test = pd.read_table('test-A/in.tsv.xz', compression='xz', sep='\\t', header=None, quoting=3)\n",
|
||||
"x_test = x_test[0].str.lower()\n",
|
||||
"y_train = pd.read_table('train/expected.tsv', sep='\\t', header=None, quoting=3)\n",
|
||||
"y_train = y_train[0]\n",
|
||||
"x_train = [word_tokenize(x) for x in x_train]\n",
|
||||
"x_dev = [word_tokenize(x) for x in x_dev]\n",
|
||||
"x_test = [word_tokenize(x) for x in x_test]\n",
|
||||
"x_train = [d2v(doc) for doc in x_train]\n",
|
||||
"x_dev = [d2v(doc) for doc in x_dev]\n",
|
||||
"x_test = [d2v(doc) for doc in x_test]\n",
|
||||
"model = NeuralNetworkModel()\n",
|
||||
"BATCH_SIZE = 10\n",
|
||||
"criterion = torch.nn.BCELoss()\n",
|
||||
"optimizer = torch.optim.Adam(model.parameters())\n",
|
||||
"for epoch in range(BATCH_SIZE):\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",
|
||||
" optimizer.zero_grad()\n",
|
||||
" outputs = model(X.float())\n",
|
||||
" loss = criterion(outputs, y)\n",
|
||||
" loss.backward()\n",
|
||||
" optimizer.step()\n",
|
||||
"y_dev = []\n",
|
||||
"y_test = []\n",
|
||||
"model.eval()\n",
|
||||
"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",
|
||||
" y = (outputs > 0.5)\n",
|
||||
" y_dev.extend(y)\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(y)\n",
|
||||
"\n",
|
||||
"y_dev = np.asarray(y_dev, dtype=np.int32)\n",
|
||||
"Y_dev = pd.DataFrame({'label': y_dev})\n",
|
||||
"y_test = np.asarray(y_test, dtype=np.int32)\n",
|
||||
"Y_test = pd.DataFrame({'label': y_test})\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)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.9.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
116
Untitled.py
Normal file
116
Untitled.py
Normal file
@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
# In[2]:
|
||||
|
||||
|
||||
def getInput(path):
|
||||
with open(path,encoding='utf-8') as f:
|
||||
return f.readlines()
|
||||
|
||||
|
||||
# In[6]:
|
||||
|
||||
|
||||
import gensim.downloader as gensim
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import torch
|
||||
from nltk.tokenize import word_tokenize
|
||||
|
||||
|
||||
# In[8]:
|
||||
|
||||
|
||||
word2vec = gensim.load('word2vec-google-news-300')
|
||||
|
||||
|
||||
# In[5]:
|
||||
|
||||
|
||||
# train_in=getInput('./train/in.tsv')
|
||||
# train_expected=getInput('./train/expected.tsv')
|
||||
# test_in=getInput('./test-A/in.tsv')
|
||||
# dev_in=getInput('./dev-0/in.tsv')
|
||||
# dev_expected=getInput('./dev-0/expected.tsv')
|
||||
|
||||
|
||||
# In[14]:
|
||||
|
||||
|
||||
class NeuralNetworkModel(torch.nn.Module):
|
||||
def __init__(self):
|
||||
super(NeuralNetworkModel, self).__init__()
|
||||
self.l01 = torch.nn.Linear(300, 300)
|
||||
self.l02 = torch.nn.Linear(300, 1)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.l01(x)
|
||||
x = torch.relu(x)
|
||||
x = self.l02(x)
|
||||
x = torch.sigmoid(x)
|
||||
return x
|
||||
|
||||
def d2v(doc):
|
||||
return np.mean([word2vec[word] for word in doc if word in word2vec] or [np.zeros(300)], axis=0)
|
||||
x_train = pd.read_table('train/in.tsv.xz', compression='xz', sep='\t', header=None, error_bad_lines=False, quoting=3)
|
||||
x_train = x_train[0].str.lower()
|
||||
x_dev = pd.read_table('dev-0/in.tsv.xz', compression='xz', sep='\t', header=None, quoting=3)
|
||||
x_dev = x_dev[0].str.lower()
|
||||
x_test = pd.read_table('test-A/in.tsv.xz', compression='xz', sep='\t', header=None, quoting=3)
|
||||
x_test = x_test[0].str.lower()
|
||||
y_train = pd.read_table('train/expected.tsv', sep='\t', header=None, quoting=3)
|
||||
y_train = y_train[0]
|
||||
x_train = [word_tokenize(x) for x in x_train]
|
||||
x_dev = [word_tokenize(x) for x in x_dev]
|
||||
x_test = [word_tokenize(x) for x in x_test]
|
||||
x_train = [d2v(doc) for doc in x_train]
|
||||
x_dev = [d2v(doc) for doc in x_dev]
|
||||
x_test = [d2v(doc) for doc in x_test]
|
||||
model = NeuralNetworkModel()
|
||||
BATCH_SIZE = 10
|
||||
criterion = torch.nn.BCELoss()
|
||||
optimizer = torch.optim.Adam(model.parameters())
|
||||
for epoch in range(BATCH_SIZE):
|
||||
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)
|
||||
optimizer.zero_grad()
|
||||
outputs = model(X.float())
|
||||
loss = criterion(outputs, y)
|
||||
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())
|
||||
y = (outputs > 0.5)
|
||||
y_dev.extend(y)
|
||||
|
||||
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(y)
|
||||
|
||||
y_dev = np.asarray(y_dev, dtype=np.int32)
|
||||
Y_dev = pd.DataFrame({'label': y_dev})
|
||||
y_test = np.asarray(y_test, dtype=np.int32)
|
||||
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)
|
||||
|
||||
|
||||
# In[ ]:
|
||||
|
||||
|
||||
|
||||
|
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
1148
dev-0/out.tsv
1148
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
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
858
test-A/out.tsv
858
test-A/out.tsv
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
Loading…
Reference in New Issue
Block a user