准确性和损失结果

时间:2019-11-24 17:47:28

标签: keras metrics

我得到了卷积神经网络的以下结果。

Results

这是一个回归问题,具有2个输入和3个输出变量。这里的损失是mse,指标是我定义的一种自定义指标。

有人能猜出在450个纪元左右我的指标结果出现波动的原因是什么(不要理会测试指标。这是错误的,我一会知道为什么训练指标就可以尽快解决它振荡这么大)?如有需要,我们将乐意提供更多信息(例如指标的定义或其他内容)。

编辑:我正在使用您可以在此处看到的代码:

Original code

我已经稍微改变了这个问题。我正在尝试进行回归,而不是作者在那里所做的分类。我一直在使用Colab,这是我对原始代码进行一些修改后编写的代码:

# Install tensorflow 2.x in Colab

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
    pass

# Import libraries  
from __future__ import absolute_import, division, print_function, unicode_literals

#Power data classification/regression with CNN
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import time as time
import matplotlib.pyplot as plt
import pydot
import csv as csv
import keras.backend as K
%matplotlib inline
print("TensorFlow version:",tf.__version__)

!wget https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_classification_labels.csv
!wget https://raw.githubusercontent.com/sibyjackgrove/CNN-on-Wind-Power-Data/master/MISO_power_data_input.csv

#Read total rows in csv file without loading into memory
def data_set_size(csv_file):
    with open(csv_file) as csvfile:
        csv_rows = 0
        for _ in csvfile:
            csv_rows += 1
    print("Numer of rows in csv:",csv_rows)
    return csv_rows-1            #Remove header from count and return

csv_file = "./MISO_power_data_classification_labels.csv"
n_train = data_set_size(csv_file)
print("Training data set size:",n_train)

#Python generator to supply batches of traning data during training with loading full data set to memory
def power_data_generator(batch_size,gen_type=''):
    valid_size = max(1,np.int(0.2*batch_size))
    while 1:
        df_input=pd.read_csv('./MISO_power_data_input.csv',usecols =['Wind_MWh','Actual_Load_MWh'],chunksize =24*(batch_size+valid_size), iterator=True)
        df_target=pd.read_csv('./MISO_power_data_classification_labels.csv',usecols =['Mean Wind Power','Standard Deviation','WindShare'],chunksize =batch_size+valid_size, iterator=True)
        for chunk, chunk2 in  zip(df_input,df_target):      
            InputX = chunk.values       
            InputX = np.resize(InputX,(batch_size+valid_size,24,2,1))
            InputY = chunk2.values
            InputY = np.resize(InputY,(batch_size+valid_size,3))
            if gen_type =='training':
                yield (InputX[0:batch_size],InputY[0:batch_size])
            elif gen_type =='validation':
                yield (InputX[batch_size:batch_size+valid_size],InputY[batch_size:batch_size+valid_size])
            elif gen_type =='inference':
                yield InputX

@tf.function
def nossa_metrica(y_true, y_pred):
  truepotventolow = K.cast(K.less_equal(y_true[:,0], 4000), 'int8')
  predpotventolow = K.cast(K.less_equal(y_pred[:,0], 4000), 'int8')
  potventolow = K.sum(truepotventolow*predpotventolow)
  truepotventomed = K.cast(K.greater(y_true[:,0], 4000) & K.less_equal(y_true[:,0], 8500), 'int8')
  predpotventomed = K.cast(K.greater(y_pred[:,0], 4000) & K.less_equal(y_pred[:,0], 8500), 'int8')
  potventomed = K.sum(truepotventomed*predpotventomed)
  truepotventohigh = K.cast(K.greater(y_true[:,0], 8500), 'int8')
  predpotventohigh = K.cast(K.greater(y_pred[:,0], 8500), 'int8')
  potventohigh = K.sum(truepotventohigh*predpotventohigh)
  truedesvpadlow = K.cast(K.less_equal(y_true[:,1], 1150), 'int8')
  preddesvpadlow = K.cast(K.less_equal(y_pred[:,1], 1150), 'int8')
  desvpadlow = K.sum(truedesvpadlow*preddesvpadlow)
  truedesvpadmed = K.cast(K.greater(y_true[:,1], 1150) & K.less_equal(y_true[:,1], 2300), 'int8')
  preddesvpadmed = K.cast(K.greater(y_pred[:,1], 1150) & K.less_equal(y_pred[:,1], 2300), 'int8')
  desvpadmed = K.sum(truedesvpadmed*preddesvpadmed)
  truedesvpadhigh = K.cast(K.greater(y_true[:,1], 2300), 'int8')
  preddesvpadhigh = K.cast(K.greater(y_pred[:,1], 2300), 'int8')
  desvpadhigh = K.sum(truedesvpadhigh*preddesvpadhigh)
  truewlslow = K.cast(K.less_equal(y_true[:,2], 0.075), 'int8')
  predwlslow = K.cast(K.less_equal(y_pred[:,2], 0.075), 'int8')
  wlslow = K.sum(truewlslow*predwlslow)   
  truewlshigh = K.cast(K.greater(y_true[:,2], 0.075), 'int8')
  predwlshigh = K.cast(K.greater(y_pred[:,2], 0.075), 'int8')
  wlshigh = K.sum(truewlshigh*predwlshigh)
  return (potventolow+potventomed+potventohigh+desvpadlow+desvpadmed+desvpadhigh+wlslow+wlshigh)/(batch_size*Yclasses)

#Define model using Keras
Yclasses = 3 #Number of output classes
batch_size = 10
valid_size = max(1,np.int(0.2*batch_size))
bias0 =  'zeros'
kernel0 = 'glorot_uniform'

model = keras.Sequential([
    tf.keras.layers.Input(shape=(24,2,1),name='InputLayer'),            
    tf.keras.layers.Conv2D(filters=4,kernel_size=(6,2),strides=(1,1),activation='relu',bias_initializer=bias0,kernel_initializer=kernel0,name='ConvLayer1'),
    tf.keras.layers.Conv2D(filters=4,kernel_size=(6,1),strides=(1,1),activation='relu',bias_initializer=bias0,kernel_initializer=kernel0,name='ConvLayer2'),
    tf.keras.layers.Flatten(name="Flatten"),
    tf.keras.layers.Dense(units = 8,activation='relu',bias_initializer=bias0,kernel_initializer=kernel0,name='FeedForward1'),
    tf.keras.layers.Dense(units = Yclasses,bias_initializer=bias0,kernel_initializer=kernel0,name='OutputLayer'),
])
model.compile(loss='mse',optimizer='adam',verbose = 2,metrics=[nossa_metrica])
model.summary()

train_generator= power_data_generator(batch_size=batch_size,gen_type='training')
valid_generator= power_data_generator(batch_size=batch_size,gen_type='validation')
number_of_batches = np.int32(n_train/(batch_size+max(1,np.int32(0.2*batch_size)))) 
#Training starts
t = time.time()
history = model.fit(train_generator, steps_per_epoch= number_of_batches,epochs=600,validation_data=valid_generator, validation_steps=number_of_batches,verbose=2)
print("Time: %.3f minutes" % ((time.time() - t)/60))
model.save('model.h5')
print("Model Saved")

import matplotlib.pyplot as plt
plt.plot(history.history['nossa_metrica'])
plt.plot(history.history['val_nossa_metrica'])
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(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

0 个答案:

没有答案