训练准确率先是增加,然后是断断续续的下降。修复?[Keras][TensorFlow后端]

2024-03-29 04:47:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做二进制分类。你知道吗

因此,在训练我的模型时,训练精度在提高,但在某些时期它会突然下降。下面是一个图片来说明。我做错什么了?为什么会这样?怎么解释?我怎样才能解决这个问题?你知道吗

而且,训练精度和验证精度(特别是验证精度)在大多数时间都接近1(100%),在历元周期的早期。为什么?这是好事还是坏事?我不这么认为对吗?你知道吗

这是数据:https://drive.google.com/open?id=1--1OoFHdOjb2ARyJ2dD80Zw4RkvCY0lK

“Gewicht”是输出,我在下面的代码中将其转换为1和0。你知道吗

下面的代码是我尝试过的:

代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 18 15:44:44 2019

@author: Shahbaz Shah Syed
"""

#Import the required Libraries
from sklearn.metrics import confusion_matrix, precision_score
from sklearn.model_selection import train_test_split
from keras.layers import Dense,Dropout
from keras.models import Sequential
from keras.regularizers import l2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

##EXTRACT THE DATA AND SPLITTING IN TRAINING AND TESTING-----------------------

Input = 'DATA_Gewicht.xlsx'
Tabelle = pd.read_excel(Input,names=['Plastzeit Z [s]','Massepolster [mm]',
                                'Zylind. Z11 [°C]','Entformen[s]',
                                'Nachdr Zeit [s]','APC+ Vol. [cm³]',
                                'Energie HptAntr [Wh]','Fläche WkzDr1 [bar*s]',
                                'Fläche Massedr [bar*s]',
                                'Fläche Spritzweg [mm*s]', 'Gewicht'])

Gewicht = Tabelle['Gewicht']


#Toleranz festlegen
toleranz = 0.5

#guter Bereich für Gewicht
Gewicht_mittel = Gewicht.mean()
Gewicht_abw = Gewicht.std()
Gewicht_tol = Gewicht_abw*toleranz

Gewicht_OG = Gewicht_mittel+Gewicht_tol
Gewicht_UG = Gewicht_mittel-Gewicht_tol


#Gewicht Werte in Gut und Schlecht zuordnen
G = []
for element in Gewicht:
    if element > Gewicht_OG or element < Gewicht_UG:
        G.append(0)
    else:
        G.append(1)      
G = pd.DataFrame(G)
G=G.rename(columns={0:'Gewicht_Value'})
Gewicht = pd.concat([Gewicht, G], axis=1)

#extracting columns from sheets
Gewicht_Value = Gewicht['Gewicht_Value']



x = Tabelle.drop(columns=['Gewicht'])
y = Gewicht_Value

#Split the train and test/validation set
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.10, random_state=0)
x_train.shape,y_train.shape,x_test.shape,y_test.shape


##Creating a Neural Network----------------------------------------------------

#define and use a Sequential model
model = Sequential() #Sequential model is a linear stack of layers

#Hidden Layer-1/Input Layer
model.add(Dense(200,activation='relu',input_dim=10,kernel_regularizer=l2(0.01))) #adding a layer
model.add(Dropout(0.3, noise_shape=None, seed=None))
#Hidden Layer-2
model.add(Dense(200,activation = 'relu',kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3, noise_shape=None, seed=None))
#Output layer
model.add(Dense(1,activation='sigmoid'))

#Compile the Model
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

#Check the Model summary
model.summary()


##TRAINING the Neural Network--------------------------------------------------

#Train the Model
model_output = model.fit(x_train,y_train,epochs=500,batch_size=20,verbose=1,validation_data=(x_test,y_test),)
print('Training Accuracy : ' , np.mean(model_output.history['accuracy']))
print('Validation Accuracy : ' , np.mean(model_output.history['val_accuracy']))


##CHECKING PREDICTION----------------------------------------------------------

#Do a Prediction and check the Precision
y_pred = model.predict(x_test)
rounded = [round(x[0]) for x in y_pred]
y_pred1 = np.array(rounded,dtype='int64')
confusion_matrix(y_test,y_pred1)
precision_score(y_test,y_pred1)


#Plot the model accuracy over epochs
# Plot training & validation accuracy values
plt.plot(model_output.history['accuracy'])
plt.plot(model_output.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(model_output.history['loss'])
plt.plot(model_output.history['val_loss'])
plt.title('model_output loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

我想看到的是下图。你知道吗

https://user-images.githubusercontent.com/55457221/67140808-a45e4580-f25e-11e9-89f7-1812a2d04e7d.png

https://user-images.githubusercontent.com/55457221/67140810-aaecbd00-f25e-11e9-9e76-ed737f11aee3.png

我“想看”的图像的控制台/日志(第二个2个图像):

纪元500/500 691/691[===============================]-0s 271us/步-损耗:0.5075-精度:0.7496-val\ U损耗:0.4810-val\ U精度:0.7792 训练精度:0.72937775 验证准确度:0.77620780957222


实际结果:

https://user-images.githubusercontent.com/55457221/67140782-5d705000-f25e-11e9-9425-5cc624311e39.png

https://user-images.githubusercontent.com/55457221/67140795-7d077880-f25e-11e9-955e-bfacbe2a1a92.png

我“认为错误”的图像的控制台/日志(前2个图像):

纪元500/500 774/774[===============================]-0s 506us/步-损失:0.1957-精度:0.9109-val\损失:0.0726-val\精度:1.0000 训练精度:0.9189251 验证准确度:0.97920969683151

希望你能帮我。提前谢谢各位。你知道吗


Tags: thefromhttpstestimportoutputmodel精度
1条回答
网友
1楼 · 发布于 2024-03-29 04:47:42

在训练数据之前,您应该对数据进行随机洗牌

def Randomizing():
    df = pd.DataFrame({"D1":range(5), "D2":range(5)})
    print(df)
    df2 = df.reindex(np.random.permutation(df.index))
    print(df2)


Randomizing()

这是一个示例代码,您可以在读取数据后为dataframe Tabelle执行该代码,以便对数据进行无序排列

希望这有帮助

相关问题 更多 >