如何将字符串值传递到情感分析RNN顺序模型并获得预测

时间:2018-10-23 05:58:43

标签: python machine-learning keras recurrent-neural-network sentiment-analysis

我使用自己的数据集以及一些较小的修改来重新创建了一个情感分析机器学习项目,以缩短完成时间,我可以创建良好的模型,对其进行编译,拟合和测试,而不会出现问题,但是问题出在如何向模型传递新的字符串/文章,作为回报,它将通过关于字符串注释是肯定还是否定的预测,并希望有人可以帮助我。

我在下面发布了我的代码以供您查看。

class tensor_rnn():
def __init__(self, corp_paths, hidden_layers=3, loadfile=True):
    self.h_layers = hidden_layers
    self.num_words = []
    if loadfile == False:
        data_set = pd.DataFrame(columns=['Article', 'Polarity'])
        craptopass = []
        for files in os.listdir(corp_paths[0]):
            with open(corp_paths[0] + '\\' + files, 'r', errors='replace') as text_file:
                line = text_file.readline().replace('|', '')
                text_file.close()
            if len(line.split(' ')) > 3:
                line = ''.join([i if ord(i) < 128 else ' ' for i in line])
                craptopass.append([line, 1])
        good = data_set.append(pd.DataFrame(craptopass, columns=['Article', 'Polarity']), ignore_index=True)
        data_set = pd.DataFrame(columns=['Article', 'Polarity'])
        craptopass = []
        for files in os.listdir(corp_paths[1]):
            with open(corp_paths[1] + '\\' + files, 'r', errors='replace') as text_file:
                line = text_file.readline().replace('|', '')
                text_file.close()
            if len(line.split(' ')) > 3:
                line = ''.join([i if ord(i) < 128 else ' ' for i in line])
                craptopass.append([line, -1])
        bad = data_set .append(pd.DataFrame(craptopass, columns=['Article', 'Polarity']), ignore_index=True)
        for line in good['Article'].tolist():
            counter = len(line.split())
            self.num_words.append(counter)

        for line in bad['Article'].tolist():
            counter = len(line.split())
            self.num_words.append(counter)
        self.features = pd.concat([good, bad]).reset_index(drop=True)
        # self.features = self.features.str.replace(',', '')
        self.features.to_csv('Headlines.csv', sep='|')
    else:
        self.features = pd.read_csv('Headlines.csv', sep='|')
        self.features['totalwords'] = self.features['Article'].str.count(' ') + 1
        self.num_words.extend(self.features['totalwords'].tolist())

    self.features = shuffle(self.features)
    self.max_len = len(max(self.features['Article'].tolist()))
    tokenizer = self.tok = preprocessing.text.Tokenizer(num_words=len(self.num_words), split=' ')
    self.tok.fit_on_texts(self.features['Article'].values)
    X = tokenizer.texts_to_sequences(self.features['Article'].values)
    self.X = preprocessing.sequence.pad_sequences(X)
    self.Y = pd.get_dummies(self.features['Polarity']).values
    self.X_train, self.X_test, self.Y_train, self.Y_test = train_test_split(self.X, self.Y,
                                                                            test_size=0.20, random_state=36)

def RNN(self):
    embed_dim = 128
    lstm_out = 128
    model = Sequential()
    model.add(Embedding(len(self.num_words), embed_dim, input_length=self.X.shape[1]))
    model.add(Bidirectional(CuDNNLSTM(lstm_out)))
    model.add(Dropout(0.2))
    model.add(Dense(2, activation='softmax'))
    opt = Adam(lr=0.0001, decay=1e-4)   #1e-3
    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model

def model_train(self):
    self.model = self.RNN()

def model_test(self):
    batch_size = 128
    self.model.fit(self.X_train, self.Y_train, epochs=4, batch_size=batch_size, verbose=2,
                                callbacks=[EarlyStopping(monitor='val_loss', min_delta=0.0001,
                                                         patience=5, verbose=2, mode='auto')], validation_split=0.2)


if __name__ == "__main__":
    paths = 'PATHS TO ARTICLES'
    a = tensor_rnn([paths + '\\pos', paths + '\\neg'])
    a.model_train()
    a.model_test()
    a.model.save('RNNModelArticles.h5', include_optimizer=True)

1 个答案:

答案 0 :(得分:1)

您所需要做的就是以与预处理训练文本相同的方式对要输入到模型的新文本进行预处理。之后,您应该有一个预测方法,该方法将以模型在训练中输出预测的相同方式输出预测。因此,在predict方法中,您应该编写如下内容:

git checkout -b <my-new-branch>
git add *
git commit -m "updated"

这是否为您澄清了事情?