我遇到尺寸不匹配/广播错误。我的批次大小是1000,但是对于我的logit,它以某种方式减少到250。我不明白为什么我的批次大小相对于标签要减少1/4倍 这是我每一层的尺寸:
输入图层形状-> [无,28、28、1]
输入层已完成。...
Conv_1形状-> [None,28,28,32]
Conv_1完成...
Pool_1形状-> [无,14、14、32]
Conv_2形状-> [None,14,14,64]
Pool_2形状-> [无,7、7、64]
Pool_2_flat shape-> [无,3136]
密集的形状-> [无,1024]
密集的辍学完成了...
logits形状-> [无,10]
```
#!/usr/bin/env python3
from argparse import ArgumentParser
import os
import glob
import tensorflow as tf
print("running version --> {}". format(tf.__version__))
import shutil
tf.logging.set_verbosity(tf.logging.INFO)
def my_mnist(features, mode, params):
is_training = mode == tf.estimator.ModeKeys.TRAIN
with tf.name_scope('Input'):
# Input Layer
input_layer = tf.reshape(features, [-1, 28, 28, 1], name='input_reshape')
tf.summary.image('input', input_layer)
input_layer_shape = input_layer.get_shape()
print("Input Layer shape --> {}".format(input_layer_shape.as_list()))
print("Input layer done....")
with tf.name_scope('Conv_1'):
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=(5, 5),
padding='same',
activation=tf.nn.relu,
trainable=is_training)
conv1_shape = conv1.get_shape()
print("Conv_1 shape --> {}".format(conv1_shape.as_list()))
print("Conv_1 done...")
# Pooling Layer #1
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=(2, 2), strides=2, padding='same')
pool1_shape = pool1.get_shape()
print("Pool_1 shape --> {}".format(pool1_shape.as_list()))
with tf.name_scope('Conv_2'):
# Convolutional Layer #2 and Pooling Layer #2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=(5, 5),
padding='same',
activation=tf.nn.relu,
trainable=is_training)
conv2_shape = conv2.get_shape()
print("Conv_2 shape --> {}".format(conv2_shape.as_list()))
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=(2, 2), strides=2, padding='same')
pool2_shape = pool2.get_shape()
print("Pool_2 shape --> {}".format(pool2_shape.as_list()))
with tf.name_scope('Dense_Dropout'):
# Dense Layer
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
pool2_flat_shape = pool2_flat.get_shape()
print("Pool_2_flat shape --> {}".format(pool2_flat_shape.as_list()))
# pool2_flat = tf.contrib.layers.flatten(pool2)
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu, trainable=is_training)
dense_shape = dense.get_shape()
print("Dense shape --> {}".format(dense_shape.as_list()))
dropout = tf.layers.dropout(inputs=dense, rate=params.dropout_rate, training=is_training)
print("dense dropout done...")
with tf.name_scope('Predictions'):
# Logits Layer
logits = tf.layers.dense(inputs=dropout, units=10, trainable=is_training)
logits_shape = logits.get_shape()
print("logits shape --> {}".format(logits_shape.as_list()))
return logits
def cnn_model_fn(features, labels, mode, params):
"""Model function for CNN."""
print("Creating logits..................")
logits = my_mnist(features, mode, params)
print("Logits done.....................")
predicted_logits = tf.argmax(input=logits, axis=1)
scores = tf.nn.softmax(logits, name='softmax_tensor')
# Generate Predictions
predictions = {
'classes': predicted_logits,
'probabilities': scores
}
export_outputs = {
'prediction': tf.estimator.export.ClassificationOutput(
scores=scores,
classes=tf.cast(predicted_logits, tf.string))
}
# PREDICT
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)
print("Calculating the loss...")
# TRAIN and EVAL
loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=params.learning_rate)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels=labels, predictions=predictions["classes"])
}
# return tf.estimator.EstimatorSpec(
# mode=mode,
# loss=loss,
# train_op=train_op,
# eval_metric_ops=eval_metric,
# predictions=predictions,
# export_outputs=export_outputs)
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops, export_outputs=export_outputs)
def data_input_fn(filenames, batch_size=1000, shuffle=True):
def _parser(record):
features = {
'label': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string)
}
parsed_record = tf.parse_single_example(record, features)
image = tf.decode_raw(parsed_record['image_raw'], tf.float32)
label = tf.cast(parsed_record['label'], tf.int32)
return image, label
def _input_fn():
dataset = tf.data.TFRecordDataset(filenames).map(_parser)
if shuffle:
dataset = dataset.shuffle(buffer_size=100000)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, tf.one_hot(labels,depth=10)
return _input_fn
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument(
"--data-directory",
default='/tmp/data',
help='Directory where TFRecords are stored'
)
parser.add_argument(
'--model-directory',
default='/tmp/mnisttraining',
help='Directory where model summaries and checkpoints are stored'
)
args = parser.parse_args()
tf.logging.set_verbosity(tf.logging.INFO)
run_config = tf.contrib.learn.RunConfig(
model_dir=args.model_directory,
save_checkpoints_steps=20,
save_summary_steps=20)
hparams = tf.contrib.training.HParams(
learning_rate=0.001,
dropout_rate=0.4,
data_directory=os.path.expanduser(args.data_directory))
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn,
config=run_config,
params=hparams
)
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=20)
if os.path.isdir('/tmp/mnisttraining'):
print("dir already present, deleting...")
shutil.rmtree('/tmp/mnisttraining')
train_batch_size = 1000
train_steps = 55000 // train_batch_size # len dataset // batch size
train_input_fn = data_input_fn(glob.glob(os.path.join(hparams.data_directory, 'train.tfrecords')),
batch_size=train_batch_size)
eval_input_fn = data_input_fn(glob.glob(os.path.join(hparams.data_directory, 'validation.tfrecords')), batch_size=100)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=train_steps)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)
# experiment = tf.contrib.learn.Experiment(
# mnist_classifier,
# train_input_fn=train_input_fn,
# eval_input_fn=eval_input_fn,
# train_steps=train_steps
# )
tf.estimator.train_and_evaluate(mnist_classifier, train_spec, eval_spec)
# Export for serving
# mnist_classifier.export_savedmodel(
# os.path.join(hparams.data_directory, 'serving'),
# serving_input_receiver_fn
# )
尽管提供1000作为批量大小,但请帮助我准确找出导致批量大小变化的原因