Softmax returning only 0 and 1

jdeawick: <p>I’m running Conv1d on a time series with 23 annual (2001 - 2023) observations.  Target is 6 defined classes. Softmax is the last step in the model.  All trials only 0 or 1, no matter the activation function.  I’ve tried ReLU, ELU(1), LeakyReLU, PReLU, and Tanh</p>
<h1><a name="p-464285-model-1" class="anchor" href="#p-464285-model-1"></a>Model</h1>
<p>class CNN1(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.layer1=nn.Sequential(<br>
nn.Conv1d(1,10,3,1,1,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(10))<br>
self.layer2=nn.Sequential(<br>
nn.Conv1d(10,12,5,1,2,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(12))<br>
self.layer3=nn.Sequential(<br>
nn.Conv1d(12,14,7,1,3,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(14))<br>
self.layer4=nn.Sequential(<br>
nn.Conv1d(14,16,9,1,4,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(16))<br>
self.layer5=nn.Sequential(<br>
nn.Conv1d(16,18,11,1,5,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(18))<br>
self.layer6=nn.Sequential(<br>
nn.Conv1d(18,20,13,1,6,bias=False),<br>
nn.PReLU(),<br>
nn.BatchNorm1d(20),<br>
nn.Flatten())<br>
self.layer7=nn.Sequential(<br>
nn.Linear(460,50,True),<br>
# nn.ReLU(),<br>
nn.Linear(50,6,True),<br>
nn.Softmax(dim=1))<br>
def forward(self, x):<br>
x=self.layer1(x)<br>
x=self.layer2(x)<br>
x=self.layer3(x)<br>
x=self.layer4(x)<br>
x=self.layer5(x)<br>
x=self.layer6(x)<br>
x=self.layer7(x)<br>
return x</p>
<p>m=CNN1().to(‘cuda:0’)<br>
def weight_init(m):<br>
if isinstance(nn.Conv1d,CNN1) or isinstance(nn.Linear,CNN1):<br>
torch.nn.init.xavier_normal_(m.weight)</p>
<p>weight_init(m)</p>
<p><span class="hashtag-raw">#print</span> model details<br>
def param_count(m):<br>
for p in m.parameters():<br>
count = sum(p.numel() for p in m.parameters() if p.requires_grad)<br>
return count</p>
<p>print(summary(m,(1,23)))</p>
<h1><a name="p-464285-model-parameters-2" class="anchor" href="#p-464285-model-parameters-2"></a>model parameters</h1>
<p>epochs = 10<br>
lossL1 = nn.CrossEntropyLoss()<br>
optimizer=torch.optim.AdamW(m.parameters(), lr=0.001, weight_decay=0.0002)</p>
<p><span class="hashtag-raw">#implementation</span><br>
print(time.strftime(‘%<a class="hashtag-cooked" href="/c/c/11" data-type="category" data-slug="c" data-id="11"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>C++</span></a>’,time.localtime()))<br>
m.train()<br>
for epoch in range(epochs):<br>
running_loss=0<br>
preds=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for i, (X, y) in enumerate(train_loader):<br>
yhat=m(X)<br>
preds.append([yhat])<br>
loss = lossL1(yhat,y)<br>
running_loss += loss.item()<br>
epoch_loss = running_loss / len(train_loader)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()<br>
print(f’{epoch}, {epoch_loss: .4f}')</p>
<p>print(time.strftime(‘%<a class="hashtag-cooked" href="/c/c/11" data-type="category" data-slug="c" data-id="11"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>C++</span></a>’,time.localtime()))</p>

ptrblck: <p><code>nn.CrossEntropyLoss()</code> expects raw logits so remove the <code>nn.Softmax</code> from your model.</p>