神经网络中的损失并没有减少

时间:2017-12-29 14:34:56

标签: neural-network keras

我正在实施this paper所描述的CNN变体。 我的问题是损失并没有减少,我也不明白为什么。关于准确性也必须说同些(坚持0.5或更低)。

这是2类分类的问题。我正在使用此website

中的数据

我怀疑优化器所以我改变它没有任何改进。我很确定我使用的数据是可以的,因为我在LSTM上使用它并且分类器很好。

这是我的代码:

from keras.layers import Embedding
from keras.layers import Conv2D
from keras.models import Sequential
from keras.layers import MaxPooling2D
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Input
from keras import backend as K

from keras.models import Model
import tensorflow as tf
from sklearn import preprocessing
import keras
import numpy as np
import pdb

#using multiple filters
config = tf.ConfigProto()

# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True

# Only allow a total of half the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 0.7

# Create a session with the above options specified.
config.gpu_options.per_process_gpu_memory_fraction = 0.65
K.tensorflow_backend.set_session(tf.Session(config=config))
class Classifier():
    def __init__(self,Vocab,maxlen=75):
        self.model=Sequential()
        self.EMBED_DIM=30
        self.batch_size=30
        self.MAX_SEQUENCE_LENGTH=maxlen
        self.nb_labels=2
        self.model = Sequential()
        self.Vocab=Vocab
    def fit(self, X,y):
        #pdb.set_trace()

        mainIn=Input(shape=(self.MAX_SEQUENCE_LENGTH,), dtype='int32', name='main_input')

        x=Embedding(len(self.Vocab)+2,self.EMBED_DIM,input_length=self.MAX_SEQUENCE_LENGTH)(mainIn)

        x=Reshape((1,self.MAX_SEQUENCE_LENGTH, self.EMBED_DIM))(x)

        x1=Conv2D(128, strides=2,kernel_size=5 ,activation="relu", padding='same')(x)

        x1=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-5+1,1),padding='same')(x1)
        x1=Flatten()(x1)
        x2=Conv2D(128, strides=2, kernel_size=4, activation="sigmoid", padding='same')(x)
        x2=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-4+1,1),padding='same')(x2)

        x2=Flatten()(x2)
        x3=Conv2D(128, strides=2, kernel_size=3, activation="tanh", padding='same')(x)
        x3=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-3+1,1),padding='same')(x3)

        x3=Flatten()(x3)

        combinedX=keras.layers.concatenate([x1,x2,x3],axis=1)


        combinedX=Dense(64, activation="relu")(combinedX)
        combinedX=Dropout(0.2)(combinedX)
        #output=Dense(self.nb_labels, activation="sigmoid")(combinedX)
        #output=Dense(2, activation="softmax")(combinedX)
        output=Dense(1, activation="sigmoid")(combinedX)


        #encoder =preprocessing.LabelEncoder()
        #encoder.fit(y)
        #encoded_Y = encoder.transform(y)
        #labels=keras.utils.to_categorical(encoded_Y, num_classes=2)
        labels=y
        pdb.set_trace()
        inputs2=X
        self.model = Model(inputs=mainIn, outputs=output)

       # self.model.compile(loss='binary_crossentropy',
       #                 optimizer='adam',
       #                 metrics=['acc'])

        self.model.compile(loss='binary_crossentropy',
                        optimizer='rmsprop',
                        metrics=['acc'])

        self.model.fit(inputs2,labels,epochs=7, batch_size=self.batch_size)
    def predict(self, X):
        return self.model.predict(np.array(X))

    def predict_proba(self, X):

        return self.model.predict(np.array(X), self.batch_size)

这是我预处理数据的代码:

     #loading the file
file2=pd.read_csv("Sentiment_Analysis Dataset.csv",error_bad_lines=False)
        #splitting into train et test set

from sklearn.model_selection import train_test_split
Text=list(file2.SentimentText)
file2.groupby('Sentiment').count()
train_data,test_data,train_label,test_label=train_test_split(Text, file2.Sentiment, test_size=0.4, random_state=42)
#Buidling the dictionary

vocabDic=dict()
for document in train_data:
    document=document.split(" ")
    for word in document:
        if word not in vocabDic:
            vocabDic[word]=len(vocabDic)+1
vocabDic['unk']=len(vocabDic)+1

#coding the documents
def codeDocuments(documents,dictionnary):
    documentsArray=list()
    for i,document in enumerate(documents):
        tempList=list()
        document=document.split(" ")
        for word in document:
            if word in vocabDic:
                word=vocabDic[word]
            else:
                word=vocabDic['unk']
            tempList.append(word)
        documentsArray.append(tempList)
    return np.array(documentsArray)
train_docs=codeDocuments(train_data,vocabDic)
test_docs=codeDocuments(test_data,vocabDic)

#padding the documents


from keras.preprocessing import sequence

maxlen=75
train_set = sequence.pad_sequences(train_docs, maxlen=maxlen)
test_set = sequence.pad_sequences(test_docs, maxlen=maxlen)
#calling the model
model=Classifier(vocabDic,maxlen)
model.fit(train_set[:50000],train_label[:50000])

0 个答案:

没有答案