-
Notifications
You must be signed in to change notification settings - Fork 39
/
tf_test.py
189 lines (178 loc) · 5.4 KB
/
tf_test.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# import numpy as np
# import tensorflow.contrib.legacy_seq2seq as tf_seq2seq
# from tensorflow.contrib.rnn.python.ops import core_rnn, core_rnn_cell
#
#
# def decoder_loop_function(prev, i):
# # TODO Change Placeholder call for actual function
# return prev
#
#
# cell = core_rnn_cell.LSTMCell(256)
#
#
# # Try encapsulating encoder & decoder in functions, to be run in a Lambda layer
# def tf_seq2seq_encoder(encoder_inputs):
# _, enc_state = core_rnn.static_rnn(
# cell,
# encoder_inputs,
# dtype=float
# )
#
# return enc_state
#
#
# # Initialize Keras input
# # Initialize decoder input (loop_function?)
# enc_state_f = tf_seq2seq_encoder(np.zeros((1, 5, 5)))
#
#
# def tf_seq2seq_attention_decoder(decoder_inputs):
# (outputs, state) = tf_seq2seq.attention_decoder(
# decoder_inputs,
# enc_state_f,
# cell,
# loop_function=decoder_loop_function
# )
#
# return outputs
#
#
# (dec_output, dec_state) = tf_seq2seq_attention_decoder(np.zeros((1, 5)))
#
################################################################################
# import tensorflow as tf
#
# # Model parameters
# W = tf.Variable([.3], tf.float32)
# b = tf.Variable([-.3], tf.float32)
# # Model input and output
# x = tf.placeholder(tf.float32)
# linear_model = W * x + b
# y = tf.placeholder(tf.float32)
# # loss
# loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# # optimizer
# optimizer = tf.train.GradientDescentOptimizer(0.01)
# train = optimizer.minimize(loss)
# # training data
# x_train = [1, 2, 3, 4]
# y_train = [0, -1, -2, -3]
# # training loop
# init = tf.global_variables_initializer()
# sess = tf.Session()
# sess.run(init) # reset values to wrong
# for i in range(1000):
# sess.run(train, {x: x_train, y: y_train})
#
# # evaluate training accuracy
# curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
# print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
#
# # Build a dataflow graph.
# c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
# d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
# e = tf.matmul(c, d)
#
# # Construct a `Session` to execute the graph.
# sess = tf.Session()
#
# # Execute the graph and store the value that `e` represents in `result`.
# result = sess.run(e)
#
# ################################################################################
import numpy as np
import tensorflow as tf
from tensorflow.contrib.legacy_seq2seq.python.ops import seq2seq as tf_s2s
from tensorflow.contrib.rnn.python import ops as tf_rnn
from six.moves import xrange # pylint: disable=redefined-builtin
# TODO Input these variables as TF Tensors
batch_size = 2
timesteps = 5
# tensor_enc = tf.placeholder(
# dtype=tf.float32,
# shape=(batch_size, timesteps, 4),
# name='encoder_in')
# tensor_dec = tf.placeholder(
# dtype=tf.float32,
# shape=(batch_size, timesteps, 3),
# name='decoder_in')
tensor_enc = [tf.placeholder(
dtype=tf.float32,shape=(timesteps, 4)) for _ in xrange(batch_size)]
tensor_dec = [tf.placeholder(
dtype=tf.float32,shape=(timesteps, 3)) for _ in xrange(batch_size)]
# print('logging')
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
# logger = logging.getLogger()
# logger.setLevel(logging.INFO)
logging.info("test")
# logger = logging.getLogger()
# logger.setLevel(logging.INFO)
logging.info('printing shape of tensor_enc')
logging.info(tensor_enc[0].get_shape())
# print(tensor_enc[0].get_shape())
# print('logging')
# params_enc = np.zeros((4, 1)) # [0] * 4
# params_dec = np.zeros((3, 1)) # [0] * 3
#
# frames_enc = [params_enc] * timesteps
# frames_dec = [params_dec] * timesteps
#
# seqs_enc = [frames_enc] * batch_size
# seqs_dec = [frames_dec] * batch_size
seqs_enc = [np.random.rand(timesteps, 4) for _ in xrange(batch_size)]
seqs_dec = [np.random.rand(timesteps, 3) for _ in xrange(batch_size)]
s2s = tf_s2s.basic_rnn_seq2seq(
tensor_enc,
tensor_dec,
tf_rnn.core_rnn_cell.LSTMCell(256)
)
with tf.Session() as sess:
# feed_dict = {tensor_enc:seqs_enc, tensor_dec: seqs_dec}
feed_dict = {}
for i, d in zip(tensor_enc, seqs_enc):
feed_dict[i] = d
for i, d in zip(tensor_dec, seqs_dec):
feed_dict[i] = d
init = tf.global_variables_initializer()
sess.run(init)
basic_seq2seq = sess.run(s2s, feed_dict=feed_dict) # tensor_enc: seqs_enc, tensor_dec: seqs_dec})
# print(basic_seq2seq)
################################################################################
# # Test decoder inputs
# import numpy as np
# from six.moves import xrange
#
# batch_size = 3
# decoder_size = 7
# GO_ID = 'GO'
# PAD_ID = 'PAD'
# EOS_ID = 'EOS'
#
# decoder_batch = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'],
# ['i', 'j', 'k', 'l']]
# decoder_inputs = []
#
# for i in xrange(batch_size):
# # Decoder inputs get an extra "GO" symbol, and are padded then.
# decoder_input = decoder_batch[i]
# decoder_pad_size = decoder_size - len(decoder_input)
# # decoder_inputs.append([data_utils.GO_ID] + decoder_input +
# decoder_inputs.append(
# [[GO_ID] + [in_val] for in_val in decoder_input] +
# [[PAD_ID] * 2] * decoder_pad_size)
#
# # Set last decoder input's flag to EOS
# decoder_inputs[i][len(decoder_input) - 1][0] = EOS_ID
#
# # print(decoder_inputs)
#
# batch_decoder_inputs = []
#
# for length_idx in xrange(decoder_size):
# batch_decoder_inputs.append(
# np.array([decoder_inputs[batch_idx][length_idx]
# for batch_idx in xrange(batch_size)]))
#
# print(batch_decoder_inputs)