【问题标题】:Is it possible to use a super class in this exemple, common to all the classes I created?在这个例子中是否可以使用我创建的所有类共有的超类?
【发布时间】:2020-07-14 16:13:33
【问题描述】:

我有所有这些非常相似的类,除了它们的 covar_module。我想知道是否可以创建一个通用的超类,它具有所有属性和功能,而子类只有 covar_module。我对python很陌生,所以我不知道synthax会怎样。

class RbfGP(ExactGP, GPyTorchModel):
    _num_outputs = 1  # to inform GPyTorchModel API

    def __init__(self, train_X, train_Y):
        # squeeze output dim before passing train_Y to ExactGP
        super().__init__(train_X, train_Y.squeeze(-1), GaussianLikelihood())
        self.mean_module = ConstantMean()
        self.covar_module = gpytorch.kernels.ScaleKernel(
            base_kernel= gpytorch.kernels.RBFKernel(ard_num_dims=train_X.shape[-1]),
        )
        self.to(train_X)  # make sure we're on the right device/dtype

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return MultivariateNormal(mean_x, covar_x)


class Matern12GP(ExactGP, GPyTorchModel):
    _num_outputs = 1  # to inform GPyTorchModel API

    def __init__(self, train_X, train_Y):
        # squeeze output dim before passing train_Y to ExactGP
        super().__init__(train_X, train_Y.squeeze(-1), GaussianLikelihood())
        self.mean_module = ConstantMean()
        self.covar_module = gpytorch.kernels.ScaleKernel(
            base_kernel=gpytorch.kernels.MaternKernel(nu=0.5, ard_num_dims=train_X.shape[-1]),
        )
        self.to(train_X)  # make sure we're on the right device/dtype

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return MultivariateNormal(mean_x, covar_x)


class Matern32GP(ExactGP, GPyTorchModel):
    _num_outputs = 1  # to inform GPyTorchModel API

    def __init__(self, train_X, train_Y):
        # squeeze output dim before passing train_Y to ExactGP
        super().__init__(train_X, train_Y.squeeze(-1), GaussianLikelihood())
        self.mean_module = ConstantMean()
        self.covar_module = gpytorch.kernels.ScaleKernel(
            base_kernel=gpytorch.kernels.MaternKernel(nu=1.5, ard_num_dims=train_X.shape[-1]),
        )
        self.to(train_X)  # make sure we're on the right device/dtype

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return MultivariateNormal(mean_x, covar_x)

【问题讨论】:

    标签: python superclass


    【解决方案1】:

    为什么不这样:

    class GP(ExactGP, GPyTorchModel):
        _num_outputs = 1  # to inform GPyTorchModel API
    
        def __init__(self, train_X, train_Y, base_kernel, **kwargs):
            # squeeze output dim before passing train_Y to ExactGP
            super().__init__(train_X, train_Y.squeeze(-1), GaussianLikelihood())
            self.mean_module = ConstantMean()
            self.covar_module = gpytorch.kernels.ScaleKernel(
                base_kernel=base_kernel(ard_num_dims=train_X.shape[-1], **kwargs),
            )
            self.to(train_X)  # make sure we're on the right device/dtype
    
        def forward(self, x):
            mean_x = self.mean_module(x)
            covar_x = self.covar_module(x)
            return MultivariateNormal(mean_x, covar_x)
    

    然后

    rbf_gp = GP(train_x, train_y, base_kernel=gpytorch.kernels.RBFKernel)
    
    matern_12_gp = GP(train_x, train_y, base_kernel=gpytorch.kernels.MaternKernel, nu=0.5)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2020-04-20
      相关资源
      最近更新 更多