Can someone please alter this code in order to depend on pytorch rather than tensorflow

oussama_harrouche: <p><span class="hashtag">#Hyperparameter</span> Settings</p>
<h1>
<a name="common-hyperparameter-settings-1" class="anchor" href="#common-hyperparameter-settings-1"></a>Common hyperparameter settings</h1>
<p>n_hid = 500<br>
n_dim = 5<br>
n_layers = 2<br>
gk_size = 3</p>
<h1>
<a name="different-hyperparameter-settings-for-each-dataset-2" class="anchor" href="#different-hyperparameter-settings-for-each-dataset-2"></a>Different hyperparameter settings for each dataset</h1>
<p>if dataset == ‘ML-100K’:<br>
lambda_2 = 20.  # l2 regularisation<br>
lambda_s = 0.006<br>
iter_p = 5  # optimisation<br>
iter_f = 5<br>
epoch_p = 30  # training epoch<br>
epoch_f = 60<br>
dot_scale = 1  # scaled dot product</p>
<p>elif dataset == ‘ML-1M’:<br>
lambda_2 = 70.<br>
lambda_s = 0.018<br>
iter_p = 50<br>
iter_f = 10<br>
epoch_p = 20<br>
epoch_f = 30<br>
dot_scale = 0.5</p>
<p>elif dataset == ‘Douban’:<br>
lambda_2 = 10.<br>
lambda_s = 0.022<br>
iter_p = 5<br>
iter_f = 5<br>
epoch_p = 20<br>
epoch_f = 60<br>
dot_scale = 2<br>
R = tf.placeholder(“float”, [n_m, n_u])<br>
<span class="hashtag">#Network</span> Function<br>
def local_kernel(u, v):</p>
<pre><code>dist = tf.norm(u - v, ord=2, axis=2)
hat = tf.maximum(0., 1. - dist**2)

return hat
</code></pre>
<p>def kernel_layer(x, n_hid=n_hid, n_dim=n_dim, activation=tf.nn.sigmoid, lambda_s=lambda_s, lambda_2=lambda_2, name=‘’):</p>
<pre><code>with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
    W = tf.get_variable('W', [x.shape[1], n_hid])
    n_in = x.get_shape().as_list()[1]
    u = tf.get_variable('u', initializer=tf.random.truncated_normal([n_in, 1, n_dim], 0., 1e-3))
    v = tf.get_variable('v', initializer=tf.random.truncated_normal([1, n_hid, n_dim], 0., 1e-3))
    b = tf.get_variable('b', [n_hid])

w_hat = local_kernel(u, v)

sparse_reg = tf.contrib.layers.l2_regularizer(lambda_s)
sparse_reg_term = tf.contrib.layers.apply_regularization(sparse_reg, [w_hat])

l2_reg = tf.contrib.layers.l2_regularizer(lambda_2)
l2_reg_term = tf.contrib.layers.apply_regularization(l2_reg, [W])

W_eff = W * w_hat  # Local kernelised weight matrix
y = tf.matmul(x, W_eff) + b
y = activation(y)

return y, sparse_reg_term + l2_reg_term
</code></pre>
<p>def global_kernel(input, gk_size, dot_scale):</p>
<pre><code>avg_pooling = tf.reduce_mean(input, axis=1)  # Item (axis=1) based average pooling
avg_pooling = tf.reshape(avg_pooling, [1, -1])
n_kernel = avg_pooling.shape[1].value

conv_kernel = tf.get_variable('conv_kernel', initializer=tf.random.truncated_normal([n_kernel, gk_size**2], stddev=0.1))
gk = tf.matmul(avg_pooling, conv_kernel) * dot_scale  # Scaled dot product
gk = tf.reshape(gk, [gk_size, gk_size, 1, 1])

return gk
</code></pre>
<p>def global_conv(input, W):</p>
<pre><code>input = tf.reshape(input, [1, input.shape[0], input.shape[1], 1])
conv2d = tf.nn.relu(tf.nn.conv2d(input, W, strides=[1,1,1,1], padding='SAME'))

