Python Input error for LSTM neural network

AI Thread Summary
The discussion revolves around a user attempting to classify a DNA sequence using a Long Short-Term Memory (LSTM) neural network but encountering a shape mismatch error during training. The user provided code for data preprocessing, including one-hot encoding for both the DNA sequences and their corresponding classes. The error message indicated a shape incompatibility between the output of the model and the expected shape of the labels. After receiving feedback, the user identified the issue as stemming from two arrays having different shapes and resolved it by utilizing the NumPy "reshape" function. The user acknowledged their beginner status in Python and expressed gratitude for the assistance.
BRN
Messages
107
Reaction score
10
Hi everyone,

I have to classify a DNA sequence with a LSTM neural network but I have a problem with the inputs shame. Both the sequence and the class are encoded with One Hot Encoding and my code is this:

[CODE lang="python" title="python code"]
import pandas as pd
import numpy as np

data = pd.read_csv('splice.data', header = None)

data_shuffled = data.sample(frac = 1).reset_index(drop = True)

# space removing
for i in range (len(data)):
data.loc[2] = data.loc[2].strip()

raw_data = np.array(data)

def one_hot_encoder(data):
x = []
y = []

for i in range (len(data)):
oh_class = np.zeros((1, 3))
if data[0] == 'EI': oh_class[0][0] = 1
elif data[0] == 'IE': oh_class[0][1] = 1
else: oh_class[0][2] = 1

y.append(oh_class)

oh_seq = np.zeros((len(data[0][2]), 4))
for j in range (len(data[0][2])):
if data[2][j] == 'A': oh_seq[j][0] = 1
elif data[2][j] == 'C': oh_seq[j][1] = 1
elif data[2][j] == 'G': oh_seq[j][2] = 1
else: oh_seq[j][3] = 1

x.append(oh_seq)

return np.array(x), np.array(y)

x_seq, y_class = one_hot_encoder(raw_data)

# Split into validation and training data
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_seq, y_class, test_size = 0.2, random_state = 1)

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout

#Initialize the RNN

model = Sequential()
model.add(LSTM(units = 50, activation = 'relu', return_sequences = True, input_shape = (x_train.shape[1], x_train.shape[2])))
model.add(Dropout(0.2))
model.add(LSTM(units = 60, activation = 'relu', return_sequences = True))
model.add(Dropout(0.3))
model.add(LSTM(units = 80, activation = 'relu', return_sequences = True))
model.add(Dropout(0.4))
model.add(LSTM(units = 120, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 3, activation='softmax'))
model.summary()

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(x_train, y_train, epochs = 100, batch_size = 80, validation_split = 0.1)
[/CODE]

The error I receive is this

[CODE lang="python" title="python error"]
ValueError: Shapes (None, 1, 3) and (None, 3) are incompatible
[/CODE]

Can anyone tell me how to solve?

Thanks!
 

Attachments

Technology news on Phys.org
Somewhere you are trying to equate or merge (or somehow deal with) two arrays of different shapes. Which line is giving the error you pasted? Please include the whole error message.
 
I'm sorry if I answer only now.

I resolved the issue and yes, and I had two arrays with different shapes. Now, I discover "reshape" function in numpy.

I'm a newbie with Python ...

Anyway, thanks for your concern!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top