gpt4 book ai didi

python - Tensorflow:如何在约束下最小化

转载 作者:行者123 更新时间:2023-12-02 09:56:48 49 4
gpt4 key购买 nike

我面临着一个受约束、等式和不等式约束的数值优化问题。看起来这个任务的一切都在 tensorflow 中,阅读诸如https://www.tensorflow.org/api_docs/python/tf/contrib/constrained_optimization之类的文档.

虽然我缺少一个最小的工作示例。我进行了大量的谷歌搜索,但没有结果。有人可以与我分享一些有用的资源吗?最好以 Eager 模式运行。

编辑:

我现在找到了https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/constrained_optimization

我仍然欢迎任何额外的资源。

最佳答案

您可以使用TFCO适用于 TF > 1.4。

这是一个我们想要最小化的具体示例:

(x - 2) ^ 2 + y

s.t.

  • x + y = 1
  • x > 0
  • y > 0
import tensorflow as tf

# Use the GitHub version of TFCO
# !pip install git+https://github.com/google-research/tensorflow_constrained_optimization
import tensorflow_constrained_optimization as tfco

class SampleProblem(tfco.ConstrainedMinimizationProblem):
def __init__(self, loss_fn, weights):
self._loss_fn = loss_fn
self._weights = weights

@property
def num_constraints(self):
return 4

def objective(self):
return loss_fn()

def constraints(self):
x, y = self._weights
sum_weights = x + y
lt_or_eq_one = sum_weights - 1
gt_or_eq_one = 1 - sum_weights
constraints = tf.stack([lt_or_eq_one, gt_or_eq_one, -x, -y])
return constraints

x = tf.Variable(0.0, dtype=tf.float32, name='x')
y = tf.Variable(0.0, dtype=tf.float32, name='y')

def loss_fn():
return (x - 2) ** 2 + y

problem = SampleProblem(loss_fn, [x, y])

optimizer = tfco.LagrangianOptimizer(
optimizer=tf.optimizers.Adagrad(learning_rate=0.1),
num_constraints=problem.num_constraints
)

var_list = [x, y] + problem.trainable_variables + optimizer.trainable_variables()

for i in range(10000):
optimizer.minimize(problem, var_list=var_list)
if i % 1000 == 0:
print(f'step = {i}')
print(f'loss = {loss_fn()}')
print(f'constraint = {(x + y).numpy()}')
print(f'x = {x.numpy()}, y = {y.numpy()}')

关于python - Tensorflow:如何在约束下最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401610/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com