flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/home/liu/lbc/lunwen/zhangliang/rotation and TT/MyWork/experiments/cifar-10/data/', 'Directory to put the training data.')
f = np.load(FLAGS.data_dir + '/cifar.npz')
images = np.reshape(f['train_images'], [-1, 32, 32, 3])
labels = f['train_labels']
def aug_train(image, aux):
aug_image = tf.pad(image, [[4, 4], [4, 4], [0, 0]])
aug_image = tf.random_crop(aug_image, [32, 32, 3])
aug_image = tf.image.random_flip_left_right(aug_image)
aug_image = tf.image.random_contrast(aug_image, 0.75, 1.25)
aug_image = (aug_image - aux['mean']) / aux['std']
return aug_image
dataset_train = tf.data.Dataset.from_tensor_slices(
{'images': tf.to_float(images), 'labels': tf.to_int32(labels)})
aux = { 'mean': np.mean(images, axis=0), 'std': np.std(images, axis=0)}
dataset_train = dataset_train.map( lambda x: {'images': aug_train(x['images'], aux), 'labels': x['labels']})
dataset_train_batch = dataset_train.shuffle(buffer_size=1000).batch(100)
flat_size = 3136
num_class = 10
dim_hidden = 1024
verbose_interval = 10
layer_cnn0 = tf.layers.Conv2D(32, 5, activation = tf.nn.relu)
layer_pool0 = tf.layers.MaxPooling2D(2, 2)
layer_cnn1 = tf.layers.Conv2D(64, 5, activation = tf.nn.relu)
layer_pool1 = tf.layers.MaxPooling2D(2, 2)
layer_flatten = tf.layers.Flatten()
layer_fc0 = tf.layers.Dense(dim_hidden, activation = tf.nn.relu)
layer_dropout = tf.layers.Dropout(0.75)
layer_fc1 = tf.layers.Dense(num_class, activation = None)
def loss(step, x, y):
inputs = tf.constant(x, name = "inputs")
cnn0 = layer_cnn0(inputs)
pool0 = layer_pool0(cnn0)
cnn1 = layer_cnn1(pool0)
pool1 = layer_pool1(cnn1)
flatten = layer_flatten(pool1)
fc0 = layer_fc0(flatten)
dropout = layer_dropout(fc0)
logits = layer_fc1(dropout)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits)
loss = tf.reduce_mean(loss)
if step % verbose_interval == 0:
print(loss)
return loss
optimizer = tf.train.AdamOptimizer(learning_rate = 1e-3)
step = 0
for a in tfe.Iterator(dataset_train_batch):
step += 1
batch_data, batch_label = a['images'], a['labels']
optimizer.minimize(lambda: loss(step, batch_data, batch_label))
首先,我很抱歉我的英语不好。 这是一个在急切模式下训练具有张量流1.6的CIFAR-10的网络。当我使用相同的网络训练MNIST时,5批次后损失从2.3降至0.02。但是,当我使用网络训练CIFAR-10时,损失不会很快下降。在100个培训批次中,损失上下波动。开始损失为2.5,最终损失为1.7。我对使用正确的方法处理CIFAR-10中的数据感到困惑。 谢谢你的回答!