【问题标题】:How can I train multiple models in one python module in TFLearn?如何在 TFLearn 的一个 python 模块中训练多个模型?
【发布时间】:2016-11-25 10:57:25
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: python tensorflow tflearn


    【解决方案1】:

    我遇到了同样的问题。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2018-04-02
      • 2019-12-25
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多