在 tensorflow 中编写以下 CNN

Writing the following CNN in tensorflow

我是这个深度学习的新手。我通过阅读和尝试实现一个真实的网络来了解基础知识,以了解 how/if 它是否真的有效。我选择了 Tensorflow in digits 和以下网络,因为它们给出了带有训练材料的确切架构。 Steganalysis with DL 我通过查看现有网络的数字和 Tensorflow 文档,为使用 DL 的隐写分析中的体系结构编写了以下代码。

    from model import Tower
from utils import model_property
import tensorflow as tf
import tensorflow.contrib.slim as slim
import utils as digits

class UserModel(Tower):

    @model_property
    def inference(self):
        x = tf.reshape(self.x, shape=[-1, self.input_shape[0], self.input_shape[1], self.input_shape[2]])
        with slim.arg_scope([slim.conv2d, slim.fully_connected],
                            weights_initializer=tf.contrib.layers.xavier_initializer(),
                            weights_regularizer=slim.l2_regularizer(0.0001)):
            conv1 = tf.layers.conv2d(inputs=x, filters=64, kernel_size=7, padding='same', strides=2, activation=tf.nn.relu)
            rnorm1 = tf.nn.local_response_normalization(input=conv1)
            conv2 = tf.layers.conv2d(inputs=rnorm1, filters=16, kernel_size=5, padding='same', strides=1, activation=tf.nn.relu)
            rnorm2 = tf.nn.local_response_normalization(input=conv2) 
            flatten = tf.contrib.layers.flatten(rnorm2)
            fc1 = tf.contrib.layers.fully_connected(inputs=flatten, num_outputs=1000, activation_fn=tf.nn.relu)
            fc2 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=1000, activation_fn=tf.nn.relu)
            fc3 = tf.contrib.layers.fully_connected(inputs=fc2, num_outputs=2)
            sm = tf.nn.softmax(fc3)
            return fc3

    @model_property
    def loss(self):
        model = self.inference
        loss = digits.classification_loss(model, self.y)
        accuracy = digits.classification_accuracy(model, self.y)
        self.summaries.append(tf.summary.scalar(accuracy.op.name, accuracy))
        return loss

我试过 运行 但准确率很低。有人可以告诉我我做的是否完全错误或有什么问题并告诉我如何正确编码吗?

更新:谢谢 Nessuno!通过你提到的修复,我想出了这个代码:

from model import Tower
from utils import model_property
import tensorflow as tf
import tensorflow.contrib.slim as slim
import utils as digits

class UserModel(Tower):

    @model_property
    def inference(self):
        x = tf.reshape(self.x, shape=[-1, self.input_shape[0], self.input_shape[1], self.input_shape[2]])
        with slim.arg_scope([slim.conv2d, slim.fully_connected],
                            weights_initializer=tf.contrib.layers.xavier_initializer(),
                            weights_regularizer=slim.l2_regularizer(0.00001)):
            conv1 = tf.layers.conv2d(inputs=x, filters=64, kernel_size=7, padding='Valid', strides=2, activation=tf.nn.relu)
            rnorm1 = tf.nn.local_response_normalization(input=conv1)
            conv2 = tf.layers.conv2d(inputs=rnorm1, filters=16, kernel_size=5, padding='Valid', strides=1, activation=tf.nn.relu)
            rnorm2 = tf.nn.local_response_normalization(input=conv2) 
            flatten = tf.contrib.layers.flatten(rnorm2)
            fc1 = tf.contrib.layers.fully_connected(inputs=flatten, num_outputs=1000, activation_fn=tf.nn.relu)
            fc2 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=1000, activation_fn=tf.nn.relu)
            fc3 = tf.contrib.layers.fully_connected(inputs=fc2, num_outputs=2, activation_fn=None)
            return fc3

    @model_property
    def loss(self):
        model = self.inference
        loss = digits.classification_loss(model, self.y)
        accuracy = digits.classification_accuracy(model, self.y)
        self.summaries.append(tf.summary.scalar(accuracy.op.name, accuracy))
        return loss

求解器类型是 SGD。学习率为 0.001。我正在改组训练 data.I 已将训练数据增加到 6000 个(每个类别 3000 个,其中 20% 用于验证)。我从 this link 下载了训练数据。但我只得到下图。我认为这是过度拟合。您对提高验证准确性有什么建议吗?

在 NVIDIA 数字中,classification_loss,与在 tensorflow 中完全一样 tf.nn.softmax_cross_entropy_with_logits 期望输入一个 linear 神经元层。

相反,您作为输入传递 sm = tf.nn.softmax(fc3),因此您应用了 softmax 操作 2 次,这是您的低准确度的原因。

为了解决这个问题,只需将模型输出层更改为

fc3 = slim.fully_connected(fc2, 2, activation_fn=None, scope='fc3')
return fc3