Пример #1
0
if __name__ == '__main__':
    # Create CNN using ONNX
    onnx_file = d5net.export_network('simple_cnn', BATCH_SIZE)
    model = d5.parser.load_and_parse_model(onnx_file)

    # Recover input and output nodes (assuming only one input and one output)
    INPUT_NODE = model.get_input_nodes()[0].name
    OUTPUT_NODE = model.get_output_nodes()[0].name

    # Create dataset and add loss function to model
    train_set, test_set = d5ds.load_mnist(INPUT_NODE, LABEL_NODE)
    model.add_operation(
        d5.ops.SoftmaxCrossEntropy([OUTPUT_NODE, LABEL_NODE], 'loss'))

    # Create executor and reference SGD optimizer
    tensorflow_executor = d5tf.from_model(model)
    optimizer = d5tf.GradientDescent(tensorflow_executor, 'loss')

    # Initial test set accuracy check
    n = len(test_set)
    wrong = 0
    for i in range(len(test_set) // BATCH_SIZE):
        inp = test_set[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
        out = tensorflow_executor.inference(inp)
        wrong += np.sum(inp[LABEL_NODE] != np.argmax(out[OUTPUT_NODE], axis=1))
    print("Accuracy before: {}".format((n - wrong) / n))

    # Train for one epoch (direct pass over Dataset without a Sampler)
    for i in trange(len(train_set) // BATCH_SIZE):
        inp = train_set[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
        optimizer.step(inp)
Пример #2
0
                                     BATCH_SIZE,
                                     classes=ds_cls,
                                     shape=(ds_c, ds_h, ds_w))
    model = d5.parser.load_and_parse_model(onnx_file)

    # Recover input and output nodes (assuming only one input and one output)
    INPUT_NODE = model.get_input_nodes()[0].name
    OUTPUT_NODE = model.get_output_nodes()[0].name

    # Create dataset and add loss function to model
    train_set, test_set = d5ds.load_dataset(dsname, INPUT_NODE, LABEL_NODE)
    model.add_operation(
        d5.ops.SoftmaxCrossEntropy([OUTPUT_NODE, LABEL_NODE], 'loss'))

    # Create executor and reference SGD optimizer
    executor = d5tf.from_model(model)
    optimizer = d5ref.GradientDescent(executor, 'loss')

    # Create samplers
    train_sampler = d5.ShuffleSampler(train_set, BATCH_SIZE)
    test_sampler = d5.ShuffleSampler(test_set, BATCH_SIZE)

    # Create runner (training/test manager)
    runner = d5.Trainer(train_sampler, test_sampler, executor, optimizer,
                        OUTPUT_NODE)
    #############################

    # Set up events to stop training after reaching the desired test accuracy
    events = d5.DefaultTrainerEvents(MAX_EPOCHS) + [
        d5.training_events.AccuracyAbortEvent(accuracy)
    ]
Пример #3
0
        z_t = self.z[param_name]

        z_t2 = z_t - self.alpha_t * eta_t * grad
        y_t2 = old_param - eta_t * grad

        self.z[param_name] = z_t2
        self.y[param_name] = y_t2
        self.squares[param_name] = squared_grad
        adjusted_lr = self.lr / (self.eps + np.sqrt(squared_grad))

        self.init = False
        return old_param - adjusted_lr * grad

if __name__ == '__main__':
    from deep500 import networks as d5net, datasets as d5ds
    from deep500.frameworks import tensorflow as d5tf
    from deep500.frameworks import reference as d5ref
    batch_size = 1024

    # Create network and dataset
    net, innode, outnode = d5net.create_model('simple_cnn', batch_size)
    net.add_operation(d5.ops.LabelCrossEntropy([outnode, 'label'], 'loss'))
    train, test = d5ds.load_dataset('mnist', innode, 'label')

    # Create executor and optimizer
    executor = d5tf.from_model(net)
    opt = AcceleGradOptimizer(executor)
    
    # Run training
    d5.test_training(executor, train, test, opt, 5, batch_size, outnode)