小编TMu*_*r83的帖子

Integrating a function with singularities using scipy's quad routine

I am using the quad function from scipy.integrate v0.19.1 to integrate functions with a square-root like singularity at each end of the integration interval such as for example

In [1]: quad(lambda x: 1/sqrt(1-x**2), -1, 1)
Run Code Online (Sandbox Code Playgroud)

(I use the sqrt function from numpy v1.12.0) which immediately yields the correct result pi:

Out[1]: (3.141592653589591, 6.200897573194197e-10)
Run Code Online (Sandbox Code Playgroud)

According to the documentation of the quad function the keyword points should be used to indicate the locations of singularities or discontinuities of the integrand, but …

python scipy quad

7
推荐指数
1
解决办法
4606
查看次数

在优化问题中使用神经网络

我正在尝试使用Keras来建立一个神经网络,该网络应该近似一个未知函数F(x)F(x)然后应使用所得的神经网络近似值来求和的最小值G(x) + F(x),其中G(x)有一些任意函数。我当前面临的问题是,神经网络逼近F(x)不够平滑,因此局部优化器陷入了微小的局部极小值中。对于能改善结果或解决此问题的任何想法,我将深表感谢。

简单示例:最小化方差

让我说明上非常简单的例子问题:我会尽量教神经网络的功能F(x) = 4*var(x)x = [x1,...,xN]0 <= xi <= 1,这里var(x)是指矢量的变化x。随后,我将尝试查找F(x)约束条件下x具有给定均值的神经网络表示的最小值。此示例的完整代码可以在sec中找到3以下。

1.神经网络的创建和训练

首先,我为以下近似创建一个神经网络F(x)

N = 6  # Dimension of input vector x

# Set up the neural network
model = Sequential()
model.add(Dense(50, activation='sigmoid', input_dim=N))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='mse')
Run Code Online (Sandbox Code Playgroud)

随后,我生成训练数据并训练模型:

# Generate training data
n_train = 100000  # …
Run Code Online (Sandbox Code Playgroud)

optimization machine-learning scipy neural-network keras

6
推荐指数
0
解决办法
163
查看次数

scipy 的 shgo 优化器无法最小化方差

为了熟悉全局优化方法,特别是shgo优化器,scipy.optimize v1.3.0我尝试在具有给定平均值的约束下最小化向量的方差var(x)x = [x1,...,xN]0 <= xi <= 1x

import numpy as np
from scipy.optimize import shgo

# Constraint
avg = 0.5  # Given average value of x
cons = {'type': 'eq', 'fun': lambda x: np.mean(x)-avg}

# Minimize the variance of x under the given constraint
res = shgo(lambda x: np.var(x), bounds=6*[(0, 1)], constraints=cons)
Run Code Online (Sandbox Code Playgroud)

shgo方法在这个问题上失败了:

>>> res
     fun: 0.0
 message: 'Failed to find a feasible minimiser point. Lowest sampling point …
Run Code Online (Sandbox Code Playgroud)

optimization scipy shgo

5
推荐指数
0
解决办法
2162
查看次数