Skip to content

Commit

Permalink
Merge pull request #23 from BerkeleyLab/main
Browse files Browse the repository at this point in the history
Merging now.
  • Loading branch information
JordanWelsman authored Feb 16, 2023
2 parents df29a4e + 6363fcd commit 47f46f9
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion COPYRIGHT.md
Original file line number Diff line number Diff line change
@@ -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 [[email protected]](mailto:[email protected]).

Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
</div>
<div align="center">

[![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)
Expand Down
4 changes: 2 additions & 2 deletions nexport/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nexport/calculators.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/colors.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/generic.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
24 changes: 18 additions & 6 deletions nexport/models.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion nexport/pytorch/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/pytorch/activation.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
24 changes: 14 additions & 10 deletions nexport/pytorch/exporting.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -157,15 +158,15 @@ 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.
"""
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)
Expand All @@ -182,15 +183,15 @@ 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
keeps array elements on one line.
"""
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:
Expand Down Expand Up @@ -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":
Expand All @@ -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("}")

Expand Down
2 changes: 1 addition & 1 deletion nexport/pytorch/importing.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/tensorflow/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/tensorflow/exporting.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nexport/tensorflow/importing.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 4 additions & 4 deletions nexport/utils.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 47f46f9

Please sign in to comment.