7

I am new to Keras and I was trying to build a text-classification CNN model using Python 3.6 when I encountered this error :

AttributeError: 'Model' object has no attribute 'name'

This is the code that I have written :

print("\nCreating Model...")
x1 = Input(shape=(seq_len1, 100), name='x1')
x2 = Input(shape=(seq_len2, 100), name='x2')
x1 = Reshape((seq_len1, embedding_dim, 1))(x1)
x2 = Reshape((seq_len2, embedding_dim, 1))(x2)

conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], 1), padding='valid', kernel_initializer='normal', activation='relu')
conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], 1), padding='valid', kernel_initializer='normal', activation='relu')
conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], 1), padding='valid', kernel_initializer='normal', activation='relu')

maxpool = MaxPool2D(pool_size=(2, 1), strides=(1,1), padding='valid')

output1 = conv_0(x1)
output1 = maxpool(output1)
output1 = conv_1(output1)
output1 = maxpool(output1)
output1 = conv_2(output1)
output1 = maxpool(output1)
.
.
# Same for output2
.
concatenated_tensor = Concatenate(axis=1)([output1, output2])
flatten = Flatten()(concatenated_tensor)
#dropout = Dropout(drop)(flatten)
output = Dense(units=1024, activation='relu')(flatten)
output = Dense(units=1024, activation='relu')(output)
output = Dense(units=1, activation='softmax')(output)

# this creates a model that includes
model = Model(inputs=[x1, x2], outputs=[output])

The error is encountered at last line. Please help me resolve this

EDIT :

Traceback (most recent call last):
  File "model.py", line 91, in <module>
    model = Model(inputs=[x1, x2], outputs=[out])
  File "/../../anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 91, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 183, in _init_graph_network
    'The tensor that caused the issue was: ' +
AttributeError: 'Model' object has no attribute 'name'
7
  • Can you add the trace, when does your code try to access the name? If you don't specify name argument it will not be set. Commented Jul 9, 2018 at 20:43
  • can you tell Keras version and os? Commented Jul 9, 2018 at 20:44
  • @L-- Keras version is : 2.2.0 and OS is : CentOS 7 Commented Jul 9, 2018 at 20:50
  • @nuric I have added the trace. Commented Jul 9, 2018 at 20:53
  • The trace doesn't look complete, are you sure you copied it correctly? Commented Jul 9, 2018 at 20:55

2 Answers 2

18

x1 and x2 point to the Reshape layers for the input and not the Input layers themselves.

Sign up to request clarification or add additional context in comments.

Comments

0

The same error popped up for me when i used custom "swish" activation function. I could resolve the same , with this code

class Swish(Activation):
    def __init__(self, activation, **kwargs):
        super(Swish, self).__init__(activation, **kwargs)
        self.__name__ = 'swish'
def swish(x,beta=1):
    return (x*sigmoid(x*beta))
get_custom_objects().update({'swish': Swish(swish)})

Comments

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.