Make it simpler
This commit is contained in:
@@ -2,7 +2,6 @@ from keras.models import Model
|
|||||||
from keras import layers
|
from keras import layers
|
||||||
from keras.layers import Input, Dense
|
from keras.layers import Input, Dense
|
||||||
from keras.utils import plot_model
|
from keras.utils import plot_model
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import sys, os, string
|
import sys, os, string
|
||||||
|
|
||||||
@@ -14,10 +13,10 @@ INPUT_VOCAB_SIZE = len(characters)
|
|||||||
LINE_SIZE = 100
|
LINE_SIZE = 100
|
||||||
|
|
||||||
def encode_one_hot(s):
|
def encode_one_hot(s):
|
||||||
"""One-hot encode all characters of the given string.
|
|
||||||
"""
|
|
||||||
all = []
|
all = []
|
||||||
for c in s:
|
for c in s:
|
||||||
|
if c not in characters:
|
||||||
|
continue
|
||||||
x = np.zeros((INPUT_VOCAB_SIZE))
|
x = np.zeros((INPUT_VOCAB_SIZE))
|
||||||
index = char_indices[c]
|
index = char_indices[c]
|
||||||
x[index] = 1
|
x[index] = 1
|
||||||
@@ -25,13 +24,11 @@ def encode_one_hot(s):
|
|||||||
return all
|
return all
|
||||||
|
|
||||||
def decode_one_hot(x):
|
def decode_one_hot(x):
|
||||||
"""Return a string from a one-hot-encoded matrix
|
|
||||||
"""
|
|
||||||
s = []
|
s = []
|
||||||
for onehot in x:
|
for onehot in x:
|
||||||
one_index = np.where(onehot == 1) # one_index is a tuple of two things
|
one_index = np.where(onehot == 1) # tuple of two things
|
||||||
if len(one_index[0]) > 0:
|
if len(one_index[1]) > 0:
|
||||||
n = one_index[0][0]
|
n = one_index[1][0]
|
||||||
c = indices_char[n]
|
c = indices_char[n]
|
||||||
s.append(c)
|
s.append(c)
|
||||||
return ''.join(s)
|
return ''.join(s)
|
||||||
@@ -60,11 +57,8 @@ def normalization_layer_set_weights(n_layer):
|
|||||||
n_layer.set_weights(wb)
|
n_layer.set_weights(wb)
|
||||||
return n_layer
|
return n_layer
|
||||||
|
|
||||||
|
|
||||||
def build_model():
|
def build_model():
|
||||||
print('Build model...')
|
# Normalize characters using a shared dense model
|
||||||
|
|
||||||
# Normalize every character in the input, using a shared dense model
|
|
||||||
n_layer = Dense(INPUT_VOCAB_SIZE)
|
n_layer = Dense(INPUT_VOCAB_SIZE)
|
||||||
raw_inputs = []
|
raw_inputs = []
|
||||||
normalized_outputs = []
|
normalized_outputs = []
|
||||||
@@ -75,17 +69,10 @@ def build_model():
|
|||||||
normalized_outputs.append(filtered_char)
|
normalized_outputs.append(filtered_char)
|
||||||
normalization_layer_set_weights(n_layer)
|
normalization_layer_set_weights(n_layer)
|
||||||
|
|
||||||
merged_output = layers.concatenate(normalized_outputs, axis=-1)
|
model = Model(inputs=raw_inputs, outputs=normalized_outputs)
|
||||||
|
|
||||||
reshape = layers.Reshape((LINE_SIZE, INPUT_VOCAB_SIZE, ))
|
|
||||||
reshaped_output = reshape(merged_output)
|
|
||||||
|
|
||||||
model = Model(inputs=raw_inputs, outputs=reshaped_output)
|
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
model = build_model()
|
model = build_model()
|
||||||
#model.summary()
|
|
||||||
plot_model(model, to_file='normalization.png', show_shapes=True)
|
plot_model(model, to_file='normalization.png', show_shapes=True)
|
||||||
|
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
@@ -100,9 +87,7 @@ with open(sys.argv[1]) as f:
|
|||||||
data[j].append(np.zeros((INPUT_VOCAB_SIZE)))
|
data[j].append(np.zeros((INPUT_VOCAB_SIZE)))
|
||||||
|
|
||||||
inputs = [np.array(e) for e in data]
|
inputs = [np.array(e) for e in data]
|
||||||
|
|
||||||
preds = model.predict(inputs)
|
preds = model.predict(inputs)
|
||||||
normal = decode_one_hot(preds[0])
|
normal = decode_one_hot(preds)
|
||||||
|
|
||||||
# print(decode_one_hot(onehots))
|
|
||||||
print(normal)
|
print(normal)
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ def decode_one_hot(x):
|
|||||||
s = []
|
s = []
|
||||||
for onehot in x:
|
for onehot in x:
|
||||||
one_index = np.where(onehot == 1) # one_index is a tuple of two things
|
one_index = np.where(onehot == 1) # one_index is a tuple of two things
|
||||||
if len(one_index[1]) > 0:
|
if len(one_index[0]) > 0:
|
||||||
n = one_index[1][0]
|
n = one_index[0][0]
|
||||||
c = indices_char[n]
|
c = indices_char[n]
|
||||||
s.append(c)
|
s.append(c)
|
||||||
return ''.join(s)
|
return ''.join(s)
|
||||||
@@ -119,7 +119,7 @@ def build_model():
|
|||||||
# Find the space characters
|
# Find the space characters
|
||||||
words_output = layers.Lambda(SpaceDetector)(reshaped_output)
|
words_output = layers.Lambda(SpaceDetector)(reshaped_output)
|
||||||
|
|
||||||
model = Model(inputs=raw_inputs, outputs=normalized_outputs)
|
model = Model(inputs=raw_inputs, outputs=words_output)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user