def __init__(self): """Sets the default training hyperparameters.""" #user-defined parameters FLAGS, unparsed = parse_args() # Number of examples per epoch of training data. self.num_examples_per_epoch = FLAGS.num_examples_per_epoch#6000 # Optimizer for training the model. self.optimizer = FLAGS.optimizer#"SGD" # Learning rate for the initial phase of training. self.initial_learning_rate = FLAGS.initial_learning_rate#2.0 self.learning_rate_decay_factor = FLAGS.learning_rate_decay_factor#0.5 self.num_epochs_per_decay = FLAGS.num_epochs_per_decay#8.0 # Learning rate when fine tuning the Inception v3 parameters. self.train_inception_learning_rate = FLAGS.train_inception_learning_rate#0.0005 # If not None, clip gradients to this value. self.clip_gradients = FLAGS.clip_gradients#5.0 # How many model checkpoints to keep. self.max_checkpoints_to_keep = FLAGS.max_checkpoints_to_keep#10 #self.keep_checkpoint_every_n_hours = 0.05
def __init__(self): """Sets the default model hyperparameters.""" #user-defined parameters FLAGS, unparsed = parse_args() # # File pattern of sharded TFRecord file containing SequenceExample protos. # Must be provided in training and evaluation modes. self.input_file_pattern = FLAGS.input_file_pattern#None # Image format ("jpeg" or "png"). self.image_format = "jpeg" # Approximate number of values per input shard. Used to ensure sufficient # mixing between shards in training. self.values_per_input_shard = 2300 # Minimum number of shards to keep in the input queue. self.input_queue_capacity_factor = 2 # Number of threads for prefetching SequenceExample protos. self.num_input_reader_threads = 1 # Name of the SequenceExample context feature containing image data. self.image_feature_name = "image/data" # Name of the SequenceExample feature list containing integer captions. self.caption_feature_name = "image/caption_ids" # Number of unique words in the vocab (plus 1, for <UNK>). # The default value is larger than the expected actual vocab size to allow # for differences between tokenizer versions used in preprocessing. There is # no harm in using a value greater than the actual vocab size, but using a # value less than the actual vocab size will result in an error. self.vocab_size = 12000 # Number of threads for image preprocessing. Should be a multiple of 2. self.num_preprocess_threads = 4 # Batch size. self.batch_size = FLAGS.batch_size#10 # File containing an Inception v3 checkpoint to initialize the variables # of the Inception model. Must be provided when starting training for the # first time. self.inception_checkpoint_file = FLAGS.inception_checkpoint_file#None # Dimensions of Inception v3 input images. self.image_height = 299 self.image_width = 299 # Scale used to initialize model variables. self.initializer_scale = 0.08 # LSTM input and output dimensionality, respectively. self.embedding_size = FLAGS.embedding_size#512 self.num_lstm_units = FLAGS.num_lstm_units#512 # If < 1.0, the dropout keep probability applied to LSTM variables. self.lstm_dropout_keep_prob = FLAGS.lstm_dropout_keep_prob#0.7
def get_train_data(vocabulary, batch_size, seq_length): # num_words_for_tgraining = 100000 # text = vocabulary[:num_words_for_tgraining] # print(len(text)) ################## # Your Code here ################## #dic,reversed_dic,max_index = gen_dictionary(vocabulary) data, count, dictionary, reversed_dictionary = build_dataset( vocabulary, 10000) data, count, dictionary, reversed_dictionary = build_dataset( vocabulary, len(count)) logging.debug('vol len:' + str(len(count))) FLAGS, unparsed = parse_args() #f = open(FLAGS.dictionary, "wb") #j =json.dumps(dictionary) #print(j) #f.close() int_text = sentence_to_int_array(vocabulary, dictionary) #int_text = index_data(data,dictionary) # 计算有多少个batch可以创建 n_batches = (len(int_text) // (batch_size * seq_length)) # 计算每一步的原始数据,和位移一位之后的数据 batch_origin = np.array(int_text[:n_batches * batch_size * seq_length]) batch_shifted = np.array(int_text[1:n_batches * batch_size * seq_length + 1]) # 将位移之后的数据的最后一位,设置成原始数据的第一位,相当于在做循环 batch_shifted[-1] = batch_origin[0] batch_origin_reshape = np.split(batch_origin.reshape(batch_size, -1), n_batches, 1) batch_shifted_reshape = np.split(batch_shifted.reshape(batch_size, -1), n_batches, 1) batches = np.array(list(zip(batch_origin_reshape, batch_shifted_reshape))) return batches, len(count)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import logging import os import tensorflow as tf import utils from model import Model from utils import read_data from utils import index_data from flags import parse_args FLAGS, unparsed = parse_args() logging.basicConfig( format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s', level=logging.DEBUG) vocabulary = read_data(FLAGS.text) print('Data size', len(vocabulary)) with open(FLAGS.dictionary, encoding='utf-8') as inf: dictionary = json.load(inf, encoding='utf-8') with open(FLAGS.reverse_dictionary, encoding='utf-8') as inf: reverse_dictionary = json.load(inf, encoding='utf-8')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import numpy as np import tensorflow as tf from flags import parse_args FLAGS, unparse = parse_args() class Model(): def __init__(self, learning_rate=0.001, batch_size=16, num_steps=32, num_words=5000, dim_embedding=128, rnn_layers=3): r"""初始化函数 Parameters ---------- learning_rate : float 学习率. batch_size : int batch_size. num_steps : int RNN有多少个time step,也就是输入数据的长度是多少. num_words : int 字典里有多少个字,用作embeding变量的第一个维度的确定和onehot编码. dim_embedding : int