簡體   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