python中inf_Python numpy.inf方法代码示例

该段代码展示了如何在 Python 中使用 numpy 库的 inf 方法,特别是在构建和验证核心集(coreset)的过程中。代码导入了 autograd.numpy 模块,用以创建和操作数据,并检查核心集的一系列属性,如权重的单调性、误差计算的准确性等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 需要导入模块: from autograd import numpy [as 别名]

# 或者: from autograd.numpy import inf [as 别名]

def coreset_single(N, D, dist, algn):

#sys.stderr.write('n: ' + str(N) + ' d: ' +str(D) + ' dist: ' + str(dist) + ' salgn: ' + str(algn) + '\n')

x, mu0, Sig0, Sig = gendata(N, D, dist)

Sig0inv = np.linalg.inv(Sig0)

Siginv = np.linalg.inv(Sig)

mup, Sigp = weighted_post(mu0, np.linalg.inv(Sig0), np.linalg.inv(Sig), x, np.ones(x.shape[0]))

anm, alg = algn

coreset = alg(x, mu0, Sig0, Sig)

#incremental M tests

prev_err = np.inf

for m in range(1, N+1):

coreset.build(m)

muw, Sigw = weighted_post(mu0, Sig0inv, Siginv, x, coreset.weights())

w = coreset.weights()

#check if coreset for 1 datapoint is immediately optimal

if x.shape[0] == 1:

assert np.fabs(w - np.array([1])) < tol, anm +" failed: coreset not immediately optimal with N = 1. weights: " + str(coreset.weights()) + " mup = " + str(mup) + " Sigp = " + str(Sigp) + " muw = " + str(muw) + " Sigw = " + str(Sigw)

#check if coreset is valid

assert (w > 0.).sum() <= m, anm+" failed: coreset size > m"

assert (w > 0.).sum() == coreset.size(), anm+" failed: sum of coreset.weights()>0 not equal to size(): sum = " + str((coreset.weights()>0).sum()) + " size(): " + str(coreset.size())

assert np.all(w >= 0.), anm+" failed: coreset has negative weights"

#check if actual output error is monotone

err = weighted_post_KL(mu0, Sig0inv, Siginv, x, w, reverse=True if 'Reverse' in anm else False)

assert err - prev_err < tol, anm+" failed: error is not monotone decreasing, err = " + str(err) + " prev_err = " +str(prev_err)

#check if coreset is computing error properly

assert np.fabs(coreset.error() - err) < tol, anm+" failed: error est is not close to true err: est err = " + str(coreset.error()) + ' true err = ' + str(err)

prev_err = err

#save incremental M result

w_inc = coreset.weights()

#check reset

coreset.reset()

err = weighted_post_KL(mu0, Sig0inv, Siginv, x, np.zeros(x.shape[0]), reverse=True if 'Reverse' in anm else False)

assert coreset.M == 0 and np.all(np.fabs(coreset.weights()) == 0.) and np.fabs(coreset.error() - err) < tol and not coreset.reached_numeric_limit, anm+" failed: reset() did not properly reset"

#check build up to N all at once vs incremental

#do this test for all except bin, where symmetries can cause instabilities in the choice of vector / weights

if dist != 'bin':

coreset.build(N)

w = coreset.weights()

err = weighted_post_KL(mu0, Sig0inv, Siginv, x, w, reverse=True if 'Reverse' in anm else False)

err_inc = weighted_post_KL(mu0, Sig0inv, Siginv, x, w_inc, reverse=True if 'Reverse' in anm else False)

assert np.sqrt(((w - w_inc)**2).sum()) < tol, anm+" failed: incremental buid up to N doesn't produce same result as one run at N : \n error = " +str(err) + " error_inc = " + str(err_inc)

#check if coreset with all_data_wts is optimal

coreset._update_weights(coreset.all_data_wts)

assert coreset.error() < tol, anm + " failed: coreset with all_data_wts does not have error 0"

搭建无人机import gym from gym import spaces import itertools import numpy as np import dynamic_uav_new as dy # 假设有一个无人机动力学模型 import copy import math from math_tool import * import matplotlib.backends.backend_agg as agg import matplotlib.pyplot as plt import matplotlib.transforms as transforms import matplotlib.cm as cm from PIL import Image import matplotlib.image as mpimg import random class UAVEnv(): def __init__(self, boundary, length=2, num_obstacle=3, num_agents=4): self.length = length # 边界长度 self.num_obstacle = num_obstacle #障碍物数量 self.num_agents = num_agents #总智能体数量(3无人机+1目标) self.boundary = boundary #边界坐标 self.time_step = 0.1 # 步长 #运动参数 self.v_max = 0.1#无人机最大速度 self.v_max_e = 0.05 #目标最大速度 self.a_max = 0.04 #无人机加速度限制 self.a_max_e = 0.05 #目标加速度限制 self.L_sensor = 0.2 # 激光雷达最大探测距离 self.num_lasers = 16 # 激光束数量 self.multi_current_lasers = [[self.L_sensor for _ in range(self.num_lasers)] for _ in range(self.num_agents)] self.agents = ['agent_0', 'agent_1', 'agent_2', 'target'] self.info = np.random.get_state() # 随机种子 self.obstacles = [obstacle() for _ in range(self.num_obstacle)] self.history_positions = [[] for _ in range(num_agents)] # 定义动作空间 self.action_space = { 'agent_0': spaces.Box(low=-np.inf, high=np.inf, shape=(2,)), 'agent_1': spaces.Box(low=-np.inf, high=np.inf, shape=(2,)), 'agent_2': spaces.Box(low=-np.inf, high=np.inf, shape=(2,)), 'target': spaces.Box(low=-np.inf, high=np.inf, shape=(2,)) } # action represents [a_x,a_y] self.observation_space = { 'agent_0': spaces.Box(low=-np.inf, high=np.inf, shape=(26,)), 'agent_1': spaces.Box(low=-np.inf, high=np.inf, shape=(26,)), 'agent_2': spaces.Box(low=-np.inf, high=np.inf, shape=(26,)), 'target': spaces.Box(low=-np.inf, high=np.inf, sha模拟环境
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值