Tensorflow在到达train_steps之前停止训练

时间:2018-11-26 17:27:32

标签: python tensorflow

我正在使用基于Tensorflow教程的以下脚本为其dnn分类器。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import pandas as pd
import argparse
import tensorflow as tf


# specify col.names.
names = [ 
'FTR', #Full Time Result (H=Home Win, D=Draw, A=Away Win)
'P1_HT_H_HS', #'P = 'Past HT = hometeam  H = Homegame, for last letters see guide
'P1_HT_H_AS',
'P1_HT_H_HST',
]

# specify dtypes.
dtypes = {
'FTR': str,
'P1_HT_H_HS': np.float32, #'P = 'Past HT = hometeam  H = Homegame: np.float32, for last letters see guide
'P1_HT_H_AS': np.float32,
'P1_HT_H_HST': np.float32,
}

df = pd.read_csv('last_games.csv', names=names) #, dtype=dtypes , na_values='NaN') #,encoding ="UTF-8")
print(df.head(n=10))


def load_data(y_name="FTR", train_fraction=0.9, seed=None):
    # Load the raw data columns.
    data = df
    print(data.head(n=10))

    # Shuffle the data
    np.random.seed(seed)

    # Split the data into train/test subsets.
    x_train = data.sample(frac=train_fraction, random_state=seed)
    x_test = data.drop(x_train.index)

    # Extract the label from the features DataFrame. df.pop = Return item and drop from frame
    y_train = x_train.pop(y_name)
    y_test = x_test.pop(y_name)

    return (x_train, y_train), (x_test, y_test)

load_data()


def features_columns():

    feature_columns = [

    #numeric
    tf.feature_column.numeric_column('P1_HT_H_HS'), #tf.feature_column.numeric_column('P = tf.feature_column.numeric_column('Past HT = hometeam  H = Homegame), for last letters see guide
    tf.feature_column.numeric_column('P1_HT_H_AS'),
    tf.feature_column.numeric_column('P1_HT_H_HST'),

    ]
    return feature_columns

features_columns()
dropout = 0.1
log_dir = ("C:\\Users\\azach\\Desktop\\python\\betzeug\\model2\\")


parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=500000, type=int, help='number of training steps')
num_epochs = 3


#custom activation function
def leaky_relu(x):
     return tf.nn.relu(x) - 0.01 * tf.nn.relu(-x)

def main(argv):
"""Builds, trains, and evaluates the model."""
args = parser.parse_args(argv[1:])

(train_x, train_y), (test_x, test_y) = load_data()




# Build the training dataset.

training_input_fn = tf.estimator.inputs.pandas_input_fn(x=train_x, y=train_y, batch_size=batch_size,
                                                        shuffle=True, num_epochs=num_epochs)

# Build the validation dataset.

eval_input_fn = tf.estimator.inputs.pandas_input_fn(x=test_x, y=test_y, batch_size=batch_size, shuffle=False)

model = tf.estimator.DNNClassifier(feature_columns=features_columns(),
                                        # Rule of Thumbs: number of hidden neurons = (inputlayer+outputlayer)/2
                                        hidden_units=[87,87],
                                        # The model must choose between 3 classes.
                                        n_classes=3,
                                        dropout=dropout,
                                        activation_fn = leaky_relu,
                                        label_vocabulary = ('H','D','A'),
                                        model_dir=log_dir,
                                        optimizer=tf.train.ProximalAdagradOptimizer(
                                                learning_rate=0.1,
                                                l1_regularization_strength=0.001
                                                ))

# Train the model.
# By default, the Estimators log output every 1000 steps.
model.train(input_fn=training_input_fn, steps=train_steps)


eval_result = model.evaluate(input_fn=eval_input_fn)

print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

print('Training complete.')

if __name__ == "__main__":
# The Estimator periodically generates "INFO" logs; make these logs visible.
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main=main)

我正在尝试在数据集中训练dnn.class罐头。经过几百步,培训停止:

INFO:tensorflow:loss = 1069.3333, step = 1520 (93.351 sec)
INFO:tensorflow:Saving checkpoints for 1562 into    C:\Users\azach\Desktop\python\betzeug\modelasd2\model.ckpt.
INFO:tensorflow:Loss for final step: 991.7263.
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-11-26-17:00:17
INFO:tensorflow:Graph was finalized.

我找不到指示tensorflow停止的代码行。另外parser.add_argument有什么问题?

如何进行大量的培训步骤?

0 个答案:

没有答案