return tf.reshape(conv2d, [conv2d.shape[1], conv2d.shape[2]])
</code></pre>
<p><span class="hashtag">#Network</span> Instantiation<br>
<span class="hashtag">#Pre-training</span><br>
y = R<br>
reg_losses = None</p>
<p>for i in range(n_layers):<br>
y, reg_loss = kernel_layer(y, name=str(i))<br>
reg_losses = reg_loss if reg_losses is None else reg_losses + reg_loss</p>
<p>pred_p, reg_loss = kernel_layer(y, n_u, activation=tf.identity, name=‘out’)<br>
reg_losses = reg_losses + reg_loss</p>
<h1>
<a name="l2-loss-3" class="anchor" href="#l2-loss-3"></a>L2 loss</h1>
<p>diff = train_m * (train_r - pred_p)<br>
sqE = tf.nn.l2_loss(diff)<br>
loss_p = sqE + reg_losses</p>
<p>optimizer_p = tf.contrib.opt.ScipyOptimizerInterface(loss_p, options={‘disp’: True, ‘maxiter’: iter_p, ‘maxcor’: 10}, method=‘L-BFGS-B’)<br>
<span class="hashtag">#Fine-tuning</span><br>
y = R<br>
reg_losses = None</p>
<p>for i in range(n_layers):<br>
y, _ = kernel_layer(y, name=str(i))</p>
<p>y_dash, _ = kernel_layer(y, n_u, activation=tf.identity, name=‘out’)</p>
<p>gk = global_kernel(y_dash, gk_size, dot_scale)  # Global kernel<br>
y_hat = global_conv(train_r, gk)  # Global kernel-based rating matrix</p>
<p>for i in range(n_layers):<br>
y_hat, reg_loss = kernel_layer(y_hat, name=str(i))<br>
reg_losses = reg_loss if reg_losses is None else reg_losses + reg_loss</p>
<p>pred_f, reg_loss = kernel_layer(y_hat, n_u, activation=tf.identity, name=‘out’)<br>
reg_losses = reg_losses + reg_loss</p>
<h1>
<a name="l2-loss-4" class="anchor" href="#l2-loss-4"></a>L2 loss</h1>
<p>diff = train_m * (train_r - pred_f)<br>
sqE = tf.nn.l2_loss(diff)<br>
loss_f = sqE + reg_losses</p>
<p>optimizer_f = tf.contrib.opt.ScipyOptimizerInterface(loss_f, options={‘disp’: True, ‘maxiter’: iter_f, ‘maxcor’: 10}, method=‘L-BFGS-B’)<br>
<span class="hashtag">#Evaluation</span> code<br>
def dcg_k(score_label, k):<br>
dcg, i = 0., 0<br>
for s in score_label:<br>
if i &lt; k:<br>
dcg += (2<strong>s[1]-1) / np.log2(2+i)<br>
i += 1<br>
return dcg<br>
def ndcg_k(y_hat, y, k):<br>
score_label = np.stack([y_hat, y], axis=1).tolist()<br>
score_label = sorted(score_label, key=lambda d:d[0], reverse=True)<br>
score_label_ = sorted(score_label, key=lambda d:d[1], reverse=True)<br>
norm, i = 0., 0<br>
for s in score_label_:<br>
if i &lt; k:<br>
norm += (2</strong>s[1]-1) / np.log2(2+i)<br>
i += 1<br>
dcg = dcg_k(score_label, k)<br>
return dcg / norm<br>
def call_ndcg(y_hat, y):<br>
ndcg_sum, num = 0, 0<br>
y_hat, y = y_hat.T, y.T<br>
n_users = y.shape[0]</p>
<pre><code>for i in range(n_users):
    y_hat_i = y_hat[i][np.where(y[i])]
    y_i = y[i][np.where(y[i])]

    if y_i.shape[0] &lt; 2:
        continue

    ndcg_sum += ndcg_k(y_hat_i, y_i, y_i.shape[0])  # user-wise calculation
    num += 1

