def conv2d(h_num_filters, h_filter_width, h_stride=1, h_dilation_rate=1, h_use_bias=True, h_padding='SAME'): def compile_fn(di, dh): conv_op = tf.layers.Conv2D(dh['num_filters'], dh['filter_width'], dh['stride'], use_bias=dh['use_bias'], dilation_rate=dh['dilation_rate'], padding=dh['padding']) def fn(di): return {'out': conv_op(di['in'])} return fn return siso_tensorflow_module( 'Conv2D', compile_fn, { 'num_filters': h_num_filters, 'filter_width': h_filter_width, 'stride': h_stride, 'use_bias': h_use_bias, 'dilation_rate': h_dilation_rate, 'padding': h_padding })
def global_pool2d(): def compile_fn(di, dh): def fn(di): return {'out': tf.reduce_mean(di['in'], [1, 2])} return fn return siso_tensorflow_module('GlobalAveragePool', compile_fn, {})
def fc_layer(h_num_units): def compile_fn(di, dh): fc = tf.layers.Dense(dh['num_units']) def fn(di): return {'out': fc(di['in'])} return fn return siso_tensorflow_module('FCLayer', compile_fn, {'num_units': h_num_units})
def dropout(h_keep_prob): def compile_fn(di, dh): p = tf.placeholder(tf.float32) def fn(di): return {'out': tf.nn.dropout(di['in'], p)} return fn, {p: dh['keep_prob']}, {p: 1.0} return siso_tensorflow_module('Dropout', compile_fn, {'keep_prob': h_keep_prob})
def batch_normalization(): def compile_fn(di, dh): p_var = tf.placeholder(tf.bool) bn = tf.layers.BatchNormalization() def fn(di): return {'out': bn(di['in'], training=p_var)} return fn, {p_var: 1}, {p_var: 0} return htf.siso_tensorflow_module('BatchNormalization', compile_fn, {})
def dropout(h_drop_rate): def compile_fn(di, dh): p = tf.placeholder(tf.float32) def fn(di): return {'out': tf.nn.dropout(di['in'], p)} return fn, {p: 1.0 - dh['drop_rate']}, {p: 1.0} return htf.siso_tensorflow_module('Dropout', compile_fn, {'drop_rate': h_drop_rate})
def avg_pool2d(h_kernel_size, h_stride=1): def compile_fn(di, dh): def fn(di): return { 'out': tf.nn.avg_pool(di['in'], [1, dh['kernel_size'], dh['kernel_size'], 1], [1, dh['stride'], dh['stride'], 1], 'SAME') } return fn return siso_tensorflow_module('MaxPool2D', compile_fn, { 'kernel_size': h_kernel_size, 'stride': h_stride, })
def nonlinearity(h_nonlin_name): def compile_fn(di, dh): def fn(di): nonlin_name = dh['nonlin_name'] if nonlin_name == 'relu': Out = tf.nn.relu(di['in']) elif nonlin_name == 'tanh': Out = tf.nn.tanh(di['in']) elif nonlin_name == 'elu': Out = tf.nn.elu(di['in']) else: raise ValueError return {"out": Out} return fn return htf.siso_tensorflow_module('Nonlinearity', compile_fn, {'nonlin_name': h_nonlin_name})
def relu(): return siso_tensorflow_module( 'ReLU', lambda di, dh: lambda di: {'out': tf.nn.relu(di['in'])}, {})