-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
85 lines (70 loc) · 3.15 KB
/
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
import tensorflow as tf
import numpy as np
# st = tf.SparseTensor([[0,0,2],[1,1,1],[2,2,0]], [1,1,1], shape=[3,3,3])
# # st = tf.SparseTensor(indices=[[0],[3],[5]], values=[1,1,1], shape=[6])
#
# with tf.Session() as session:
# print session.run(st)
# print session.run(
# tf.sparse_tensor_to_dense(st)
# )
# dense = tf.sparse_tensor_to_dense(st)
# nonzeros = tf.count_nonzero(dense)
#
# print session.run([
# dense,
# nonzeros
# ]
# )
#
# stuff = tf.constant([[0,0,2],[1,1,1],[2,2,0],[0,1,6]])
# print session.run([
# tf.squeeze(tf.slice(stuff, [0,0], [-1,2])),
# tf.squeeze(tf.slice(stuff, [0,2], [-1,1]))
# ])
#
# tens = tf.Variable(np.array([[[1, 0, 0],[0, 1, 0]],[[0,0,1],[1,0,.5]],
# [[0, 1, .5], [0, 0, 1]], [[0, 0, 1], [0, 1, 0]]]), tf.float32)
#
# with tf.Session() as session:
# session.run(tf.global_variables_initializer())
# print session.run(tens)
# print session.run(
# tf.argmax(tens, dimension=2)
# )
# lets = session.run(tf.argmax(tens, dimension=2))
# for x in lets:
# phrase = ''.join(['a'*l for l in x])
# print phrase
inputs = np.array([[
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,]]], dtype=np.float32)
inputs *= 10
labels_indices = np.array([[0,0],[0,1]], dtype=np.int32)
labels_values = np.array([4, 2], dtype=np.int32)
labels = tf.SparseTensor(labels_indices, labels_values, inputs.shape[:2])
# labels = tf.SparseTensor([[0,0], [0,2], [0,3], [1,0], [1,1]], [0, 1, 3, 2, 2], [2,5])
# inputs = np.array([[[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,0],[0,0,0,0,1]],
# [[0,0,1,0,0],[0,0,1,0,0],[0,0,0,0,1],[0,0,0,0,1]]], dtype=np.float32)
lengths = [10]
# lengths = [3, 2]
loss = tf.nn.ctc_loss(inputs, labels, lengths, time_major=False, ctc_merge_repeated=False)
decoder_inputs = inputs.transpose(1,0,2)
decoder = tf.nn.ctc_greedy_decoder(decoder_inputs, lengths)
with tf.Session() as session:
print session.run(tf.sparse_tensor_to_dense(labels))
print session.run(tf.arg_max(tf.nn.softmax(inputs), dimension=2))
print session.run(decoder)
print session.run(loss)