Exemplo n.º 1
0
           biases=np.array([0.0, 0.0])),
    Linear(input_size=2,
           output_size=1,
           weights=np.array([[3.0], [4.0]]),
           biases=np.array([0.0]))
])

n_epochs = 2
eps = 1e-4

#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, eval_list = train(net,
                             inputs,
                             targets,
                             loss=MSE(),
                             optimizer=LM_cond(float(sys.argv[1]),
                                               float(sys.argv[2])),
                             iterator=BatchIterator(batch_size=len(inputs)),
                             num_epochs=n_epochs,
                             eps=eps)

# for x, y in zip(inputs, targets)
#     predicted = net.forward(x)
#     print(x, predicted, y)

ex = np.arange(0, 5, 0.2)
ey = []
test_loss = []
for val in ex:
    predicted = net.forward([val])
    ey.append(predicted)

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')




# for x, y in zip(inputs, targets)
#     predicted = net.forward(x)
#     print(x, predicted, y)



#print(f'Levenberg Marquardt com busca linear\nloss = {loss_list[len(loss_list) - 1]:.2f}')

ex = np.linspace(0,20,200)
Exemplo n.º 3
0
# ])
net = NeuralNet([
    Linear(input_size=30, output_size=24),
    Tanh(),
    Linear(input_size=24, output_size=30),
    Tanh(),
    Linear(input_size=30, output_size=35),
    Tanh(),
    Linear(input_size=35, output_size=1),
    Sigmoid()
])

n_epochs = 200
loss_list = train(net,
                  inputs,
                  targets,
                  optimizer=Adam(lr=1e-2, gamma1=0.3, gamma2=0.4),
                  iterator=BatchIterator(128),
                  num_epochs=n_epochs)

y_pred = []
for x in X_test[0:1000]:
    y_pred.append(net.forward(x))
y_pred = np.array(y_pred)

aux = X_test[0:1000]
indices_1 = np.where(aux == 0)
print('fraudes:', indices_1[0])

plt.title("Erro quadrático x Tempo")
plt.xlabel("número de iterações")
plt.ylabel("erro quadrático")
Exemplo n.º 4
0
        return [0, 1, 0, 0]
    else:
        return [1, 0, 0, 0]


def binary_encode(x: int) -> List[int]:
    """
    10 digit binary encoding of x
    """
    return [x >> i & 1 for i in range(10)]


inputs = np.array([binary_encode(x) for x in range(101, 1024)])

targets = np.array([fizz_buzz_encode(x) for x in range(101, 1024)])

net = NeuralNet([
    Linear(input_size=10, output_size=50),
    Tanh(),
    Linear(input_size=50, output_size=4)
])

train(net, inputs, targets, num_epochs=5000, optimizer=SGD(lr=0.001))

for x in range(1, 101):
    predicted = net.forward(binary_encode(x))
    predicted_idx = np.argmax(predicted)
    actual_idx = np.argmax(fizz_buzz_encode(x))
    labels = [str(x), "fizz", "buzz", "fizzbuzz"]
    print(x, labels[predicted_idx], labels[actual_idx])
           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,
                  targets,
                  loss=MSE(),
                  optimizer=LM_cond(1e3),
                  iterator=BatchIterator(batch_size=5),
                  num_epochs=n_epochs)
end_time = time.time()
print(f'Tempo gasto no treinamento: {end_time - start_time}s')

ex = np.linspace(0, 20, 200)
ey = []
test_loss = []
for val in ex:
    predicted = net.forward([val])
    ey.append(predicted)

plt.plot(
    ex,
Exemplo n.º 6
0
"""
The canonical example of a function that can't
be learned with a simple linear model is XOR
"""

import numpy as np

from joelnet.train import train
from joelnet.nn import NeuralNet
from joelnet.layers import Linear, Tanh

inputs = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])

targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]])

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

train(net, inputs, targets)

for x, y in zip(inputs, targets):
    predicted = net.forward(x)
    print(x, predicted, y)
Exemplo n.º 7
0
           output_size=2,
           weights=np.random.randn(2, 2),
           biases=np.random.randn(2)),
    reLu(),
    Linear(input_size=2,
           output_size=1,
           weights=np.random.randn(2, 1),
           biases=np.random.randn(1))
])

