-
Notifications
You must be signed in to change notification settings - Fork 0
Test/nn format #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Test/nn format #47
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import getpass | ||
|
|
||
| from sequences import Dictionary, DictionaryOperations | ||
| from sequences import Sequence, SequenceOperations | ||
| from lstm_formatter import XmlOperations | ||
| from lstm_formatter import LSTMFormatter | ||
|
|
||
| if __name__ == "__main__": | ||
| KOTLIN_VOCAB_FILE = "./data/kotlin_vocab.json" | ||
| process = "PrepareTrainDataFromASTXml" | ||
| inp_words = 4 | ||
| units = 96 | ||
| sequence_operations = SequenceOperations() | ||
| dictionary_operations = DictionaryOperations() | ||
| xml_operations = XmlOperations() | ||
|
|
||
| dictionary = dictionary_operations.load( | ||
| filepath = KOTLIN_VOCAB_FILE, | ||
| username = getpass.getuser(), | ||
| process = process | ||
| ) | ||
|
|
||
| model_filename = f"./data/lstm-kotlin-n{inp_words}_v{dictionary.size()}_u{units}.h1.keras" | ||
| sequences = xml_operations.loadSequencesUseCase( | ||
| directory="../../generated/kotlin/", | ||
| filename="output_tree_Kotlin.xml", | ||
| dictionary= dictionary | ||
| ) | ||
| if sequences.is_err(): | ||
| print(f"Error loading sequences: {sequences.unwrap_err()}") | ||
| exit(1) | ||
| sequences = sequences.unwrap() | ||
| sequences.author = getpass.getuser() | ||
| sequences.process = process | ||
|
|
||
| formatter = LSTMFormatter(inp_words=inp_words) | ||
| if (not formatter.loadModel(model_filename)): | ||
| print(f"Error loading model") | ||
| exit(1) | ||
|
|
||
| # formatter.trainModel(sequences) | ||
| # formatter.model.save("./data/lstm-kotlin-n4.h1.keras") | ||
|
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| __pycache__/ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import os | ||
| from tensorflow.keras.models import Sequential | ||
| from tensorflow.keras.layers import LSTM, Dense, Embedding | ||
| from keras import optimizers | ||
| from tensorflow.keras.models import load_model | ||
| from sequences import Sequence | ||
| from sequences import Dictionary | ||
| import numpy as np | ||
| from tensorflow.keras.utils import to_categorical | ||
| from keras.callbacks import ModelCheckpoint | ||
|
|
||
| class LSTMFormatter: | ||
| def __init__(self, inp_words: int = 4): | ||
| self.inp_words = inp_words | ||
| self.paddingVec = [0] * (inp_words - 1) | ||
| self.filename = "" | ||
| self.model = None | ||
| self.rms = None | ||
|
|
||
| def loadModel(self, filename: str) -> bool: | ||
| self.filename = filename | ||
| self.rms = optimizers.RMSprop(learning_rate=0.0005) | ||
| if os.path.exists(filename): | ||
| self.model = load_model(filename) | ||
| self.model.compile(optimizer=self.rms, loss='sparse_categorical_crossentropy') | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def defineModel(self, units: int, dictionary: Dictionary, filename: str): | ||
| self.filename = filename | ||
| self.rms = optimizers.RMSprop(learning_rate=0.0005) | ||
| if os.path.exists(filename): | ||
| self.loadModel(filename) | ||
| return | ||
| self.model = Sequential() | ||
| dictionary_size = dictionary.size() + 1 # +1 for padding token | ||
| self.model.add(Embedding(dictionary_size, | ||
| output_dim=units, | ||
| input_length=self.inp_words, | ||
| mask_zero=True)) | ||
| self.model.add(LSTM(units)) | ||
| self.model.add(Dense(dictionary_size, activation='softmax')) | ||
| self.model.build(input_shape=(None, self.inp_words)) | ||
| self.model.summary() | ||
| self.model.compile(optimizer=self.rms, loss='sparse_categorical_crossentropy') | ||
|
|
||
| def trainModel(self, sequence: Sequence): | ||
| vectors = [it['sequence'] for it in sequence.entries.values()] | ||
| vectors = [self.paddingVec + sb for sb in vectors] | ||
| X = [] | ||
| Y = [] | ||
| for sb in vectors: | ||
| for i in range(len(sb) - self.inp_words): | ||
| X.append(sb[i:i + self.inp_words]) | ||
| Y.append(sb[i + self.inp_words]) | ||
| X = np.array(X) | ||
| Y = np.array(Y) | ||
| print(f"X shape: {X.shape}, Y shape: {Y.shape}") | ||
|
|
||
| checkpoint = ModelCheckpoint(self.filename, monitor='val_loss', verbose=1, save_best_only=True, mode='min') | ||
| history = self.model.fit(x = X, | ||
| y = Y, | ||
| batch_size=16, | ||
| validation_split = 0.2, | ||
| callbacks=[checkpoint], | ||
| epochs=4096) | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| from lxml import etree | ||
| from sequences import Sequence | ||
| from sequences import Dictionary | ||
| import copy | ||
| from datetime import datetime | ||
| from result import Result, Err, Ok | ||
|
|
||
| class XmlOperations: | ||
| OPEN_ONLY_TAGS = [ | ||
| "WorkingDirectory", | ||
| "PackageDirectory", | ||
| "VariableName", | ||
| "CommentLeaf", | ||
| "AstTypeLeaf", | ||
| "ImportLeaf", | ||
| "Space", | ||
| "NlSeparator", | ||
| "Indent", | ||
| "Keyword" | ||
| ] | ||
| SKIP_TAGS = [ | ||
| "FileMetaInformation" | ||
| ] | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
| def process_childs(self, elem, vocab, id): | ||
| for child in elem: | ||
| if child.tag in XmlOperations.SKIP_TAGS: | ||
| ## Skip this tag and its children | ||
| continue | ||
| tagName = child.tag | ||
| tagCanBeClosed = tagName not in XmlOperations.OPEN_ONLY_TAGS | ||
| openTag = f"{tagName}_open" if tagCanBeClosed else f"{tagName}" | ||
| if tagName == "Keyword": | ||
| openTag = f"{tagName}_{child.attrib['name']}" | ||
|
|
||
| if openTag not in vocab: | ||
| vocab[openTag] = {"id": id, "priority": 0} | ||
| id += 1 | ||
|
|
||
| if tagCanBeClosed: | ||
| closeTag = f"{tagName}_close" | ||
| if closeTag not in vocab: | ||
| vocab[closeTag] = {"id": id, "priority": 0} | ||
| id += 1 | ||
|
|
||
| id = self.process_childs(child, vocab, id) | ||
|
|
||
| return id | ||
|
|
||
| def refreshDictionaryUseCase(self, directory, filename, dictionary: Dictionary) -> Dictionary: | ||
| tree = etree.parse(directory + "/" + filename) | ||
| root = tree.getroot() | ||
| newDictionary = copy.deepcopy(dictionary) | ||
| id = newDictionary.nextId() | ||
| for child in root: | ||
| print(f"Child tag: {child.tag}, attributes: {child.attrib}") | ||
| id = self.process_childs(child, newDictionary.entries, id) | ||
|
|
||
| print(f"Vocabulary size: {len(newDictionary.entries)}") | ||
| newDictionary.updateDate = datetime.now().isoformat() | ||
| return newDictionary | ||
|
|
||
| def prepareTrainingSequencesUseCase(self, directory, filename, dictionary: Dictionary) -> Sequence: | ||
| tree = etree.parse(directory + "/" + filename) | ||
| root = tree.getroot() | ||
| sequences = Sequence(username="", process="") | ||
|
|
||
| for child in root: | ||
| blockName = child.attrib['name'] | ||
| # print(f"Child tag: {child.tag}, attributes: {child.attrib['name']}") | ||
| sequence = [] | ||
| self.process_childs_for_sequence(child, dictionary, sequence) | ||
| sequences.entries[blockName] = {"sequence": sequence} | ||
|
|
||
| print(f"Training sequence length: {len(sequences.entries)}") | ||
| return sequences | ||
|
|
||
| def process_childs_for_sequence(self, elem, dictionary: Dictionary, sequence: list): | ||
| for child in elem: | ||
| if child.tag in XmlOperations.SKIP_TAGS: | ||
| ## Skip this tag and its children | ||
| continue | ||
| tagName = child.tag | ||
| tagCanBeClosed = tagName not in XmlOperations.OPEN_ONLY_TAGS | ||
| openTag = f"{tagName}_open" if tagCanBeClosed else f"{tagName}" | ||
| if tagName == "Keyword": | ||
| openTag = f"{tagName}_{child.attrib['name']}" | ||
|
|
||
| if openTag in dictionary.entries: | ||
| sequence.append(dictionary.entries[openTag]["id"]) | ||
|
|
||
| if tagCanBeClosed: | ||
| closeTag = f"{tagName}_close" | ||
| if closeTag in dictionary.entries: | ||
| # Process children first (depth-first) | ||
| self.process_childs_for_sequence(child, dictionary, sequence) | ||
| sequence.append(dictionary.entries[closeTag]["id"]) | ||
| else: | ||
| raise Exception(f"'{closeTag}' not found in vocabulary") | ||
| return sequence | ||
|
|
||
| def loadSequencesUseCase(self, directory, filename, dictionary: Dictionary) -> Result[Sequence, str]: | ||
| tree = etree.parse(directory + "/" + filename) | ||
| root = tree.getroot() | ||
| sequences = Sequence(username="", process="") | ||
|
|
||
| for child in root: | ||
| blockName = child.attrib['name'] | ||
| sequence = [] | ||
| try: | ||
| self.process_childs_for_sequence(child, dictionary, sequence) | ||
| except Exception as e: | ||
| return Err(f"Error processing block '{blockName}': {str(e)}") | ||
| sequences.entries[blockName] = {"sequence": sequence} | ||
|
|
||
| print(f"Sequences count: {len(sequences.entries)}") | ||
| return Ok(sequences) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # lstm_formatter/__init__.py | ||
|
|
||
| from .XmlOperations import XmlOperations | ||
| from .LSTMFormatter import LSTMFormatter | ||
|
|
||
| __all__ = ['XmlOperations', 'LSTMFormatter'] |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded value of 4096 epochs is a magic number that should be configurable. Consider making this a parameter or class attribute to improve maintainability.