-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrun_tf_basic_rnn_matmul.py
131 lines (81 loc) · 3.07 KB
/
run_tf_basic_rnn_matmul.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#-*- coding: utf-8 -*-
#! /usr/bin/env python
'''
filename: run_tf_basic_matmul.py
This script is for implementation of a basic rnn network
using tf.matmul() and tf.tanh()
author: Jaewook Kang @ 2018 Sep
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import datetime
import numpy as np
import tensorflow as tf
model_config = \
{
'batch_size': None,
'n_input' : 3,
'n_output' : 5,
'num_steps' : 2,
'dtype' : tf.float32
}
weigth_initializer = tf.contrib.layers.xavier_initializer()
def get_rnn_model(X0,X1,scope):
Wx_shape = [model_config['n_input'],
model_config['n_output']]
Wh_shape = [model_config['n_output'],
model_config['n_output']]
bias_shape = [1,model_config['n_output']]
with tf.name_scope(name=scope,values=[X0,X1]):
Wx = tf.get_variable(name='weight_x',
shape=Wx_shape,
initializer=weigth_initializer)
Wh = tf.get_variable(name='weight_h',
shape=Wh_shape,
initializer=weigth_initializer)
b = tf.get_variable(name='bias',
shape=bias_shape,
initializer=weigth_initializer)
Y0 = tf.tanh(tf.matmul(X0,Wx) + b)
Y1 = tf.tanh(tf.matmul(X1,Wx) + tf.matmul(Y0,Wh) + b)
return Y0,Y1
if __name__ == '__main__':
input_shape = [model_config['batch_size'],
model_config['n_input']]
output_shape = [model_config['batch_size'],
model_config['n_output']]
X0 = tf.placeholder(dtype = model_config['dtype'],
shape = input_shape,
name = 'X0')
X1 = tf.placeholder(dtype= model_config['dtype'],
shape= input_shape,
name = 'X1')
scope = 'basic_rnn_matmul_model'
Y0,Y1 = get_rnn_model(X0,X1,scope)
# tensorboard summary
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = 'tf_logs/rnn_basic_matmul'
subdir = "{}/run-{}/".format(root_logdir, now)
logdir = './pb_and_ckpt/' + subdir
if not tf.gfile.Exists(logdir):
tf.gfile.MakeDirs(logdir)
summary_writer = tf.summary.FileWriter(logdir=logdir)
summary_writer.add_graph(graph=tf.get_default_graph())
init = tf.global_variables_initializer()
X0_batch = np.array([[0,1,2],
[3,4,5],
[6,7,8],
[9,0,1],
])
X1_batch = np.array([[9,8,7],
[6,5,4],
[3,2,1],
[0,9,8]])
with tf.Session() as sess:
sess.run(init)
Y0_val,Y1_val = sess.run(fetches=[Y0,Y1],feed_dict={X0:X0_batch,
X1:X1_batch})
print('Y0_val = %s' % Y0_val)
print('Y1_val = %s' % Y1_val)
summary_writer.close()