From 451c5d99377760a7f700f3118309522ee27c1e8e Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Thu, 26 Dec 2019 10:34:07 -0800 Subject: [PATCH] Make it simpler --- 35-dnn-no-learning/normalize-chars.py | 31 +++++++-------------------- 35-dnn-no-learning/tf-35.py | 6 +++--- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/35-dnn-no-learning/normalize-chars.py b/35-dnn-no-learning/normalize-chars.py index dd20fee..eed489d 100644 --- a/35-dnn-no-learning/normalize-chars.py +++ b/35-dnn-no-learning/normalize-chars.py @@ -2,7 +2,6 @@ from keras.models import Model from keras import layers from keras.layers import Input, Dense from keras.utils import plot_model - import numpy as np import sys, os, string @@ -14,10 +13,10 @@ INPUT_VOCAB_SIZE = len(characters) LINE_SIZE = 100 def encode_one_hot(s): - """One-hot encode all characters of the given string. - """ all = [] for c in s: + if c not in characters: + continue x = np.zeros((INPUT_VOCAB_SIZE)) index = char_indices[c] x[index] = 1 @@ -25,13 +24,11 @@ def encode_one_hot(s): return all def decode_one_hot(x): - """Return a string from a one-hot-encoded matrix - """ s = [] for onehot in x: - one_index = np.where(onehot == 1) # one_index is a tuple of two things - if len(one_index[0]) > 0: - n = one_index[0][0] + one_index = np.where(onehot == 1) # tuple of two things + if len(one_index[1]) > 0: + n = one_index[1][0] c = indices_char[n] s.append(c) return ''.join(s) @@ -60,11 +57,8 @@ def normalization_layer_set_weights(n_layer): n_layer.set_weights(wb) return n_layer - def build_model(): - print('Build model...') - - # Normalize every character in the input, using a shared dense model + # Normalize characters using a shared dense model n_layer = Dense(INPUT_VOCAB_SIZE) raw_inputs = [] normalized_outputs = [] @@ -75,17 +69,10 @@ def build_model(): normalized_outputs.append(filtered_char) normalization_layer_set_weights(n_layer) - merged_output = layers.concatenate(normalized_outputs, axis=-1) - - reshape = layers.Reshape((LINE_SIZE, INPUT_VOCAB_SIZE, )) - reshaped_output = reshape(merged_output) - - model = Model(inputs=raw_inputs, outputs=reshaped_output) - + model = Model(inputs=raw_inputs, outputs=normalized_outputs) return model model = build_model() -#model.summary() plot_model(model, to_file='normalization.png', show_shapes=True) 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))) inputs = [np.array(e) for e in data] - preds = model.predict(inputs) - normal = decode_one_hot(preds[0]) + normal = decode_one_hot(preds) -# print(decode_one_hot(onehots)) print(normal) diff --git a/35-dnn-no-learning/tf-35.py b/35-dnn-no-learning/tf-35.py index dfb6c5b..40fae90 100644 --- a/35-dnn-no-learning/tf-35.py +++ b/35-dnn-no-learning/tf-35.py @@ -34,8 +34,8 @@ def decode_one_hot(x): s = [] for onehot in x: one_index = np.where(onehot == 1) # one_index is a tuple of two things - if len(one_index[1]) > 0: - n = one_index[1][0] + if len(one_index[0]) > 0: + n = one_index[0][0] c = indices_char[n] s.append(c) return ''.join(s) @@ -119,7 +119,7 @@ def build_model(): # Find the space characters 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