Keras多层神经网络精度

时间:2019-05-26 20:37:20

标签: python tensorflow keras neural-network model-fitting

我使用precipitation data in Australia的Keras构建了一个简单的多层NN。该代码包含4个输入列:['MinTemp', 'MaxTemp', 'Rainfall', 'WindGustSpeed']并针对RainTomorrow输出进行训练。

我已经将数据划分为训练/测试桶,并将所有值转换为0 <= n <= 1。当我尝试运行model.fit时,我的损耗值稳定在〜13.2,但我的精度始终为0.0。记录的拟合间隔的示例是:

...
Epoch 37/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.1274 - acc: 0.0000e+00 - val_loss: -16.1168 - val_acc: 0.0000e+00
Epoch 38/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.1457 - acc: 0.0000e+00 - val_loss: -16.1168 - val_acc: 0.0000e+00
Epoch 39/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.1315 - acc: 0.0000e+00 - val_loss: -16.1168 - val_acc: 0.0000e+00
Epoch 40/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.1797 - acc: 0.0000e+00 - val_loss: -16.1168 - val_acc: 0.0000e+00
Epoch 41/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.1844 - acc: 0.0000e+00 - val_loss: -16.1169 - val_acc: 0.0000e+00
Epoch 42/200
113754/113754 [==============================] - 0s 2us/step - loss: -13.2205 - acc: 0.0000e+00 - val_loss: -16.1169 - val_acc: 0.0000e+00
Epoch 43/200
...

如何修改以下脚本,以提高准确性,并且谓词输出返回0到1之间的值(0:不下雨,1:下雨)?

import keras
import sklearn.model_selection
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler

labelencoder = LabelEncoder()

# read data, replace NaN with 0.0
csv_data = pd.read_csv('weatherAUS.csv', header=0)
csv_data = csv_data.replace(np.nan, 0.0, regex=True)

# Input/output columns scaled to 0<=n<=1
x = csv_data.loc[:, ['MinTemp', 'MaxTemp', 'Rainfall', 'WindGustSpeed']]
y = labelencoder.fit_transform(csv_data['RainTomorrow'])
scaler_x = MinMaxScaler(feature_range =(-1, 1))
x = scaler_x.fit_transform(x)
scaler_y = MinMaxScaler(feature_range =(-1, 1))
y = scaler_y.fit_transform([y])[0]

# Partitioned data for training/testing
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.2)

# model
model = keras.models.Sequential() 
model.add( keras.layers.normalization.BatchNormalization(input_shape=tuple([x_train.shape[1]])))
model.add(keras.layers.core.Dense(4, activation='relu'))
model.add(keras.layers.core.Dropout(rate=0.5))
model.add(keras.layers.normalization.BatchNormalization())
model.add(keras.layers.core.Dense(4, activation='relu'))
model.add(keras.layers.core.Dropout(rate=0.5))
model.add(keras.layers.normalization.BatchNormalization())
model.add(keras.layers.core.Dense(4, activation='relu'))
model.add(keras.layers.core.Dropout(rate=0.5))
model.add(keras.layers.core.Dense(1,   activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=["accuracy"])

callback_early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto')

model.fit(x_train, y_train, batch_size=1024, epochs=200, validation_data=(x_test, y_test), verbose=1, callbacks=[callback_early_stopping])

y_test = model.predict(x_test.values)

3 个答案:

答案 0 :(得分:1)

enter image description here

如您所见,神经网络输出(最后一层)中使用的S形激活函数的范围是0到1。

请注意,您的标签(y)已重新调整为-1到1。

我建议您将y范围更改为0到1,并保持S型输出。

答案 1 :(得分:1)

因此,S形范围从0到1。 您的MinMaxscaler会将数据从-1缩放到1。

您可以通过在输出层中将'sigmoid'替换为'tanh'来解决它,因为tanh的输出范围是-1至1

答案 2 :(得分:1)

这两个答案都可以用来解决您的网络输出与y向量值不在同一范围内的事实。将您的最后一层调整为tanh激活,或者将y-向量范围更改为[0,1]。

但是,您的网络损失函数和度量标准是出于分类目的而定义的,即您尝试回归时([-1,1]之间的连续值)。使用的最常见的损失函数和准确性指标是均方误差或均值绝对误差。因此,我建议您更改以下内容:

model.compile(loss='mse', optimizer='rmsprop', metrics=['mse, 'mae'])