-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_tf.keras.py
executable file
·49 lines (39 loc) · 1.29 KB
/
load_tf.keras.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
#! /usr/bin/env python3
import numpy as np
from random import shuffle
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import load_model
# data set
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
classes = len(alphabet)
# letter<->integer encoding
char_to_int = {c: i for i, c in enumerate(alphabet)}
int_to_char = {i: c for c, i in char_to_int.items()}
# encode inputs
X = [char_to_int[c] for c in alphabet[:25]]
# reshape to (samples, time steps, features)
X = np.reshape(X, (-1, 1, 1))
# normalize
X = X / classes
# encode targets
y = [char_to_int[c] for c in alphabet[1:]]
# one hot encoding
y = to_categorical(y)
# model loading
model = load_model('tf.keras.h5')
# model evaluation
scores = model.evaluate(X, y, verbose=0)
print('Model accuracy: {:.2f}%\n'.format(scores[1] * 100))
# finally, run some predictions (out of order)
l = list(alphabet[:25])
shuffle(l)
for a in l:
# prepare input data
x = np.reshape([char_to_int[a]], (1, 1, 1)) / classes
# feed prepared input data to the model
pred_y = model.predict(x, verbose=0)
# convert output back to something lisible
real_y = int_to_char[np.argmax(pred_y)]
# check if output match our expectations
expected = chr(ord(a) + 1)
print('{} -> {} {}'.format(a, real_y, '' if real_y == expected else '*'))