喀拉拉邦的培训问题

时间:2018-12-08 11:12:06

标签: python-3.x keras sentiment-analysis

我正在尝试训练我的lstm模型以进行情感分析,但是在显示以下输出后,该程序根本无法继续执行:

F:\Softwares\Anaconda\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Extracting features & training batches
Training...
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, 134, 70)           42481880  
_________________________________________________________________
dropout_1 (Dropout)          (None, 134, 70)           0         
_________________________________________________________________
lstm_1 (LSTM)                (None, 128)               101888    
_________________________________________________________________
dense_1 (Dense)              (None, 64)                8256      
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0         
_________________________________________________________________
activation_1 (Activation)    (None, 64)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 65        
_________________________________________________________________
activation_2 (Activation)    (None, 1)                 0         
=================================================================
Total params: 42,592,089
Trainable params: 42,592,089
Non-trainable params: 0
_________________________________________________________________
None
Train on 360000 samples, validate on 90000 samples
Epoch 1/8
2018-12-08 15:56:04.680836: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

下面的代码已被注释掉了,因为它被用来预先在磁盘上保存一些文本数据。现在,代码仅使用训练和测试文本数据来训练lstm模型。如下所示:

import pandas as pd
import Preprocessing as pre
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.utils import shuffle
import pickle
import numpy as np
import sys
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.layers import LSTM
from keras.preprocessing.sequence import pad_sequences
from keras.models import model_from_json
from keras.preprocessing.text import Tokenizer
import os

# fileDir = os.path.dirname(os.path.realpath('__file__'))
# df = pd.read_csv(os.path.join(fileDir, '../Dataset/tweets.csv'),header=None,encoding = "ISO-8859-1")
# df=shuffle(df)
# length=df.size
#
# train=[]
# test=[]
# Y=[]
# Y2=[]
#
# count=450000
# for a in range(450000):   #loading data
#     b=pre.preprocess_tweet(df[1][a])
#     label=int(df[0][a])
#     train.append(b)
#     Y.append(label)
#     count-=1
#     print("Loading training data...",  count)
#
# with open('training_data(latest).obj', 'wb') as fp:
#     pickle.dump(train, fp)
# with open('training_labels(latest).obj', 'wb') as fp:
#     pickle.dump(Y, fp)
with open ('training_data(latest).obj', 'rb') as fp:
    train = pickle.load(fp)
with open ('training_labels(latest).obj', 'rb') as fp:
    Y = pickle.load(fp)

# count=156884
# for a in range(450000,606884):   #loading testin data
#     b = pre.preprocess_tweet(df[1][a])
#     label=int(df[0][a])
#     test.append(b)
#     Y2.append(label)
#     count-=1
#     print("Loading testing data...",  count)
#
# with open('testing_data(latest).obj', 'wb') as fp:
#     pickle.dump(test, fp)
# with open('testing_labels(latest).obj', 'wb') as fp:
#     pickle.dump(Y2, fp)

with open ('testing_data(latest).obj', 'rb') as fp:
    test = pickle.load(fp)
with open ('testing_labels(latest).obj', 'rb') as fp:
    Y2 = pickle.load(fp)

# vectorizer = CountVectorizer(analyzer = "word",tokenizer = None, preprocessor = None, stop_words = None, max_features = 2000)
# # # fit_transform() does two functions: First, it fits the model
# # # and learns the vocabulary; second, it transforms our training data
# # # into feature vectors. The input to fit_transform should be a list of
# # # strings.
#
# train = vectorizer.fit_transform(train)
# test = vectorizer.transform(test)
tokenizer = Tokenizer(split=' ')
tokenizer.fit_on_texts(train)
train = tokenizer.texts_to_sequences(train)
max_words = 134
train = pad_sequences(train, maxlen=max_words)
tokenizer.fit_on_texts(test)
test = tokenizer.texts_to_sequences(test)
test = pad_sequences(test, maxlen=max_words)

print('Extracting features & training batches')

print("Training...")
embedding_size=32
model = Sequential()
model.add(Embedding(606884, 70, input_length=134))
model.add(Dropout(0.4))
model.add(LSTM(128))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

batch_size = 100
num_epochs = 8

model.fit(train, np.array(Y),  batch_size=batch_size, epochs=num_epochs ,validation_split=0.2,shuffle=True,verbose=2)

# Save the weights
model.save_weights('LSTM_model_weights_updated.h5')

# Save the model architecture
with open('LSTM_model_updated.json', 'w') as f:
    f.write(model.to_json())
# #
# Model reconstruction from JSON file
# with open(os.path.join(fileDir, '../Dataset/LSTM_model.json'), 'r') as f:
#     model = model_from_json(f.read())
#
# # Load weights into the new model
# model.load_weights(os.path.join(fileDir, '../Dataset/LSTM_model_weights.h5'))
# model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

scores = model.evaluate(test, np.array(Y2))
print('Evaluation Test accuracy:', scores[1])


count=0
sum=0
#
#
b=model.predict(test)
for a in b:
    print(count)
    if a<0.5:
        sum = sum + abs(Y2[count] - 0)  # error finding
    else:
        sum=sum+ abs(Y2[count]-1)    #error finding
    count+=1

acc=100-((sum/156884)*100)
print ("Accuracy=",acc,"count",count)

1 个答案:

答案 0 :(得分:1)

  

总参数:42,592,089
  可训练的参数: 42,592,089
  不可训练的参数:0

您的模型具有 4,200万个可训练参数,对于您的计算机配置(CPU,RAM等)来说,太多,因此无法处理。有哪些选择?

  • 使用较小的模型
  • 使用功能更强大的计算机(当然带有GPU)
  • 考虑使用crestlepaperspace之类的在线云解决方案