By ninja


2018-07-10 11:07:29 8 Comments

I want a Keras model which always outputs a constant value of a desired output shape.

def build_model(input_shape, output_shape)
    input = tf.keras.layers.Input(shape=(512,512,3))
    x = tf.keras.backend.constant(1, shape=output_shape)
    output = tf.keras.layers.Lambda(lambda x: x)(x)
    model = Model(inputs=input, outputs=output)
    return model

model = build_model((512,512,3), (512,512,32))

I get the following error:

Output tensors to a Model must be the output of a TensorFlow Layer (thus holding past layer metadata). Found: Tensor("Const_3:0", shape=(512, 512, 32), dtype=float32)

How can I fix it?

Update

Input and output are indeed not connected. I want to test the performance of my processing pipeline with the lowest GPU load possible. I think that always outputting the same value without doing any computations won't use the GPU much. But I still make sure that my data is properly loaded (input layer).

1 comments

@ninja 2018-07-11 06:12:55

The issue was indeed that output and input need to be connected. I couldn't use an activation layer because the output should be of a different shape than the input. Thus I ended up concatenating the input 11 times and slice it again to get an output of the correct shape with 0 trainable parameters.

The final model building function looks like this:

def build_model(input_shape=(512,512,3)):
    input = tf.keras.layers.Input(shape=input_shape)
    lamb = tf.keras.layers.Lambda(lambda x: tf.slice(tf.concat([x]*11, axis=3), begin=(0,0,0,0), size=(-1,512,512,32)))
    output = lamb(inp)
    model = Model(inputs=input, outputs=output)
    return model

Related Questions

Sponsored Content

2 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] building keras Model ValueError

5 Answered Questions

[SOLVED] Tensor is not an element of this graph; deploying Keras model

1 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] keras: extractor one layer as a model

  • 2018-07-27 18:17:42
  • Xingyu Gu
  • 126 View
  • 0 Score
  • 1 Answer
  • Tags:   keras

1 Answered Questions

Sponsored Content