Python 中组合分类和回归的神经网络模型

一些预测问题需要为每个输入示例预测数字和类别标签值。 如何针对需要多个输出的问题开发单独的回归和分类模型。 如何开发和评估能够同时进行回归和分类预测的神经网络模型。
回归和分类的单一模型 单独的回归和分类模型 鲍鱼数据集 回归模型 分类模型 组合回归和分类模型
# load and summarize the abalone datasetfrom pandas import read_csvfrom matplotlib import pyplot# load dataseturl = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/abalone.csv'dataframe = read_csv(url, header=None)# summarize shapeprint(dataframe.shape)# summarize first few linesprint(dataframe.head())
(4177, 9)0123456780 M 0.4550.3650.0950.51400.22450.10100.150151 M 0.3500.2650.0900.22550.09950.04850.07072 F 0.5300.4200.1350.67700.25650.14150.21093 M 0.4400.3650.1250.51600.21550.11400.155104 I 0.3300.2550.0800.20500.08950.03950.0557
# split into input (X) and output (y) variablesX, y = dataset[:, 1:-1], dataset[:, -1]X, y = X.astype('float'), y.astype('float')n_features = X.shape[1]
# split data into train and test setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)
# define the keras modelmodel = Sequential()model.add(Dense(20, input_dim=n_features, activation='relu', kernel_initializer='he_normal'))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dense(1, activation='linear'))
# compile the keras modelmodel.compile(loss='mse', optimizer='adam')
# fit the keras model on the datasetmodel.fit(X_train, y_train, epochs=150, batch_size=32, verbose=2)
# evaluate on test setyhat = model.predict(X_test)error = mean_absolute_error(y_test, yhat)print('MAE: %.3f'% error)
# regression mlp model for the abalone datasetfrom pandas import read_csvfrom tensorflow.keras.models importSequentialfrom tensorflow.keras.layers importDensefrom sklearn.metrics import mean_absolute_errorfrom sklearn.model_selection import train_test_split# load dataseturl = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/abalone.csv'dataframe = read_csv(url, header=None)dataset = dataframe.values# split into input (X) and output (y) variablesX, y = dataset[:, 1:-1], dataset[:, -1]X, y = X.astype('float'), y.astype('float')n_features = X.shape[1]# split data into train and test setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)# define the keras modelmodel = Sequential()model.add(Dense(20, input_dim=n_features, activation='relu', kernel_initializer='he_normal'))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dense(1, activation='linear'))# compile the keras modelmodel.compile(loss='mse', optimizer='adam')# fit the keras model on the datasetmodel.fit(X_train, y_train, epochs=150, batch_size=32, verbose=2)# evaluate on test setyhat = model.predict(X_test)error = mean_absolute_error(y_test, yhat)print('MAE: %.3f'% error)
Epoch145/15088/88- 0s- loss: 4.6130Epoch146/15088/88- 0s- loss: 4.6182Epoch147/15088/88- 0s- loss: 4.6277Epoch148/15088/88- 0s- loss: 4.6437Epoch149/15088/88- 0s- loss: 4.6166Epoch150/15088/88- 0s- loss: 4.6132MAE: 1.554
# encode strings to integery = LabelEncoder().fit_transform(y)n_class = len(unique(y))
# define the keras modelmodel = Sequential()model.add(Dense(20, input_dim=n_features, activation='relu', kernel_initializer='he_normal'))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dense(n_class, activation='softmax'))
# compile the keras modelmodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
# evaluate on test setyhat = model.predict(X_test)yhat = argmax(yhat, axis=-1).astype('int')acc = accuracy_score(y_test, yhat)print('Accuracy: %.3f'% acc)
# classification mlp model for the abalone datasetfrom numpy import uniquefrom numpy import argmaxfrom pandas import read_csvfrom tensorflow.keras.models importSequentialfrom tensorflow.keras.layers importDensefrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing importLabelEncoder# load dataseturl = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/abalone.csv'dataframe = read_csv(url, header=None)dataset = dataframe.values# split into input (X) and output (y) variablesX, y = dataset[:, 1:-1], dataset[:, -1]X, y = X.astype('float'), y.astype('float')n_features = X.shape[1]# encode strings to integery = LabelEncoder().fit_transform(y)n_class = len(unique(y))# split data into train and test setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)# define the keras modelmodel = Sequential()model.add(Dense(20, input_dim=n_features, activation='relu', kernel_initializer='he_normal'))model.add(Dense(10, activation='relu', kernel_initializer='he_normal'))model.add(Dense(n_class, activation='softmax'))# compile the keras modelmodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam')# fit the keras model on the datasetmodel.fit(X_train, y_train, epochs=150, batch_size=32, verbose=2)# evaluate on test setyhat = model.predict(X_test)yhat = argmax(yhat, axis=-1).astype('int')acc = accuracy_score(y_test, yhat)print('Accuracy: %.3f'% acc)
Epoch145/15088/88- 0s- loss: 1.9271Epoch146/15088/88- 0s- loss: 1.9265Epoch147/15088/88- 0s- loss: 1.9265Epoch148/15088/88- 0s- loss: 1.9271Epoch149/15088/88- 0s- loss: 1.9262Epoch150/15088/88- 0s- loss: 1.9260Accuracy: 0.274
# encode strings to integery_class = LabelEncoder().fit_transform(y)n_class = len(unique(y_class))
# split data into train and test setsX_train, X_test, y_train, y_test, y_train_class, y_test_class = train_test_split(X, y, y_class, test_size=0.33, random_state=1)
# inputvisible = Input(shape=(n_features,))hidden1 = Dense(20, activation='relu', kernel_initializer='he_normal')(visible)hidden2 = Dense(10, activation='relu', kernel_initializer='he_normal')(hidden1)
# regression outputout_reg = Dense(1, activation='linear')(hidden2)
# classification outputout_clas = Dense(n_class, activation='softmax')(hidden2)
# define modelmodel = Model(inputs=visible, outputs=[out_reg, out_clas])
# compile the keras modelmodel.compile(loss=['mse','sparse_categorical_crossentropy'], optimizer='adam')
plot_model()函数的import语句。# plot graph of modelplot_model(model, to_file='model.png', show_shapes=True)
# fit the keras model on the datasetmodel.fit(X_train, [y_train,y_train_class], epochs=150, batch_size=32, verbose=2)
# make predictions on test setyhat1, yhat2 = model.predict(X_test)
# calculate error for regression modelerror = mean_absolute_error(y_test, yhat1)print('MAE: %.3f'% error)
# evaluate accuracy for classification modelyhat2 = argmax(yhat2, axis=-1).astype('int')acc = accuracy_score(y_test_class, yhat2)print('Accuracy: %.3f'% acc)
# mlp for combined regression and classification predictions on the abalone datasetfrom numpy import uniquefrom numpy import argmaxfrom pandas import read_csvfrom sklearn.metrics import mean_absolute_errorfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing importLabelEncoderfrom tensorflow.keras.models importModelfrom tensorflow.keras.layers importInputfrom tensorflow.keras.layers importDensefrom tensorflow.keras.utils import plot_model# load dataseturl = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/abalone.csv'dataframe = read_csv(url, header=None)dataset = dataframe.values# split into input (X) and output (y) variablesX, y = dataset[:, 1:-1], dataset[:, -1]X, y = X.astype('float'), y.astype('float')n_features = X.shape[1]# encode strings to integery_class = LabelEncoder().fit_transform(y)n_class = len(unique(y_class))# split data into train and test setsX_train, X_test, y_train, y_test, y_train_class, y_test_class = train_test_split(X, y, y_class, test_size=0.33, random_state=1)# inputvisible = Input(shape=(n_features,))hidden1 = Dense(20, activation='relu', kernel_initializer='he_normal')(visible)hidden2 = Dense(10, activation='relu', kernel_initializer='he_normal')(hidden1)# regression outputout_reg = Dense(1, activation='linear')(hidden2)# classification outputout_clas = Dense(n_class, activation='softmax')(hidden2)# define modelmodel = Model(inputs=visible, outputs=[out_reg, out_clas])# compile the keras modelmodel.compile(loss=['mse','sparse_categorical_crossentropy'], optimizer='adam')# plot graph of modelplot_model(model, to_file='model.png', show_shapes=True)# fit the keras model on the datasetmodel.fit(X_train, [y_train,y_train_class], epochs=150, batch_size=32, verbose=2)# make predictions on test setyhat1, yhat2 = model.predict(X_test)# calculate error for regression modelerror = mean_absolute_error(y_test, yhat1)print('MAE: %.3f'% error)# evaluate accuracy for classification modelyhat2 = argmax(yhat2, axis=-1).astype('int')acc = accuracy_score(y_test_class, yhat2)print('Accuracy: %.3f'% acc)

Epoch145/15088/88- 0s- loss: 6.5707- dense_2_loss: 4.5396- dense_3_loss: 2.0311Epoch146/15088/88- 0s- loss: 6.5753- dense_2_loss: 4.5466- dense_3_loss: 2.0287Epoch147/15088/88- 0s- loss: 6.5970- dense_2_loss: 4.5723- dense_3_loss: 2.0247Epoch148/15088/88- 0s- loss: 6.5640- dense_2_loss: 4.5389- dense_3_loss: 2.0251Epoch149/15088/88- 0s- loss: 6.6053- dense_2_loss: 4.5827- dense_3_loss: 2.0226Epoch150/15088/88- 0s- loss: 6.5754- dense_2_loss: 4.5524- dense_3_loss: 2.0230MAE: 1.495Accuracy: 0.256
