Контролируемая учебная сессия: график завершен и не может быть изменен - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть файл tfrecords, из которого я хочу создать пакеты данных.Я хочу обучить слой кодирования моей модели много шагов при вызове пакетов.Для этого я использовал приведенный ниже код для создания пакетов и получения следующего пакета:

def write_and_encode(data_list, tfrecord_filename):
    writer = tf.python_io.TFRecordWriter(tfrecord_filename)
    for label, data_matrix in data_list:
        example = tf.train.Example(features=tf.train.Features(
            feature={
                "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
                "data_raw": tf.train.Feature(bytes_list=tf.train.BytesList(value=[data_matrix.tostring()]))
            }
        ))
        writer.write(example.SerializeToString())

    writer.close()


def read_and_decode(tfrecord_filename):
    reader = tf.TFRecordReader()
    filename_queue = tf.train.string_input_producer([tfrecord_filename],)
    _, serialized_example = reader.read(filename_queue)
    feature = tf.parse_single_example(serialized_example,
                                      features={
                                          "label": tf.FixedLenFeature([], tf.int64),
                                          "data_raw": tf.FixedLenFeature([], tf.string)
                                      })
    data = tf.decode_raw(feature["data_raw"], tf.float64)
    data = tf.reshape(data, [FLAGS.image_rows, FLAGS.image_cols])
    return data, feature["label"]
def train_input_fn():

    tfrecord_file = "../resources/train_tfrecord"  
    dataset = tf.data.TFRecordDataset(tfrecord_file)
    dataset = dataset.map(parser)

    train_dataset = dataset.repeat(FLAGS.num_epochs).batch(FLAGS.batch_size)
    train_iterator = train_dataset.make_one_shot_iterator()

    features, labels = train_iterator.get_next()
    return features, labels

def parser(record_line):

    features = {
        "label": tf.FixedLenFeature([], tf.int64),
        "data_raw": tf.FixedLenFeature([], tf.string)
    }
    parsed = tf.parse_single_example(record_line, features=features)
    label = tf.cast(parsed["label"], tf.int32) - 1  
    data = tf.decode_raw(parsed["data_raw"], tf.float64)
    data = tf.reshape(data, [FLAGS.image_rows, FLAGS.image_cols])
    data = tf.cast(data, tf.float32)
    return data, label

Чтобы обучить слой кодирования, я выполняю следующее:

def train_layer(output_layer, layer_loss,optimizer):
    """Train each encoding layer for 1000 steps"""
    layer_name = output_layer.name.split('/')[0]
    print('Pretraining {}'.format(layer_name))
    num_steps = 1000
    step=1
    features, labels=train_input_fn()
    input_l = tf.reshape(features, [-1, FLAGS.image_rows, FLAGS.image_cols, 1])
    while step <= num_steps:

         instance_batch, label_batch = tf.train.shuffle_batch([input_l], batch_size=5, capacity=200, min_after_dequeue=100)

    _out_layer, _layer_loss,_ = sess.run([output_layer, layer_loss, optimizer],
      feed_dict ={features:instance_batch,labels:label_batch})

    #print(_layer_loss)
    step += 1
    print('layer finished')

ДляКонфигурация отслеживаемой учебной сессии, я реализую ее следующим образом:

"""Use a MonitoredTrainingSession for running the computations.  It makes running on distributed systems
possible, handles checkpoints, saving summaries, and restoring from crashes easy."""

#create hooks to pass to the session.  These can be used for adding additional calculations, loggin, etc.
#This hook simply tells the session how many steps to run
hooks=[tf.train.StopAtStepHook(last_step=10000)]

#This command collects all summary ops that have been added to the graph and prepares them to run in the next session
tf.summary.merge_all()
logs_dir = 'logs'
with tf.train.MonitoredTrainingSession(hooks=hooks, checkpoint_dir=logs_dir,save_summaries_steps=100) as sess:

    start_time = time.time()

    """First train each layer one at a time, freezing weights from previous layers.
    This was accomplished by declaring which variables to update when each layer optimizer was defined."""
    for layer_dict in model_layers:
        output_layer = layer_dict['output_layer']
        layer_loss = layer_dict['layer_loss']
        optimizer = layer_dict['optimizer']
        train_layer( output_layer, layer_loss, optimizer)


 #Now train the whole network for classification allowing all weights to change.
    while not sess.should_stop():
        _y, _cross_entropy, _net_op, _accuracy = sess.run([y, cross_entropy, net_op, accuracy], feed_dict={x:instance_batch,y_labels:label_batch})
print(_accuracy)
print('Training complete\n')

Когда я запускаю свой код, я получаю сообщение об ошибке:

повысить RuntimeError («График завершен ине может быть изменено. ") RuntimeError: График завершен и не может быть изменен.

Указывается, что источником ошибки является train_layer:

train_layer (output_layer, layer_loss, оптимизатор) Файл "aut.py", строка 111, в функциях train_layer, метки = train_input_fn () Файл "aut.py", строка 67, в файле train_input_fn dataset = tf.data.TFRecordDataset (tfrecord_file)

Я думаю, что модель не может загрузить следующую партию.Как я могу решить эту проблему?

...