コード例 #1
0
def get_line(data, lr=0.0001):
    X = np.array([p[0] for p in data]).reshape(-1, 1)
    y = np.array([p[1] for p in data]).reshape(-1, 1)

    losses = []
    model = Model([Dense(1, 1)], MSE())

    for _ in range(50):
        h = model.forward(X)
        losses.append(model.backward(h, y, lr))

    layer = model.layers[0]
    return layer.W[0, 0], layer.b[0, 0], losses
コード例 #2
0
from lib import Model
from lib.nodes import Dense, Tanh
from lib.losses import MSE

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

y = np.array([0, 1, 1, 0]).reshape(-1, 1)

lr = 0.1

errs = []
for _ in range(20):
    model = Model([Dense(2, 4), Tanh(), Dense(4, 1)], MSE())
    for _ in range(1000):
        pred = model.forward(X)
        model.backward(pred, y, lr)

    err = np.abs(y - model.forward(X)).sum()
    errs.append(err)


plt.hist(errs)
plt.show()