Skip to content

Instantly share code, notes, and snippets.

@dokato
Last active June 18, 2020 00:01
Show Gist options
  • Save dokato/ac5961454c9979229ff07dd78729504d to your computer and use it in GitHub Desktop.
Save dokato/ac5961454c9979229ff07dd78729504d to your computer and use it in GitHub Desktop.
siamese.ipynb
import pickle
import itertools
import numpy as np
import tensorflow.keras as keras
with open('HackathonMetadata.pkl','rb') as ff:
metad = pickle.load(ff)
with open('HackathonData_Homology0.pkl','rb') as ff:
homol = pickle.load(ff)
def pairs_generator(batch_size = 1024):
iterator = itertools.combinations(range(homol['indata'].shape[0]), r=2)
bl1, bl2 = [], []
for e, i in enumerate(iterator):
bl1.append(i[0])
bl2.append(i[1])
if e % batch_size == 0 and e != 0:
yield [homol['indata'][bl1], homol['indata'][bl2]], homol['distdata'][bl1,bl2]
bl1 = []
bl2 = []
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Lambda
from datget import pairs_generator
input_shape = (328)
left_input = layers.Input(input_shape)
right_input = layers.Input(input_shape)
model = keras.Sequential()
model.add(layers.Dense(60, activation='sigmoid',))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(2, activation='sigmoid'))
encoded_l = model(left_input)
encoded_r = model(right_input)
norm = keras.layers.LayerNormalization(axis=1)
encoded_l = norm(encoded_l)
encoded_r = norm(encoded_r)
dist_output = layers.Dot(axes=1)([encoded_l, encoded_r])
prediction = layers.Dense(1,activation='sigmoid')(dist_output)
siamese_net = keras.Model(inputs=[left_input,right_input], outputs=prediction)
optimizer = keras.optimizers.Adam(lr = 0.00006)
siamese_net.compile(loss="binary_crossentropy",optimizer=optimizer)
epochs = 100
siamese_net.fit_generator(pairs_generator(), steps_per_epoch=1000, epochs= epochs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment