Hey guys,
A noob in pytorch here. My model planning for a task includes combining a feature extractor model which is a conv 1d model with multiple layers with a prediction model which is a stacked lstm layers. I am having a hard time understanding how to combine both these models while the initialization stages.
One of the sample models I checked initialized the feature extraction model in the main prediction model itself and then picked the inputs for the prediction model from the feature extractor.
Here is a noob attempt of how I am trying to do and probably not understanding if it is even right way to approach.
class PredictionModel(nn.Module):
def __init__(self, features, targets, nlayers,convlayers, hsize_pred,hsize_fe,dropout):
super().__init__()
FE = FeatureExtractor(features, convlayers, hsize_fe,dropout) # can I connect my FE model from # here in the main prediction model??
new_features = FE.size()[2] # this is of course throwing an error but I just planned to pick new features like this. is it even right approach??
layers = []
for _ in range(nlayers):
if len(layers) ==0:
layers.append(nn.LSTM(new_features,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
layers.append(nn.LSTM(hsize_pred,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
layers.append(nn.LSTM(hsize_pred,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
else:
layers.append(nn.LSTM(hsize_pred,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
layers.append(nn.LSTM(hsize_pred,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
layers.append(nn.LSTM(hsize_pred,hsize_pred))
layers.append(nn.Dropout(dropout))
layers.append(nn.Tanh())
layers.append(nn.Linear(445,targets))
self.model = nn.Sequential(*layers)
def forward(self,x):
self.x = x
return self.model(self.x)
class FeatureExtractor(nn.Module):
def __init__(self, features, convlayers, hsize_fe,dropout):
super().__init__()
# self.features = features
# self.targets = targets
# self.layers = nlayers
layers = []
for _ in range(convlayers):
if len(layers) == 0:
layers.append(nn.Conv1d(features, hsize_fe, 3))
layers.append(nn.BatchNorm1d(hsize_fe))
layers.append(nn.Dropout(dropout))
layers.append(nn.ReLU())
else:
layers.append(nn.Conv1d(hsize_fe, hsize_fe, 3))
layers.append(nn.BatchNorm1d(hsize_fe))
layers.append(nn.Dropout(dropout))
layers.append(nn.ReLU())
layers.append(nn.Linear(26,features))
self.model = nn.Sequential(*layers)
def forward(self,x):
self.x = x
return self.model(self.x)
Some questions are in the code comment section.
How to proceed further in this quest.
Any help is greatly helpful for me.
Thanks