Example #1
0
def main(argv):
    args = parser.parse_args(argv[1:])

    # Fetch the data
    (train_x, train_y), (test_x, test_y) = simu_data.load_data()

    # Feature columns describe how to use the input.
    my_feature_columns = []
    for key in train_x.keys():
        my_feature_columns.append(
            tf.feature_column.numeric_column(key=key))

    # Build 2 hidden layer DNN with 10, 10 units respectively.
    classifier = tf.estimator.Estimator(
        model_fn=gLasso_model,
        params={
            'feature_columns': my_feature_columns,
            # Two hidden layers of 20 nodes each.
            'hidden_units': [20, 20],
            # The model output.
            'n_response': 1,
        })

    # Train the Model.
    classifier.train(
        input_fn=lambda: simu_data.train_input_fn(
            train_x, train_y, args.batch_size),
        steps=args.train_steps)

    # Evaluate the model.
    eval_result = classifier.evaluate(
        input_fn=lambda: simu_data.eval_input_fn(test_x, test_y, args.batch_size))

    # extract variables from model
    var_dict = dict()
    for var_name in classifier.get_variable_names():
        var_dict[var_name] = classifier.get_variable_value(var_name)

    print('\nTest set MSE: {MSE:0.3f}\n'.format(**eval_result))
Example #2
0
## layers = 0
csvFile0 = open("result0.csv", "a")
fileHeader = [
    "Nk", "n", "method", "snr", "rho", "p", "reg", "cos_dis", "mse",
    "spec_norm", "hs_norm"
]
writer = csv.writer(csvFile0)
writer.writerow(fileHeader)

for method in method_list:
    for reg in reg_factors:
        for k in np.arange(0, N_runs):
            print("Run ", k + 1, " of ", N_runs, "...\n", end="")
            (train_x, train_y), (test_x, test_y) = \
                simu_data.load_data(y_name="y", n=n,
                                    method=method, rho=rho, snr=snr, p=p)
            my_feature_columns = []
            for key in train_x.keys():
                my_feature_columns.append(
                    tf.feature_column.numeric_column(key=key))

            classifier = tf.estimator.Estimator(model_fn=gLasso_model,
                                                params={
                                                    'feature_columns':
                                                    my_feature_columns,
                                                    'hidden_units': [0],
                                                    'n_response': 1,
                                                    'reg': reg,
                                                })

            classifier.train(input_fn=lambda: simu_data.train_input_fn(