NVIDIA / sentiment-discovery

Unsupervised Language Modeling at scale for robust sentiment classification
Other
1.06k stars 202 forks source link

Visualisation issues? #21

Closed yashkumaratri closed 6 years ago

yashkumaratri commented 6 years ago

Hi,

I am trying to clone your heatmap function but I am failing , Can you please point out where I am wrong?

https://github.com/yashkumaratri/testrepo/blob/master/heatmap.ipynb

it'll be a great help.(Running on openai model)

raulpuric commented 6 years ago

Curious what the error was

yashkumaratri commented 6 years ago

Re wrote the function again and solved it

def plot_neuron_heatmap(text, values, n_limit=120, savename='fig1.png',cell_height=0.325, cell_width=0.15, dpi=200):
    text = text.replace('\n', '\\n')
    text = np.array(list(text + ' ' * (-len(text) % n_limit)))
    if len(values) > text.size:
        values = np.array(values[:text.size])
    else:
        t = values
        values = np.zeros(text.shape, dtype=np.float)
        values[:len(t)] = t
    text = text.reshape(-1, n_limit)
    values = values.reshape(-1, n_limit)
    mask = np.zeros(values.shape, dtype=np.bool)
    mask.ravel()[values.size:] = True
    mask = mask.reshape(-1, n_limit)
    plt.figure(figsize=(cell_width * n_limit, cell_height * len(text)))
    hmap = sns.heatmap(values, annot=text,mask=mask, fmt='', vmin=-3, vmax=3, cmap='RdYlGn', xticklabels=False, yticklabels=False, cbar=False)
    plt.subplots_adjust()
    plt.savefig(savename if savename else 'fig1.png', dpi=dpi)
raulpuric commented 6 years ago

Ahh so you were having an alignment problem between the length of the heatmap values and the output text. Glad to see you solved it.