官方文档:Tensorpack tutorial
- logger.auto_set_dir(),logger.set_logger_dir(dirname, action=None)
Use logger.set_logger_dir() to set log directory to “./train_log/{scriptname}:{name}”. “scriptname”就是当前python文件的名字
action 有三种类型:[“k”(keep),”d”(delete),”q”(quit)]
- BatchData()
假如:BatchData(dataset.Mnist(‘train’), 128),len(Mnist(‘train’) )=60000,batch_size=128,最后的len(BatchData) = 60000//128 = 468;BatchData(dataset.Mnist(‘train’), 128, remainder=True),那么len(BatchData) = 469。
- PrintData(train),默认num=1
打印前几个Datapoints,方便debug。
- Mymodel(ModelDesc)
class MyModel(ModelDesc):
def inputs(self):
"""
Define all the inputs (with type, shape, name) that the graph will need.
"""
return [tf.TensorSpec(shape, dtype, name), tf.TensorSpec(shape, dtype, name), ... ]
def build_graph(self, tensorA, tensorB, ...): # inputs
"""This function should build the model which takes the input variables (defined above)
and return cost at the end."""
return cost # returns the cost tensor
def optimizer(self):
return tf.train.GradientDescentOptimizer(0.1)
- TrainConfig
config = TrainConfig(
model=MyModel()
dataflow=my_dataflow,
# data=my_inputsource, # alternatively, use an InputSource
callbacks=[...], # some default callbacks are automatically applied
# some default monitors are automatically applied
steps_per_epoch=300, # default to the size of your InputSource/DataFlow
)
- 模型保存
ModelSaver(), # save the model after every epoch
MaxSaver('validation_accuracy'), # save the model with highest accuracy (prefix 'validation_')
- Argscope
with argscope(Conv2D, filters=32, kernel_size=3, activation=tf.nn.relu):
l = (LinearWrap(image) # the starting brace is only for line-breaking
.Conv2D('conv0')
.MaxPooling('pool0', 2)
.Conv2D('conv1', padding='SAME')
.Conv2D('conv2', kernel_size=5)
.FullyConnected('fc0', 512, activation=tf.nn.relu)
.Dropout('dropout', rate=0.5)
.tf.multiply(0.5)
.apply(func, *args, **kwargs)
.FullyConnected('fc1', units=10, activation=tf.identity)())
with argscope([Conv2D, MaxPooling], data_format='NCHW'), argscope(Conv2D, kernel_size=1):
l2 = LinearWrap(image).Conv2D('conv3', strides=2).MaxPooling('pool3', 2)
等价于下面:
l = Conv2D('conv0', image, 32, 3, activation=tf.nn.relu)
l = MaxPooling('pool0', l, 2)
l = Conv2D('conv1', l, 32, 3, padding='SAME', activation=tf.nn.relu)
l = Conv2D('conv2', l, 32, 5, activation=tf.nn.relu)
l = FullyConnected('fc0', l, 512, activation=tf.nn.relu)
l = Dropout('dropout', l, rate=0.5)
l = tf.multiply(l, 0.5)
l = func(l, *args, **kwargs)
l = FullyConnected('fc1', l, 10, activation=tf.identity)
l2 = Conv2D('conv3', image, 32, 1, strides=2, data_format='NCHW', activation=tf.nn.relu)
l2 = MaxPooling('pool3', l2, 2, data_format='NCHW')
- launch_train_with_config(config, trainer)
等价下面两个函数
trainer.setup_graph(
my_model.get_input_signature(),
my_input_source, # or QueueInput(my_dataflow)
my_model.build_graph,
my_model.get_optimizer)
trainer.train_with_defaults(
callbacks=config.callbacks,
monitors=config.monitors,
session_creator=config.session_creator,
session_init=config.session_init,
steps_per_epoch=config.steps_per_epoch,
starting_epoch=config.starting_epoch,
max_epoch=config.max_epoch,
extra_callbacks=config.extra_callbacks)