簡體   English   中英

神經網絡 output 的兩個錯誤

[英]two errors with neural network output

在此處輸入圖像描述

大家好,我制作了一個神經網絡 model 來預測列 label

我做了以下步驟

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)

這段代碼我有兩個問題首先當我運行這部分時

 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()

它引發錯誤,因為它沒有識別 Val_acc 但它看到 val_loss > 為什么會這樣?

第二,當我嘗試讓 Y_pred 使用以下代碼計算精度、召回率等時

 y_pred = model.predict(X_test)

它給了我 y_pred 作為連續變量,如 (0.093,0.933) 而不是 (0 和 1) 所以它在計算任何指標時給我錯誤>>現在任何一個為什么這些錯誤會引起任何幫助將不勝感激

1-使用loss=model.history.history並在此字典中通過loss.keys()檢查鍵以查看 val_acc 是否存在。

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可以在度量中使用的 True False 向量。 閾值是可選的,取決於上下文,但您可以使用.5 作為通用閾值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM