matplotlib中绘制动态图像(实时打印)

使用plt.ion()

 实时打印拟合过程:

import numpy as np
import torch
from torch.autograd import Variable
import torch.nn.functional
import matplotlib.pyplot as plt

x=torch.unsqueeze(torch.linspace(-1,1,100),dim=1)

y=x.pow(2)+0.2*torch.rand(x.size())

x,y=Variable(x),Variable(y)
plt.scatter(x.numpy(),y.numpy())
plt.show()

class Net(torch.nn.Module):
    def __init__(self,n_feature,n_hidden,n_output):
        super(Net,self).__init__()
        self.hidden=torch.nn.Linear(n_feature,n_hidden)
        self.predict=torch.nn.Linear(n_hidden,n_output)


    def forward(self,x):
        x=torch.relu(self.hidden(x))
        x=self.predict(x)
        return x

net=Net(1,10,1)
print(net)
plt.show()

#实时打印
plt.ion()

optimizer=torch.optim.SGD(net.parameters(),lr=0.1)
loss_func=torch.nn.MSELoss()

for t in range(100):
    predictioin=net(x)

    loss=loss_func(predictioin,y)

    optimizer.zero_grad()
    loss.backward()

    optimizer.step()

    if t%5==0:
        plt.cla()
        plt.scatter(x.data.numpy(),y.data.numpy())
        plt.plot(x.data.numpy(),predictioin.data.numpy(),'r-',lw=5)
        plt.text(0.5,0,'Loss=%.4f' % loss.data.numpy(),fontdict={'size':20,'color':'red'})
        plt.pause(0.1)

 

 matplotlib中绘制动态图像(实时打印)_第1张图片

你可能感兴趣的:(Python语法注意点)