<jupyter_start><jupyter_text>Pick an easy example and do it in Numpy<jupyter_code>f1 = np.array([[1.,2.,3.],[4.,5.,6.]])
f1
f2 = np.array([[7.,8.,9.],[10.,11.,12.]])
f2
np.sum(f1*f2)
np.matmul(f1.flatten(),np.transpose(f2.flatten()))
F = np.array([f1,f2])
print F.shape
F
np.sum(np.square(f2.flatten()))
F_flat = F.reshape(F.shape[0],F.shape[1]*F.shape[2])
F_flat
np.matmul(F_flat,F_flat.T)
np.dot(F_flat,F_flat.T)<jupyter_output><empty_output><jupyter_text>slightly tougher example<jupyter_code>f1 = np.array([[1.,2.],[3.,4.],[5.,6.]])
f2 = np.array([[7.,8.],[9.,10.],[11.,12.]])
f3 = np.array([[13.,14.],[15.,16.],[17.,18.]])
F = np.array([f1,f2,f3])
F.shape
F_flat = F.reshape(F.shape[0],F.shape[1]*F.shape[2])
F_flat
F_flat.T
np.matmul(F_flat,F_flat.T)
np.dot(F_flat,F_flat.T)<jupyter_output><empty_output><jupyter_text>TensorFlow<jupyter_code>def Gram_Matrix(F_batch):
    F = F_batch[0]
    s = tf.shape(F)
    F_flat = tf.reshape(F,shape=(s[0],s[1]*s[2]))
    G = tf.matmul(F_flat,tf.transpose(F_flat))
    return G
g = tf.Graph()
with g.as_default():
    F = tf.placeholder(tf.float32)
    G = Gram_Matrix(F)
sess = tf.Session(graph = g)
writer = tf.train.SummaryWriter('./logs/',graph=g)

F_in = np.array([[f1,f2,f3]])
fd = {F : F_in}
sess.run(G,feed_dict=fd)<jupyter_output>WARNING:tensorflow:From <ipython-input-18-1126d643465b>:2 in <module>.: __init__ (from tensorflow.python.training.summary_io) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.FileWriter. The interface and behavior is the same; this is just a rename.<jupyter_text>With Keras<jupyter_code>from keras import backend as K
def gram_matrix(x):
    features = K.batch_flatten(K.permute_dimensions(x,(2,0,1)))
    gram = K.dot(features, K.transpose(features))
    return gram
with g.as_default():
    F = tf.placeholder(tf.float32)
    G_keras = gram_matrix(F)
# sess = tf.Session(graph = g)
# writer = tf.train.SummaryWriter('./logs/',graph=g)

F_in = np.array([[f1,f2,f3]])
fd = {F : F_in}
sess.run(G_keras,feed_dict=fd)
F_in.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2017167381974249, 'fraction_numerical': 0.04911778731521221, 'mean_word_length': 5.3003003003003, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '48207', 'license': 'MIT', 'n_tokens_mistral': 977, 'n_tokens_neox': 935, 'n_words': 139, 'path': 'Gram matrix.ipynb', 'repo_name': 'tmulc18/ArtisticStyle', 'size': '2097'}