Tensorflow:层大小取决于批量大小? [英] Tensorflow: Layer size dependent on batch size?

查看:41
本文介绍了Tensorflow:层大小取决于批量大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试熟悉 Tensorflow 库,但有一个相当基本的问题困扰着我.

I am currently trying to get familiar with the Tensorflow library and I have a rather fundamental question that bugs me.

在为 MNIST 分类构建卷积神经网络时,我尝试使用自己的 model_fn.其中通常会出现以下行来重塑输入特征.

While building a convolutional neural network for MNIST classification I tried to use my own model_fn. In which usually the following line occurs to reshape the input features.

x = tf.reshape(x, shape=[-1, 28, 28, 1]),其中 -1 表示输入批次大小.

x = tf.reshape(x, shape=[-1, 28, 28, 1]), with the -1 referring to the input batch size.

因为我使用这个节点作为我的卷积层的输入,

Since I use this node as input to my convolutional layer,

x = tf.reshape(x, shape=[-1, 28, 28, 1]) 
conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)

这是否意味着我所有网络层的大小都取决于批量大小?

does this mean that the size all my networks layers are dependent on the batch size?

我尝试在单个测试输入上冻结和运行图形,这仅在我提供 n=batch_size 测试图像时才有效.

I tried freezing and running the graph on a single test input, which will only work if I provide n=batch_size test images.

你能给我一个关于如何在预测时让我的网络在任何输入批量上运行的提示吗?此外,我猜在网络定义中使用 tf.reshape 节点(参见 cnn_layout 中的第一个节点)并不是提供服务的最佳输入.

Can you give me a hint on how to make my network run on any input batchsize while predicting? Also I guess using the tf.reshape node (see first node in cnn_layout) in the network definition is not the best input for serving.

我将附加我的网络层和 model_fn

I will append my network layer-up and the model_fn

def cnn_layout(features,reuse,is_training):
 with tf.variable_scope('cnn',reuse=reuse):
    # resize input to [batchsize,height,width,channel]
    x = tf.reshape(features['x'], shape=[-1,30,30,1], name='input_placeholder')
    # conv1, 32 filter, 5 kernel
    conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu, name='conv1')
    # pool1, 2 stride, 2 kernel
    pool1 = tf.layers.max_pooling2d(conv1, 2, 2, name='pool1')
    # conv2, 64 filter, 3 kernel
    conv2 = tf.layers.conv2d(pool1, 64, 3, activation=tf.nn.relu, name='conv2')
    # pool2, 2 stride, 2 kernel
    pool2 = tf.layers.max_pooling2d(conv2, 2, 2, name='pool2')
    # flatten pool2
    flatten = tf.contrib.layers.flatten(pool2)
    # fc1 with 1024 neurons
    fc1 = tf.layers.dense(flatten, 1024, name='fc1')
    # 75% dropout
    drop = tf.layers.dropout(fc1, rate=0.75, training=is_training, name='dropout')
    # output logits
    output = tf.layers.dense(drop, 1, name='output_logits')
    return output


def model_fn(features, labels, mode):
    # setup two networks one for training one for prediction while sharing weights
    logits_train = cnn_layout(features=features,reuse=False,is_training=True)
    logits_test = cnn_layout(features=features,reuse=True,is_training=False)

    # predictions
    predictions = tf.round(tf.sigmoid(logits_test),name='predictions')
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)

    # define loss and optimizer
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_train,labels=labels),name='loss')
    optimizer = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE, name='optimizer')
    train = optimizer.minimize(loss, global_step=tf.train.get_global_step(),name='train')

    # accuracy for evaluation
    accuracy = tf.metrics.accuracy(labels=labels,predictions=predictions,name='accuracy')

    # summarys for tensorboard
    tf.summary.scalar('loss',loss)

    # return training and evalution spec
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=predictions,
        loss=loss,
        train_op=train,
        eval_metric_ops={'accuracy':accuracy}
    )

谢谢!

推荐答案

我只想简要地提供我找到的解决方案.因为我不想构建可扩展的生产级模型,而是在 python 中构建一个简单的模型运行器来在本地执行我的 CNN.

I just want to briefly provide my found solution. Since I did not want to build a scalable production grade model, but a simple model runner in python to execute my CNN locally.

要导出我使用的模型,

input_size = 900

def serving_input_receiver_fn():
    inputs = {"x": tf.placeholder(shape=[None, input_size], dtype=tf.float32)}
    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

model.export_savedmodel(
    export_dir_base=model_dir,
    serving_input_receiver_fn=serving_input_receiver_fn)

为了加载和运行它(不需要再次定义模型),我使用了 tensorflow 预测器类.

To load and run it (without needing the model definition again) I used the tensorflow predictor class.

from tensorflow.contrib import predictor

class TFRunner:
    """ runs a frozen cnn graph """
    def __init__(self,model_dir):
        self.predictor = predictor.from_saved_model(model_dir)

    def run(self, input_list):
        """ runs the input list through the graph, returns output """
        if len(input_list) > 1:
            inputs = np.vstack(input_list)
            predictions = self.predictor({"x": inputs})
        elif len(input_list) == 1:
            predictions = self.predictor({"x": input_list[0]})
        else:
            predictions = []
        return predictions

这篇关于Tensorflow:层大小取决于批量大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