Guest User

Untitled

a guest
Nov 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import types
  2. import tensorflow as tf
  3. import numpy as np
  4.  
  5. # Expressions are represented as lists of lists,
  6. # in lisp style -- the symbol name is the head (first element)
  7. # of the list, and the arguments follow.
  8.  
  9. # add an expression to an expression list, recursively if necessary.
  10. def add_expr_to_list(exprlist, expr):
  11. # if expr is a atomic type
  12. if isinstance(expr, list):
  13. # Now for rest of expression
  14. for e in expr[1:]:
  15. # Add to list if necessary
  16. if not (e in exprlist):
  17. add_expr_to_list(exprlist, e)
  18. # Add index in list.
  19. exprlist.append(expr)
  20.  
  21. def expand_subexprs(exprlist):
  22. new_exprlist = []
  23. orig_indices = []
  24. for e in exprlist:
  25. add_expr_to_list(new_exprlist, e)
  26. orig_indices.append(len(new_exprlist)-1)
  27. return new_exprlist, orig_indices
  28.  
  29. def compile_expr(exprlist, expr):
  30. # start new list starting with head
  31. new_expr = [expr[0]]
  32. for e in expr[1:]:
  33. new_expr.append(exprlist.index(e))
  34. return new_expr
  35.  
  36. def compile_expr_list(exprlist):
  37. new_exprlist = []
  38. for e in exprlist:
  39. if isinstance(e, list):
  40. new_expr = compile_expr(exprlist, e)
  41. else:
  42. new_expr = e
  43. new_exprlist.append(new_expr)
  44. return new_exprlist
  45.  
  46. def expand_and_compile(exprlist):
  47. l, orig_indices = expand_subexprs(exprlist)
  48. return compile_expr_list(l), orig_indices
  49.  
  50. def new_weight(N1,N2):
  51. return tf.Variable(tf.random_normal([N1,N2]))
  52. def new_bias(N_hidden):
  53. return tf.Variable(tf.random_normal([N_hidden]))
  54.  
  55. def build_weights(exprlist,N_hidden,inp_vec_len,out_vec_len):
  56. W = dict() # dict of weights corresponding to each operation
  57. b = dict() # dict of biases corresponding to each operation
  58. W['input'] = new_weight(inp_vec_len, N_hidden)
  59. W['output'] = new_weight(N_hidden, out_vec_len)
  60. for expr in exprlist:
  61. if isinstance(expr, list):
  62. idx = expr[0]
  63. if idx not in W.keys():
  64. W[idx] = [new_weight(N_hidden,N_hidden) for i in expr[1:]]
  65. b[idx] = new_bias(N_hidden)
  66. return (W,b)
  67.  
  68. def build_rnn_graph(exprlist,W,b,inp_vec_len):
  69. # with W built up, create list of variables
  70. # intermediate variables
  71. in_vars = [e for e in exprlist if not isinstance(e,list)]
  72. N_input = len(in_vars)
  73. inp_tensor = tf.placeholder(tf.float32, (N_input, inp_vec_len), name='input1')
  74. V = [] # list of variables corresponding to each expr in exprlist
  75. for expr in exprlist:
  76. if isinstance(expr, list):
  77. # intermediate variables
  78. idx = expr[0]
  79. # add bias
  80. new_var = b[idx]
  81. # add input variables * weights
  82. for i in range(1,len(expr)):
  83. new_var = tf.add(new_var, tf.matmul(V[expr[i]], W[idx][i-1]))
  84. new_var = tf.nn.relu(new_var)
  85. else:
  86. # base (input) variables
  87. # TODO : variable or placeholder?
  88. i = in_vars.index(expr)
  89. i_v = tf.slice(inp_tensor, [i,0], [1,-1])
  90. new_var = tf.nn.relu(tf.matmul(i_v,W['input']))
  91. V.append(new_var)
  92. return (inp_tensor,V)
  93.  
  94. # take a compiled expression list and build its RNN graph
  95. def complete_rnn_graph(W,V,orig_indices,out_vec_len):
  96. # we store our matrices in a dict;
  97. # the dict format is as follows:
  98. # 'op':[mat_arg1,mat_arg2,...]
  99. # e.g. unary operations: '-':[mat_arg1]
  100. # binary operations: '+':[mat_arg1,mat_arg2]
  101. # create a list of our base variables
  102. N_output = len(orig_indices)
  103. out_tensor = tf.placeholder(tf.float32, (N_output, out_vec_len), name='output1')
  104.  
  105. # output variables
  106. ce = tf.reduce_sum(tf.zeros((1,1)))
  107. for idx in orig_indices:
  108. o = tf.nn.softmax(tf.matmul(V[idx], W['output']))
  109. t = tf.slice(out_tensor, [idx,0], [1,-1])
  110. ce = tf.add(ce, -tf.reduce_sum(t * tf.log(o)), name='loss')
  111. # TODO: output variables
  112. # return weights and variables and final loss
  113. return (out_tensor, ce)
  114.  
  115.  
  116. # from subexpr_lists import *
  117. a = [ 1, ['+',1,1], ['*',1,1], ['*',['+',1,1],['+',1,1]], ['+',['+',1,1],['+',1,1]], ['+',['+',1,1],1 ], ['+',1,['+',1,1]]]
  118. # generate training graph
  119. l,o=expand_and_compile(a)
  120. W,b = build_weights(l,10,1,2)
  121. i_t,V = build_rnn_graph(l,W,b,1)
  122. o_t,ce = complete_rnn_graph(W,V,o,2)
  123. # generate testing graph
  124. a = [ ['+',['+',['+',1,1],['+',['+',1,1],['+',1,1]]],1] ] # 7
  125. l_tst,o_tst=expand_and_compile(a)
  126. i_t_tst,V_tst = build_rnn_graph(l_tst,W,b,1)
  127.  
  128. out_batch = np.transpose(np.array([[1,0,1,0,0,1,1],[0,1,0,1,1,0,0]]))
  129. print(ce)
  130. train_step = tf.train.GradientDescentOptimizer(0.001).minimize(ce)
  131. init = tf.initialize_all_variables()
  132. sess = tf.Session()
  133. sess.run(init)
  134. for i in range(5000):
  135. sess.run(train_step, feed_dict={i_t:np.array([[1]]),o_t:out_batch})
  136. print(l)
  137. print(l_tst)
  138. print(sess.run(tf.nn.softmax(tf.matmul(V[1], W['output'])), feed_dict={i_t:np.array([[1]])}))
  139. print(sess.run(tf.nn.softmax(tf.matmul(V[-1], W['output'])), feed_dict={i_t:np.array([[1]])}))
  140. print(sess.run(tf.nn.softmax(tf.matmul(V_tst[-2], W['output'])), feed_dict={i_t_tst:np.array([[1]])}))
  141. print(sess.run(tf.nn.softmax(tf.matmul(V_tst[-1], W['output'])), feed_dict={i_t_tst:np.array([[1]])}))
Add Comment
Please, Sign In to add comment