Few-shot classifiers are trained on episodes that mimic the test situation: a few labeled support examples from a handful of classes, and queries to classify. A matching network predicts a query's label as an attention-weighted vote of the support labels. Implement:
sample_episode(y, n_way, k_shot, n_query, rng): y is an array of integer labels and rng a np.random.Generator. Draw the episode classes with rng.choice(np.unique(y), n_way, replace=False). Then for each episode class in that order, take the indices of that class in increasing order, permute them with rng.permutation, and use the first k_shot as support and the next n_query as queries. Return (support_idx, query_idx, classes) as Python lists, grouped by class in episode order.
matching_predict(f_query, g_support, y_support, classes): f_query is (Q, d) and g_support is (M, d) embeddings. The attention of query on support example is a softmax over the support set of cosine similarities, with . Return the (Q, len(classes)) array whose entry is .
episode_nll(probs, y_query, classes): the mean of over the queries, natural log, as a Python float.