繁体   English   中英

如何在TFLearn的一个python模块中训练多个模型?

[英]How can I train multiple models in one python module in TFLearn?

我正在尝试使用TFLearn在一个python模块中训练两个模型。 我对所有图层都使用restore=False 调用第二个模型的fit方法时出现错误:

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

我有同样的问题。

with tf.Graph().as_default():放在with tf.Graph().as_default(): i_model = create_classification_model and a_model = create_classification_model并正确缩进。

或在这里检查操作方式https://github.com/tflearn/tflearn/blob/master/examples/basics/logical.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM