Example #1
0
def main(argv):    
    if args.seed:
        np.random.seed(args.seed)

    map = Map(MAP_WIDTH, MAP_HEIGHT)
    net = NeuralNetwork(2, args.layer_neurons, 1, args.hidden_layers, args.bias)
    print net
    if  args.train:
        # тренировочные данные
        train_d0, train_d1 = map.dataset(0, MAP_WIDTH + MAP_HEIGHT), \
                             map.dataset(1, MAP_WIDTH + MAP_HEIGHT)
        td0 = np.array([[0]] * train_d0.shape[0], dtype=float)
        td1 = np.array([[1]] * train_d1.shape[0], dtype=float)
        t = np.concatenate((td0, td1), axis=0) # уже нормализован
        # вход
        x = np.concatenate((train_d0, train_d1), axis=0)
        x_normalized = x / np.amax(x, axis=0)
        
        print 'Training...'
        if args.logging:
            with open('training.log', 'w') as f:
                for epoch in xrange(args.epochs):
                    f.write('Epoch {}\n'.format(epoch))
                    f.write("Input:\n{}\n".format(x_normalized.T))
                    f.write("Actual Output:\n{}\n".format(t.T))
                    f.write("Predicted Output:\n{}\n".format(np.round(net.forward(x_normalized).T)))
                    f.write("Loss:\n{}\n\n".format(str(np.mean(np.square(t - net.forward(x_normalized))))))
                    net.train(x_normalized, t)
        else:
            for epoch in xrange(args.epochs):
                net.train(x_normalized, t, args.alpha, args.train_speed)
        print "Saving weights..."
        net.save_weights(W_PREFIX)
        print 'Done.'
    else:
        train_d0 = train_d1 = np.array([])
        if os.path.exists('{}_0.w.txt'.format(W_PREFIX)):
           print "Loading weights..."
           net.load_weights(W_PREFIX)
           print 'Done.'
        else:
            print "No weights were found!"

    if args.seed:
        np.random.seed(args.seed + 1)
    
    # вход
    zds0, zds1 = np.random.randint(2, 20), np.random.randint(2, 20)
    d0, d1 = map.dataset(0, zds0), map.dataset(1, zds1)
    x = np.concatenate((d0, d1), axis=0)
    x_normalized = x / np.amax(x, axis=0)
    # ожидаемые данные для проверки
    td0 = np.array([[0]] * d0.shape[0], dtype=float)
    td1 = np.array([[1]] * d1.shape[0], dtype=float)
    t = np.concatenate((td0, td1), axis=0) # уже нормализован
    # выход
    y = np.round(net.predict(x_normalized))
    if args.verbose:
        print "Input:"
        print x
        print "Output (Expected):"
        print t
        print "Output (Actual):"
        print y

    res = (y == t)
    if res.all():
        print "\nAll Good!"
    else:
        print "{}% are good!".format(res.sum() * 100 / len(res))

    if args.plotting:
        # фильтрация 'попаданий' и 'промахов'
        good = []
        bad = []
        for i, v in enumerate(res):
            if v:
                good.append(x[i])
            else:
                bad.append(x[i])
        map.plot(np.array(good), np.array(bad), train_d0, train_d1, args.plot_name)
Z_train = res.reshape(Xn * Yn, 1)
Z_train = remap(Z_train, np.min(Z_train), np.max(Z_train), 0.01, 0.99)

Z_ideal = f(X, Y)

epochs = 2000
err = []
for i in range(epochs):
    err.append(np.max(network.train(XY_train, Z_train)))
    if i % 100 == 0:
        print("running " + str(i) + " epoch")
print("error on end of train = " + str(err[-1]))

XY_pred = np.stack((X.ravel(), Y.ravel()), axis=-1)
Z_predicted = network.predict(XY_pred).reshape(Xn, Yn)  # XY_train
Z_predicted = remap(Z_predicted, 0.05, 0.95, np.min(Z_train), np.max(Z_train))

fig = plt.figure(0)
ax = fig.gca(projection='3d')
plot = ax.plot_wireframe(X, Y, Z_ideal, color='red', linewidth=1)
fig.suptitle('Input function', fontsize=16)

fig2 = plt.figure(1)
ax = fig2.gca(projection='3d')
plot2 = ax.plot_wireframe(X, Y, Z_predicted, color='gray', linewidth=0.5)
fig2.suptitle('Network approximation cfg:' + str(config) + "\n avg err = {:7f}".format(err[-1]), fontsize=16)

err_fig = plt.figure(2)
ax = err_fig.add_subplot(111)
ax.plot(range(epochs), err)
Example #3
0
import numpy as np
import matplotlib.pyplot as plt
from nnet import NeuralNetwork
import activation_functions as funcs

config = [1, 5, 5, 1]
net = NeuralNetwork(config,
                    learning_rate=0.1,
                    act_func=funcs.sigmoid,
                    df_act_func=funcs.df_sigmoid)


def f(x):
    return x**2


vf = np.vectorize(f)

x = np.array(np.linspace(-1, 1, num=10), ndmin=2)
y = vf(x)

epochs = 5000
err = net.fit_raw(x, y, epochs, True, 10)

test_sample = np.array([0.1])
y_pred = net.predict(test_sample)

plt.plot(np.array(range(epochs) + 1), np.array(err))
plt.show()
Example #4
0
validate_file = open("../mnist_data/mnist_test_10.csv")
validate_list = validate_file.readlines()
validate_file.close()

scorecard = []
for i in range(len(validate_list)):
    raw_values = validate_list[i].split(',')
    label = raw_values[0]
    x = np.asfarray(raw_values[1:])
    x = remap(x, 0, 255, 0.01, 0.99)

    y = np.zeros(10) + 0.01
    y[int(label)] = 0.99

    y_pred = net.predict(x)

    # plt.imshow(x.reshape((28, 28)), cmap='Greys', interpolation=None)
    # plt.suptitle(label)
    # plt.show()
    # print("res " + str(y_pred))
    pred_num = np.argmax(y_pred)

    if pred_num == int(label):
        scorecard.append(1)
    else:
        scorecard.append(0)

    print(label + " predicted as " + str(pred_num))

# accuracy