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

issue 27543 workaround, allow model with inputs not used to be saved successfully #30244

Closed
wants to merge 1 commit into from

Conversation

henrysky
Copy link

This is a workaround for #27543, allowing model with inputs not used to be saved successfully

I think this PR should also be merged to r1.14 and r2.0 branch, this issue does not exist before r1.14

import numpy as np

import tensorflow as tf
import tensorflow.keras as tfk
Sequence = tfk.utils.Sequence

Dense = tfk.layers.Dense
Input = tfk.layers.Input

Model = tfk.models.Model

def special_loss(weights):
    def special_loss_internal(true, pred):
        return (true - pred / weights)
    return special_loss_internal

# Model 1 which does not have Flatten
input_tensor1 = Input(shape=[200], name='input_1')
input_tensor2 = Input(shape=[10], name='input_2')
output_tensor1 = Dense(units=10, name='output_1')(input_tensor1)
output_tensor2 = Dense(units=10, name='output_2')(input_tensor1)

neuralnet = Model(inputs=[input_tensor1, input_tensor2], outputs=[output_tensor1, output_tensor2])
neuralnet.compile(loss=special_loss(input_tensor2), optimizer='adam')

neuralnet.save("test.h5")

which performs successfully with this patch.

and to load the .h5 model back

import numpy as np

import tensorflow as tf
import tensorflow.keras as tfk
Sequence = tfk.utils.Sequence

Dense = tfk.layers.Dense
Input = tfk.layers.Input

Model = tfk.models.Model

def special_loss(weights):
    def special_loss_internal(true, pred):
        return (true - pred / weights)
    return special_loss_internal

# Model 1 which does not have Flatten
input_tensor1 = Input(shape=[200], name='input_1')
input_tensor2 = Input(shape=[10], name='input_2')
output_tensor1 = Dense(units=10, name='output_1')(input_tensor1)
output_tensor2 = Dense(units=10, name='output_2')(input_tensor1)

neuralnet = Model(inputs=[input_tensor1, input_tensor2], outputs=[output_tensor1, output_tensor2])
neuralnet.compile(loss=special_loss(input_tensor2), optimizer='adam')

model = neuralnet.load_weights('test.h5')

which also performs successfully with this patch.

@tensorflow-bot tensorflow-bot bot added the size:XS CL Change Size: Extra Small label Jun 29, 2019
@gbaned gbaned self-assigned this Jun 29, 2019
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Jun 29, 2019
@gbaned gbaned requested a review from tomhennigan June 29, 2019 08:31
@tomhennigan tomhennigan requested review from k-w-w and removed request for tomhennigan June 29, 2019 09:35
@k-w-w
Copy link
Contributor

k-w-w commented Jul 2, 2019

@henrysky Thanks for adding this workaround! This issue has been fixed in this change: 401bbfc#diff-4ee308ea180d49ae81691348531a2b6d

@k-w-w k-w-w closed this Jul 2, 2019
PR Queue automation moved this from Assigned Reviewer to Closed/Rejected Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes size:XS CL Change Size: Extra Small
Projects
PR Queue
  
Closed/Rejected
Development

Successfully merging this pull request may close these issues.

None yet

4 participants