import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class sentimentAnalyser(nn.Module) :
def __init__(self, vocab_dim, embed_dim, num_filters, FFN_units, dropout_rate, training, batch_size) :
super(sentimentAnalyser, self).__init__()
self.sentence_len = 73
self.embed_dim = embed_dim
self.batch_size = batch_size
self.embedding = nn.Embedding(vocab_dim, embed_dim)
self.bigram = nn.Conv2d(in_channels=1,
out_channels=num_filters,
kernel_size=(2, embed_dim))
self.trigram = nn.Conv2d(in_channels=1,
out_channels=num_filters,
kernel_size=(3, embed_dim))
self.fourgram = nn.Conv2d(in_channels=1,
out_channels=num_filters,
kernel_size=(4, embed_dim))
self.maxpool = lambda x : torch.max(x, dim=-1).values
self.dense1 = nn.Linear(in_features=num_filters*3,
out_features=FFN_units)
self.dropout = nn.Dropout(p=dropout_rate)
self.dense2 = nn.Linear(in_features=FFN_units,
out_features=2)
def forward(self, x, training) :
x = self.embedding(x).view((self.batch_size, 1, self.sentence_len, self.embed_dim))
x1 = torch.squeeze(self.bigram(x))
x2 = torch.squeeze(self.trigram(x))
x3 = torch.squeeze(self.fourgram(x))
x1_maxpool = self.maxpool(x1)
x2_maxpool = self.maxpool(x2)
x3_maxpool = self.maxpool(x3)
concat_x = torch.cat((x1_maxpool, x2_maxpool, x3_maxpool), dim=-1)
output = self.dense1(concat_x)
output = F.relu(output)
if training : output = self.dropout(output)
output = F.softmax(self.dense2(output)).type(torch.float32)
return output
x = torch.from_numpy(train_data).type(torch.long)
y = torch.from_numpy(train_labels).type(torch.long)
bs = 64 # batch size is 64
EPOCH = 5
model = sentimentAnalyser(vocab_dim=VOCAB_SIZE,
embed_dim=EMB_DIM,
num_filters=NUM_FILTERS,
FFN_units=FFN_UNITS,
dropout_rate=0.2,
training=True,
batch_size=bs)
loss = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
for epoch in range(EPOCH) :
i = 0
while i < x.shape[0] :
inp = x[i : min(i + bs, x.shape[0])]
out = y[i : min(i + bs, x.shape[0])]
i = i + bs
pred_out = model.forward(inp, training=True)
pred_out = torch.max(pred_out, dim=1).indices
l = loss(pred_out, out)
l.backward()
optimizer.step()
optimizer.zero_grad()
print(f'Epoch : {epoch}, Loss is {l}')
При выполнении этого кода я получаю сообщение об ошибке: _thnn_binary_cross_entropy_forward не поддерживается в CPUType для Long
Кажется, что BCELoss не определен для тензоров типа torch.long, но с другой стороны, nn.Слой вложения определен только для тензоров torch.long. Я попытался передать входной тензор как долго в nn.embedding, а затем преобразовал его в torch.float32 и другие типы, но, тем не менее, он не работает. Пожалуйста, помогите !!