Python 例如,Keras中未分配无功能存储器

Python 例如,Keras中未分配无功能存储器,python,keras,theano,handwriting-recognition,Python,Keras,Theano,Handwriting Recognition,我正在使用Keras OCR示例:用于在线手写识别,但在使用theano函数获取softmax输出时,在模型训练后面临内存分配问题。 x_列车的形状:(12001586,4)。我在12个批次中喂养1200个中风序列。 下面是代码片段: inputs = Input(name='the_input', shape=x_train.shape[1:], dtype='float32') rnn_encoded = Bidirectional(LSTM(64, return_sequences=Tru

我正在使用Keras OCR示例:用于在线手写识别,但在使用theano函数获取softmax输出时,在模型训练后面临内存分配问题。 x_列车的形状:(12001586,4)。我在12个批次中喂养1200个中风序列。 下面是代码片段:

inputs = Input(name='the_input', shape=x_train.shape[1:], dtype='float32')
rnn_encoded = Bidirectional(LSTM(64, return_sequences=True,kernel_initializer=init,bias_initializer=bias),name='bidirectional_1',merge_mode='concat',trainable=trainable)(inputs)
birnn_encoded = Bidirectional(LSTM(32, return_sequences=True,kernel_initializer=init,bias_initializer=bias),name='bidirectional_2',merge_mode='concat',trainable=trainable)(rnn_encoded)
trirnn_encoded=Bidirectional(LSTM(16,return_sequences=True,kernel_initializer=init,bias_initializer=bias),name='bidirectional_3',merge_mode='concat',trainable=trainable)(birnn_encoded)
output = TimeDistributed(Dense(28, name='dense',kernel_initializer=init,bias_initializer=bias))(trirnn_encoded)
y_pred = Activation('softmax', name='softmax')(output)
model=Model(inputs=inputs,outputs=y_pred)
labels = Input(name='the_labels', shape=[max_len], dtype='int32') 
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])
model = Model(inputs=[inputs, labels, input_length, label_length], outputs=loss_out)
opt=RMSprop(lr=0.001,clipnorm=1.)
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=opt)
gc.collect()
my_generator = generator(x_train,y_train,batch_size)
hist= model.fit_generator(my_generator,epochs=80,steps_per_epoch=100,shuffle=True,use_multiprocessing=False,workers=1)
model.save(mfile)
test_func = K.function([inputs], [y_pred])

内存分配错误发生在最后一行。我在AWS上使用的是一台32GB RAM机器,带有8VCPU。当我为较少的时间段(大约30-40个)运行代码时,不会出现错误,但大多数情况下,当我为大量的时间段(例如80-100个)运行代码时,会出现错误。我还附上了错误的截图。除了减少数据集大小或纪元数之外,请向我建议是否有其他解决方案。

我无法删除内存错误,但我能够获得测试结果。我只是保存了这个模型,然后在另一个脚本中加载它。加载时,我使用建议的答案从模型中获得相同的测试函数,并在循环中为其提供训练/测试数据,以获得解码字符串并计算精度。这也不会导致任何内存错误