-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc8e55a
commit 6e8ef2e
Showing
2 changed files
with
135 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +0,0 @@ | ||
import sqlite3 | ||
import numpy as np | ||
|
||
train_couter = 0 | ||
validation_counter = 0 | ||
test_counter = 0 | ||
|
||
def ont_hot(sequences, nexts, batch_size): | ||
x = np.zeros((batch_size, 40, 128), dtype=np.bool) | ||
y = np.zeros((batch_size, 128), dtype=np.bool) | ||
|
||
for i, sequence in enumerate(sequences): | ||
if nexts[i] < 0 or nexts[i] > 128: | ||
y[i, 97] = 1 | ||
else: | ||
y[i, nexts[i]] = 1 | ||
|
||
return np.array(sequences), y | ||
|
||
def train_generator(batch_size): | ||
global train_couter | ||
|
||
while True: | ||
conn = sqlite3.connect('data/code.db') | ||
c = conn.cursor() | ||
|
||
sql = f"SELECT sequence, next FROM code_sequences WHERE state = 'tr' LIMIT {batch_size} OFFSET {batch_size * train_couter}" | ||
|
||
train_couter += 1 | ||
|
||
c.execute(sql) | ||
|
||
rows = c.fetchall() | ||
|
||
sequences = [] | ||
nexts = [] | ||
|
||
for sequence, next in rows: | ||
temp = [] | ||
for char in sequence: | ||
temp.append(ord(char)) | ||
|
||
sequences.append(temp) | ||
nexts.append(ord(next)) | ||
|
||
x,y = ont_hot(sequences, nexts, batch_size) | ||
|
||
assert x.shape == (batch_size, 40), "Invalid dimension for Input X" | ||
assert y.shape == (batch_size, 128), "Invalid dimension for Output Y" | ||
|
||
yield x, y | ||
|
||
def validation_generator(batch_size): | ||
global validation_counter | ||
|
||
while True: | ||
conn = sqlite3.connect('data/code.db') | ||
c = conn.cursor() | ||
|
||
sql = f"SELECT sequence, next FROM code_sequences WHERE state = 'tr' LIMIT {batch_size} OFFSET {batch_size * validation_counter}" | ||
|
||
validation_counter += 1 | ||
|
||
c.execute(sql) | ||
|
||
rows = c.fetchall() | ||
|
||
sequences = [] | ||
nexts = [] | ||
|
||
for sequence, next in rows: | ||
temp = [] | ||
for char in sequence: | ||
temp.append(ord(char)) | ||
|
||
sequences.append(temp) | ||
nexts.append(ord(next)) | ||
|
||
x,y = ont_hot(sequences, nexts, batch_size) | ||
|
||
assert x.shape == (batch_size, 40), "Invalid dimension for Input X" | ||
assert y.shape == (batch_size, 128), "Invalid dimension for Output Y" | ||
|
||
yield x, y | ||
|
||
def test_generator(batch_size): | ||
global test_counter | ||
|
||
while True: | ||
conn = sqlite3.connect('data/code.db') | ||
c = conn.cursor() | ||
|
||
sql = f"SELECT sequence, next FROM code_sequences WHERE state = 'tr' LIMIT {batch_size} OFFSET {batch_size * test_counter}" | ||
|
||
test_counter += 1 | ||
|
||
c.execute(sql) | ||
|
||
rows = c.fetchall() | ||
|
||
sequences = [] | ||
nexts = [] | ||
|
||
for sequence, next in rows: | ||
temp = [] | ||
for char in sequence: | ||
temp.append(ord(char)) | ||
|
||
sequences.append(temp) | ||
nexts.append(ord(next)) | ||
|
||
x,y = ont_hot(sequences, nexts, batch_size) | ||
|
||
assert x.shape == (batch_size, 40), "Invalid dimension for Input X" | ||
assert y.shape == (batch_size, 128), "Invalid dimension for Output Y" | ||
|
||
yield x, y | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters