Multilayer Perceptron (MLP) Keras tensorflow model

719 views Asked by At

I run in to an issue after I fit my model for training. Below is my code

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing
from tensorflow import keras
from keras.models import Sequential
from tensorflow.keras import layers
            
     
    
bitcoin_data = pd.read_csv("BitcoinHeistData.csv")
#first we'll need to normalize the dataset
normal = bitcoin_data
normalized_bitcoin_data=preprocessing.normalize(normal)
        
# make it into a dataframe
columns = bitcoin_data.columns
normalized_bitcoin_df = pd.DataFrame(normalized_bitcoin_data, columns=columns)
# start out splitting the data
xtrain = normalized_bitcoin_df
labels = normalized_bitcoin_df.drop('label', axis=1)
         
x, x_validate, y, y_validate = train_test_split(xtrain, labels, test_size=0.2, train_size=0.8)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.12, train_size=0.88)


*#This is my output for my variables so far. Exactly how I want to split it 70% - 20% - 10%
#X HERE SHAPE
#(838860, 10)
#x_test HERE SHAPE
#(100664, 10)
#x_validate HERE SHAPE
#(209715, 10)
#X x_train SHAPE
#(738196, 10)
#y HERE SHAPE
#(838860, 9)
#y_test HERE SHAPE
#(100664, 9)
#X y_validate SHAPE
#(209715, 9)
#X y_train SHAPE
#(738196, 9)*

model = Sequential()
     model.add(layers.Dense(64, activation='relu', kernel_initializer='glorot_normal', 
     bias_initializer='zeros', input_shape=(128,)))
     model.add(layers.BatchNormalization())
     model.add(layers.Dense(32, activation='relu', kernel_initializer='glorot_normal', 
     bias_initializer='zeros'))
     model.add(layers.BatchNormalization())
     model.add(layers.Dense(32, activation='relu', kernel_initializer='glorot_normal', 
     bias_initializer='zeros'))
     model.add(layers.Dense(32, activation='relu', kernel_initializer='glorot_normal', 
     bias_initializer='zeros'))
     model.add(layers.Dropout(0.4))
     model.add(layers.Dense(10, activation='softmax'))
     optimizer = keras.optimizers.RMSprop(lr=0.0005, rho=0)
     model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
        
     model.fit(x_train, y_train, epochs=20, batch_size=128)
    

#I get this error ValueError when i run my model.fit for x_train and y_train. I dont understand how to get around it though. Any help would be apricated

#ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 128 but received input with shape [None, 10]

1

There are 1 answers

0
fbagci On BEST ANSWER

Number of neuron in input layer(input_shape property) must be equal to number of column of x_train data set(x_train.shape[1]). Also number of neuron in output layer must be equal to number of column of y_train(y_train.shape[1]).