0

TFLearn を使用して、1 つの Python モジュールで 2 つのモデルをトレーニングしようとしています。restore=Falseすべてのレイヤーに使用しています。2 番目のモデルの 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

モデルの 1 つがコメント アウトされている場合、このエラーは発生しないため、1 つのモデルのみがトレーニングされます。どんな助けでも素晴らしいでしょう!tflearn または tensorflow で複数のモデルをトレーニングまたはロードする際の問題に関する以前のスタック オーバーフローの質問をすべて (私が知る限り) 実行しましたが、提案された解決策 (例: restore=False、または variable_scope の使用) は機能しませんでした。私の使用シナリオでは、1 つのモジュールを使用して複数のモデルをトレーニング (および後でロードして適合) することが非常に重要です。コードは以下のとおりです。

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
4

1 に答える 1