Skip to content
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

Problem saving Tensorflow models #133

Closed
dsagman opened this issue Mar 27, 2024 · 2 comments · Fixed by #134
Closed

Problem saving Tensorflow models #133

dsagman opened this issue Mar 27, 2024 · 2 comments · Fixed by #134

Comments

@dsagman
Copy link
Contributor

dsagman commented Mar 27, 2024

First, thanks for making this awesome library! We are using it as part of a project where we are seeing if we can increase performance on larger network layers using approximation techniques such as approximate activation, approximate matrix multiply, and loop perforation. If we get that working, we will share our results with you.

However, I ran into an issue where save_layer in model_utils.py is giving me an error that:

AttributeError: 'Dense' object has no attribute 'output_shape'

I found that layer.output.shape works, but isn't always a tuple, which runs afoul of the JSON encoder. So I added check for that too.

I modified the code as follows and it seems to work on Macos, Linux, Windows. I've included my debugging print statements so you can see what I was looking at.

def save_layer(layer):
    # print("Debugging output shape")
    # try:
    #     print("layer.output.shape: ", layer.output.shape, type(layer.output.shape))
    # except:
    #     print("layer.output.shape: ", "None")
    # try:
    #     print("layer.output_shape: ", layer.output_shape, type(layer.output_shape))
    # except:
    #     print("layer.output_shape: ", "None")
    try:
        outshape = layer.output_shape # this is the original code, but sometimes doesn't work
    except:
        outshape = layer.output.shape # if not, use this and make sure it's a tuple so it can be JSON encoded
        if type(outshape) != tuple:  
            outshape = tuple(outshape.as_list()) 
   .... rest of code is unchanged...

I suspect something changed in the keras internals in one of the versions, but can't find any reference to it.

Regardless, I hope this helps and is useful.

@jatinchowdhury18
Copy link
Owner

Thanks for the report! I think you're correct that the issue was caused by some "internal" change in Keras or TensorFlow... I'd be curious which versions of those libraries you're using when seeing this issue?

The fix that you've suggested looks good to me. If you wouldn't mind creating a PR with this change that would be great! I can do a little more testing merge the change this weekend.

@dsagman
Copy link
Contributor Author

dsagman commented Mar 28, 2024

Great! I've submitted the pull request. Hopefully correctly. (This is my first PR.)

I am using Tensorflow 2.16.1, keras 3.1.1, and Python 3.11.6 on my Windows 11 laptop where I'm submitting from. I also have roughly the same current versions of each running on: Windows 11 desktop with NVIDIA, Mac M1 Mini, and Framework Intel 12th Gen Laptop with Manjaro (Arch variant). I've been doing development with RTNeural on all of these.

(Why not just use the desktop with the NVIDIA card? Because it's my son's so he only let's me use it when I buy him pizza and am able to distract him long enough to try to train a model.)

@jatinchowdhury18 jatinchowdhury18 linked a pull request Mar 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants