我有几个查询:
1)在以下代码中,我如何将训练步骤设置为100、200、300,并使用这些训练步骤训练模型。我可以将纪元设置为100、200、300吗? 2)如何添加在超级参数中固定输出大小的密集层:fc_size = 32 3)模型准备好后,如何输入验证数据并检查预测?
from tensorflow import keras
x = tf.placeholder(dtype=tf.float64, shape=[None, 32, 32, 3])
c1 = tf.layers.conv2d(inputs=x, activation=tf.nn.relu, filters=32,
kernel_size=[5,5], padding='VALID', strides=1)
p1 = tf.layers.max_pooling2d(inputs=c1, pool_size=[2,2], strides=2)
c2 = tf.layers.conv2d(inputs=p1, activation=tf.nn.relu, filters=64,
kernel_size=[5,5], padding='VALID', strides=1)
p2 = tf.layers.max_pooling2d(inputs=c2, pool_size=[2,2], strides=2)
f = tf.contrib.layers.flatten(p2)
fc1 = tf.layers.dense(inputs=f, units=64, activation=tf.nn.relu)
logits = tf.layers.dense(inputs = fc1, units=2)
tf.layers.dropout(inputs = fc1, rate = 0.4)
y = tf.placeholder(dtype=tf.int64)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)
correct_prediction = tf.equal(tf.argmax(logits, 1), y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
optim = tf.train.AdamOptimizer(learning_rate=0.001)
trainer = optim.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
epochs = 100
for i in range(epochs):
sess.run([trainer], feed_dict={x:X_train/255., y:y_train})
[acc, l] = sess.run([accuracy, loss], feed_dict={x:X_train/255.,
y:y_train})
print('Epoch %d - Loss: | %.2f Accuracy: %.2f'%(i,np.mean(l),acc))