def evaluate_similarity(args, token_embedding, ctx, logfile=None,<EOL>global_step=<NUM_LIT:0>):
results = []<EOL>for similarity_function in args.similarity_functions:<EOL><INDENT>evaluator = nlp.embedding.evaluation.WordEmbeddingSimilarity(<EOL>idx_to_vec=token_embedding.idx_to_vec,<EOL>similarity_function=similarity_function)<EOL>evaluator.initialize(ctx=ctx)<EOL>if not args.no_hybridize:<EOL><INDENT>evaluator.hybridize()<EOL><DEDENT>for (dataset_name, dataset_kwargs,<EOL>dataset) in iterate_similarity_datasets(args):<EOL><INDENT>initial_length = len(dataset)<EOL>dataset_coded = [[<EOL>token_embedding.token_to_idx[d[<NUM_LIT:0>]],<EOL>token_embedding.token_to_idx[d[<NUM_LIT:1>]], d[<NUM_LIT:2>]<EOL>] for d in dataset if d[<NUM_LIT:0>] in token_embedding.token_to_idx<EOL>and d[<NUM_LIT:1>] in token_embedding.token_to_idx]<EOL>num_dropped = initial_length - len(dataset_coded)<EOL>if not len(dataset_coded):<EOL><INDENT>correlation = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>words1, words2, scores = zip(*dataset_coded)<EOL>pred_similarity = evaluator(<EOL>mx.nd.array(words1, ctx=ctx), mx.nd.array(words2, ctx=ctx))<EOL>sr = stats.spearmanr(pred_similarity.asnumpy(),<EOL>np.array(scores))<EOL>correlation = sr.correlation<EOL><DEDENT>logging.info(<EOL>'<STR_LIT>',<EOL>dataset.__class__.__name__, len(dataset_coded),<EOL>str(dataset_kwargs), similarity_function, correlation)<EOL>result = dict(<EOL>task='<STR_LIT>',<EOL>dataset_name=dataset_name,<EOL>dataset_kwargs=dataset_kwargs,<EOL>similarity_function=similarity_function,<EOL>spearmanr=correlation,<EOL>num_dropped=num_dropped,<EOL>global_step=global_step,<EOL>)<EOL>log_similarity_result(logfile, result)<EOL>results.append(result)<EOL><DEDENT><DEDENT>return results<EOL>
Evaluate on specified similarity datasets.