我现在正在研究一个问题,我试图使用python中tensorflow probability的优化器来解决一个简单的优化问题,我已经在r中定义过了。
步骤如下:
步骤1:定义解决rosenbrock香蕉函数的原始python问题:
import contextlib
import functools
import os
import time
import numpy as np
import pandas as pd
import scipy as sp
from six.moves import urllib
from sklearn import preprocessing
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()
import tensorflow_probability as tfp
def make_val_and_grad_fn(value_fn):
@functools.wraps(value_fn)
def val_and_grad(x):
return tfp.math.value_and_gradient(value_fn, x)
return val_and_grad
@contextlib.contextmanager
def timed_execution():
t0 = time.time()
yield
dt = time.time() - t0
print('Evaluation took: %f seconds' % dt)
def np_value(tensor):
"""Get numpy value out of possibly nested tuple of tensors."""
if isinstance(tensor, tuple):
return type(tensor)(*(np_value(t) for t in tensor))
else:
return tensor.numpy()
def run(optimizer):
optimizer() # Warmup.
with timed_execution():
result = optimizer()
return np_value(result)
def run(optimizer):
optimizer() # Warmup.
with timed_execution():
result = optimizer()
return np_value(result)
@make_val_and_grad_fn
def rosenbrock_test(coord):
x, y = coord[..., 0], coord[..., 1]
return((5.0-x)**2 + 10.0 * (y-x**2)**2)
optim_results = tfp.optimizer.lbfgs_minimize(
rosenbrock_test,
initial_position=rosenbrock_start,
tolerance=1e-12)
rosenbrock_start = tf.constant([2.,2.])
optim_results = tfp.optimizer.lbfgs_minimize(
rosenbrock_test,
initial_position=rosenbrock_start,
tolerance=1e-12)
rosenbrock_start = tf.constant([2.,2.])
print('L-BFGS Results')
print('Converged:', optim_results.converged)
print('Location of the minimum:', optim_results.position)
print('Number of iterations:', optim_results.num_iterations)
第2步:在r中定义相同的函数:
rosenbrock_for_r <- function(coord){
x <- coord[1]
y <- coord[2]
return( (5-x)^2 + 10 * (y-x^2)^2 ) }
rosenbrock_for_r(c(2,2))
步骤3:为r函数定义python Package 器:
def rosenbrock_R(coord):
return(r.rosenbrock_for_r(coord))
错误发生在以下步骤:
temp = [2.0,2.0]
tfp.math.value_and_gradient(rosenbrock_R, [2.,2.])
错误是:
typeerror:rosenbrock\u r()接受1个位置参数,但给出了2个
我尝试过调查是否向函数输入了错误的内容,但实现与本机实现相同:
def rosenbrock_alt(coord):
x, y = coord[..., 0], coord[..., 1]
return((5.0-x)**2 + 10.0 * (y-x**2)**2)
temp = tf.constant([2.0,2.0])
tfp.math.value_and_gradient(rosenbrock_alt,temp)
这将产生预期的输出:
(tf.tensor:shape=(),dtype=float32,numpy=49.0,tf.tensor:shape=(2,),dtype=float32,numpy=array([154.,-40.],dtype=float32))
1条答案
按热度按时间disbfnqx1#
tfp.math.value_and_gradient
将列表解压为多个参数,并对每个参数进行区分。你得把衣服包起来np.array
或者tf.convert_to_tensor
.此外,还不清楚你将如何得到一个梯度为
rosenbrock_for_r
. 你可能需要使用类似你可以用
tf.py_function
在tf图中嵌入eager/r代码。