Home > Software engineering >  Concatenation layer raise Value Error: as_list() is not defined on an unknown Tensor Shape
Concatenation layer raise Value Error: as_list() is not defined on an unknown Tensor Shape

Time:05-15

I'm Trying to making DNN using Wide Deep Nural Network using keras the following code produces the following after trying to implement it, I also making my custome activation function Randomized Relu here is the code for the Activation:

class RRELU(keras.layers.Layer):
    def __init__(self, lower, upper, **kwargs):
        super().__init__(**kwargs)
        self.lower = lower
        self.upper = upper
def call(self, inputs, training=None):
    if training:
        return tf.where(inputs >= 0, inputs, inputs / 
                        np.random.uniform(self.lower, self.upper, 1)[0])
    return tf.where(inputs >= 0, inputs, 2*inputs/(self.lower self.upper))
def compute_output_shape(self, batch_input_shape):
  return tf.TensorShape(batch_input_shape.as_list())
def get_config(self):
  base_config = super().get_config()
  return {**base_config, 'lower': self.lower, 'upper': self.upper}

here is the code for the model.

layer = []
layer.append(keras.layers.Input(shape = X_train.toarray().shape[1:]))
for i in range(10):
  layer.append(keras.layers.Dense(300))
  layer.append(RRELU(3, 8))
layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
layer.append(keras.layers.Dense(2, activation='softmax'))
model39 = keras.models.Sequential(layer)

This line is producing the error:

layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))

Error msg:

ValueError                                Traceback (most recent call last)
<ipython-input-28-ad6c20fa06bd> in <module>()
      4   layer.append(keras.layers.Dense(300))
      5   layer.append(RRELU(3, 8))
----> 6 layer.append(keras.layers.Concatenate()([layer[0], layer[-1]]))
      7 layer.append(keras.layers.Dense(2, activation='softmax'))
      8 model39 = keras.models.Sequential(layer)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py in as_list(self)
   1221     """
   1222     if self._dims is None:
-> 1223       raise ValueError("as_list() is not defined on an unknown TensorShape.")
   1224     return [dim.value for dim in self._dims]
   1225 

ValueError: as_list() is not defined on an unknown TensorShape.

Thnks in Advance for any helpful comments.

CodePudding user response:

The reason you get this error is explained in the documentation of tf.keras.Sequential:

"A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor."

This is due to the fact that when a layer is added to a Sequential model, the inputs of the added layer are automatically set to be the outputs of the previous one. Having a concatenation like yours would imply that the output of the first Dense layer would be used by both the next layer and the Concatenate layer downstream, which is against the idea of the Sequential models.

The solution is using the Functional API instead:

import numpy as np
X_train = np.zeros((1, 100))

layer = []
layer.append(keras.layers.Input(shape=X_train.shape[1:]))
layer.append(keras.layers.Dense(300)(layer[-1]))
for i in range(10):
  layer.append(keras.layers.Dense(300)(layer[-1]))
  layer.append(RRELU(3, 8)(layer[-1]))
layer.append(keras.layers.Dense(2, activation='softmax')(layer[-1]))
layer.append(keras.layers.Concatenate()([layer[1], layer[-1]]))

model = tf.keras.Model(inputs=layer[0], outputs=layer[-1])
model.summary()
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_1 (InputLayer)           [(None, 100)]        0           []                               
                                                                                                  
 dense (Dense)                  (None, 300)          30300       ['input_1[0][0]']                
                                                                                                  
 dense_1 (Dense)                (None, 300)          90300       ['dense[0][0]']                  
                                                                                                  
 rrelu (RRELU)                  (None, 300)          0           ['dense_1[0][0]']                
                                                                                                  
 dense_2 (Dense)                (None, 300)          90300       ['rrelu[0][0]']                  
                                                                                                  
 rrelu_1 (RRELU)                (None, 300)          0           ['dense_2[0][0]']                
                                                                                                  
...
                                                                                                  
 dense_10 (Dense)               (None, 300)          90300       ['rrelu_8[0][0]']                
                                                                                                  
 rrelu_9 (RRELU)                (None, 300)          0           ['dense_10[0][0]']               
                                                                                                  
 dense_11 (Dense)               (None, 2)            602         ['rrelu_9[0][0]']                
                                                                                                  
 concatenate (Concatenate)      (None, 302)          0           ['dense[0][0]',                  
                                                                  'dense_11[0][0]']               
                                                                                                  
==================================================================================================
Total params: 933,902
Trainable params: 933,902
Non-trainable params: 0
__________________________________________________________________________________________________
  • Related