<jupyter_start><jupyter_text>Add Final Layer<jupyter_code>#Add final layers
bottleneck_tensor = sess.graph.get_tensor_by_name(ensure_name_has_port(BOTTLENECK_TENSOR_NAME))
layer_weights = tf.Variable(
  tf.truncated_normal([2048, class_count], stddev=0.001),
  name='final_weights')
layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
logits = tf.matmul(bottleneck_tensor, layer_weights) + layer_biases
a = tf.nn.softmax(logits, name=final_tensor_name)
ground_truth_placeholder = tf.placeholder(tf.float32,
                                        [None, class_count],
                                        name=ground_truth_tensor_name)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
  logits, ground_truth_placeholder)
cross_entropy_mean = tf.reduce_mean(cross_entropy)
x = [(a.name, a.values()) for a in sess.graph.get_operations()]
for layer, values in x[:20]:
    print layer
    #res = values[0].eval()
    
#pool3_tensor = sess.graph.get_tensor_by_name(':0')
image = to_rgb(image_hdf5['Chunk5'][0][:,:,0])
print np.array(image).shape
plt.imshow(image, cmap=mpl.cm.gray)

# x = [(a.name, a.values()) for a in sess.graph.get_operations()]
# for layer, values in x[:20]:
#     print layer

#sess.run(pool3_tensor, {'ExpandDims:0': a})
date_to_pool3_scores = {}
date_to_softmax_scores = {}
pool3_tensor = sess.graph.get_tensor_by_name('pool_3:0')

for era in date_to_images:
    print era
    date_to_pool3_scores[era] = {}
    date_to_softmax_scores[era] = {}

    for img in date_to_images[era]:
        idx = date_to_images[era][img]
        
        chk = idx/5000
        chunk = "Chunk" + str(chk)
        i = idx % 5000
        if chunk not in image_hdf5: continue

        a = [to_rgb(image_hdf5[chunk][i][:,:,0])]
        predictions = sess.run(pool3_tensor, {'ExpandDims:0': a})[:,0,0,:]
        date_to_pool3_scores[era][img] = predictions[0]
        
        scores = np.dot(predictions, fw) + fb
        scores = np.array([softmax(scores[j]) for j in range(scores.shape[0])])
        date_to_softmax_scores[era][img] = scores[0]

pickle.dump((date_to_pool3_scores, date_to_softmax_scores), open("/data/decorations_by_date/pool3_softmax_scores.pkl", 'w'))
date_to_pool3_scores
date_to_softmax_scores
import pandas as pd
import seaborn as sns
# learn the order of labels
date_to_softmax_arr = {}
for era in date_to_softmax_scores:
    date_to_softmax_arr[era] = pd.DataFrame.from_dict(date_to_softmax_scores[era]).mean(axis=1)
date_to_softmax_arr = pd.DataFrame.from_dict(date_to_softmax_arr)
date_to_softmax_arr
labels = ["1850-1869", "1800-1849", "1870-1889", "1750-1799", "1700-1749", "pre-1700", "post-1890"]
labels_sorted = ["pre-1700", "1700-1749", "1750-1799", "1800-1849", "1850-1869", "1870-1889", "post-1890"]
labels_idx_pos = [labels]
date_to_softmax_arr = date_to_softmax_arr[labels]
date_to_softmax_arr
sns.heatmap(date_to_softmax_arr)  # we can see that the closer the dates the more likely things are to cluster
# heatmap it, rank neurons from low->high to high->low
date_to_pool3_arr = {}
for era in date_to_softmax_scores:
    date_to_pool3_arr[era] = pd.DataFrame.from_dict(date_to_pool3_scores[era]).mean(axis=1)
date_to_pool3_arr = pd.DataFrame.from_dict(date_to_pool3_arr)
date_to_pool3_arr = date_to_pool3_arr[labels_sorted]
diff = date_to_pool3_arr["post-1890"] - date_to_pool3_arr["pre-1700"]
diff.sort_values(inplace=True)
sns.tsplot(diff)
plt.xlabel("Ranked neurons")
plt.ylabel("1890 activation - 1700 activation")
sns.heatmap(date_to_pool3_arr.iloc[diff.index[:100]])
plt.title("100 filters whose activations decrease most over time")
date_to_pool3_arr.iloc[diff.index[:5]]
sns.heatmap(date_to_pool3_arr.iloc[diff.index[-100:]])
plt.title("100 filters whose activations increase most over time")
date_to_pool3_arr.iloc[diff.index[-5:]]
sns.tsplot(date_to_pool3_arr.iloc[diff.index[-5:]].iloc[0])
sns.tsplot(date_to_pool3_arr.iloc[diff.index[-5:]].iloc[1])
sns.tsplot(date_to_pool3_arr.iloc[diff.index[-5:]].iloc[2])
# can do clustering so that we can see which neurons are most activated for each era

# what other layers are there that we could look at?<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1325156325156325, 'fraction_numerical': 0.0481000481000481, 'mean_word_length': 5.1071953010279, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '665436', 'license': 'MIT', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1589, 'n_words': 272, 'path': 'scripts/visualize_neurons_by_decade.ipynb', 'repo_name': 'ludazhao/art_history_net', 'size': '4158'}