Git Product home page Git Product logo

Comments (1)

yuanzhi-zhu avatar yuanzhi-zhu commented on July 21, 2024 5

fixed

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

schedule = 'EDM'

if schedule == 'VESDE':
    sigma_min = 0.02
    sigma_max = 100
    rho = 0
elif schedule == 'EDM':
    sigma_min = 0.002
    sigma_max = 80
    rho = 7.0

def gauss_norm(x):
    return np.exp(-x**2/2)/np.sqrt(2*np.pi)

def pred_x0_theory(x,sigma_t):
    c = np.array([
        [-1],
        [ 1],
    ])
    nominator = 0
    denominator = 0
    for i in range(c.shape[0]):
        nominator += c[i] * gauss_norm((x-c[i])/sigma_t)
        denominator += gauss_norm((x-c[i])/sigma_t)
    return nominator / (denominator + 0)

# define the ODE
def model(x, t):
    def s_t(t):
        return 1

    def s_t_p(t):
        return 0

    def sigma_t(t):
        if schedule == 'VESDE':
            # return self.sigma_min**2 * (self.sigma_max**2/self.sigma_min**2)**t
            return np.sqrt(t)
        elif schedule == 'EDM':
            # rho_inv = 1.0 / self.rho
            # sigmas = self.sigma_min**rho_inv + t * (
            #     self.sigma_max**rho_inv - self.sigma_min**rho_inv
            # )
            # sigmas = sigmas**self.rho
            # return sigmas
            return t

    def sigma_t_p(t, h=1e-5):
        return (sigma_t(t + h) - sigma_t(t - h)) / (2. * h)

    first_term = s_t_p(t) / s_t(t) * x
    score = (pred_x0_theory(x/s_t(t), sigma_t(t)) - x) / sigma_t(t)**2
    second_term = s_t(t)**2 * sigma_t_p(t) * sigma_t(t) * score
    dxdt = first_term - second_term
    return dxdt


# time points
t = np.linspace(0.001,25,10000)
batch_size = 30
noise_init = 0.001

# Initial conditions
x1_values = np.random.normal(1, noise_init, (batch_size,))
x1m_values = np.random.normal(-1, noise_init, (batch_size,))
x0_values = np.concatenate((x1_values, x1m_values))

# For each initial condition, solve the ODE and plot the solution
for x0 in x0_values:
    x = odeint(model, x0, t)
    plt.plot(t, x, 'orange')

plt.xlabel('t')
plt.ylabel('x')
plt.title('Trajectories of the ODE')
plt.show()

from edm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.