0

As I said in the title I want to know which weights in the first layer of my mlp model are connected to a certain freature.

So let's say that i have an ordered list of feature names

feature_names = data.feat_names

and a matrix with a shape (number of samples, number of features) where each feature is represented by a column of a matrix.

X = data.predictors

Since the order is the same between feature_names and the X columns, the first feature contained in feature_names is represented by the first column in X and so on.

After that i use dataloader to load the training and test set (both obtained from X)

trainloader = torch.utils.data.DataLoader(list(zip(X_train, y_train)), batch_size=7, shuffle=True)
valloader = torch.utils.data.DataLoader(list(zip(X_val, y_val)), batch_size=1, shuffle=True)

At this point I train my mlp model defined by two hidden layers and that's it. The problem is that I'd like to know which are the values of the weights connected to a certain input variable "i".

enter image description here

For example, in picture above I may want to know which are the values of the weights that connect the feature x_1 to the network.

I proceeded in this way:

mlp.fc1.weight.numpy()[:,i]

where mlp is my model, fc1 is the first layer and "i" represents the feature for which I want to retrieve the weights. Is it the right solution? Basically with this last line of code I'm taking the i-th column of matrix of weights of the first mlp layer.

0 Answers0