Exemple #1
0
from models import Linear2
from sklearn import datasets
from core.Optimizers import sgd
from core.Functions import one_hot_f
import numpy as np


def test(model, datas, labels, tagets):
    acc = 0
    for i in range(len(datas)):
        sample = datas[i]
        if len(sample.shape) < 2:
            sample = np.expand_dims(sample, axis=1)
        res = model.val(sample)
        if tagets[i] == np.argmax(res, axis=0):
            acc += 1

    print('[ acc ]: ', acc / len(datas))


iris = datasets.load_iris()
iris_label_one_hot = one_hot_f(iris.target, num_classes=3)

print(Linear2.description)
Linear2.compile()
optimizer = sgd(0.009)
# test(Linear2, iris.data, iris_label_one_hot, iris.target)
optimizer.fit(Linear2, iris.data, iris_label_one_hot, epoch=2000)
test(Linear2, iris.data, iris_label_one_hot, iris.target)
        if np.argmax(res) == np.argmax(test_label):
            cnt_correct += 1
        cnt_tot += 1

    return cnt_correct / cnt_tot


fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images,
                               test_labels) = fashion_mnist.load_data()

# origin = np.random.randn(3, 28, 28).astype(np.float32)
# input_image = train_images[0, :, :].reshape(1,28,28)
train_images = np.expand_dims(train_images / 255, axis=-1)
test_images = np.expand_dims(test_images / 255, axis=-1)
train_labels = one_hot_f(train_labels, num_classes=10)
test_labels = one_hot_f(test_labels, num_classes=10)

small_train_images = train_images[0:10000]
small_train_labels = train_labels[0:10000]
small_test_images = train_images[0:100]
small_test_labels = train_labels[0:100]

Linear1.compile()
train_iterator = batch_iterator()
acc = test(Linear1, test_images, test_labels)
print('[ acc ]: ', acc)
optimizer = bgd(0.001)
optimizer.fit(Linear1, train_images, train_labels, train_iterator, epoch=10)
acc = test(Linear1, test_images, test_labels)
print('[ acc ]: ', acc)
Exemple #3
0
        test_inputs = cp.array(test_inputs)
        test_labels = cp.array(test_labels)
    for i in range(num_of_sample):
        test_input = test_inputs[i:i + 1]
        test_label = test_labels[i]
        res = model.forward_prop(test_input)
        if np.argmax(res) == np.argmax(test_label):
            cnt_correct += 1
        cnt_tot += 1

    acc = cnt_correct / cnt_tot
    print('[ accuracy ]: ', acc)
    return acc


fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images,
                               test_labels) = fashion_mnist.load_data()

train_images = np.expand_dims(train_images / 255, axis=-1)[0:6000]
test_images = np.expand_dims(test_images / 255, axis=-1)
train_labels = one_hot_f(train_labels, num_classes=10)[0:6000]
test_labels = one_hot_f(test_labels, num_classes=10)

CNN4.compile()
train_iterator = batch_iterator(batch_sz=16)
optimizer = adam(lr=1e-3)
for i in range(100):
    optimizer.fit(CNN4, train_images, train_labels, train_iterator, epoch=1)
    test(CNN4, train_images, train_labels)