Loading a test set when extracting a d-vector - model wants the training set size

Viewed 18

So I have to extract a d-vector (d-vector is the averaged activation from the last hidden layer of this DNN) of the taught model. I pass in a checkpoint from the last epoch and want to create a list of vectors. My training set is 1000 but my testing one is 20.

x = torch.stack(array_of_test_items)
checkpoint = torch.load(path, map_location=torch.device('cpu'))

model = Model(length_of_the_test_item_array) #20
model.load_state_dict(checkpoint['state_dict']) #a .tar from torch.save
model.eval()
with torch.no_grad():
    dvectors = model.extractTheVector(x)

but it throws an error of :

size mismatch for classifier.proj.weight: copying a param with shape torch.Size([1000, 1024]) from checkpoint, the shape in current model is torch.Size([20, 1024]). size mismatch for classifier.proj.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([20]).

In my Model class:

def extractTheVector(self, x):
    batch_size, num_chunks, features  = x.shape
    x = x.reshape((batch_size * num_chunks, 1, features))
    embeddings = self.encoder(x)
    embeddings = embeddings.reshape(batch_size, num_chunks, -1)
    return self.classifier.generateVector(embeddings) #dvectors

in Classifier:

class Classifier(nn.Module):
    def __init__(self, num_speakers): #num speakers = 1000 or 20
        super(Classifier, self).__init__()
        self.fc1 = nn.Linear(1024, 1024) #inner layer
        self.proj = nn.Linear(1024, num_speakers)

    def generateVector(self, x):
        x = self.encode(x)
        x = torch.nn.functional.normalize(x, dim=-1)
        x = torch.mean(x, 1)
        return x

    def encode(self, x):
        return Fun.relu(self.fc1(x))

    def forward(self, x):
        x = self.encode(x)
        x = Fun.softmax(self.proj(x), dim=1)
        return x

Why is it acting as if it's telling it to keep on learning?

1 Answers

What did you expect? you have learned and stored a function that maps R1024 to R1000 and are trying to load its parameters onto a function that maps R1024 to R20.

You should not be loading the entire module but rather its fc1 submodule. Your class structure is not very well fit for your task, ideally you'd have the encoder (and all of its fluff) as a distinct module.

Related