在tensorflow中编写以下CNN
我对这门深奥的学问还不熟悉。我通过阅读和尝试实现一个真正的网络来了解它如何/是否真正起作用,从而学到了一些基本知识。我选择了数字Tensorflow和以下网络,因为它们给出了训练装备的精确体系结构。 我通过查看数字和Tensorflow文档中的现有网络,为DL隐写分析中的体系结构编写了以下代码在tensorflow中编写以下CNN,tensorflow,deep-learning,nvidia-digits,Tensorflow,Deep Learning,Nvidia Digits,我对这门深奥的学问还不熟悉。我通过阅读和尝试实现一个真正的网络来了解它如何/是否真正起作用,从而学到了一些基本知识。我选择了数字Tensorflow和以下网络,因为它们给出了训练装备的精确体系结构。 我通过查看数字和Tensorflow文档中的现有网络,为DL隐写分析中的体系结构编写了以下代码 from model import Tower from utils import model_property import tensorflow as tf import tensorflow
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
我试过运行它,但精确度很低。有人能告诉我我是否做得完全错误,或者它有什么问题,并告诉我如何正确地编码它吗
更新:谢谢你!通过您提到的修复,我得到了以下代码:
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。我正在整理培训数据。我已经将每个类别的培训数据增加到6000 3000个,其中20%保留用于验证。我从下载了培训数据。但我只得到下面的图表。我觉得这太合适了。您对提高验证准确性有何建议
在NVIDIA数字中,分类丢失,正如tensorflow所期望的那样,输入一层线性神经元
相反,您将作为输入sm=tf.nn.softmaxfc3传递,因此您将应用softmax操作2次,这就是您精度低的原因
为了解决这个问题,只需将模型输出层更改为
fc3 = slim.fully_connected(fc2, 2, activation_fn=None, scope='fc3')
return fc3
你能把数字、分类、丢失和数字的代码贴出来吗?上面是我完整的网络。数字。分类\丢失和数字。分类\准确性本身不是数字的一部分吗?我通过上面的网络查看了Nvidia Digits上已经存在的LeNet和AlexNet。好的,这是Nvidia Digits。我给你答案谢谢!我按照你提到的方式编辑了代码。你能看一下编辑过的问题吗?你应该打开另一个问题,并将其标记为已解决。但是,如果这是过度拟合,则必须向模型添加一些正则化。您可以对权重和/或批次标准化使用dropout和/或L2正则化。。。