如何将循环限制为指定的迭代次数?

时间:2018-02-24 07:54:28

标签: python tensorflow neural-network conv-neural-network

我正在使用神经网络在nfs中自驾车。

我有hdf5格式的训练数据。使用张量流来训练模型。

起初我认为它需要100,但它不会在100之后停止。

我是否必须手动停止(ctrl + C)?

import h5py 
import numpy as np 
import tensorflow as tf 
from googlenet import googlenet
#from alexnet import alexnet

# Training Parameters
learning_rate = 0.001
WIDTH = 200
HEIGHT = 150
EPOCHS = 100
MODEL_NAME = "draj_mod"

f = h5py.File("wasd_training_data.hdf5", "r")
print("File loaded")
model = googlenet(WIDTH, HEIGHT, learning_rate)

DATASET_COUNTER = 0 
for dataset in f.keys():
    DATASET_COUNTER+=1

DATASET_COUNTER = int(DATASET_COUNTER/2) 

for i in range(EPOCHS):
    for counter in range(DATASET_COUNTER):
        #HDF5 data is stored as dataset_#_X or dataset_#_Y
        label_X = "dataset" + str(counter) + "_X"
        label_Y = "dataset" + str(counter) + "_Y"

        data_X = np.array(f[label_X])
        data_Y = np.array(f[label_Y])

        train_data_X = data_X[:-500].reshape(-1, WIDTH, HEIGHT, 3)
        train_data_Y = data_Y[:-500]

        test_data_X = data_X[-500:].reshape(-1, WIDTH, HEIGHT, 3)
        test_data_Y = data_Y[-500:]

        model.fit({'input' : train_data_X}, {'targets' : train_data_Y}, n_epoch = 1, validation_set=({'input': test_data_X}, {'targets': test_data_Y}), snapshot_step = 1000, show_metric=True, run_id=MODEL_NAME)
    if(i%5 == 0):
        model.save(MODEL_NAME)

    # tensorboard --logdir=foo:F:/Workspace/nfssd/log
    #model.fit(X, Y, n_epoch=1000, validation_set=0.1, shuffle=True,
    #        show_metric=True, batch_size=64, snapshot_step=200,
    #        snapshot_epoch=False, run_id='googlenet_oxflowers17')

1 个答案:

答案 0 :(得分:1)

当你在嵌套循环中调用下面提到的行时,它将被称为EPOCHS * DATASET_COUNTER次。

model.fit({'input' : train_data_X}, {'targets' : train_data_Y}, n_epoch = 1, validation_set=({'input': test_data_X}, {'targets': test_data_Y}), snapshot_step = 1000, show_metric=True, run_id=MODEL_NAME)

如果您希望仅运行一百次,请将所有数据集合在一起,然后在第二个循环(DATASET-COUNTER)之外并直接在第一个循环(EPOCH之一)内调用。