简体   繁体   中英

two errors with neural network output

在此处输入图像描述

hi every one i make a neural network model to predict column label

i make the following steps

df=pd.read_csv('fortest.csv',low_memory=False, error_bad_lines = False)
dataset = df.values
X = dataset[:,1:8]

X.shape
min_max_scaler =MinMaxScaler()
X_scale= min_max_scaler.fit_transform(X)
X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)

# then make the neural using functional API

model = keras.Sequential()
model.add(layers.Dense(6, input_dim=6, activation='relu'))
model.add(layers.Dense(3, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(layers.Dense(3, activation='relu'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(1, activation='sigmoid', name='class'))
model.compile(loss='categorical_crossentropy',loss_weights={'class':0.5}, optimizer='adam', metrics=['accuracy'])
hist = model.fit(X_train, Y_train, batch_size=3, epochs=100, validation_data=(X_val, Y_val))


plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='lower right')
plt.show()


plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper right')
plt.show()


model.evaluate(X_test, Y_test)[1]
y_pred = model.predict(X_test)

i have two problems with this code First when i run this part

 plt.plot(hist.history['accuracy'])
    plt.plot(hist.history['val_acc'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Val'], loc='lower right')
    plt.show()

it raise error as it didn't identify Val_acc but it see val_loss > why this?

second when i try to get Y_pred to calculate precision, recall, etc using the following code

 y_pred = model.predict(X_test)

it give me y_pred as a continuous variables like (0.093,0.933) not (0 and 1) so it give me error when calculating any metrics >> any one now why these error raised any help will be appreciated

1- use loss=model.history.history and in this dict check keys by loss.keys() to see if val_acc is there or not.

2- your last layer is a sigmoid function and the output of a sigmoid function is a probability between 0 and 1.to make it binary you can choose 2 ways: - model.predict_classes(X_test) - predictions=y_pred>threshold this gives you a True False vector which you can use in metrics. threshold is optional and depend on context but you can use.5 as a common threshold.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM