寻找方程的自洽解

Finding the self-consistent solution to an equation

此问题的底部是一组从已发布的神经网络模型转录而来的函数。当我调用 R 时,出现以下错误:

RuntimeError: maximum recursion depth exceeded while calling a Python object

请注意,在对 R 的每次调用中,都会对网络中的每个其他神经元进行递归调用 R。这就是导致超出递归深度的原因。 R 的每个 return 值都取决于所有其他值(网络涉及 N = 512 总值。)有谁知道应该使用什么方法来计算自洽解R?请注意 R 本身是一个平滑函数。我试过将其视为向量求根问题——但在这种情况下,512 个维度不是独立的。有这么多的自由度,根本找不到根(使用 scipy.optimize 函数)。 Python 是否有任何工具可以帮助解决这个问题?也许使用像 Mathematica 这样的工具来解决 R 会更自然?我不知道这通常是如何完成的。

"""Recurrent model with strong excitatory recurrence."""


import numpy as np


l = 3.14


def R(x_i):

    """Steady-state firing rate of neuron at location x_i.

    Parameters
    ----------
    x_i : number
      Location of this neuron.

    Returns
    -------
    rate : float
      Firing rate.

    """

    N = 512
    T = 1

    x = np.linspace(-2, 2, N)
    sum_term = 0
    for x_j in x:
        sum_term += J(x_i - x_j) * R(x_j)

    rate = I_S(x_i) + I_A(x_i) + 1.0 / N * sum_term - T

    if rate < 0:
        return 0

    return rate


def I_S(x):
    """Sensory input.

    Parameters
    ----------
    x : number
      Location of this neuron.

    Returns
    -------
    float
      Sensory input to neuron at x.

    """
    S_0 = 0.46
    S_1 = 0.66
    x_S = 0
    sigma_S = 1.31
    return S_0 + S_1 * np.exp(-0.5 * (x - x_S) ** 2 / sigma_S ** 2)


def I_A(x):
    """Attentional additive bias.

    Parameters
    ----------
    x : number
      Location of this neuron.

    Returns
    -------
    number
      Additive bias for neuron at x.

    """
    x_A = 0
    A_1 = 0.089
    sigma_A = 0.35
    A_0 = 0
    sigma_A_prime = 0.87
    if np.abs(x - x_A) < l:
        return (A_1 * np.exp(-0.5 * (x - x_A) ** 2 / sigma_A ** 2) +
                A_0 * np.exp(-0.5 * (x - x_A) ** 2 / sigma_A_prime ** 2))
    return 0


def J(dx):
    """Connection strength.

    Parameters
    ----------
    dx : number
      Neuron i's distance from neuron j.

    Returns
    -------
    number
      Connection strength.

    """
    J_0 = -2.5
    J_1 = 8.5
    sigma_J = 1.31
    if np.abs(dx) < l:
        return J_0 + J_1 * np.exp(-0.5 * dx ** 2 / sigma_J ** 2)
    return 0


if __name__ == '__main__':

    pass

使用sys.setrecursionlimit更改最大递归深度

import sys
sys.setrecursionlimit(10000)

def rec(i):
    if i > 1000:
        print 'i is over 1000!'
        return
   rec(i + 1)

rec(0)

更多信息:https://docs.python.org/3/library/sys.html#sys.setrecursionlimit`

这个递归永远不会结束,因为在递归调用之前没有终止条件,调整最大递归深度没有帮助

def R(x_i): 
   ...
   for x_j in x:
       sum_term += J(x_i - x_j) * R(x_j)

也许你应该做类似

的事情
# some suitable initial guess
state = guess

while True: # or a fixed number of iterations
   next_state = compute_next_state(state)

   if some_condition_check(state, next_state):
       # return answer
       return state 

   if some_other_check(state, next_state):
       # something wrong, terminate
      raise ...