Keras model.fit()显示为nan的损失

时间:2019-01-01 12:37:14

标签: python tensorflow keras

我正在尝试为仪器检测训练我的模型。从第一个时期开始,输出显示为损失:nan 。我尝试更改损失函数,激活函数,并添加了一些诸如Dropout的正则化,但是并没有影响结果。

代码如下:

from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Dropout
from keras.optimizers import Adam

import pickle
import os
import numpy as np
from sklearn.model_selection import train_test_split

def one_hot_encoding(target):
    Instruments = ['vio', 'pia', 'tru', 'flu']
    enc_tar = np.zeros([len(target), 4])

    for i in range(len(target)):
        enc_tar[i][Instruments.index(target[i])] = 1

    return enc_tar

def create_model_cnn(inp_shape):
    classifier = Sequential()

    classifier.add(Conv2D(25, kernel_size = 3, activation = 'relu', input_shape = inp_shape))
    classifier.add(Conv2D(10, kernel_size = 3, activation = 'relu'))
    classifier.add(Flatten())
    classifier.add(Dense(4, activation = 'softmax'))

    adam = Adam(0.001)

    classifier.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])

    return classifier

def create_model_mlp(inp_shape):
    classifier = Sequential()

    classifier.add(Dense(22, activation = 'softsign', input_shape = (42,)))
    classifier.add(Dropout(0.25))
    classifier.add(Dense(10, activation = 'softsign'))
    classifier.add(Dropout(0.25))
    classifier.add(Dense(4, activation = 'softmax'))

    adam = Adam(0.0001)

    classifier.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])

    return classifier

def get_weights(classifier):
    return classifier.get_weights()

def set_weights(classifier, weights):
    classifier.set_weights(weights)
    return classifier

def train_model(classifier, data, target, epoch = 40):
    classifier.fit(data, target, epochs = epoch, validation_split=0.4, batch_size = 32, verbose = 1)
    return classifier

def predict(classifier, data):
    return classifier.predict(data)

if __name__ == '__main__':

    #Get the data and the target
    [data, target] = pickle.load(open('../input/music-features/feat_targ.pickle', 'rb'))

    #if 'model.pickle' not in os.listdir():
        #Generate the classifiers
    cnn_classifier = create_model_cnn((6, 7, 1))
    mlp_classifier = create_model_mlp((42))
    #     else:
    #         #Load the existing model (from a pickle dump)
    #         classifier = pickle.load(open('model.pickle', 'rb'))

    tr_data, tst_data, tr_target, tst_target = train_test_split(data, target)

    tr_data_lin = np.array(tr_data)
    tr_data = tr_data_lin.reshape((tr_data_lin.shape[0], 6, 7, 1))

    tst_data_lin = np.array(tst_data)
    tst_data = tst_data_lin.reshape((tst_data_lin.shape[0], 6, 7, 1))

    enc_target = one_hot_encoding(tr_target)

    #print(tr_data, enc_target)
#     train_model(cnn_classifier, tr_data, enc_target)
    train_model(mlp_classifier, tr_data_lin, enc_target)

#    pickle.dump([cnn_classifier, mlp_classifier], open('model.pickle', 'wb'))

训练数据和测试数据来自形状为(15000,42)的泡菜文件。

0 个答案:

没有答案