start_time = time.time()
try:
    loss_list, eval_list = train(net,
                                 inputs,
                                 targets,
                                 loss=MSE(),
                                 optimizer=LM_cond(1e15),
                                 iterator=BatchIterator(batch_size=5),
                                 num_epochs=n_epochs,
                                 eps=eps)
except np.linalg.LinAlgError as err:
    print('Interrompido por matriz singular')
    end_time = time.time()
end_time = time.time()
time_spent = end_time - start_time
print(f'\nt: {time_spent}s')

ex = np.linspace(0, n_epochs, n_epochs)
print(f'lenex: {len(ex)}\n lenev: {len(eval_list)}')
plt.scatter(ex, abs(np.log(loss_list)))
plt.axis([0, n_epochs, 0, n_epochs])
plt.show()
])

# net = NeuralNet([
#     Linear(input_size=30, output_size=2),
#     Tanh(),
#     Linear(input_size=2, output_size=1),
#     Sigmoid()
# ])

n_epochs = 20
#loss_list = train(net, inputs,targets, loss = MSE() ,optimizer = Adam(lr = 1e-2, gamma1 = 0.3, gamma2 = 0.3), iterator = BatchIterator(1024), num_epochs = n_epochs)
try:
    loss_list = train(net,
                      inputs,
                      targets,
                      loss=MSE(),
                      optimizer=LM_cond(1e4, 1e2),
                      iterator=BatchIterator(len(inputs)),
                      num_epochs=n_epochs)
except np.linalg.LinAlgError as err:
    print('Interrompido por matriz singular')

y_pred = []
for x in X_test:
    y_pred.append(net.forward(x))
y_pred = np.array(y_pred)

plt.title("Erro quadrático x Tempo")
plt.xlabel("número de iterações")
plt.ylabel("erro quadrático")
plt.scatter(list(range(0, n_epochs)), loss_list)
Exemplo n.º 9
0
NUM_EPOCHS = 10000
inputs = np.array([
    binary_encode(x, NUM_ENCODE_BITS)
    for x in range(101, 1024)
])

targets = np.array([
    fizz_buzz_encode(x)
    for x in range(101, 1024)
])

net = NeuralNet([
    Linear(input_size=NUM_ENCODE_BITS, output_size=50),
    Tanh(),
    Linear(input_size=50, output_size=4)
])

train(net=net,
      inputs=inputs,
      targets=targets,
      num_epochs=NUM_EPOCHS,
      optimizer=SGD(lr=0.001))

for x in range(1, 101):
    predicted = net.forward(inputs=binary_encode(x))
    predicted_idx = np.argmax(predicted) # largest value is predicted class
    actual_idx = np.argmax(fizz_buzz_encode(x))
    labels = [str(x), 'fizz', 'buzz', 'fizzbuzz']
    print(x, labels[predicted_idx], labels[actual_idx])

Exemplo n.º 10
0
def binary_encode(x: int) -> List[int]:
    """
    10 digit binary encoding of x
    """
    return [x >> i & 1 for i in range(10)]


def binary_decode(bitlist: List) -> int:
    pass


inputs = np.array([binary_encode(x) for x in range(101, 1024)])

targets = np.array([fizz_buzz_encode(x) for x in range(101, 1024)])

net = NeuralNet([
    Linear(input_size=10, output_size=50),
    Tanh(),
    Linear(input_size=50, output_size=4)
])

train(net, inputs, targets, num_epochs=20, loss=MSE(), optimizer=SGD(lr=0.001))

inputs = np.array([binary_encode(x) for x in range(1, 101)])

targets = np.array([fizz_buzz_encode(x) for x in range(1, 101)])

labels = ["x", "fizz", "buzz", "fizzbuzz"]

test(net, inputs, targets, labels, binary_decode)
Exemplo n.º 11
0
targets = np.array([[0], [1], [1], [0]])

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

n_epochs = 10000
loss_list = train(net,
                  inputs,
                  targets,
                  loss=Log_loss(),
                  optimizer=SGD(lr=1e-5),
                  iterator=BatchIterator(4),
                  num_epochs=n_epochs)

for x, y in zip(inputs, targets):
    predicted = net.forward(x)
    print(x, predicted, y)

plt.title("Erro quadrático x Tempo")
plt.xlabel("número de iterações")
plt.ylabel("erro quadrático")
plt.scatter(list(range(0, n_epochs)), loss_list)
plt.show()