Skip to content

Instantly share code, notes, and snippets.

@prl900
Created June 13, 2019 00:30
Show Gist options
  • Save prl900/18b0b1c9cfb2a4f23629053d775c3cf8 to your computer and use it in GitHub Desktop.
Save prl900/18b0b1c9cfb2a4f23629053d775c3cf8 to your computer and use it in GitHub Desktop.
import tensorflow as tf
import numpy as np
import pandas as pd
import imageio
im_orig = imageio.imread("cameraman.jpg")[::4,::4].astype(np.float32) / 255
im_orig_df = pd.DataFrame(im_orig)
im_df_masked = im_orig_df.copy()
im_df_masked.iloc[40,:]=np.nan
np_mask = im_df_masked.notnull()
imageio.imsave("in.png", im_df_masked.values)
# Boolean mask for computing cost only on valid (not missing) entries
tf_mask = tf.Variable(np_mask.values)
im = tf.constant(im_df_masked.values)
shape = im_df_masked.values.shape
#latent factors
rank = 12
# Initializing random H and W
temp_H = np.random.randn(rank, shape[1]).astype(np.float32)
temp_H = np.divide(temp_H, temp_H.max())
temp_W = np.random.randn(shape[0], rank).astype(np.float32)
temp_W = np.divide(temp_W, temp_W.max())
H = tf.Variable(temp_H)
W = tf.Variable(temp_W)
WH = tf.matmul(W, H)
#cost of Frobenius norm
cost = tf.reduce_sum(tf.pow(tf.boolean_mask(im, tf_mask) - tf.boolean_mask(WH, tf_mask), 2))
# Learning rate
lr = 0.001
train_step = tf.train.GradientDescentOptimizer(lr).minimize(cost)
init = tf.global_variables_initializer()
# Clipping operation. This ensure that W and H learnt are non-negative
clip_W = W.assign(tf.maximum(tf.zeros_like(W), W))
clip_H = H.assign(tf.maximum(tf.zeros_like(H), H))
clip = tf.group(clip_W, clip_H)
steps = 10000
with tf.Session() as sess:
sess.run(init)
for i in range(steps):
sess.run(train_step)
sess.run(clip)
if i%100==0:
print("\nCost: %f" % sess.run(cost))
print("*"*40)
learnt_W = sess.run(W)
learnt_H = sess.run(H)
pred = np.dot(learnt_W, learnt_H)
pred_df = pd.DataFrame(pred)
print(pred_df)
imageio.imsave("out.png", pred_df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment