Skip to content

Instantly share code, notes, and snippets.

View shshemi's full-sized avatar

Shayan Hashemi shshemi

  • Finland
View GitHub Profile
@shshemi
shshemi / notebook.ipynb
Last active February 3, 2024 23:44
Solving the knapsack problem using neural networks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def rand_img(size):
return np.random.randint(0, 256, size) / 255.0
def rand_sentence(len, dict_len):
return np.random.randint(0, dict_len, len)
def data_generator(image_size, sentence_len, dict_len, batch_size=32):
while True:
x_img = np.zeros((batch_size, image_size[0], image_size[1], image_size[2]))
x_sen = np.zeros((batch_size, sentence_len))
y_img = np.zeros((batch_size, image_size[0], image_size[1], image_size[2]))
y_sen = np.zeros((batch_size, sentence_len, dict_len))
for i in range(batch_size):
img = rand_img(image_size)
sentence = rand_sentence(sentence_len, dict_len)
sentence_onehot = onehot(sentence, dict_len)
image_shape = (100, 100, 3)
sentence_len = 100
dict_len = 200
gen = data_generator(image_shape, sentence_len, dict_len, 32)
model, encoder, decoder = get_model(image_shape, sentence_len, dict_len)
model.fit_generator(gen, 512, 348, callbacks=[
ModelCheckpoint("best_weights.h5",monitor="loss",
verbose=1,
save_weights_only=True,
save_best_only=True)
model.load_weights("best_weights.h5")
img = np.expand_dims(img_to_array(load_img("tony_stark.jpg")) / 255.0, axis=0)
sen = ascii_encode('Anthony Edward "Tony" Stark is a character portrayed by Robert Downey Jr. in the MCU film franchise', sentence_len)
y_img = encoder.predict([img, sen])
y_sen = decoder.predict(y_img)
dec_sen = ascii_decode(y_sen)
def get_model(image_shape, sentence_len, dict_len):
# the encoder part
input_img = Input(image_shape)
input_sen = Input((sentence_len,))
embed_sen = Embedding(dict_len, 100)(input_sen)
embed_sen = Flatten()(embed_sen)
embed_sen = Reshape((image_shape[0], image_shape[1], 1))(embed_sen)
convd_img = Conv2D(20, 1, activation="relu")(input_img)
cat_tenrs = Concatenate(axis=-1)([embed_sen, convd_img])
def ascii_encode(message, sentence_len):
sen = np.zeros((1, sentence_len))
for i, a in enumerate(message.encode("ascii")):
sen[0, i] = a
return sen
def ascii_decode(message):
return ''.join(chr(int(a)) for a in message[0].argmax(-1))
def brute_force_knapsack(x_weights, x_prices, x_capacity):
item_count = x_weights.shape[0]
picks_space = 2 ** item_count
best_price = -1
best_picks = np.zeros(item_count)
for p in range(picks_space):
picks = [int(c) for c in f"{p:0{item_count}b}"]
price = np.dot(x_prices, picks)
weight = np.dot(x_weights, picks)
if weight <= x_capacity and price > best_price:
def metric_overprice(input_prices):
def overpricing(y_true, y_pred):
y_pred = K.round(y_pred)
return K.mean(K.batch_dot(y_pred, input_prices, 1) - K.batch_dot(y_true, input_prices, 1))
return overpricing