keras 中的训练问题
Training issue in keras
我正在尝试训练我的 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)
Total params: 42,592,089
Trainable params: 42,592,089
Non-trainable params: 0
您的模型有超过 4200 万个可训练参数,这对于您机器的配置(CPU、RAM 太多等),因此无法处理。有哪些选择?
- 使用较小的模型
- 使用更好更强大的计算机(当然还有 GPU)
- 考虑使用像 crestle or paperspace
这样的在线云解决方案
我正在尝试训练我的 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)
Total params: 42,592,089
Trainable params: 42,592,089
Non-trainable params: 0
您的模型有超过 4200 万个可训练参数,这对于您机器的配置(CPU、RAM 太多等),因此无法处理。有哪些选择?
- 使用较小的模型
- 使用更好更强大的计算机(当然还有 GPU)
- 考虑使用像 crestle or paperspace 这样的在线云解决方案