1

在适合我的模型进行训练后,我遇到了一个问题。下面是我的代码

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)
    

#我在为 x_train 和 y_train运行model.fit时收到此错误 ValueError 。我不明白如何解决它。任何帮助都会得到帮助

#ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 128,但接收到形状为 [None, 10] 的输入

4

1 回答 1

1

输入层中的神经元数(input_shape 属性)必须等于 x_train 数据集的列数(x_train.shape[1])。此外,输出层中的神经元数必须等于 y_train(y_train.shape[1]) 的列数。

于 2020-10-19T14:16:52.403 回答