这是我关于stackoverflow的第一个问题。如果我格式化错误,请告诉我!非常感谢!
所以我试图最大化一个函数(下面的第一行)enter image description here
我已附上我的损失函数代码:
def loss(model, y_interior, S_interior, y_terminal, S_terminal):
''' Compute total loss for training.
Args:
model: DGM model object
t_interior: sampled time points in the interior of the function's domain
S_interior: sampled space points in the interior of the function's domain
t_terminal: sampled time points at terminal point (vector of terminal times)
S_terminal: sampled space points at terminal time
'''
# Loss term #1: PDE
# compute function value and derivatives at current sampled points
W = model(y_interior, S_interior)
W_s = tf.gradients(W, S_interior)[0]
W_y = tf.gradients(W, y_interior)[0]
W_yy = tf.gradients(W, y_interior)[0]
def f(params):
# print(params) # <-- you'll see that params is a NumPy array
v_1, v_2 = params # <-- for readability you may wish to assign names to the component variables
return -((v_1*y_interior*sigma)**2/2*W_yy+((1-v_1)*y_interior*r+v_1*y_interior*alpha)*W_y+np.math.exp(-p*S_interior)*v_2**gamma)
initial_guess = [1, 1]
result = optimize.minimize(f, initial_guess)
if result.success:
fitted_params = result.x
fitted_value = result.fun
print(fitted_params)
else:
raise ValueError(result.message)
# compute average L2-norm of differential operator
L1 = tf.reduce_mean(tf.square(fitted_value))
L2 = tf.reduce_mean(tf.square(W_s))
# Loss term #2: boundary condition
# no boundary condition for this problem
# Loss term #3: initial/terminal condition
target_payoff = tf.nn.relu(S_terminal - 100)
fitted_payoff = model(y_terminal, S_terminal)
L3 = tf.reduce_mean( tf.square(fitted_payoff - target_payoff) )
return L1, L2, L3
但是错误消息是:TypeError:必须是实数,而不是张量
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-8c310392196a> in <module>
156 S_terminal_tnsr = tf.placeholder(tf.float32, [None,1])
157 # loss
--> 158 L1_tnsr, L2_tnsr, L3_tnsr = loss(model, y_interior_tnsr, S_interior_tnsr, y_terminal_tnsr, S_terminal_tnsr)
159 loss_tnsr = L1_tnsr +L2_tnsr
160
<ipython-input-11-8c310392196a> in loss(model, y_interior, S_interior, y_terminal, S_terminal)
109
110 initial_guess = [1, 1]
--> 111 result = optimize.minimize(f, initial_guess)
112
113 if result.success:
~\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
602 return _minimize_cg(fun, x0, args, jac, callback, **options)
603 elif meth == 'bfgs':
--> 604 return _minimize_bfgs(fun, x0, args, jac, callback, **options)
605 elif meth == 'newton-cg':
606 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in
_minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options) 1001 func_calls, f = wrap_function(f, args) 1002
-> 1003 old_fval = f(x0) 1004 1005 if fprime is None:
~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
325 def function_wrapper(*wrapper_args):
326 ncalls[0] += 1
--> 327 return function(*(wrapper_args + args))
328
329 return ncalls, function_wrapper
<ipython-input-11-8c310392196a> in f(params)
104 v_1, v_2 = params # <-- for readability you may wish to assign names to the component variables
105
--> 106 return -((v_1*y_interior*sigma)**2/2*W_yy+((1-v_1)*y_interior*r+v_1*y_interior*alpha)*W_y+np.math.exp(-p*S_interior)*v_2**gamma)
107
108
我认为问题在于,当我最大化函数时,函数f中的梯度仍然是张量,而不是标量。如果您需要更多信息,请与我们联系。非常感谢!
这里的问题似乎是
def f(params):
# print(params) # <-- you'll see that params is a NumPy array
v_1, v_2 = params # <-- for readability you may wish to assign names to the component variables
return -((v_1*y_interior*sigma)**2/2*W_yy+((1-v_1)*y_interior*r+v_1*y_interior*alpha)*W_y+np.math.exp(-p*S_interior)*v_2**gamma)
return是Tensor
,但调用者希望返回标量/整数。您怀疑的是哪个。
在发生乘法之前将numpy数组强制转换为张量,结果是张量。
尝试使用它来查看效果
tensor = tf.constant(10)
np_type = np.array([1])
>>> <tf.Tensor: shape=(1,), dtype=int32, numpy=array([100], dtype=int32)>
这应该可以解决问题
def f(params):
# print(params) # <-- you'll see that params is a NumPy array
v_1, v_2 = params # <-- for readability you may wish to assign names to the component variables
tensor_loss = -((v_1*y_interior*sigma)**2/2*W_yy\
+((1-v_1)*y_interior*r+v_1*y_interior*alpha)*W_y\
+np.math.exp(-p*S_interior)*v_2**gamma)
return np.array(tensor_loss)[0]