In the deep belief net of Hinton, Osindero and Teh (2006) the class label is part of the top-level RBM. To classify an image they pass it up to the penultimate layer, then turn on each label unit in turn and compute the exact free energy of the resulting visible vector of the top RBM. The label whose vector has the lowest free energy is the network's answer. Implement this test procedure.
Implement classify_by_free_energy(X, recog, W_top, a_top, c_top, n_labels).
Up-pass. X has shape (N, D). recog is a list of (W, c) pairs from the bottom up; each maps a representation to . Use these probabilities (a deterministic up-pass, as in the paper's reported results). The result is the penultimate representation of width (if recog is empty, is X).
Top RBM. Its visible vector is : the penultimate values followed by n_labels label units, with only label on. W_top has shape (P + n_labels, T), a_top (length + n_labels) holds the visible biases and c_top (length ) the hidden biases. The free energy of a visible vector is
and the label distribution is with .
Output: a dict with "free_energy" (shape (N, n_labels)), "probs" (shape (N, n_labels)) and "pred" (the label with the lowest free energy for each case, the smallest index on ties).