如何在python中优化参数

bvn4nwqk  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(276)

我用下面的代码对我的数据进行分类。但是我想优化这些参数(learning\u rate,epochs,dropout,hidden1)以最大限度地提高精度,其中参数的搜索空间分别是[0.0001,0.5],[150,300][0.01,0.5],[8,32]

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('dataset', 'citeseer', 'Dataset string.')  # 'cora', 'citeseer', 'pubmed'
flags.DEFINE_string('model', 'gcn', 'Model string.')  # 'gcn', 'gcn_cheby', 'dense'
flags.DEFINE_float('learning_rate', 0.05, 'Initial learning rate.')
flags.DEFINE_integer('epochs', 200, 'Number of epochs to train.')
flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
flags.DEFINE_float('dropout', 0.5, 'Dropout rate (1 - keep probability).')
flags.DEFINE_float('weight_decay', 5e-4, 'Weight for L2 loss on embedding matrix.')
flags.DEFINE_integer('early_stopping', 10, 'Tolerance for early stopping (# of epochs).')
flags.DEFINE_integer('max_degree', 3, 'Maximum Chebyshev polynomial degree.')

# Load data

adj, features, y_train, y_val, labels, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.dataset)

# Create model

model = model_func(placeholders, input_dim=selectedfeatures[2][1], logging=True)

# Initialize session

sess = tf.Session()

# Define model evaluation function

def evaluate(selectedfeatures, support, labels, mask, placeholders):
t_test = time.time()
feed_dict_val = construct_feed_dict(selectedfeatures, support, labels, mask, placeholders)
outs_val = sess.run([model.loss, model.accuracy], feed_dict=feed_dict_val)
return outs_val[0], outs_val[1], (time.time() - t_test)

# Init variables

sess.run(tf.global_variables_initializer())

cost_val = []

# Train model

for epoch in range(FLAGS.epochs):

t = time.time()

# Construct feed dictionary

feed_dict = construct_feed_dict(selectedfeatures, support, y_train, train_mask, placeholders)
feed_dict.update({placeholders['dropout']: FLAGS.dropout})

# Training step

outs = sess.run([model.opt_op, model.loss, model.accuracy], feed_dict=feed_dict)

# Validation

cost, acc, duration = evaluate(selectedfeatures, support, y_val, val_mask, placeholders)
cost_val.append(cost)

# Print results

print("Epoch:", '%04d' % (epoch + 1), "train_loss=", "{:.5f}".format(outs[1]),
      "train_acc=", "{:.5f}".format(outs[2]), "val_loss=", "{:.5f}".format(cost),
      "val_acc=", "{:.5f}".format(acc), "time=", "{:.5f}".format(time.time() - t))

if epoch > FLAGS.early_stopping and cost_val[-1] > np.mean(cost_val[-(FLAGS.early_stopping+1):-1]):
    print("Early stopping...")
    break

print("Optimization Finished!")

# Testing

test_cost, test_acc, test_duration = evaluate(selectedfeatures, support, y_test, test_mask, 
placeholders)
print("Test set results:", "cost=", "{:.5f}".format(test_cost),
  "accuracy=", "{:.5f}".format(test_acc), "time=", "{:.5f}".format(test_duration))

谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题