2016-11-25 145 views
0

我想在一个使用TFLearn的python模块中训练两个模型。我为所有图层使用restore=False。如果模型中的一个已被注释掉,因此只有一种模式被训练不会发生如何在TFLearn中的一个python模块中训练多个模型?

Traceback (most recent call last): 
    File "multiple_models.py", line 76, in <module> 
    a_model.fit(X_inputs=X, Y_targets=Y, validation_set=0.1, show_metric=True, batch_size=None, shuffle=True, n_epoch=20) # 100% of data being used for validation 
    File "/Users/swarbhanu/miniconda2/lib/python2.7/site-packages/tflearn/models/dnn.py", line 182, in fit 
    self.targets) 
    File "/Users/swarbhanu/miniconda2/lib/python2.7/site-packages/tflearn/utils.py", line 289, in feed_dict_builder 
    feed_dict[net_inputs[i]] = x 
IndexError: list index out of range 

此错误:当第二个模型的拟合方法叫我收到错误。任何帮助将是伟大的!我已经经历了(据我所知)所有以前的堆栈溢出问题,关于在tflearn或tensorflow中训练或加载多个模型的问题,但建议的解决方案(例如:restore=False或使用variable_scope)对我无效。在我的使用场景中,使用一个模块来训练(以及稍后加载和调整)多个模型是非常重要的。代码如下:

import os.path 
import numpy as np 
import tflearn 
from tflearn.layers.core import input_data, fully_connected 
from tflearn.layers.normalization import batch_normalization 
from tflearn.layers.recurrent import bidirectional_rnn, BasicLSTMCell 
from tflearn.layers.estimator import regression 

import tensorflow as tf 

i_model_file = 'example1.tfl' 
a_model_file = 'example2.tfl' 

batch_size = 50 
sequence_len = 10 
sequence_unit_array_size = 300 
output_array_size = 1 

# Set parameters 
i_num_lstm_units = 128 
i_num_labels = 5 
i_learning_rate = 0.001 

a_num_lstm_units = 128 
a_num_labels = 4 
a_learning_rate = 0.001 

def create_data(batch_size, sequence_len, sequence_unit_array_size, num_labels): 
    shape_x = (batch_size,sequence_len,sequence_unit_array_size) 
    shape_y = (batch_size, num_labels) 
    X = np.random.random(shape_x) 
    Y = np.zeros(shape_y) 
    ind = np.random.randint(low=0,high=num_labels,size=batch_size) 
    for b in xrange(batch_size): 
     Y[b][ind[b]] = 1 
    return X, Y 

def create_classification_model(target_name, num_lstm_units, num_labels, learning_rate, saved_model_file): 
    with tf.variable_scope(target_name): 
     input_layer = input_data(shape=[None, sequence_len, sequence_unit_array_size]) 
     conv = tflearn.conv_1d(input_layer, nb_filter=2, filter_size=3, regularizer='L2', weight_decay=0.0001,restore=False) 
     bnorm1 = batch_normalization(conv,restore=False) 
     birnn = bidirectional_rnn(bnorm1, BasicLSTMCell(num_lstm_units), BasicLSTMCell(num_lstm_units)) 
     bnorm2 = batch_normalization(birnn, restore=False) 
     conn = fully_connected(bnorm2, n_units=num_labels, activation='softmax',restore=False) 
     regress = regression(conn, optimizer='adam', learning_rate= learning_rate, loss='categorical_crossentropy', shuffle_batches=True,restore=False) 
     model = tflearn.DNN(regress, clip_gradients=0., tensorboard_verbose=3) 

     return model 

i_model = create_classification_model('intent', num_lstm_units=i_num_lstm_units, num_labels=i_num_labels, learning_rate=i_learning_rate, saved_model_file=i_model_file) 

# Get data 
X, Y = create_data(batch_size = batch_size, sequence_len = sequence_len, sequence_unit_array_size = sequence_unit_array_size, num_labels=i_num_labels) 

for overalliter in xrange(1): 
    i_model.fit(X_inputs=X, Y_targets=Y, validation_set=0.1, show_metric=True, batch_size=None, shuffle=True, 
       n_epoch=20) # 100% of data being used for validation 
    i_model.save(i_model_file) 

    # Predicting on sample sentences 
    X_new, _ = create_data(batch_size = 1, sequence_len = sequence_len, sequence_unit_array_size = sequence_unit_array_size, num_labels=i_num_labels) 
    Y_new = i_model.predict(X_new) 

    print "X_new: ", X_new 
    print "Y_predicted: ", Y_new 

a_model = create_classification_model('action', num_lstm_units=a_num_lstm_units, num_labels=a_num_labels, learning_rate=a_learning_rate, saved_model_file=a_model_file) 
print a_model 

# Training data 
X, Y = create_data(batch_size = batch_size, sequence_len = sequence_len, sequence_unit_array_size = sequence_unit_array_size, num_labels=a_num_labels) 

for overalliter in xrange(1): 
    a_model.fit(X_inputs=X, Y_targets=Y, validation_set=0.1, show_metric=True, batch_size=None, shuffle=True, n_epoch=20) # 100% of data being used for validation 
    a_model.save(a_model_file) 

    # Predicting on sample sentences 
    X_new, _ = create_data(batch_size = 1, sequence_len = sequence_len, sequence_unit_array_size = sequence_unit_array_size, num_labels=a_num_labels) 
    Y_new = a_model.predict(X_new) 

    print "X_new: ", X_new 
    print "Y_predicted: ", Y_new 

回答