使用 Tensorflow 的 CNN 模型
CNN model with Tensorflow
我正在通过在 Tensorflow 中构建卷积神经网络模型来进行字符识别。我的模型有 2 个 Conv 层,然后是 2 个全连接层。我有大约 78K 个来自训练的图像和 13K 个用于测试的图像。当我执行模型时,我在测试集上的准确率约为 92.xx%。当我在 Tensorboard 上可视化我的准确率和损失曲线时。我有一条 竖线 ,但我不知道为什么会这样?我得到了这样的曲线 Accuracy and Cross Entropy curve when viewed on tensorboard。
权重和偏差的分布曲线也显示一条垂直线Left side shows testing parameters (weights and bias) and right side shows training parameters on first conv layer
非常感谢这方面的任何帮助。 !!
def conv_layer(input, size_in, size_out, name="conv"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([5, 5, size_in, size_out], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1],padding="VALID")
act = tf.nn.relu(conv + b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")`
def fc_layer(input, size_in, size_out, name="fc"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([size_in, size_out], stddev=0.1), name="W") # Truncated_normal
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
act = tf.matmul(input, w) + b
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act
def model(use_two_conv, use_two_fc):
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, 1024], name="x")
x_image = tf.reshape(x, [-1, 32, 32, 1])
tf.summary.image('input', x_image, 3)
y = tf.placeholder(tf.float32, shape=[None,46], name="labels")
if use_two_conv:
conv1 = conv_layer(x_image, 1, 4, "conv1")
conv_out = conv_layer(conv1,4,16,"conv2")
else:
conv1 = conv_layer(x_image, 1, 16, "conv1")
conv_out = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
flattened = tf.reshape(conv_out, [-1, 5 * 5 * 16])
if use_two_fc:
fc1 = fc_layer(flattened, 5 * 5 * 16, 200, "fc1")
relu = tf.nn.relu(fc1)
tf.summary.histogram("fc1/relu", relu)
logits = fc_layer(fc1, 200, 46, "fc2")
else:
logits = fc_layer(flattened, 5*5*16, 46, "fc")
我过去遇到过这个问题,是因为使用了
writer.add_summary(current_summary)
而不是
writer.add_summary(current_summary, epoch)
(使用通用变量名称,因为提问者代码的相关部分未发布。)例如,
summary_op = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter("/Whatever/Path", sess.graph)
for iteration in range(1001):
if epoch % 100 == 0:
_, current_summary = sess.run([training_op, summary_op])
writer.add_summary(current_summary, iteration)
else:
_ = sess.run(training_op)
我正在通过在 Tensorflow 中构建卷积神经网络模型来进行字符识别。我的模型有 2 个 Conv 层,然后是 2 个全连接层。我有大约 78K 个来自训练的图像和 13K 个用于测试的图像。当我执行模型时,我在测试集上的准确率约为 92.xx%。当我在 Tensorboard 上可视化我的准确率和损失曲线时。我有一条 竖线 ,但我不知道为什么会这样?我得到了这样的曲线 Accuracy and Cross Entropy curve when viewed on tensorboard。
权重和偏差的分布曲线也显示一条垂直线Left side shows testing parameters (weights and bias) and right side shows training parameters on first conv layer
非常感谢这方面的任何帮助。 !!
def conv_layer(input, size_in, size_out, name="conv"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([5, 5, size_in, size_out], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1],padding="VALID")
act = tf.nn.relu(conv + b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")`
def fc_layer(input, size_in, size_out, name="fc"):
with tf.name_scope(name):
w = tf.Variable(tf.random_normal([size_in, size_out], stddev=0.1), name="W") # Truncated_normal
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
act = tf.matmul(input, w) + b
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act
def model(use_two_conv, use_two_fc):
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, 1024], name="x")
x_image = tf.reshape(x, [-1, 32, 32, 1])
tf.summary.image('input', x_image, 3)
y = tf.placeholder(tf.float32, shape=[None,46], name="labels")
if use_two_conv:
conv1 = conv_layer(x_image, 1, 4, "conv1")
conv_out = conv_layer(conv1,4,16,"conv2")
else:
conv1 = conv_layer(x_image, 1, 16, "conv1")
conv_out = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
flattened = tf.reshape(conv_out, [-1, 5 * 5 * 16])
if use_two_fc:
fc1 = fc_layer(flattened, 5 * 5 * 16, 200, "fc1")
relu = tf.nn.relu(fc1)
tf.summary.histogram("fc1/relu", relu)
logits = fc_layer(fc1, 200, 46, "fc2")
else:
logits = fc_layer(flattened, 5*5*16, 46, "fc")
我过去遇到过这个问题,是因为使用了
writer.add_summary(current_summary)
而不是
writer.add_summary(current_summary, epoch)
(使用通用变量名称,因为提问者代码的相关部分未发布。)例如,
summary_op = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter("/Whatever/Path", sess.graph)
for iteration in range(1001):
if epoch % 100 == 0:
_, current_summary = sess.run([training_op, summary_op])
writer.add_summary(current_summary, iteration)
else:
_ = sess.run(training_op)