Пример #1
0
import matplotlib.pyplot as plt
from joelnet.train import train
from joelnet.nn import NeuralNet
from joelnet.layers import Linear, Tanh, Sigmoid, reLu
from joelnet.data import BatchIterator
from joelnet.optim import SGD, RMSProp, SGD_Nesterov, Adam
from joelnet.loss import MSE, Log_loss
import random

inputs = np.array([[1], [2], [3], [4], [5]])

targets = np.array([[1], [4], [9], [16], [25]])

net = NeuralNet([
    Linear(input_size=1, output_size=2),
    reLu(),
    Linear(input_size=2, output_size=1)
])

n_epochs = 1
#loss_list = train(net, inputs,targets, optimizer = Adam(lr = 1e-2, gamma1 = 0.3, gamma2 = 0.3),iterator = BatchIterator(batch_size = 5), num_epochs = 1000)
loss_list = train(net,
                  inputs,
                  targets,
                  loss=MSE(),
                  optimizer=SGD(lr=1e-3),
                  iterator=BatchIterator(batch_size=5),
                  num_epochs=n_epochs)
for x, y in zip(inputs, targets):
    predicted = net.forward(x)
    print(x, predicted, y)
	inputs.append([i])
inputs = np.array(inputs)

# targets = np.array([
#     [1],
#     [4],
#     [9],
#     [16],
#     [25]
# ])
targets = inputs**2


net = NeuralNet([
    Linear(input_size=1, output_size=2, weights = np.array([[1.0,2.0]]), biases = np.array([0.0, 0.0])),
    reLu(),
    Linear(input_size=2, output_size=1, weights = np.array([[3.0],[4.0]]), biases = np.array([0.0])),
    reLu()
])



n_epochs = 1000

#loss_list = train(net, inputs,targets, optimizer = Adam(lr = 1e-2, gamma1 = 0.3, gamma2 = 0.3),iterator = BatchIterator(batch_size = 5), num_epochs = 1000)
start_time = time.time()
loss_list = train(net, inputs,targets, loss = MSE() ,optimizer = SGD(1e-5), iterator = BatchIterator(batch_size =  5), num_epochs = n_epochs, eps = 2000)
end_time = time.time()
print(f'Tempo gasto no treinamento: {end_time - start_time}s')

targets = inputs**2

plt.title("Regressão de x² - LM com busca linear")

plt.axis([0, 20, 0, 300])
aux = np.arange(21)
plt.scatter(aux, aux**2, s=30, c="red")

#=========== ALPHA 1E3 ===============

net = NeuralNet([
    Linear(input_size=1,
           output_size=2,
           weights=np.array([[1.0, 2.0]]),
           biases=np.array([0.0, 0.0])),
    reLu(),
    Linear(input_size=2,
           output_size=1,
           weights=np.array([[3.0], [4.0]]),
           biases=np.array([0.0])),
    reLu()
])

n_epochs = 500
print('========= Método LM com busca linear =========')
print('treinando com 500 epochs')
print(f'alpha: {1e3:.1E}')
#loss_list = train(net, inputs,targets, optimizer = Adam(lr = 1e-2, gamma1 = 0.3, gamma2 = 0.3),iterator = BatchIterator(batch_size = 5), num_epochs = 1000)
start_time = time.time()
loss_list = train(net,
                  inputs,