return ndcg_sum / num
</code></pre>
<p><span class="hashtag">#Training</span> and Test Loop<br>
best_rmse_ep, best_mae_ep, best_ndcg_ep = 0, 0, 0<br>
best_rmse, best_mae, best_ndcg = float(“inf”), float(“inf”), 0</p>
<p>time_cumulative = 0<br>
init = tf.global_variables_initializer()</p>
<p>with tf.Session() as sess:<br>
sess.run(init)<br>
for i in range(epoch_p):<br>
tic = time()<br>
optimizer_p.minimize(sess, feed_dict={R: train_r})<br>
pre = sess.run(pred_p, feed_dict={R: train_r})</p>
<pre><code>    t = time() - tic
    time_cumulative += t
    
    error = (test_m * (np.clip(pre, 1., 5.) - test_r) ** 2).sum() / test_m.sum()  # test error
    test_rmse = np.sqrt(error)

    error_train = (train_m * (np.clip(pre, 1., 5.) - train_r) ** 2).sum() / train_m.sum()  # train error
    train_rmse = np.sqrt(error_train)

    print('.-^-._' * 12)
    print('PRE-TRAINING')
    print('Epoch:', i+1, 'test rmse:', test_rmse, 'train rmse:', train_rmse)
    print('Time:', t, 'seconds')
    print('Time cumulative:', time_cumulative, 'seconds')
    print('.-^-._' * 12)

for i in range(epoch_f):
    tic = time()
    optimizer_f.minimize(sess, feed_dict={R: train_r})
    pre = sess.run(pred_f, feed_dict={R: train_r})

    t = time() - tic
    time_cumulative += t
    
    error = (test_m * (np.clip(pre, 1., 5.) - test_r) ** 2).sum() / test_m.sum()  # test error
    test_rmse = np.sqrt(error)

    error_train = (train_m * (np.clip(pre, 1., 5.) - train_r) ** 2).sum() / train_m.sum()  # train error
    train_rmse = np.sqrt(error_train)

    test_mae = (test_m * np.abs(np.clip(pre, 1., 5.) - test_r)).sum() / test_m.sum()
    train_mae = (train_m * np.abs(np.clip(pre, 1., 5.) - train_r)).sum() / train_m.sum()

    test_ndcg = call_ndcg(np.clip(pre, 1., 5.), test_r)
    train_ndcg = call_ndcg(np.clip(pre, 1., 5.), train_r)

    if test_rmse &lt; best_rmse:
        best_rmse = test_rmse
        best_rmse_ep = i+1

    if test_mae &lt; best_mae:
        best_mae = test_mae
        best_mae_ep = i+1

    if best_ndcg &lt; test_ndcg:
        best_ndcg = test_ndcg
        best_ndcg_ep = i+1

    print('.-^-._' * 12)
    print('FINE-TUNING')
    print('Epoch:', i+1, 'test rmse:', test_rmse, 'test mae:', test_mae, 'test ndcg:', test_ndcg)
    print('Epoch:', i+1, 'train rmse:', train_rmse, 'train mae:', train_mae, 'train ndcg:', train_ndcg)
    print('Time:', t, 'seconds')
    print('Time cumulative:', time_cumulative, 'seconds')
    print('.-^-._' * 12)
</code></pre>
<h1>
<a name="final-result-5" class="anchor" href="#final-result-5"></a>Final result</h1>
<p>print(‘Epoch:’, best_rmse_ep, ’ best rmse:‘, best_rmse)<br>
print(‘Epoch:’, best_mae_ep, ’ best mae:’, best_mae)<br>
print(‘Epoch:’, best_ndcg_ep, ’ best ndcg:', best_ndcg)</p>