I want to visualize the reult after each convolution layer and full connected layer?
Can anybody show me how to achieve this?please help me…
For this example :
#coding=utf8
import torch
from torch import nn
import torch.nn.functional as F
from torch import optim
from numpy import pi, sin, linspace
import numpy as np
import torch.utils.data as Data
#1
torch.manual_seed(1)
learning_rate=1e-2
num_epoches=10
#2
t = torch.linspace(0, 1, 1024*256)
modes = torch.sin(2 * pi * 5 * t)
label=torch.ones(256).long()
modes.requires_grad_(True)
X=modes.view(256,1,1024)
#3
class CNN_1D(nn.Module):
def init(self,in_dim,n_class):
super(CNN_1D,self).init()
self.conv1=nn.Sequential(
nn.Conv1d(in_dim,512,3,padding=3),
nn.LeakyReLU(),
nn.MaxPool1d(2,2),
nn.Conv1d(512,128,3,padding=3),
nn.LeakyReLU(),
nn.MaxPool1d(2,2)
)
self.fc=nn.Sequential(
nn.Linear(128*259,640),
nn.LeakyReLU(),
nn.Linear(640,160),
nn.Dropout(),
nn.Linear(160,n_class)
)
def forward(self,x):
out =self.conv1(x)
out =out.view(x.size(0),-1)
out =self.fc(out)
return out
net =CNN_1D(1,2)
print(net)
criterion =nn.CrossEntropyLoss()
optimzer =optim.Adam(net.parameters())
#4
for epoch in range(num_epoches):
out =net(X)
loss =criterion(out,label)
_,pred=torch.max(out,1)
num_correct = (pred==label).sum().item()
running_acc = num_correct/X.size(0)
optimzer.zero_grad()
loss.backward()
optimzer.step()
print(“Trian:{} epoch ;Loss:{:.6%} ; Acc:{:.6%}”.format(epoch+1,loss.item(),running_acc))
print("\n")