pymc.join_非共享输入#
- pymc.join_nonshared_inputs(point, outputs, inputs, shared_inputs=None, make_inputs_shared=False)[源代码]#
创建新的输出和输入 TensorVariables,其中非共享输入被合并为一个展平的向量输入。
- 参数:
- 点 :
dict
of {str
: array_like}python:dict of {python:str}array_like} 映射每个输入变量名称到数值变量的字典。这些值用于提取每个输入变量的形状,以建立连接输入与原始输入之间的正确映射。假设每个变量的形状是固定的。
- 输出 :
list
的TensorVariable
python:TensorVariable 列表 输出 TensorVariables 列表,其非共享输入将被替换为连接的向量输入。
- 输入 :
list
的TensorVariable
python:TensorVariable 列表 将被替换为连接向量输入的输入 TensorVariables 列表。
- shared_inputspython:dict of {TensorVariable}TensorSharedVariable}, 可选
子图替换中TensorVariable及其关联的TensorSharedVariable的字典。
- make_inputs_shared : 布尔值,默认值
False
bool, 默认 python:False 是否将连接的向量输入设为共享变量。
- 点 :
- 返回:
- new_outputs :
list
的TensorVariable
python:TensorVariable 列表 依赖于 joined_inputs 和新共享变量作为输入的新输出 outputs TensorVariables 列表。
- joined_inputs
TensorVariable
new_outputs 的连接输入向量 TensorVariable
- new_outputs :
示例
连接一个简单 PyTensor 图的输入。
import pytensor.tensor as pt import numpy as np from pymc.pytensorf import join_nonshared_inputs # Original non-shared inputs x = pt.scalar("x") y = pt.vector("y") # Original output out = x + y print(out.eval({x: np.array(1), y: np.array([1, 2, 3])})) # [2, 3, 4] # New output and inputs [new_out], joined_inputs = join_nonshared_inputs( point={ # Only shapes matter "x": np.zeros(()), "y": np.zeros(3), }, outputs=[out], inputs=[x, y], ) print(new_out.eval({ joined_inputs: np.array([1, 1, 2, 3]), })) # [2, 3, 4]
加入模型 logp 的输入值变量。
import pymc as pm with pm.Model() as model: mu_pop = pm.Normal("mu_pop") sigma_pop = pm.HalfNormal("sigma_pop") mu = pm.Normal("mu", mu_pop, sigma_pop, shape=(3, )) y = pm.Normal("y", mu, 1.0, observed=[0, 1, 2]) print(model.compile_logp()({ "mu_pop": 0, "sigma_pop_log__": 1, "mu": [0, 1, 2], })) # -12.691227342634292 initial_point = model.initial_point() inputs = model.value_vars [logp], joined_inputs = join_nonshared_inputs( point=initial_point, outputs=[model.logp()], inputs=inputs, ) print(logp.eval({ joined_inputs: [0, 1, 0, 1, 2], })) # -12.691227342634292
与上述相同,但 mu_pop 值变量被共享。
from pytensor import shared mu_pop_input, *other_inputs = inputs shared_mu_pop_input = shared(0.0) [logp], other_joined_inputs = join_nonshared_inputs( point=initial_point, outputs=[model.logp()], inputs=other_inputs, shared_inputs={ mu_pop_input: shared_mu_pop_input }, ) print(logp.eval({ other_joined_inputs: [1, 0, 1, 2], })) # -12.691227342634292