用于培训CNN我有一个尺寸为16x12x1的5(batch_size)输入,我使用的架构是 conv1 - > maxpool - > conv2 - > maxpool - >完全connceted - > SOFTMAX。 conv1:5x5x1,16个过滤器。 conv2:5x5x16,16个过滤器。 fc:16 * 4 * 3,64个隐藏节点。 softmax:8输出
我的网络没有学到任何东西。它也没有显示任何错误。 成本函数一致为2.05xxx 可能的原因是什么? 测试的准确度为0.2或有时为0.0。
这是示例代码:
layer1_weights = tf.Variable(tf.random_normal( [5,5,1,16], stddev=0.01))
layer1_biases = tf.Variable(tf.zeros([16]))
layer2_weights = tf.Variable(tf.random_normal( [5,5,16,16], stddev=0.01))
layer2_biases = tf.Variable(tf.zeros([16]))
fc_weights = tf.Variable(tf.truncated_normal([16*4*3, 64], stddev=0.01))
fc_biases = tf.Variable(tf.constant(1.0, shape=[64]))
Softmax_weights= tf.Variable(tf.truncated_normal([64, 8], stddev=0.01))
Softmax_biases = tf.Variable(tf.constant(1.0, shape=[8]))
def mycnn(x):
conv1 = tf.nn.relu(tf.add(tf.nn.conv2d(x, layer1_weight, strides=[1, 1, 1, 1], padding='SAME'), layer1_biases))
layer1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv2 = tf.nn.relu(tf.add(tf.nn.conv2d(layer1, layer2_weights, strides=[1, 1, 1, 1], padding='SAME'), layer2_biases))
layer2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
shape = layer2.get_shape().as_list()
reshape = tf.reshape(layer2, [shape[0], shape[1] * shape[2] * shape[3]])
FClayer = tf.nn.relu(tf.add(tf.matmul(reshape, model.fc_weights), model.fc_biases))
softmax_layer = tf.add(tf.matmul(FClayer, model.Softmax_weights), model.Softmax_biases)
return softmax_layer
def reshapedata(data_train,data_test,model):
data_train = np.reshape(data_train,[-1,16,12,1])
data_test = np.reshape(data_test,[-1, 16,12,1])
return data_train,data_test
def batchdata(data,label, batchsize):
# generate random number required to batch data
order_num = random.sample(range(1, len(data)), batchsize)
data_batch = []
label_batch = []
for i in range(len(order_num)):
data_batch.append(data[order_num[i-1]])
label_batch.append(label[order_num[i-1]])
return data_batch, label_batch
sess = tf.Session()
data_train, data_test, label_train, label_test = load_data(Datainfo, ClassInfo, folder)
data_train, data_test, = reshapedata(data_train, data_test, model)
# input output placeholders
x = tf.placeholder(tf.float32, [5, 16,12,1])
y_ = tf.placeholder(tf.float32, [5, 8])
y = mycnn(x)
# loss
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
# train step
train_step = tf.train.AdamOptimizer(1e-3).minimize(cost)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(STEPS):
image_batch, label_batch = batchdata(data_train, label_train, model.BATCH_SIZE)
c = sess.run( cost, feed_dict={x: image_batch, y_: label_batch})
print('Epoch',i,'completed out of',STEPS,'loss:',c )
sess.run(train_step, feed_dict={ x: image_batch, y_: label_batch})
我的成本函数
Epoch 495 completed out of 500 loss: 2.09191
Epoch 496 completed out of 500 loss: 2.1098`
Epoch 497 completed out of 500 loss: 2.05045
Epoch 498 completed out of 500 loss: 2.08442
Epoch 499 completed out of 500 loss: 2.09257
我在网络FClayer = tf.nn.dropout(FClayer,0.8)
中添加了一行,而我的成本函数显示了一些趋势但不流畅。
当我添加那段代码时发生了什么?有关平滑成本函数的任何建议
图:x轴 - 迭代次数,y轴 - 成本
当我使用辍学值时,测试样品的0.6精度为63%,当测试样品的辍学0.8和1.0准确度分别为80%和40%时。