import numpy as np
import tensorflow as tf
print(tf.__version__)
1.13.1
n_inputs = 3
n_neurons = 5
ㅊ
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()
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer.
X0_batch = np.array(
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 0, 1]]
)
X1_batch = np.array(
[[9, 8, 7],
[0, 0, 0],
[6, 5, 4],
[3, 2, 1]]
)
with tf.Session() as sess:
init.run()
Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0:X0_batch, X1:X1_batch})
print(Y0_val)
[[-0.9999428 0.8142273 -0.89699703 -0.51680404 0.994335 ] [-1. 0.96428037 -0.46683785 -0.9999996 0.999998 ] [-1. 0.99356204 0.4174478 -1. 1. ] [-0.9979181 0.99981457 0.98542804 -1. -0.99998975]]
print(Y1_val)
[[-1. -0.9072738 0.9998315 -1. 1. ] [-0.03871869 -0.9949366 0.5307218 0.9179147 0.99971414] [-1. -0.98553795 0.99953645 -1. 1. ] [-1. -0.13850287 0.8804973 -0.99971765 0.9999961 ]]
X0 = tf.placeholder(tf.float32, [None, n_inputs])
X1 = tf.placeholder(tf.float32, [None, n_inputs])
basic_rnn = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, reuse=True)
output_seqs, states = tf.contrib.rnn.static_rnn(basic_rnn, [X0, X1], dtype=tf.float32)
init=tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
output_seqs, states = sess.run([output_seqs, states], feed_dict={X0:X0_batch, X1:X1_batch})
print(output_seqs)
[array([[-0.11086969, -0.8644573 , 0.8264698 , -0.6449973 , 0.3823741 ], [ 0.7227251 , -0.9990468 , 0.993787 , -0.99651307, 0.64120334], [ 0.9593735 , -0.9999937 , 0.9997956 , -0.99997175, 0.80672956], [ 0.9981162 , 0.46572423, -0.538953 , -0.999847 , -0.99559104]], dtype=float32), array([[ 0.99731344, -0.9999985 , 0.99958646, -0.99999034, 0.9126751 ], [-0.17122112, -0.6988485 , -0.05396171, -0.03301296, 0.93879026], [ 0.94880253, -0.99978197, 0.96938735, -0.9995523 , 0.9801305 ], [ 0.94319993, -0.4077534 , 0.02760991, -0.97863674, -0.26508206]], dtype=float32)]
print(states)
[[ 0.99731344 -0.9999985 0.99958646 -0.99999034 0.9126751 ] [-0.17122112 -0.6988485 -0.05396171 -0.03301296 0.93879026] [ 0.94880253 -0.99978197 0.96938735 -0.9995523 0.9801305 ] [ 0.94319993 -0.4077534 0.02760991 -0.97863674 -0.26508206]]