Hi,
Apologies if my model itself is downright trash, please correct in that case. I am just learning pytorch.
So, I am trying to do sentiment analysis on imdb dataset. 1-gram, sending the word index to the embedding-bag, then single fc layer.
class ffn(nn.Module):
def __init__(self):
super().__init__()
self.embedBag = nn.EmbeddingBag(1000, 20,sparse=True)
self.fc = nn.Linear(20, 1)
def forward(self, x):
x = self.embedBag(x)
y = self.fc(x)
return t.sigmoid(y)
mod = ffn()
#using batch_size 1 so that no padding is required
imdb_data = t.utils.data.DataLoader(imdb_ds(train_x, train_y),batch_size=1)
optim = t.optim.Adagrad(mod.parameters(), lr=0.001 )
loss_ls = []
for i,(x,y) in enumerate(imdb_data):
optim.zero_grad()
ybar = mod(x)
loss = F.binary_cross_entropy(ybar,t.tensor(y,dtype=t.float32))
loss_ls.append(loss.item())
loss.backward()
optim.step()
if i%5000==0: print(loss)
Is there a problem with loss? Is sigmoid giving problem? My loss is just stuck around 0.7, randomly fluctuating.
It’s usually better to remove the sigmoid and use nn.BCEWithLogitsLoss
or F.binary_cross_entropy_with_logits
.
However, your model might be too small, so I would recommend to add a relu and another linear layer to it.
Let me know, if that helps. 
1 Like
Thanks for your suggestion. It really helped 
2 questions, if you don’t mind:
- I used binary cross with logits. But then when I wrote my function to find accuracy, I had to take sigmoid of the output of the model. So, isn’t it better to directly put sigmoid or softmax in model itself? When does the reverse become more useful?
- Why have functions like sigmoid, tanh have been moved from nn.functional to torch.sigmoid, torch.tanh? Them being inside nn.functional makes kinda more sense intuitively, that functions are inside functional. Pretty sure team pytorch has better reasons than mine
As the Sigmoid/Relu are still inside torch.nn, I feel it is a bit confusing.
I feel they should be moved to torch.math, and these sigmoid/relu should be moved to torch.math.functional.
I wouldn’t create a new math
namespace, as this would mean that also matmul
, dot
, sum
etc. should be moved there, wouldn’t it?
Feel free to add your suggestions to the linked GitHub issue to discuss it further. 
1 Like