-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_Char_DenseCNN.py
204 lines (135 loc) · 6.61 KB
/
C_Char_DenseCNN.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
190
191
192
193
194
195
196
197
198
199
200
201
# coding: utf-8
# # Character Level CNN
# In[1]:
import Corpus.gutenberg as corpus
from TextPreprocess.Tokenizer.Stanford import tokenize
from Utils.visual import hist, tally
from Utils.debug import dump
from Utils.generator import sliding_window_random_access, transform
from Utils.FS.file import save, load
from Utils.keras import compact_embedding
from Utils.misc import batch
from Utils.indexer import build_index, index_2_one_hot
from keras.preprocessing.sequence import pad_sequences
from keras_tqdm import TQDMNotebookCallback
import numpy as np
# In[2]:
import nltk
#nltk.corpus.gutenberg.raw('austen-emma.txt')
nltk.corpus.gutenberg.fileids()
# In[3]:
#data = nltk.corpus.gutenberg.raw(['austen-emma.txt', 'austen-persuasion.txt', 'austen-sense.txt'])
data = nltk.corpus.gutenberg.raw(['austen-emma.txt'])
# In[4]:
def char_generator():
for char in data:
yield char
# In[5]:
s2i, i2s, size = build_index(char_generator())
# In[6]:
#MAX_SEQ_LENGTH = max([len(word) for word in data])
SEQ_LENGTH = 128
# In[7]:
NUM_SYMBOL = size
NUM_SYMBOL
# In[8]:
NUM_SAMPLE = len(data)
NUM_SAMPLE
# In[9]:
from keras.layers import MaxPooling1D, Input, Conv1D, Conv2D, Conv2DTranspose, Dense, Flatten, Dropout, Reshape, Embedding, Concatenate
from keras.models import Model, Sequential
from keras.regularizers import l2
from keras.optimizers import Adam
import numpy as np
import tensorflow as tf
def create_baseline():
activation = 'selu'
padding = 'same'
use_bias = False
layer = 4
dim = [100, 200, 400, 800, 1600, 3200]
kernel = [2, 2, 2, 2, 2, 2]
strides = [1, 1, 1, 1, 1, 1]
EMBEDDING_SIZE=100
inp = Input(shape=(SEQ_LENGTH,))
emb = Embedding(NUM_SYMBOL, EMBEDDING_SIZE, input_length=SEQ_LENGTH)(inp)
x = emb
for l in range(layer):
x = Conv1D(dim[l], kernel[l], strides=strides[l], activation=activation, padding=padding, use_bias=use_bias, name="Conv{}".format(l))(x)
x = Concatenate(name="MixLayer{}".format(l))([x, emb])
x = Dense(100, activation='selu')(x)
x = Flatten()(x)
#x = Dropout(0.2)(x)
#x = Dense(1600, activation='selu')(x)
#x = Dropout(0.2)(x)
x = Dense(NUM_SYMBOL, activation='softmax')(x)
model = Model(inp, x)
#opt = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0002)
model.compile(loss='categorical_crossentropy',
optimizer='adam')
return model
# In[10]:
model = create_baseline()
model.summary()
# In[11]:
from keras.utils.np_utils import to_categorical
def encode_word(word):
unpad = batch(s2i, word)
one_hot = to_categorical(unpad, num_classes=NUM_SYMBOL)
return one_hot
def decode_word(one_hot):
return i2s( np.random.choice(list(range(NUM_SYMBOL)), p = one_hot) )
def decode_word_2(one_hot):
return i2s( np.argmax(one_hot) )
# last char is used as output
# so set it like gen = sliding_window(SEQ_LENGTH + 1)(data)
def sample_generator(sliding_window_generator, batch_size = 64):
data = []
label = []
for window in sliding_window_generator:
#data.append(encode_word(window[:-1]))
data.append(batch(s2i, window[:-1]))
label.append(encode_word(window[-1:])[0])
if len(data) == batch_size:
yield (np.array(data), np.array(label))
data = []
label = []
# In[12]:
gen = {}
size = {}
gen['train'], gen['test'], size['train'], size['test'] = sliding_window_random_access(data, SEQ_LENGTH + 1)
print(next(sample_generator(gen['train'], 2))[0].shape)
print(next(sample_generator(gen['test'], 2))[1].shape)
# In[13]:
from keras.callbacks import Callback, ModelCheckpoint
def testing(model):
seed = """Your child comes home and presents you with a drawing of your house. There's a blue house, a yellow sun, and a green sky. You admire their handiwork and then gently ask why the sky is green. Shouldn't it be blue? Most teachers and parents would have the same reaction, but before you speak, stop! That innocent little comment carries a powerful punch. Unbeknownst to you, you are about to squelch your child's natural developing creativity.
Everyone has the ability to be creative, however, Professor of Biology and neurobiologist Erin Clabough Ph.D. writes that "creativity can be easily crushed by goals imposed by others." Not everyone needs to see the world in the same light- and they shouldn't. Before you mention that sky should be blue, consider your reasons carefully. Your child can see that a sky is blue, but in their world it isn't. Allow them the freedom to be creative. Creativity fosters critical thinking and problem solving skills. It helps people to deal with stress and adapt to changes.
"""
for i in range(500):
seed = seed + decode_word(model.predict(np.array([batch(s2i,seed[-SEQ_LENGTH:])]))[0])[0]
print(seed)
seed = """Your child comes home and presents you with a drawing of your house. There's a blue house, a yellow sun, and a green sky. You admire their handiwork and then gently ask why the sky is green. Shouldn't it be blue? Most teachers and parents would have the same reaction, but before you speak, stop! That innocent little comment carries a powerful punch. Unbeknownst to you, you are about to squelch your child's natural developing creativity.
Everyone has the ability to be creative, however, Professor of Biology and neurobiologist Erin Clabough Ph.D. writes that "creativity can be easily crushed by goals imposed by others." Not everyone needs to see the world in the same light- and they shouldn't. Before you mention that sky should be blue, consider your reasons carefully. Your child can see that a sky is blue, but in their world it isn't. Allow them the freedom to be creative. Creativity fosters critical thinking and problem solving skills. It helps people to deal with stress and adapt to changes.
"""
for i in range(500):
seed = seed + decode_word_2(model.predict(np.array([batch(s2i,seed[-SEQ_LENGTH:])]))[0])[0]
print(seed)
class testSample(Callback):
def on_epoch_end(self, batch, logs={}):
testing(model)
# In[14]:
from keras_tqdm import TQDMNotebookCallback
mc = ModelCheckpoint('./model/char_cnn_2.hdf5', monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)
BATCH_SIZE = 512
model.fit_generator(
sample_generator(gen['train'], BATCH_SIZE),
size['train'] // BATCH_SIZE,
validation_data = sample_generator(gen['test'], BATCH_SIZE),
validation_steps = size['test'] // BATCH_SIZE,
epochs=200000,
callbacks = [testSample(), mc]
#verbose=0, callbacks=[TQDMNotebookCallback(), testSample()]
)
# In[ ]:
# In[ ]: