예제 #1
0
def main(max_iter):
    seed = 100
    nb_data = 1000

    print("loading data ....")
    mnist = fetch_mldata('MNIST original',
                         data_home=os.path.join(os.path.dirname(__file__),
                                                './data'))
    X_train = mnist.data.reshape((-1, 1, 28, 28)) / 255.0
    np.random.seed(seed)
    X_train = np.random.permutation(X_train)[:nb_data]
    y_train = mnist.target
    np.random.seed(seed)
    y_train = np.random.permutation(y_train)[:nb_data]
    n_classes = np.unique(y_train).size

    print("building model ...")
    net = npdl.Model()
    net.add(npdl.layers.Convolution(1, (3, 3), input_shape=(None, 1, 28, 28)))
    net.add(npdl.layers.MeanPooling((2, 2)))
    net.add(npdl.layers.Flatten())
    net.add(npdl.layers.Softmax(n_out=n_classes))
    net.compile()

    print("train model ... ")
    net.fit(X_train,
            npdl.utils.data.one_hot(y_train),
            max_iter=max_iter,
            validation_split=0.1)
예제 #2
0
def main(max_iter):
    # data
    print("loading data ...")
    mnist = fetch_mldata('MNIST original',
                         data_home=os.path.join(os.path.dirname(__file__),
                                                './data'))
    X_train = mnist.data / 255.0
    y_train = mnist.target
    n_classes = np.unique(y_train).size

    # model
    print("building model ...")
    model = npdl.Model()
    model.add(
        npdl.layers.Dense(n_out=500,
                          n_in=784,
                          activation=npdl.activation.ReLU()))
    model.add(
        npdl.layers.Dense(n_out=n_classes,
                          activation=npdl.activation.Softmax()))
    model.compile(loss=npdl.objectives.SCCE(),
                  optimizer=npdl.optimizers.SGD(lr=0.001))

    # train
    print("train model ... ")
    model.fit(X_train,
              npdl.utils.data.one_hot(y_train),
              max_iter=max_iter,
              validation_split=0.1)
예제 #3
0
def main(max_iter, corpus_path=os.path.join(os.path.dirname(__file__), 'data/lm/tiny_shakespeare.txt')):
    raw_text = open(corpus_path, 'r').read()
    chars = list(set(raw_text))
    data_size, vocab_size = len(raw_text), len(chars)
    print("data has %s charactres, % unique." % (data_size, vocab_size))
    char_to_index = {ch: i for i, ch in enumerate(chars)}
    index_to_char = {i: ch for i, ch in enumerate(chars)}

    time_steps, batch_size = 30, 40

    length = batch_size * 20
    text_pointers = np.random.randint(data_size - time_steps - 1, size=length)
    batch_in = np.zeros([length, time_steps, vocab_size])
    batch_out = np.zeros([length, vocab_size], dtype=np.uint8)
    for i in range(length):
        b_ = [char_to_index[c] for c in raw_text[text_pointers[i]:text_pointers[i] + time_steps + 1]]
        batch_in[i, range(time_steps), b_[:-1]] = 1
        batch_out[i, b_[-1]] = 1

    print("Building model ...")
    net = npdl.Model()
    # net.add(model.layers.SimpleRNN(n_out=500, return_sequence=True))
    net.add(npdl.layers.SimpleRNN(n_out=500, n_in=vocab_size))
    net.add(npdl.layers.Softmax(n_out=vocab_size))
    net.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.SGD(lr=0.00001, clip=5))

    print("Train model ...")
    net.fit(batch_in, batch_out, max_iter=max_iter, batch_size=batch_size)
예제 #4
0
def main2(max_iter):
    batch_size, vocab_size, time_steps, batch_in, batch_out = get_data()

    print("Building model ...")
    net = npdl.Model()
    net.add(npdl.layers.SimpleRNN(n_out=200, n_in=vocab_size, return_sequence=True,
                                  nb_batch=batch_size, nb_seq=time_steps))
    net.add(npdl.layers.SimpleRNN(n_out=200, n_in=200))
    net.add(npdl.layers.Softmax(n_out=vocab_size))
    net.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.SGD(lr=0.00001, clip=5))

    print("Train model ...")
    net.fit(batch_in, batch_out, max_iter=max_iter, batch_size=batch_size)
