`import torch
import torchtext
from torchtext.data import TabularDataset,BucketIterator,Field
import pandas as pd
import numpy as np
df_train = pd.read_csv('V:\pythonproject\hii\train.csv')
df_test = pd.read_csv('V:\pythonproject\hii\test.csv')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tokenize = lambda x:x.split()
text = Field(tokenize=tokenize,lower=True,batch_first=True)
label = Field(sequential=False,use_vocab=False)
fields = {'text':('t',text),'label':('l',label)}
train_data,test_data = TabularDataset.splits(path='V:\pythonproject\hii\',train='train.csv',validation = 'test.csv',format='CSV',fields = fields)
text.build_vocab(train_data,max_size = 50000,min_freq=1)
train_iterator, test_iterator = BucketIterator.splits(
(train_data, test_data), batch_size=4, device=device,sort = False,shuffle=False
)
import torch.nn as nn
class RNN_LSTM(nn.Module):
def init(self, input_size, embed_size, hidden_size, num_layers):
super(RNN_LSTM, self).init()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.embedding = nn.Embedding(input_size, embed_size)
self.rnn = nn.LSTM(embed_size, hidden_size, num_layers,batch_first=True)
self.fc_out = nn.Linear(hidden_size, 1)
def forward(self, x):
# Set initial hidden and cell states
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
embedded = self.embedding(x)
outputs, _ = self.rnn(embedded, (h0, c0))
prediction = self.fc_out(outputs[ :,-1, :])
return prediction
#hyper parameters
input_size = len(text.vocab)
hidden_size = 64
num_layers = 1
embedding_size = 100
learning_rate = 0.005
num_epochs = 10
model = RNN_LSTM(input_size, embedding_size, hidden_size, num_layers).to(device)
Loss and optimizer
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
import time
start = time.time()
trn_loss = []
tst_loss = []
for epoch in range(num_epochs):
for batch_idx, batch in enumerate(train_iterator):
# Get data to cuda if possible
data = batch.t.to(device=device)
targets = batch.l.to(device=device)
# forward
scores = model(data)
loss = criterion(scores,targets.type(torch.FloatTensor).reshape(targets.shape[0],1).to(device))
# backward
optimizer.zero_grad()
loss.backward()
# gradient descent
optimizer.step()
trn_loss.append(loss)
print(f'training epoch:{epoch}--loss:{loss}')
with torch.no_grad():
for batch_idx, batch in enumerate(test_iterator):
# Get data to cuda if possible
data = batch.t.to(device=device)
targets = batch.l.to(device=device)
# forward
scores = model(data)
loss = criterion(scores,targets.type(torch.FloatTensor).reshape(targets.shape[0],1).to(device))
tst_loss.append(loss)
print(f'test epoch:{epoch}--loss:{loss}')
import matplotlib.pyplot as plt
plt.plot(trn_loss,c = 'r',label='train_loss')
plt.plot(tst_loss,c = 'b',label = 'test_loss')
plt.legend()
torch.save(model.state_dict(), 'fake_news.pt')
y_pred = []
with torch.no_grad():
for batch_idx, batch in enumerate(test_iterator):
# Get data to cuda if possible
data = batch.t.to(device=device)
targets = batch.l.to(device=device)
# forward
scores = model(data)
y_pred.extend(scores)
y_pred_np = []
for i in y_pred:
y_pred_np.append(np.array([i.to('cpu')]))
y_pred = np.array(y_pred_np)
pred = []
for i in y_pred:
if i <= 0.5:
pred.append(0)
else :
pred.append(1)
y_true = np.array(df_test['label'])
from sklearn.metrics import confusion_matrix,accuracy_score
cm = confusion_matrix(y_true, pred)
acc = accuracy_score(y_true, pred)
`
error is :
`v:\pythonproject\venv\lib\site-packages\torch\nn\modules\rnn.py in forward(self, input, hx)
580 if batch_sizes is None:
581 result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
--> 582 self.dropout, self.training, self.bidirectional, self.batch_first)
583 else:
584 result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias,
RuntimeError: stack expects a non-empty TensorList`