<jupyter_start><jupyter_text><jupyter_code>def gen_weights(shape):
  return tf.Variable(tf.random_normal(shape))
def gen_bias(shape):
  return tf.Variable(tf.random_normal([shape]))
def flatten(in_):
  return tf.reshape(in_,[int(in_.shape[0]),in_.shape[1:].num_elements()])
def dense(in_,out):
  return tf.matmul(in_,gen_weights([int(in_.shape[-1]),out]))+gen_bias(out)
def convert_func(m):
  ret = []
  ret2 = []
  for i in m:
    for j in i: #len(i) == window_size
      ret.append(j.toarray().flatten())
    ret2.append(i[len(i)>>1].toarray().flatten())
  ret = np.array(ret).reshape([len(m),window_size,dict_len])
  ret2 = np.array(ret2).reshape([len(m),dict_len])
  return ret,ret2

x = tf.placeholder(tf.float32,[None,window_size*dict_len])
y = tf.placeholder(tf.float32,[None,dict_len])
def encoder(X,out_l):
  h = tf.nn.relu(dense(X,1024))
 # h = dense(h,128)
  out = dense(h,out_l)
  #h = dense(out,128)
  h = tf.nn.relu(dense(h,1024))
  fin = dense(h,dict_len)
  return fin,out
model,vec = encoder(x,128)
vec = tf.nn.sigmoid(vec)
print(model.shape,vec.shape)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels = y,logits = model))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss)
sess = tf.Session()
with tf.device('/gpu:0'):
  sess.run(tf.global_variables_initializer())
  for i in range(n_epoch):
      items = np.random.randint(0,len(X)-window_size,batch_size)
      x_input,y_input = convert_func(X[items])
      _,l = sess.run([optimizer,loss], feed_dict={x:x_input.reshape(batch_size,x_input.shape[1]*x_input.shape[2]),y:y_input.reshape(batch_size,y_input.shape[1])})
      if not i%print_interval:
        print("EPOCH:%d Loss:%f"%(i,l))
vec_dict = {}
def create_vec_dict():
  for i in tqdm.tqdm(range(len(X))):
    x_input,y_input = convert_func(np.array([X[i]]))
    vec_dict[inv_transform(y_input[0])] = sess.run(vec,feed_dict={x:x_input.reshape(1,x_input.shape[1]*x_input.shape[2])}).flatten()
create_vec_dict()

import pickle
with open("file-128.pkl","wb") as f:
  pickle.dump(vec_dict,f)
from google.colab import files
files.download("file-128.pkl")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18452935694315004, 'fraction_numerical': 0.023765144454799627, 'mean_word_length': 5.948220064724919, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '539148', 'license': 'MIT', 'n_tokens_mistral': 919, 'n_tokens_neox': 889, 'n_words': 114, 'path': 'Word2Vec.ipynb', 'repo_name': 'captain-pool/Word2Vec', 'size': '2146'}