Пример #1
0
# training dataset
train_x, labels = create_noise_data()

# test dataset
X, Y = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(-3, 3, 100))
test_x = np.array([X.ravel(), Y.ravel()]).reshape(2, -1).T

# constructing NN
model = Neural_net(2, [10, 10, 10], 1)
model.add(['tanh', 'swish', 'tanh'])
routine = Adam(lr=0.1)
model.compile(loss='binary_crossentropy', optimizer=routine)

#---learning----
history = model.fit(train_x, labels, n_iter=max_iter, history=True)

# plot the training data
plt.scatter(train_x[labels.ravel() == 0, 0],
            train_x[labels.ravel() == 0, 1],
            marker=".",
            s=20,
            color='b')

plt.scatter(train_x[labels.ravel() == 1, 0],
            train_x[labels.ravel() == 1, 1],
            marker="x",
            s=20,
            color='r')

# plot the test data
Пример #2
0
fig = plt.figure(figsize=(15, 9))

for n, func in enumerate(multi_func, 1):
    train_y = func(train_x)
    # plotting training data
    ax = fig.add_subplot(2, 2, n)
    ax.scatter(train_x, train_y, s=12, color='blue')
    # constructing NN
    model = Neural_net(n_input=1, n_hidden=[10, 10, 10], n_output=1)
    model.add(['tanh', 'tanh', 'sigmoid'])
    routine = Adam(lr=0.1, beta_1=0.9, beta_2=0.95)
    model.compile(loss='sum_squared_error', optimizer=routine)
    #-----learning------
    import time
    start = time.time()
    history = model.fit(train_x, train_y, n_iter=max_iter, history=True)
    print(time.time() - start)
    train_acc[func_name[n - 1]] = history['acc']

    # prediction data
    y = model(x)
    ax.plot(x, y, 'r-')
    plt.xticks([-1, 0, 1])
    plt.yticks([0, 0.5, 1])
    plt.ylim([-0.1, 1.1])
    plt.subplots_adjust(wspace=0.2, hspace=0.3)
plt.show()

fig = plt.figure(figsize=(10, 4))
ax = fig.add_subplot(111)
x = np.arange(max_iter)
Пример #3
0
max_iter = 1e+4
'''#0 loading data '''
(X_train, train_t), (X_test, test_t) = load_mnist(normalize=True)
train_t = to_categorical(train_t)
'''#1 config for NN '''
model = Neural_net(784, [100, 100, 100, 100], 10, alpha=0.01)
model.add(['tanh', 'softsign', 'softplus', 'swish'])
# optimizer
routine = Adam(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=routine)
'''#2 learning '''
import time
start = time.time()
hist = model.fit(X_train,
                 train_t,
                 batch_size=256,
                 n_iter=max_iter,
                 history=True)
print(time.time() - start)
'''#3 preparing test data '''
data, label = get_mini_batch(X_test, test_t, batch_size=1)
'''#4 showing image '''
fig2 = plt.figure(figsize=(11, 4))
ax = fig2.add_subplot(111)
ax.imshow(data.reshape(28, 28), cmap='gray')
plt.tick_params(labelbottom=False, labelleft=False, bottom=False, left=False)
'''#5 output prediction data '''
prob = model(data).ravel()
prediction = np.argmax(prob)

# --- probability ---