import os
import matplotlib.pyplot as plt
import numpy as np
import neurolab as nl
os.chdir(r'D:\projects\wordpress\ex60')
os.getcwd()
#create train sets
x=np.linspace(-10,10,60)
y=np.cos(x)*0.9
size=len(x)
x_train=x.reshape(size,1)
y_train=y.reshape(size,1)
#create network with 4 layers and randomly initiate 
d=[[1,1],[45,1],[45,45,1],[45,45,45,1]]
for i in range(4):
    net=nl.net.newff([[-10,10]],d[i])
    train_net=nl.train.train_gd(net,x_train,y_train,epochs=1000,show=100)
    outp=net.sim(x_train)
    plt.subplot(2,1,1)
    plt.grid(True)
    plt.plot(train_net)
    plt.title('Hidden Layers: ' + str(i))
    plt.xlabel('Epochs')
    plt.ylabel('quadratic error')
    x2=np.linspace(-10,10,150)
    y2=net.sim(x2.reshape(x2.size,1)).reshape(x2.size)
    y3=outp.reshape(size)
    plt.subplot(2,1,2)
    plt.grid(True)
    plt.plot(x2,y2,'-',x,y,'.',x,y3,'p')
    plt.legend(['y predicted','y actual'])
    plt.savefig("example60_" + str(i) + '.png', dpi=100)
    plt.show()
    plt.close()
    

example69_0

example69_1

example69_2

example69_3

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading