0

I'm trying to build a Neural Network, based on the Inception architecture used for images, but for 1D vectors.

I have based the model I created on this one from the keras getting started guide from this link https://keras.io/getting-started/functional-api-guide/:

tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

input_vector = Input(shape=(71276,1),)

tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)

tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_3')(input_vector)
tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_2)

tower_3 = tf.keras.layers.MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_3)

output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])

model = tf.keras.models.Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
              optimizer=tf.keras.optimizers.Adam(lr=0.001),
              metrics=['mae'])

model.summary()

This is my code:

from keras.layers import Conv1D, MaxPooling1D, Input
from keras.models import Model

tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

input_vector = Input(shape=(71276,1),)

tower_1 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_1')(tower_1)

tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_2)

tower_3 = MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_3)

output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])

model = Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
              optimizer=tf.keras.optimizers.Adam(lr=0.001),
              metrics=['mae'])

model.summary()

When executing, I'm getting the following error, and don't really understand why:

AttributeError                            Traceback (most recent call last)
<ipython-input-9-2931ae837421> in <module>()
      6 input_vector = Input(shape=(71276,1),)
      7 
----> 8 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
      9 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)
     10 

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer.py in <lambda>(t)
   2056             `call` method of the layer at the call that created the node.
   2057     """
-> 2058     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
   2059                                         input_tensors)
   2060     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,

AttributeError: 'tuple' object has no attribute 'layer'

I don't have a lot of experience with convolutional layers so it is very possible I have made a very obvious mistake. Searching online I haven't been able to find someone else having the same problem.

I'm running this on Google Colaboratory, in a python 3 runtime.

Any help would be appreciated, thank you!

0

1 Answer 1

2

A few things:

  • All your layers have the same name? I bet that could cause lots of strange bugs
  • tower_3 doesn't have the same shape as the other two towers. It's impossible to concatenate. (You're using a MaxPooling1D, check the summary to confirm.)
  • You are mixing keras and tf.keras, that is certainly a huge problem. Choose only one.
Sign up to request clarification or add additional context in comments.

1 Comment

I have edited my code according to your first and last points. I'm still getting the same error as before. How do you propose to change my code according to your second point? Also according to the documentation the both have a 3D tensor as output, The MaxPooling1D: (batch_size, downsampled_steps, features) and the Conv1D: (batch, new_steps, filters).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.