-
Notifications
You must be signed in to change notification settings - Fork 2
/
one-hot.py
26 lines (20 loc) · 940 Bytes
/
one-hot.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
import string
import numpy as np
from keras.models import Sequential
from keras.layers import SimpleRNN, LSTM, Dense
samples = ['The cat sat on the mat.', 'The dog ate my homework.', 'pie in the sky', 'Activating Conda Environments in Powershell is not supported']
characters = string.printable
token_index = dict(zip(characters, range(1, len(characters) + 1)))
max_length = 50
results = np.zeros((len(samples), max_length, max(token_index.values()) + 1))
for i, sample in enumerate(samples):
for j, character in enumerate(sample[:max_length]):
index = token_index.get(character)
print(f'{character} - {index}')
results[i, j, index] = 1.
model = Sequential()
model.add(LSTM(len(characters)*max_length, input_shape = (4, 25,), activation="sigmoid"))
model.summary()
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['acc'])