Error Collections

ValueError: Shapes (None, 1) and (None, 3) are incompatible

yunajoe 2022. 9. 9. 09:21

When to occur?!

huggingface transformer 돌려보다가 오류남 
ValueError: Shapes (None, 1) and (None, 3) are incompatible

 

 


How to fix it?!

'''
Check if the last Dense Layer(output) has same number of classes as the number of target classes in the training dataset.
I made similar mistake while training the dataset and correcting it helped me.  
DenseLayer(output)결고값이 분류하려는 결과값이랑 같아야 한다고 함. 
따라서 최종분류값 갯수인, 6개에서 -> 23개로 변경해주었다.
'''


# before
y = Dense(6,activation = 'sigmoid')(out) 

# after
y = Dense(23,activation = 'sigmoid')(out)

# 출처
https://stackoverflow.com/questions/61550026/valueerror-shapes-none-1-and-none-3-are-incompatible




# before fix it 

max_len = 70
input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
input_mask = Input(shape=(max_len,), dtype=tf.int32, name="attention_mask")
embeddings = bert(input_ids,attention_mask = input_mask)[0] 
out = tf.keras.layers.GlobalMaxPool1D()(embeddings)
out = Dense(128, activation='relu')(out)
out = tf.keras.layers.Dropout(0.1)(out)
out = Dense(32,activation = 'relu')(out)
y = Dense(6,activation = 'sigmoid')(out)
model = tf.keras.Model(inputs=[input_ids, input_mask], outputs=y)
model.layers[2].trainable = True



# after fix it  

max_len = 70
input_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_ids")
input_mask = Input(shape=(max_len,), dtype=tf.int32, name="attention_mask")
embeddings = bert(input_ids,attention_mask = input_mask)[0] 
out = tf.keras.layers.GlobalMaxPool1D()(embeddings)
out = Dense(128, activation='relu')(out)
out = tf.keras.layers.Dropout(0.1)(out)
out = Dense(32,activation = 'relu')(out)
y = Dense(23,activation = 'sigmoid')(out)
model = tf.keras.Model(inputs=[input_ids, input_mask], outputs=y)
model.layers[2].trainable = True