Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow compute_output_shape() Not Working For Custom Layer

Tags:

tensorflow

I have created a custom layer (called GraphGather) in Keras, yet the output tensor prints as :

Tensor("graph_gather/Tanh:0", shape=(?, ?), dtype=float32)

For some reason the shape is being returned as (?,?), which is causing the next dense layer to raise the following error:

ValueError: The last dimension of the inputs to Dense should be defined. Found None.

The GraphGather layer code is as follows:

class GraphGather(tf.keras.layers.Layer):

  def __init__(self, batch_size, num_mols_in_batch, activation_fn=None, **kwargs):
    self.batch_size = batch_size
    self.num_mols_in_batch = num_mols_in_batch
    self.activation_fn = activation_fn
    super(GraphGather, self).__init__(**kwargs)

  def build(self, input_shape):
    super(GraphGather, self).build(input_shape)

 def call(self, x, **kwargs):
    # some operations (most of def call omitted)
    out_tensor = result_of_operations() # this line is pseudo code
    if self.activation_fn is not None:
      out_tensor = self.activation_fn(out_tensor)
    out_tensor = out_tensor
    return out_tensor

  def compute_output_shape(self, input_shape):
    return (self.num_mols_in_batch, 2 * input_shape[0][-1])}

I have also tried hardcoding compute_output_shape to be: python def compute_output_shape(self, input_shape): return (64, 150) ``` Yet the output tensor when printed is still

Tensor("graph_gather/Tanh:0", shape=(?, ?), dtype=float32)

which causes the ValueError written above.


System information

  • Have written custom code
  • **OS Platform and Distribution*: Linux Ubuntu 16.04
  • TensorFlow version (use command below): 1.5.0
  • Python version: 3.5.5
like image 711
Chase Armer Avatar asked Jun 25 '18 17:06

Chase Armer


People also ask

Is it possible to create custom layers in TensorFlow?

So, the idea is to create custom layers that are trainable, using the inheritable Keras layers in TensorFlow — with a special focus on Dense layers. What is a Layer? Figure 1. Layer — Dense Layer representation (Source: image created by Author)

What is the use of compute_output_shape function in TensorFlow?

The compute_output_shape function is used to determine the output shape. import tensorflow as tf class Spectrogram ( tf. keras. layers. Layer ): def __init__ ( self, num_freqs, max_freq, **kwargs ): super ( Spectrogram, self ). __init__ ( **kwargs ) self. num_freqs = num_freqs self. max_freq = max_freq self. input_spec = [ tf. keras. layers.

Does TF keras use compute_output_shape to set the output shape?

Currently tf.keras uses compute_output_shape to set the output shape only when layers are dynamic and can only be run eagerly.

Why use TensorFlow for machine learning?

Many machine learning models are expressible as the composition and stacking of relatively simple layers, and TensorFlow provides both a set of many common layers as well as easy ways for you to write your own application-specific layers either from scratch or as the composition of existing layers.


1 Answers

I had the same problem. My workaround was to add the following lines to the call method:

input_shape = tf.shape(x)

and then:

return tf.reshape(out_tensor, self.compute_output_shape(input_shape))

I haven't run into any problems with it yet.

like image 156
Johnny Avatar answered Nov 15 '22 04:11

Johnny