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/README.md b/README.md index e67fb28..cef659f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,9 @@
+[![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) ![GitHub repo size](https://img.shields.io/github/repo-size/JordanWelsman/nexport?style=for-the-badge) 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..8a657d9 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. @@ -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() 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..ee2c1cd 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. @@ -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: @@ -238,7 +239,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 +252,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("}") 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..822a976 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. @@ -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: 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