# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import tensorflow as tf
import random
import numpy as np
path = 'users.dat'

input_vect_size = 0
with open(path, 'rb') as input_file:
    for line in input_file:
        line = line.strip()
        line = line.split()[1:]
        for item in line:
            if int(item) > input_vect_size:
                input_vect_size = int(item)
            

with open(path, 'rb') as input_file:
    train_set = []
    val_set = []
    for line in input_file:
        line = line.strip()
        line = line.split()i
        #num_saved_items = int(line[0])
        #indices = []
        
        full_data = []
        for i in range(1, len(line)):
            full_data.append(int(i))
        training_data = get_training_data(np.array(full_data))
        val_set.append(make_sparse_from_raw_set(full_data, input_vect_size))
        train_set.append(make_sparse_from_raw_set(training_data, input_vect_size))
        
            #indices.append([0, int(line[i]) - 1])
        #values = [1] * num_saved_items
        #shape = [1, input_vect_size]
        #sv = tf.SparseTensor(indices=indices, values=values, shape=shape)

# Need to get the sampled on from test set
# -

import numpy as np
sess = tf.Session()
dv = tf.sparse_tensor_to_dense(sv)
res = sess.run(dv)


a = np.where(res > 0)[1]


def make_sparse_from_raw_set(raw_set, size):
    indices = [[0, i - 1] for i in raw_set]
    values = [1] * len(indices)
    shape = [1, size]
    #return [indices, values, shape]
    return tf.SparseTensor(indices=indices, values=values, shape=shape)


def get_training_data(full_dataset_one_d):
    '''Input: 1-D vector of just the item numbers that are included in
        in the full dataset
       Output: 2-D with the first dimension holding the test set that
               omits 20% of the data, and the second dimension including
               the full set. In this application the validation set is
               the full set.
    '''
    #TODO - Might need to check that training set statisfies a min length
    full_set = list(full_dataset_one_d)
    ind = np.random.randint(1, len(full_dataset_one_d), len(full_dataset_one_d))
    train_set_ind = np.where(ind <= 0.8 * len(full_dataset_one_d))[0]
    train_set = full_set[train_set_ind]
    
    return train_set



b, c = get_validation_set(a)

b

len_b = b.shape[0]
len_a = a.shape[0]
print float(len_b) / len_a

import tensorflow as tf
W_init_val = [tf.constant(range(i, i+2), dtype=tf.float32) for i in range(1, 7, 2)]
W_prime_init_val = [tf.constant(range(i, i+2), dtype=tf.float32) for i in range(7, 13, 2)]
b_prime_init_val = [tf.constant(2.0, dtype=tf.float32) for _ in xrange(3)]

# +
import tensorflow as tf
sess = tf.Session()
optimizer = tf.train.GradientDescentOptimizer(0.01)

y_0 = [[1, 0, 1]]
y = tf.placeholder(tf.float32, [None, 3])

# Test Gradients
weight = dict()
# weight['W'] = [tf.Variable(seq) for seq in W_init_val]
# weight['W_prime'] = [tf.Variable(seq) for seq in W_prime_init_val]
# weight['b'] = tf.Variable(tf.constant([[1.0, 1.0]], dtype=tf.float32))
# weight['b_prime'] = [tf.Variable(seq) for seq in b_prime_init_val]

#tf.pack(weight['W_prime'])[0]
weight['W'] = tf.Variable(W_init_val, name='W') 
weight['W_prime'] = tf.Variable(W_prime_init_val, name='W_prime')
weight['b'] = tf.Variable(tf.constant([[1.0, 1.0]], dtype=tf.float32), name='b')
weight['b_prime'] = tf.Variable(b_prime_init_val, dtype=tf.float32, name='b_prime') 


z = tf.add(tf.matmul(y, weight['W']), weight['b'])
y_hat = tf.add(tf.matmul(z, tf.transpose(weight['W_prime'])), weight['b_prime'])

loss_0 = tf.nn.l2_loss(tf.sub(y, y_hat))
# #loss_1 = tf.nn.l2_loss(tf.sub(y, y_hat))
# #cost = 0.5 * tf.reduce_sum(tf.nn.l2_loss(tf.sub(y, y_hat)))


# # W_var_before = [sess.run(var) for var in weight['W']]
# # W_prime_var_before = [sess.run(var) for var in weight['W_prime']]

# #Gradient
# #dw_0 = tf.gradients(loss_0, [weight['W'][0]])[0]
# #dw_prime_0 = tf. gradients(loss_0, [weight['W_prime'][0]])[0]
dw_prime_x = optimizer.compute_gradients(loss_0, var_list=[weight['W'], weight['W_prime'], weight['b'], weight['b_prime']])[1][0] #var_list=[weight['W_prime']])[0][0]
# #dw_1 = tf.gradients(loss_0, [weight['W'][1]])[0]
# #dw_prime_1 = tf. gradients(loss_0, [weight['W_prime'][1]])[0]

# #W_var = [sess.run(var) for var in weight['W']]
# #W_prime_var = [sess.run(var) for var in weight['W_prime']]

# #z_out, y_hat_out, loss, grad_w0, grad_w_prime0, grad_w1, grad_w_prime1 = sess.run((z, y_hat, loss_0, dw_0, dw_prime_0, dw_1, dw_prime_1), feed_dict={y: y_0})
sparse_update = tf.scatter_update(weight['W_prime'], [0, 2], tf.gather(dw_prime_x, [0, 2]))
# # #normalized_weights = [tf.nn.l2_normalize(var, 0) for var in weight.iteritems()]
# # #w_sum = tf.reduce_sum(normalized_weights)
sess.run(tf.initialize_all_variables())
#a = sess.run(dw_prime_x, feed_dict={y: y_0})[1][0]
W_prime_var_before = sess.run(weight['W_prime'])
# print sess.run(dw_prime_x, feed_dict={y: y_0})
sess.run(sparse_update, feed_dict={y: y_0})
# # #reg_sum = sess.run(w_sum)
W_prime_var = sess.run(weight['W_prime'])
# -

a

print W_prime_var_before
print ''
print W_prime_var

# +
l = 0.1
loss = tf.nn.l2_loss(tf.sub(y, y_hat))

dloss_d = lambda var: optimizer.compute_gradients(loss, var_list=[var])[0][0]
dCost_d = lambda var: dloss_d(var) - tf.mul(l, var)

grad_op = lambda var: optimizer.apply_gradients([[dCost_d(var), var]])
W_var_before = [sess.run(var) for var in weight['W']]
#cost = lambda var: loss - tf.mul(l, var)
#cost = 0.5 * tf.reduce_sum([tf.nn.l2_loss(vect) for vect in tf.sub(z_0, z)])
#cost = 0.5 * tf.reduce_sum(tf.nn.l2_loss(tf.sub(z_0, z)))
sess.run(grad_op(weight['W'][0]), feed_dict={y: y_0})
W_var = [sess.run(var) for var in weight['W']]
# -

W_var_before

W_var

StarcoderdataJupyterScriptsDedupFiltered