예제 #5
0
def main4(max_iter):
    # test Adagrad optimizer

    n_classes, X_train, y_train = get_data()

    # model
    print("building model ...")
    model = npdl.Model()
    model.add(npdl.layers.Dense(n_out=500, n_in=784, activation='relu'))
    model.add(npdl.layers.Softmax(n_out=n_classes))
    model.compile(loss='scce', optimizer='adagrad')

    # train
    print("train model ... ")
    model.fit(X_train,
              npdl.utils.data.one_hot(y_train),
              max_iter=max_iter,
              validation_split=0.1)
예제 #6
0
def main3(max_iter):
    # test NesterovMomentum optimizer

    n_classes, X_train, y_train = get_data()

    # model
    print("building model ...")
    model = npdl.Model()
    model.add(npdl.layers.Dense(n_out=500, n_in=784, activation='relu'))
    model.add(npdl.layers.Softmax(n_out=n_classes))
    model.compile(loss=npdl.objectives.SCCE(),
                  optimizer=npdl.optimizers.NesterovMomentum())

    # train
    print("train model ... ")
    model.fit(X_train,
              npdl.utils.data.one_hot(y_train),
              max_iter=max_iter,
              validation_split=0.1)
예제 #7
0
def main2(max_iter):
    batch_size, vocab_size, time_steps, batch_in, batch_out = get_data()

    print("Building model ...")
    net = npdl.Model()
    net.add(
        npdl.layers.BatchLSTM(n_out=300,
                              n_in=vocab_size,
                              return_sequence=False,
                              nb_batch=batch_size,
                              nb_seq=time_steps))
    # net.add(npdl.layers.MeanPooling(pool_size=(time_steps, 1)))
    # net.add(npdl.layers.Flatten())
    net.add(npdl.layers.Softmax(n_out=vocab_size))
    net.compile(loss=npdl.objectives.SCCE(),
                optimizer=npdl.optimizers.SGD(lr=0.00001, clip=5))

    print("Train model ...")
    net.fit(batch_in, batch_out, max_iter=max_iter, batch_size=batch_size)
예제 #8
0
def main(max_iter):
    n_classes, X_train, y_train = get_data()

    # model
    print("building model ...")
    model = npdl.Model()
    model.add(
        npdl.layers.Dense(n_out=500,
                          n_in=784,
                          activation=npdl.activations.ReLU()))
    model.add(
        npdl.layers.Dense(n_out=n_classes,
                          activation=npdl.activations.Softmax()))
    model.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.SGD())

    # train
    print("train model ... ")
    model.fit(X_train,
              npdl.utils.data.one_hot(y_train),
              max_iter=max_iter,
              validation_split=0.1)
예제 #9
0
def main(max_iter):
    nb_batch = 30
    nb_seq = 20

    xs, ys, x_size, y_size = prepare_data(nb_seq)

    net = npdl.Model()
    net.add(
        npdl.layers.Embedding(nb_batch=nb_batch,
                              nb_seq=nb_seq,
                              n_out=200,
                              input_size=x_size,
                              static=True))
    net.add(npdl.layers.BatchLSTM(n_out=400, return_sequence=True))
    net.add(npdl.layers.BatchLSTM(n_out=200, return_sequence=True))
    net.add(npdl.layers.MeanPooling((nb_seq, 1)))
    net.add(npdl.layers.Flatten())
    net.add(npdl.layers.Softmax(n_out=y_size))
    net.compile(loss='scce', optimizer=npdl.optimizers.SGD(lr=0.005))
    net.fit(xs,
            ys,
            batch_size=nb_batch,
            validation_split=0.1,
            max_iter=max_iter)