【发布时间】:2021-08-17 00:26:26
【问题描述】:
我想将以下网络定义设为参数化。连续和离散列的数量因数据而异。我首先从带有 relu 激活的线性传递整个输入数据,在这种情况下是 110 维。我的数据的每个分类字段的输出根据之前的 one-hot 编码数据转换而变化。我需要为它们中的每一个定义一个 nn.Linear(110, number of encodings)。
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__(110)
self.lin1 = nn.Linear(110,110)
self.lin_numerical = nn.Linear(110, 6)
self.lin_cat_job = nn.Linear(110, 9)
self.lin_cat_sex = nn.Linear(110, 2)
self.lin_cat_incomeclass = nn.Linear(110, 7)
def forward(self, x):
x = torch.relu(self.lin1(x))
x_numerical = f.leaky_relu(self.lin_numerical(x))
x_cat1 = f.gumbel_softmax(self.lin_cat_job(x), tau=0.2)
x_cat2 = f.gumbel_softmax(self.lin_cat_sex(x), tau=0.2)
x_cat3 = f.gumbel_softmax(self.lin_cat_incomeclass(x), tau=0.2)
x_final = torch.cat((x_numerical, x_cat1, x_cat2, x_cat3),1)
return x_final
我已经设法更改了 init 部分,使用离散列输入,这是一个有序字典,它具有我的数据的每个分类字段的单热编码的名称和编号作为键和值,和 Continuous_columns,它只是一个包含连续列名称的列表。但我不知道如何编辑转发部分:
class Generator(nn.Module):
def __init__(self, input_dim, continuous_columns, discrete_columns):
super(Generator, self).__init__()
self._input_dim = input_dim
self._discrete_columns = discrete_columns
self._num_continuous_columns = len(continuous_columns)
self.lin1 = nn.Linear(self._input_dim, self._input_dim)
self.lin_numerical = nn.Linear(self._input_dim, self._num_continuous_columns)
for key, value in self._discrete_columns.items():
setattr(self, "lin_cat_{}".format(key), nn.Linear(self._input_dim, value))
def forward(self, x):
x = torch.relu(self.lin1(x))
x_numerical = f.leaky_relu(self.lin_numerical(x))
####
This is the problematic part
#####
return x
【问题讨论】:
标签: python neural-network pytorch