Home > Net >  ValueError: Layer "model_1" expects 2 input(s), but it received 1 input tensors
ValueError: Layer "model_1" expects 2 input(s), but it received 1 input tensors

Time:10-29

I am trying to build a text classification model using the Bert pre train model, but I keep getting an error when I try to fit the model.

The error says

ValueError: Layer "model_1" expects 2 inputs but it received only 1 input tensor. 
Inputs received: \[\<tf.Tensor 'IteratorGetNext:0' shape=(None, 309) dtype=int32\>\]

I am also using TensorFlow and other Python libraries.

Here is my code:

import numpy as np
from data_helpers import load_data
from keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras.layers import Embedding
from sklearn.model_selection import train_test_split
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers import Dropout,Flatten
from sklearn.metrics import classification_report 
from transformers import TFBertModel

import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
from tensorflow.keras.layers import Embedding
# Data Preparation
print("Load data...")
x, y, vocabulary, vocabulary_inv = load_data()
np.save('data1-vocab.npy', vocabulary) 
sequence_length = x.shape[1]
X_train, X_test, y_train, y_test = train_test_split( x, y, test_size=0.2, random_state=42)

bert_model = TFBertModel.from_pretrained('bert-base-uncased')

def create_model(bert_model, max_len=sequence_length):
    
    ##params###
    opt = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-7)
    loss = tf.keras.losses.CategoricalCrossentropy()
    accuracy = tf.keras.metrics.CategoricalAccuracy()


    input_ids = tf.keras.Input(shape=(max_len,),dtype='int32')
    
    attention_masks = tf.keras.Input(shape=(max_len,),dtype='int32')
    
    embeddings = bert_model([input_ids,attention_masks])[1]
    
    output = tf.keras.layers.Dense(3, activation="softmax")(embeddings)
    
    model = tf.keras.models.Model(inputs = [input_ids,attention_masks], outputs = output)
    
    model.compile(opt, loss=loss, metrics=accuracy)
    
    
    return model


model = create_model(bert_model,sequence_length)
model.summary()

model.fit(X_train, y_train, epochs=32, batch_size=32,verbose=1)

I have changed the parameters of .fit() function but nothing works

CodePudding user response:

It's quite clear that your code is:

model = tf.keras.models.Model(inputs = [input_ids,attention_masks], outputs = output)

So the inputs here you need 2 inputs (input_ids and attention_masks) but in the fit function you only pass 1 inputs to the model:

model.fit(X_train, y_train, epochs=32, batch_size=32,verbose=1)

So you should learn more about the model before you fix that bug. I mean you need to know that what your model expect to and the structure of input or output of your model.

  • Related