from numpy.random import randn
def activation(x):
return 1/(1+torch.exp(-x))
dataiter = iter(trainloader)
images, labels = dataiter.next()
print("images.shape[0]=",images.shape[0])
inputs= torch.tensor(images.view(images.shape[0],-1))
print("inputs.size=",inputs.size())
w1 = randn(784,256)
b1 = randn(256)
w2 = randn(256,10)
b2 = randn(10)
inputs = torch.Tensor(inputs)
w1 = torch.Tensor(w1)
b1 = torch.Tensor(b1)
print("b1.size=",b1.size())
h=activation(torch.mm(inputs,w1)+b1)
print("h.size=",h.size())
w2 = torch.Tensor(w2)
print("w2.size=",w2.size())
h = torch.Tensor(h)
b2 = torch.Tensor(b2)
print("b2.size=",b2.size())
out = torch.mm(h,w2)+b2
print("out.size=",out.size())
结果
images.shape[0]= 64
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:7: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). import sys
inputs.size= torch.Size([64, 784]) b1.size= torch.Size([256]) h.size= torch.Size([64, 256]) w2.size= torch.Size([256, 10]) b2.size= torch.Size([10]) out.size= torch.Size([64, 10])