From c70af3d75573756bc72b373997a9f0b80496dea9 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 14:47:08 -0800 Subject: [PATCH 1/7] build: Incremented version numbers. --- COPYRIGHT.md | 2 +- LICENSE.md | 2 +- nexport/__init__.py | 4 ++-- nexport/calculators.py | 2 +- nexport/colors.py | 2 +- nexport/generic.py | 2 +- nexport/models.py | 2 +- nexport/pytorch/__init__.py | 2 +- nexport/pytorch/activation.py | 2 +- nexport/pytorch/exporting.py | 2 +- nexport/pytorch/importing.py | 2 +- nexport/tensorflow/__init__.py | 2 +- nexport/tensorflow/exporting.py | 2 +- nexport/tensorflow/importing.py | 2 +- nexport/utils.py | 2 +- setup.py | 4 ++-- 16 files changed, 18 insertions(+), 18 deletions(-) diff --git a/COPYRIGHT.md b/COPYRIGHT.md index 8864250..ce5e38b 100644 --- a/COPYRIGHT.md +++ b/COPYRIGHT.md @@ -1,6 +1,6 @@ # Copyright Notice -“Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. +“Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. If you have questions about your rights to use or distribute this software, please contact Berkeley Lab's Intellectual Property Office at [IPO@lbl.gov](mailto:IPO@lbl.gov). diff --git a/LICENSE.md b/LICENSE.md index fc6ec98..0c81d84 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # License Agreement -“Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. +“Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/nexport/__init__.py b/nexport/__init__.py index 6744257..098ea59 100644 --- a/nexport/__init__.py +++ b/nexport/__init__.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. @@ -17,7 +17,7 @@ # Dunder attributes -__version__ = "v0.4.5" # update setup.py +__version__ = "v0.4.6" # update setup.py __author__ = "Jordan Welsman" # Import submodules so submodule functions diff --git a/nexport/calculators.py b/nexport/calculators.py index a37c7ca..6d2d49e 100644 --- a/nexport/calculators.py +++ b/nexport/calculators.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/colors.py b/nexport/colors.py index 17bf1f8..a2fc7e5 100644 --- a/nexport/colors.py +++ b/nexport/colors.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.4” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/generic.py b/nexport/generic.py index 300594e..2d80f88 100644 --- a/nexport/generic.py +++ b/nexport/generic.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/models.py b/nexport/models.py index 1481713..97871e0 100644 --- a/nexport/models.py +++ b/nexport/models.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/pytorch/__init__.py b/nexport/pytorch/__init__.py index a8035dd..af3b2ee 100644 --- a/nexport/pytorch/__init__.py +++ b/nexport/pytorch/__init__.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/pytorch/activation.py b/nexport/pytorch/activation.py index 30e3827..639a980 100644 --- a/nexport/pytorch/activation.py +++ b/nexport/pytorch/activation.py @@ -1,4 +1,4 @@ -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/pytorch/exporting.py b/nexport/pytorch/exporting.py index 90a7707..0676808 100644 --- a/nexport/pytorch/exporting.py +++ b/nexport/pytorch/exporting.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.4” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/pytorch/importing.py b/nexport/pytorch/importing.py index 78e2cc0..54f9d56 100644 --- a/nexport/pytorch/importing.py +++ b/nexport/pytorch/importing.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/tensorflow/__init__.py b/nexport/tensorflow/__init__.py index a8035dd..af3b2ee 100644 --- a/nexport/tensorflow/__init__.py +++ b/nexport/tensorflow/__init__.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/tensorflow/exporting.py b/nexport/tensorflow/exporting.py index b59b617..9cdeb7a 100644 --- a/nexport/tensorflow/exporting.py +++ b/nexport/tensorflow/exporting.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/tensorflow/importing.py b/nexport/tensorflow/importing.py index 7a3c598..45c5296 100644 --- a/nexport/tensorflow/importing.py +++ b/nexport/tensorflow/importing.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/nexport/utils.py b/nexport/utils.py index 3c013cb..4d884cc 100644 --- a/nexport/utils.py +++ b/nexport/utils.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. diff --git a/setup.py b/setup.py index c7de439..5dc8de3 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ # COPYRIGHT NOTICE -# “Neural Network Export Package (nexport) v0.4.5” Copyright (c) 2023, +# “Neural Network Export Package (nexport) v0.4.6” Copyright (c) 2023, # The Regents of the University of California, through Lawrence Berkeley # National Laboratory (subject to receipt of any required approvals from # the U.S. Dept. of Energy). All rights reserved. @@ -20,7 +20,7 @@ from setuptools import setup # Arguments -version = "0.4.5" # update __init__.py +version = "0.4.6" # update __init__.py python_version = ">=3.10" # Long description from README.md From 4126f22c66a78d853a0bbcc1685d8ca6c14aed2d Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 15:39:37 -0800 Subject: [PATCH 2/7] feat: Added class for model with multiple output nodes. --- nexport/models.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/nexport/models.py b/nexport/models.py index 97871e0..8a657d9 100644 --- a/nexport/models.py +++ b/nexport/models.py @@ -26,7 +26,7 @@ # Model classes -class FFNetwork(nn.Module): +class FFNetwork(nn.Module): # Feed Forward Network def __init__(self): super(FFNetwork, self).__init__() self.flatten = nn.Flatten() @@ -38,8 +38,20 @@ def __init__(self): nn.Linear(3, 1) # 3 weights, 1 bias ) +class MONetwork(nn.Module): # Multiple Output Network + def __init__(self): + super(MONetwork, self).__init__() + self.flatten = nn.Flatten() + self.linear_relu_stack = nn.Sequential( + nn.Linear(4, 8), + nn.ReLU(), + nn.Linear(8, 16), + nn.ReLU(), + nn.Linear(16, 8), + ) + -class BFNetwork(nn.Module): +class BFNetwork(nn.Module): # Large test Network def __init__(self): super(BFNetwork, self).__init__() self.flatten = nn.Flatten() @@ -58,7 +70,7 @@ def __init__(self): ) -class ICARNetwork(nn.Module): +class ICARNetwork(nn.Module): # ICAR Network def __init__(self): super(ICARNetwork, self).__init__() self.flatten = nn.Flatten() @@ -167,7 +179,7 @@ def __init__(self): ) -class XORNetwork(nn.Module): +class XORNetwork(nn.Module): # XOR logic Network def __init__(self): super(XORNetwork, self).__init__() self.flatten = nn.Flatten() @@ -224,7 +236,7 @@ def forward(self, input): return self.linear_step_stack(input) -class AltXORNetwork(nn.Module): +class AltXORNetwork(nn.Module): # Alternative XOR logic Network def __init__(self): super(AltXORNetwork, self).__init__() self.flatten = nn.Flatten() From 20fdf4a4c537eb5bc7165d1ed5beb02de49152ef Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 15:40:17 -0800 Subject: [PATCH 3/7] fix: Fixed exporting output layer with multiple nodes. --- nexport/pytorch/exporting.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/nexport/pytorch/exporting.py b/nexport/pytorch/exporting.py index 0676808..b3088c5 100644 --- a/nexport/pytorch/exporting.py +++ b/nexport/pytorch/exporting.py @@ -238,7 +238,7 @@ def export_to_json_experimental(model: object, filename: str = None, indent: int outfile.write(f"{indent}],\n") # end of hidden layer array if layer_type == "output_layer": outfile.write(f"{indent}\"{layer_type}\": [\n") - for neuron in model_object[layer_type]: + for n, neuron in enumerate(model_object[layer_type]): outfile.write(f"{indent}{indent}" + "{\n") for param_type in neuron.keys(): if param_type == "weights": @@ -251,7 +251,10 @@ def export_to_json_experimental(model: object, filename: str = None, indent: int outfile.write(f"],\n") if param_type == "bias": outfile.write(f"{indent}{indent}{indent}\"{param_type}\": {neuron[param_type]}\n") - outfile.write(f"{indent}{indent}" + "}\n") + if n < len(model_object[layer_type]) - 1: + outfile.write(f"{indent}{indent}" + "},\n") + else: + outfile.write(f"{indent}{indent}" + "}\n") outfile.write(f"{indent}]\n") outfile.write("}") From 472414c9c0f837f4069e8a6d406ea44de72e2e47 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 16:05:23 -0800 Subject: [PATCH 4/7] docs: Added fork count shield. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e67fb28..259231c 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@
+[![GitHub forks](https://img.shields.io/github/forks/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/network/members) ![Lines of code](https://img.shields.io/tokei/lines/github/JordanWelsman/nexport?style=for-the-badge) ![GitHub repo file count](https://img.shields.io/github/directory-file-count/JordanWelsman/nexport?style=for-the-badge) ![GitHub repo size](https://img.shields.io/github/repo-size/JordanWelsman/nexport?style=for-the-badge) From 66fc820b41f96f82a69c381b567fe503896b0ef6 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 16:07:53 -0800 Subject: [PATCH 5/7] docs: Added star count shield. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 259231c..1848edd 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@
+[![GitHub Repo stars](https://img.shields.io/github/stars/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/stargazers) [![GitHub forks](https://img.shields.io/github/forks/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/network/members) ![Lines of code](https://img.shields.io/tokei/lines/github/JordanWelsman/nexport?style=for-the-badge) ![GitHub repo file count](https://img.shields.io/github/directory-file-count/JordanWelsman/nexport?style=for-the-badge) From 30bc75e4ceb89980cd4df874ca4b8c31c56556ee Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 16:08:09 -0800 Subject: [PATCH 6/7] docs: Added watchers count shield. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1848edd..cef659f 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@
[![GitHub Repo stars](https://img.shields.io/github/stars/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/stargazers) +[![GitHub watchers](https://img.shields.io/github/watchers/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/watchers) [![GitHub forks](https://img.shields.io/github/forks/JordanWelsman/nexport?style=for-the-badge)](https://github.com/JordanWelsman/nexport/network/members) ![Lines of code](https://img.shields.io/tokei/lines/github/JordanWelsman/nexport?style=for-the-badge) ![GitHub repo file count](https://img.shields.io/github/directory-file-count/JordanWelsman/nexport?style=for-the-badge) From d373829b2afbe26c51db0d6c092bb6628c10431a Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Tue, 14 Feb 2023 17:28:13 -0800 Subject: [PATCH 7/7] feat: Added acivation function parameter to metadata generator. --- nexport/pytorch/exporting.py | 15 ++++++++------- nexport/utils.py | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/nexport/pytorch/exporting.py b/nexport/pytorch/exporting.py index b3088c5..ee2c1cd 100644 --- a/nexport/pytorch/exporting.py +++ b/nexport/pytorch/exporting.py @@ -113,18 +113,19 @@ def create_layer_object(weights: list, biases: list, verbose: int = None) -> lis return neuron_list # return constructed layer -def create_model_metadata(model_name: str, model_author: str = None, using_skip_connections: bool = None) -> dict: +def create_model_metadata(model_name: str, model_author: str = None, activation_function: str = None, using_skip_connections: bool = None) -> dict: model_metadata = { "modelName": model_name, "modelAuthor": model_author, "compilationDate": str(dt.datetime.now()), + "activationFunction": activation_function, "usingSkipConnections": using_skip_connections } return model_metadata # return model metadata object -def create_model_object(model: object, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, using_skip_connections: bool = None) -> object: +def create_model_object(model: object, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, activation_function: str = None, using_skip_connections: bool = None) -> object: """ Function which creates a model object from a collection of layers instantiated with layer @@ -136,7 +137,7 @@ def create_model_object(model: object, verbose: int = None, include_metadata: bo weights, biases = create_paramater_arrays(model=model, verbose=verbose) if include_metadata: # insert model metadata into model object - model_object["metadata"] = create_model_metadata(model_name=model_name, model_author=model_author, using_skip_connections=using_skip_connections) + model_object["metadata"] = create_model_metadata(model_name=model_name, model_author=model_author, activation_function=activation_function, using_skip_connections=using_skip_connections) if verbose >= 3: # if verbose set to at least 3 print(f"{c.YELLOW}Creating layers...{c.DEFAULT}") @@ -157,7 +158,7 @@ def create_model_object(model: object, verbose: int = None, include_metadata: bo return model_object # return constructed network -def export_to_json(model: object, filename: str = None, indent: int = None, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, using_skip_connections: bool = None) -> None: +def export_to_json(model: object, filename: str = None, indent: int = None, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, activation_function: str = None, using_skip_connections: bool = None) -> None: """ Function which exports a passed model object to a JSON file. @@ -165,7 +166,7 @@ def export_to_json(model: object, filename: str = None, indent: int = None, verb t1 = t.time() model_object = {} if include_metadata: - model_object = create_model_object(model=model, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, using_skip_connections=using_skip_connections) + model_object = create_model_object(model=model, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, activation_function=activation_function, using_skip_connections=using_skip_connections) else: model_object = create_model_object(model=model, verbose=verbose) json_object = json.dumps(obj=model_object, indent=indent) @@ -182,7 +183,7 @@ def export_to_json(model: object, filename: str = None, indent: int = None, verb print(f"{c.MAGENTA} Time taken: {c.LIGHTMAGENTA}{round(time, 2)}{c.MAGENTA}s{c.DEFAULT}") -def export_to_json_experimental(model: object, filename: str = None, indent: int = None, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, using_skip_connections: bool = None) -> None: +def export_to_json_experimental(model: object, filename: str = None, indent: int = None, verbose: int = None, include_metadata: bool = None, model_name: str = None, model_author: str = None, activation_function: str = None, using_skip_connections: bool = None) -> None: """ Function which exports a passed model object to a JSON file, but @@ -190,7 +191,7 @@ def export_to_json_experimental(model: object, filename: str = None, indent: int """ t1 = t.time() model_object = create_model_object(model=model, verbose=verbose) - model_metadata = create_model_metadata(model_name=model_name, model_author=model_author, using_skip_connections=using_skip_connections) + model_metadata = create_model_metadata(model_name=model_name, model_author=model_author, activation_function=activation_function, using_skip_connections=using_skip_connections) indent = " " with open(nexport.append_extension(filename=filename, extension="json"), "w") as outfile: diff --git a/nexport/utils.py b/nexport/utils.py index 4d884cc..822a976 100644 --- a/nexport/utils.py +++ b/nexport/utils.py @@ -54,16 +54,16 @@ def detect_framework(imported: object = sys.modules.keys()) -> str: return "none" -def export(model: object, filetype: str, filename: str = "model", indent: int = 4, verbose: int = 1, include_metadata: bool = False, model_name: str = "My Model", model_author: str = None, using_skip_connections: bool = None) -> None: +def export(model: object, filetype: str, filename: str = "model", indent: int = 4, verbose: int = 1, include_metadata: bool = False, model_name: str = "My Model", model_author: str = None, activation_function: str = None, using_skip_connections: bool = None) -> None: match nexport.__framework__: case "pytorch": match filetype: case "txt": npte.export_to_file(model=model, filename=filename) case "json": - npte.export_to_json(model=model, filename=filename, indent=indent, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, using_skip_connections=using_skip_connections) + npte.export_to_json(model=model, filename=filename, indent=indent, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, activation_function=activation_function.lower(), using_skip_connections=using_skip_connections) case "json_exp": - npte.export_to_json_experimental(model=model, filename=filename, indent=indent, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, using_skip_connections=using_skip_connections) + npte.export_to_json_experimental(model=model, filename=filename, indent=indent, verbose=verbose, include_metadata=include_metadata, model_name=model_name, model_author=model_author, activation_function=activation_function.lower(), using_skip_connections=using_skip_connections) case "csv" | "xml": raise NotImplementedError(f"This feature (exporting {nexport.__framework__} in {filetype}) has not yet been implemented.") case other: