0D or 1D target tensor expected, multi-target not supported

username: li2cw
cooked: <h1><a name="in27-1" class="anchor" href="#in27-1"></a>In[27]:</h1>
<p>filename_train_images = r’D:\vision\mnist\train-images.idx3-ubyte’<br>
filename_train_labels = r’D:\vision\mnist\train-labels.idx1-ubyte’<br>
filename_test_images = r’D:\vision\mnist\t10k-images.idx3-ubyte’<br>
filename_test_labels = r’D:\vision\mnist\t10k-labels.idx1-ubyte’<br>
train_images=load_images(filename_train_images)<br>
train_labels=load_labels(filename_train_labels)<br>
test_images=load_images(filename_test_images)<br>
test_labels=load_labels(filename_test_labels)</p>
<h1><a name="in28-2" class="anchor" href="#in28-2"></a>In[28]:</h1>
<p>fig=plt.figure(figsize=(8,8))<br>
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)<br>
for i in range(30):<br>
images = np.reshape(train_images[i], [28,28])<br>
ax=fig.add_subplot(6,5,i+1,xticks=<span class="chcklst-box fa fa-square-o fa-fw"></span>,yticks=<span class="chcklst-box fa fa-square-o fa-fw"></span>)<br>
ax.imshow(images,cmap=plt.cm.binary,interpolation=‘nearest’)<br>
ax.text(0,7,str(train_labels[i]))<br>
plt.show()</p>
<h1><a name="in44-3" class="anchor" href="#in44-3"></a>In[44]:</h1>
<p>x_train=train_images<br>
y_train=train_labels<br>
x_valid=test_images<br>
y_valid=test_labels</p>
<h1><a name="in45-4" class="anchor" href="#in45-4"></a>In[45]:</h1>
<p>print(x_train.shape)</p>
<h1><a name="in46-5" class="anchor" href="#in46-5"></a>In[46]:</h1>
<p>from matplotlib import pyplot<br>
pyplot.imshow(x_train[0].reshape((28,28)),cmap=‘gray’)</p>
<h1><a name="in93-6" class="anchor" href="#in93-6"></a>In[93]:</h1>
<p>import torch<br>
x_train,y_train,x_valid,y_valid=map(torch.tensor,(x_train,y_train,x_valid,y_valid))<br>
n,c =x_train.shape<br>
x_train,x_train.shape,y_train.min(),y_train.max()<br>
x_train=torch.tensor(x_train,dtype=torch.float)<br>
y_train=y_train.long()<br>
x_valid=torch.tensor(x_valid,dtype=torch.float)<br>
y_valid=y_valid.long()<br>
print(x_train,y_train)<br>
print(‘x_train’,x_train.shape)<br>
print(y_train.min(),y_train.max())<br>
print(type(y_valid.dtype))</p>
<h1><a name="function-method-7" class="anchor" href="#function-method-7"></a>function method</h1>
<h1><a name="nnmodule-8" class="anchor" href="#nnmodule-8"></a>有待学习的参数–nn.module</h1>
<h1><a name="nnfunction-9" class="anchor" href="#nnfunction-9"></a>其他情况–nn.function</h1>
<h1><a name="in74-10" class="anchor" href="#in74-10"></a>In[74]:</h1>
<p>import torch.nn.functional as F<br>
loss_func=F.cross_entropy<br>
def model(xb):</p>
<pre><code>return xb.mm(weights)+bias
</code></pre>
<h1><a name="in90-11" class="anchor" href="#in90-11"></a>In[90]:</h1>
<p>bs=64<br>
xb=x_train[0:bs]<br>
yb=y_train[0:bs]<br>
weights=torch.randn([784,10],dtype=torch.float,requires_grad=True)<br>
bias=torch.zeros(10,requires_grad=True)<br>
print(loss_func(model(xb),yb))</p>
<h1><a name="in80-12" class="anchor" href="#in80-12"></a>In[80]:</h1>
<p>from torch import nn<br>
class Mnist_NN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.hidden1=nn.Linear(784,128)<br>
self.hidden2=nn.Linear(128,256)<br>
self.out=nn.Linear(256,10)<br>
def forward(self,x):<br>
x=F.relu(self.hidden1(x))<br>
x=F.relu(self.hidden2(x))<br>
x=self.out(x)<br>
return x</p>
<h1><a name="in81-13" class="anchor" href="#in81-13"></a>In[81]:</h1>
<p>net=Mnist_NN()<br>
print(net)</p>
<h1><a name="in82-14" class="anchor" href="#in82-14"></a>In[82]:</h1>
<p>for name,parameter in net.named_parameters():<br>
print(name,parameter,parameter.size())</p>
<h1><a name="in83-15" class="anchor" href="#in83-15"></a>In[83]:</h1>
<p><span class="hashtag-raw">#取数据</span><br>
from torch.utils.data import TensorDataset<br>
from torch.utils.data import DataLoader<br>
train_ds=TensorDataset(x_train,y_train)<br>
train_dl=DataLoader(train_ds,batch_size=bs,shuffle=True)<br>
valid_ds=TensorDataset(x_valid,y_valid)<br>
valid_dl=DataLoader(valid_ds,batch_size=bs*2)</p>
<h1><a name="in84-16" class="anchor" href="#in84-16"></a>In[84]:</h1>
<p>def get_data(train_ds,valid_ds,bs):<br>
return(DataLoader(train_ds,batch_size=bs,shuffle=True),<br>
DataLoader(valid_ds,batch_size=bs * 2)<br>
)</p>
<h1><a name="in85-17" class="anchor" href="#in85-17"></a>In[85]:</h1>
<p>import numpy as np<br>
def fit(steps,model,loss_func,opt,train_dl,valid_dl):<br>
for step in range(steps):<br>
model.train()<br>
for xb,yb in train_dl:<br>
loss_batch(model,loss_func,xb,yb,opt)<br>
model.eval()<br>
with torch.no_grad():<br>
losses,nums=zip(*[loss_batch(model,loss_func,xb,yb) for xb,yb in valid_dl])<br>
val_loss=np.sum(np.multiply(losses,nums))/np.sum(nums)<br>
print(‘当前step：’+str(step),‘当前损失：’+str(val_loss))</p>
<h1><a name="in86-18" class="anchor" href="#in86-18"></a>In[86]:</h1>
<p>from torch import optim<br>
def get_model():<br>
model=Mnist_NN()<br>
return model,optim.SGD(model.parameters(),lr=0.001)</p>
<h1><a name="in87-19" class="anchor" href="#in87-19"></a>In[87]:</h1>
<p>def loss_batch(model,loss_func,xb,yb,opt=None):<br>
loss=loss_func(model(xb),yb.long())<br>
if opt is not None:<br>
loss.backward()<br>
opt.step()<br>
opt.zero_grad()<br>
return loss.item(),len(xb)</p>
<h1><a name="in88-20" class="anchor" href="#in88-20"></a>In[88]:</h1>
<p>train_dl,valid_dl=get_data(train_ds,valid_ds,bs)<br>
model,opt=get_model()<br>
fit(25,model,loss_func,opt,train_dl,valid_dl)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2de0faa8d87e1d9b589f2eb192935b91b6e0b714.png" data-download-href="https://discuss.pytorch.org/uploads/default/2de0faa8d87e1d9b589f2eb192935b91b6e0b714" title="1637120137(1)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2de0faa8d87e1d9b589f2eb192935b91b6e0b714.png" alt="1637120137(1)" data-base62-sha1="6xRv6JBuB2mOCOSuJjUaTGa7h3K" width="690" height="436" data-dominant-color="F6F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1637120137(1)</span><span class="informations">1167×739 18.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>Check the shape of the target tensor passed to <code>nn.CrossEntropyLoss</code> as it seems to contain an unnecessary dimension.<br>
For a multi-class classification using <code>nn.CrossEntropyLoss</code> the model output should have the shape <code>[batch_size, nb_classes]</code> containing the logits and the target should have the shape <code>[batch_size]</code> containing the class indices in the range <code>[0, nb_classes-]</code>.</p>