def tsne_embedding(embeddings, reverse_dictionary, plot_only=<NUM_LIT>, second=<NUM_LIT:5>, saveable=False, name='<STR_LIT>', fig_idx=<NUM_LIT>):
import matplotlib.pyplot as plt<EOL>def plot_with_labels(low_dim_embs, labels, figsize=(<NUM_LIT>, <NUM_LIT>), second=<NUM_LIT:5>, saveable=True, name='<STR_LIT>', fig_idx=<NUM_LIT>):<EOL><INDENT>if low_dim_embs.shape[<NUM_LIT:0>] < len(labels):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if saveable is False:<EOL><INDENT>plt.ion()<EOL>plt.figure(fig_idx)<EOL><DEDENT>plt.figure(figsize=figsize)  <EOL>for i, label in enumerate(labels):<EOL><INDENT>x, y = low_dim_embs[i, :]<EOL>plt.scatter(x, y)<EOL>plt.annotate(label, xy=(x, y), xytext=(<NUM_LIT:5>, <NUM_LIT:2>), textcoords='<STR_LIT>', ha='<STR_LIT:right>', va='<STR_LIT>')<EOL><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>from sklearn.manifold import TSNE<EOL>from six.moves import xrange<EOL>tsne = TSNE(perplexity=<NUM_LIT:30>, n_components=<NUM_LIT:2>, init='<STR_LIT>', n_iter=<NUM_LIT>)<EOL>low_dim_embs = tsne.fit_transform(embeddings[:plot_only, :])<EOL>labels = [reverse_dictionary[i] for i in xrange(plot_only)]<EOL>plot_with_labels(low_dim_embs, labels, second=second, saveable=saveable, name=name, fig_idx=fig_idx)<EOL><DEDENT>except ImportError:<EOL><INDENT>_err = "<STR_LIT>"<EOL>tl.logging.error(_err)<EOL>raise ImportError(_err)<EOL><DEDENT>
Visualize the embeddings by using t-SNE.

    Parameters
    ----------
    embeddings : numpy.array
        The embedding matrix.
    reverse_dictionary : dictionary
        id_to_word, mapping id to unique word.
    plot_only : int
        The number of examples to plot, choice the most common words.
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> see 'tutorial_word2vec_basic.py'
    >>> final_embeddings = normalized_embeddings.eval()
    >>> tl.visualize.tsne_embedding(final_embeddings, labels, reverse_dictionary,
    ...                   plot_only=500, second=5, saveable=False, name='tsne')