'Sequence to sequence input dimension error
from re import search
import numpy as np
import tensorflow as tf
import pandas as pd
from tensorflow.keras.layers import Dense,LSTM,Embedding,Input,Bidirectional
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
df=pd.read_excel(r"C:/Users/Shubham/Downloads/pdf.xlsx")
df.drop('Unnamed: 0',axis=1,inplace=True)
df.columns=['incorrect','correct']
df['incorrect']=df['incorrect'].apply(lambda x: str(x))
df['correct']=df['correct'].apply(lambda x: str(x))
train=df[:1000]
test=df[1000:1100]
cv=df[1100:]
train['correct_inp']="$"+train['correct'].astype(str)
train['correct_out']=train['correct'].astype(str)+"@"
cv['correct_inp']="$"+cv['correct'].astype(str)
cv['correct_out']=cv['correct'].astype(str)+"@"
# tokenizing encoder input
tokenizer_incorr = Tokenizer(filters="",lower=False)
tokenizer_incorr.fit_on_texts(train["incorrect"].values)
incorr_train = np.array(tokenizer_incorr.texts_to_sequences(train["incorrect"].values))
incorr_cv = np.array(tokenizer_incorr.texts_to_sequences(cv["incorrect"].values))
print("vocab size of incorrrect sentences is",len(tokenizer_incorr.word_index))
# tokenizing decoder input
tokenizer_corr_inp = Tokenizer(filters="",lower=False)
tokenizer_corr_inp.fit_on_texts(train["correct_inp"].values)
corr_train_inp = np.array(tokenizer_corr_inp.texts_to_sequences(train["correct_inp"].values))
corr_cv_inp = np.array(tokenizer_corr_inp.texts_to_sequences(cv["correct_inp"].values))
print("vocab size of corrrect sentences is",len(tokenizer_corr_inp.word_index))
# tokenizing decoder output
tokenizer_corr_out = Tokenizer(filters="",lower=False)
tokenizer_corr_out.fit_on_texts(train["correct_out"].values)
corr_train_out = np.array(tokenizer_corr_out.texts_to_sequences(train["correct_out"].values))
corr_cv_out = np.array(tokenizer_corr_inp.texts_to_sequences(cv["correct_out"].values))
# padding training data
# here we are keeping max length a 25, as we ecided from EDA
incorr_train =
np.array(pad_sequences(incorr_train,maxlen=160,padding="post",truncating='post'))
corr_train_inp =
np.array(pad_sequences(corr_train_inp,maxlen=160,padding="post",truncating='post'))
corr_train_out =
np.array(pad_sequences(corr_train_out,maxlen=160,padding="post",truncating='post'))
# padding validation data
incorr_cv = np.array(pad_sequences(incorr_cv,maxlen=160,padding="post",truncating='post'))
corr_cv_inp = np.array(pad_sequences(corr_cv_inp,maxlen=160,padding="post",truncating='post'))
corr_cv_out = np.array(pad_sequences(corr_cv_out,maxlen=160,padding="post",truncating='post'))
encoder_inputs = Input(shape=(160,))
encoder_embedding = Embedding(len(tokenizer_incorr.word_index)+1,128)(encoder_inputs)
encoder_lstm=Bidirectional(LSTM(256,return_sequences=True,return_state=True))
(encoder_embedding)
encoder_outputs, fw_state_h, fw_state_c, bw_state_h, bw_state_c =encoder_lstm
decoder_inputs=Input(shape=(160,))
decoder_embedding=Embedding(len(tokenizer_incorr.word_index)+1,128)(decoder_inputs)
decoder_lstm=Bidirectional(LSTM(256,return_sequences=True,return_state=True))
(decoder_embedding,initial_state=[fw_state_h, fw_state_c, bw_state_h, bw_state_c])
decoder_outputs=decoder_lstm
decoder_outputs=Dense(len(tokenizer_corr_inp.word_index)+1,activation='softmax')
(decoder_outputs) model=Model([encoder_inputs,decoder_inputs],decoder_outputs) model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit([incorr_train,corr_train_inp],corr_train_out,batch_size=32,epochs=10,
validation_data=([incorr_cv,corr_cv_inp],corr_cv_out))
This is the error i am getting right now. Please help
ValueError: Layer "dense_5" expects 1 input(s), but it received 5 input tensors. Inputs received: [<tf.Tensor 'Placeholder:0' shape=(None, 160, 512) dtype=float32>, <tf.Tensor 'Placeholder_1:0' shape=(None, 256) dtype=float32>, <tf.Tensor 'Placeholder_2:0' shape=(None, 256) dtype=float32>, <tf.Tensor 'Placeholder_3:0' shape=(None, 256) dtype=float32>, <tf.Tensor 'Placeholder_4:0' shape=(None, 256) dtype=float32>]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
