TensorFlowã®RNNå®è£
ã¯ãµã³ãã«ãå°ãªãã
ãã¤ãããã£ããµã³ãã«ã³ã¼ãã§ã¯ã
éå®ãããä¸é¨ã®APIãã使ã£ã¦ããªããªã©å
¨ä½ã網ç¾
ãã¥ããæããããã®ã§ã
ãªãã¹ãå
¨ä½æãæãåºããããããã«ãèªåç¨ã«ã¡ã¢ã
ï¼ã¨è¨ãå²ã«åºæ¬çãªAPIãã使ã£ã¦ãªãã...)
TensorFlowã®RNNã®APIã使ããªãRNNã®å®è£
ã¾ãã¯2(ã¿ã¤ã )ã¹ãããã ãåä½ããRNNãã¤ããã
åã¹ãããã§å
¥ãããã¼ã¿ãX0, X1ã¨ãã¦placeholderãã¤ããã
åã¹ãããã§ã¯ãå
¥åã¦ãããæ°(=4)ã«ããããã¼ã¿ãç¨æã
ä»åã¯é ã層ã¯1ã¤ã¨ãã¦ã
å¯ä¸ã®é ã層ã®ã¦ãããæ°ã¯6ã
n_inputs = 4 n_neurons = 6 X0 = tf.placeholder(tf.float32, [None, n_inputs]) X1 = tf.placeholder(tf.float32, [None, n_inputs]) Wx = tf.Variable(tf.random_normal(shape=[n_inputs, n_neurons],dtype=tf.float32)) Wy = tf.Variable(tf.random_normal(shape=[n_neurons,n_neurons],dtype=tf.float32)) b = tf.Variable(tf.zeros([1, n_neurons], dtype=tf.float32)) Y0 = tf.tanh(tf.matmul(X0, Wx) + b) Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b) init = tf.global_variables_initializer()
ãããããã¯4ã¤ãã¼ã¿ãå«ãã
# t = 0 X0_batch = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) # t = 1 X1_batch = np.array([[15, 14, 13, 12], [0, 0, 0, 0], [8, 7, 6, 5], [4, 3, 2, 1]]) with tf.Session() as sess: init.run() Y0_eval, Y1_eval = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) print(Y0_eval) print(Y1_eval)
tf.contrib.rnn.static_rnn()ã使ãå®è£
ä¸è¨å®è£
ã¯ã·ã³ãã«ã ãã
ã¹ãããæ°ãå¢ãããã¨ããã¨ãããã¯ã¼ã¯ãè¤éã«ãªã£ã¦ããã
ããã§TensorFlowã®static_rnnã¨ããAPIã使ãã
static_rnnã使ãã¨ãTensorFlowã§ç¨æãããRNNã®cellãrollingããã«è¨è¿°ã§ããã
cell(ä¸ã§ã¯BasicRNNCellã使ç¨)ã¨ããã®ã¯ãåã¹ãããã«å¯¾å¿ããRNNã¦ãããã¨èããã¨ããã
static_rnnã§ããããã®cellã¨åã¹ãããã®å
¥åãAPIå
ã§ç¹ãã¦ãããã
å
·ä½çãªstatic_rnnã®å
é¨ã®å¦çã¨ãã¦ã¯ã
å
¥åããããã¨ã«cellãã³ãã¼ãã¦ãä¸ã®å®è£
ã®ããã«ç¹ãã¦ããããã ã
n_inputs = 4 n_neurons = 6 X0 = tf.placeholder(tf.float32, [None, n_inputs]) X1 = tf.placeholder(tf.float32, [None, n_inputs]) basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, [X0, X1], dtype=tf.float32) Y0, Y1 = output_seqs init = tf.global_variables_initializer()
# t = 0 X0_batch = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) # t = 1 X1_batch = np.array([[15, 14, 13, 12], [0, 0, 0, 0], [8, 7, 6, 5], [4, 3, 2, 1]]) with tf.Session() as sess: init.run() Y0_eval, Y1_eval = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch}) print(Y0_eval) print(Y1_eval)
åã¹ãããã®å ¥åãã¾ã¨ãã¦1ã¤ã¨ãã¦æ±ã
ä¸ã®å®è£
ã§ã¯ã2ã¹ãããåã®å
¥åã®placeholderãããããX0, X1ã¨å®ç¾©ãã¦ããã
ãããã¹ãããæ°ãå¤ããªãã¨X0, X1, X2... ã¨å¢ãããªãã¨ãããªããªãã®ã§ã
å
¥åãã¾ã¨ãã¦æ±ãããã«ããã
å
·ä½çã«ã¯ã
[ããããµã¤ãºãã¹ãããæ°ãå
¥åã¦ãããæ°]ã¨ããplaceholderãç¨æãã¦ã¾ã¨ãã¦æ±ãã
ããããåã¹ãããã®å
¥åãã¨ã«ãã©ã
(tf.unstackã§1次å
ç®ã«æ²¿ã£ã¦ãå¥ã
ã®ãªã¹ãã«åããï¼ã
åºåã®ã¨ããã§ã¯éã«tf.stackã使ã£ã¦ã
åã¹ãããã§ã®åºåçµæãçµåãã¦ã
[ããããµã¤ãº,ã¹ãããæ°,åºåã¦ãããæ°]ãå¾ã¦ããã
n_steps = 2 n_inputs = 4 n_neurons = 6 X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) X_seqs = tf.unstack(tf.transpose(X, perm=[1, 0, 2])) basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons) output_seqs, states = tf.contrib.rnn.static_rnn(basic_cell, X_seqs, dtype=tf.float32) outputs = tf.transpose(tf.stack(output_seqs), perm=[1, 0, 2]) init = tf.global_variables_initializer()
X_batch = np.array([ # t = 0 t = 1 [[0, 1, 2, 3], [15, 14, 13, 12]], [[4, 5, 6, 7], [0, 0, 0, 0]], [[8, 9, 10, 11], [8, 7, 6, 5]], [[12, 13, 14, 15], [4, 3, 2, 1]], ]) with tf.Session() as sess: init.run() outputs_val = outputs.eval(feed_dict={X: X_batch})
dynamic_rnnã使ã
ä¸ã®å®è£
ã«ã¯ã¾ã åé¡ãããã
éä¼æ¬æã®å¾é
è¨ç®ã®ããã«ã
é ä¼æ¬æã®åã¹ãããã®è¨ç®çµæãä¿æãã¦ç½®ãå¿
è¦ãããããã
ã¡ã¢ãªãå§è¿«ãã¦ãã¾ãã
ãããé¿ããããã«static_rnnã®ä»£ããã«dynamic_rnnã使ãã¨ããã
dynamic_rnnã«æ¸¡ãå¼æ°ã¨ãã¦swap_memoryãTrueã«ãã¦ããã°ã
GPUããCPUã«ã¡ã¢ãªéé¿ãã¦ãããã
ã¾ããå¥ã®å©ç¹ã¨ãã¦åã¹ãããã®å
¥åºåã«ã
[ããããµã¤ãº,ã¹ãããæ°,ã¦ãããæ°]ããã®ã¾ã¾ä½¿ããã®ã§ã
ä¸ã®å®è£
ã«ããããã«unstackãstackãããªãã¦ãããªãã
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
ã¹ããããã¨ã®å ¥åãå¯å¤é·ã¨ãã
ããã¾ã§ã¯åã¹ãããã§ã®å
¥åé·ã¯åºå®ã ã£ãã
ã§ãæååã¿ããã«å¯å¤é·ã®ã¹ããããå
¥ããªãã¨ãããªãå ´åãããã
ããããå ´åã«ã¯ãdynamic_rnnãstatic_rnnãå¼ã¶ã¨ãã«ãsequence_lengthãè¨å®ããã
seq_length = tf.placeholder(tf.int32, [None])
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32,
sequence_length=seq_length)
è¨å®ããsquence_lengthã«æºããªãå ¥åã¯0ã§paddingãã¦å ¥åããªããã°ãªããªã
X_batch = np.array([ # step 0 step 1 [[0, 1, 2], [9, 8, 7]], # [[3, 4, 5], [0, 0, 0]], # 0ã§ããã£ã³ã° [[6, 7, 8], [6, 5, 4]], # [[9, 0, 1], [3, 2, 1]], # ]) seq_length_batch = np.array([2, 1, 2, 2])
ãã®ä»
ãã¾BasicRNNCellãã¤ãã£ã¦ãããã
cellã¯LSTMãGRUã«å
¥ãæ¿ãããã¨ãå¯è½ã