forked from martin-gorner/tensorflow-rnn-shakespeare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn_play_greek.py
128 lines (115 loc) · 5.76 KB
/
rnn_play_greek.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
# encoding: UTF-8
# Copyright 2017 Google.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf
import numpy as np
import my_txtutils_greek
# these must match what was saved !
ALPHASIZE = my_txtutils_greek.ALPHASIZE
NLAYERS = 3
INTERNALSIZE = 512
# Data files can be downloaded from the following locations:
# - Fully trained on Shakespeare or Tensorflow Python source:
# https://drive.google.com/file/d/0B5njS_LX6IsDQ1laeDJ6dktSb3M/view?usp=sharing
# - Partially trained, to see how they make progress in training:
# https://drive.google.com/file/d/0B5njS_LX6IsDc2Y0X1VWc1pVTE0/view?usp=sharing
#shakespeareC0 = "./rnn_test_minibatchseq_1477684737-0" # random
#shakespeareC1 = "./rnn_test_minibatchseq_1477684737-150000" # mostly lower case
#shakespeareC2 = "./rnn_test_minibatchseq_1477684737-300000" # words, paragraphs
#shakespeareC3 = "./rnn_test_minibatchseq_1477684737-450000" # structure of a play
#shakespeareC4 = "./rnn_test_minibatchseq_1477684737-600000" # better structure of a play, 3-letter words in correct English
#shakespeareC5 = "./rnn_test_minibatchseq_1477684737-900000" # character names wrong but in correct place, correct 4-letter words start appearing
#shakespeareC6 = "./rnn_test_minibatchseq_1477684737-5100000" # good names, even when invented (ex: SIR NATHANIS LORD OF SYRACUSE), correct 6-8 letter words
#shakespeareC7 = "./rnn_test_minibatchseq_1477684737-10083000" # ACT V SCENE IV, [Re-enter KING JOHN with MARDIAN] but some still badly closed, DON ADRIANO DRAGHAMONE <- invented! most words correct
#shakespeareC8 = "./rnn_test_minibatchseq_1477684737-20049000" # most scenic indications correct: [Stabs aside] [Enter FERDINAND] [Dies] [Exit ROSALIND] [To COMINIUS with me]
#shakespeareC9 = "./rnn_test_minibatchseq_1477684737-47997000" # [Enter CYMBELINE and LUCETTA] [Enter PRINCE HENRY and Patroclus] [Exeunt all but two Englishman] [Enter PRINCE HENRY, and Attendants]
#shakespeareB10 = "./rnn_test_minibatchseq_1477670023-174939000" # [Re-enter LAUNIS'S] [Enter MARK ANTONY and TREBIAGO, DIONA, and CAPULET and Lords, and Soldiers]
shakespeareB10 = "./checkpoints/rnn_train_1497454445-3000000" # [Re-enter LAUNIS'S] [Enter MARK ANTONY and TREBIAGO, DIONA, and CAPULET and Lords, and Soldiers]
#
#pythonA0 = "./rnn_test_minibatchseq_1477832845-150000" # gibberish
#pythonA1 = "./rnn_test_minibatchseq_1477832845-300000" # some == and (
#pythonA2 = "./rnn_test_minibatchseq_1477832845-1050000" # correct function calls with parameters and ()
#pythonA3 = "./rnn_test_minibatchseq_1477832845-4050000" # looks like Tensorflow Python, nested () and [] ok
#pythonB10 = "./rnn_test_minibatchseq_1477834023-138609000" # can even recite the Apache license
# use topn=10 for all but the last which works with topn=2 for Shakespeare and topn=3 for Python
author = shakespeareB10
ncnt = 0
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('./checkpoints/rnn_train_1497454445-3000000.meta')
new_saver.restore(sess, author)
x = my_txtutils_greek.convert_from_alphabet(ord("K"))
x = np.array([[x]]) # shape [BATCHSIZE, SEQLEN] with BATCHSIZE=1 and SEQLEN=1
# initial values
y = x
h = np.zeros([1, INTERNALSIZE * NLAYERS], dtype=np.float32) # [ BATCHSIZE, INTERNALSIZE * NLAYERS]
for i in range(1000000000):
yo, h = sess.run(['Yo:0', 'H:0'], feed_dict={'X:0': y, 'pkeep:0': 1., 'Hin:0': h, 'batchsize:0': 1})
# If sampling is be done from the topn most likely characters, the generated text
# is more credible and more "english". If topn is not set, it defaults to the full
# distribution (ALPHASIZE)
# Recommended: topn = 10 for intermediate checkpoints, topn=2 for fully trained checkpoints
c = my_txtutils_greek.sample_from_probabilities(yo, topn=2)
y = np.array([[c]]) # shape [BATCHSIZE, SEQLEN] with BATCHSIZE=1 and SEQLEN=1
c = chr(my_txtutils_greek.convert_to_alphabet(c))
print(c, end="")
if c == '\n':
ncnt = 0
else:
ncnt += 1
if ncnt == 100:
print("")
ncnt = 0
# TITUS ANDRONICUS
#
#
# ACT I
#
#
#
# SCENE III An ante-chamber. The COUNT's palace.
#
#
# [Enter CLEOMENES, with the Lord SAY]
#
# Chamberlain Let me see your worshing in my hands.
#
# LUCETTA I am a sign of me, and sorrow sounds it.
#
# [Enter CAPULET and LADY MACBETH]
#
# What manner of mine is mad, and soon arise?
#
# JULIA What shall by these things were a secret fool,
# That still shall see me with the best and force?
#
# Second Watchman Ay, but we see them not at home: the strong and fair of thee,
# The seasons are as safe as the time will be a soul,
# That works out of this fearful sore of feather
# To tell her with a storm of something storms
# That have some men of man is now the subject.
# What says the story, well say we have said to thee,
# That shall she not, though that the way of hearts,
# We have seen his service that we may be sad.
#
# [Retains his house]
# ADRIANA What says my lord the Duke of Burgons of Tyre?
#
# DOMITIUS ENOBARBUS But, sir, you shall have such a sweet air from the state,
# There is not so much as you see the store,
# As if the base should be so foul as you.
#
# DOMITIUS ENOY If I do now, if you were not to seek to say,
# That you may be a soldier's father for the field.
#
# [Exit]