因此,我为用户定义的CNN编写了此代码。它没有文档,因此请忽略除我定义了use_CNN
方法的最后一个功能Estimator
以外的所有功能。
class CNN(object):
def __init__(self, numberOfLayers, typeOfLayer, layerActivations, filtersInEachLayer,
kernelSizeInEachLayer, stridesInEachLayer, paddingInEachLayer):
self.numberOfLayers = numberOfLayers
self.typeOfLayer = typeOfLayer
self.layerActivations = layerActivations
self.filtersInEachLayer = filtersInEachLayer
self.kernelSizeInEachLayer = kernelSizeInEachLayer
self.stridesInEachLayer = stridesInEachLayer
self.paddingInEachLayer = paddingInEachLayer
def create_a_CNN(self):
for i in range(0, self.numberOfLayers):
if(self.typeOfLayer[i] == 'conv' and i == 0):
output = tf.layers.conv2d(self.X, filters = self.filtersInEachLayer[i],
kernel_size = self.kernelSizeInEachLayer[i],
strides = self.stridesInEachLayer[i],
padding = self.paddingInEachLayer[i],
activation = self.layerActivations[i])
elif(self.typeOfLayer[i] == 'conv' and i != self.numberOfLayers-1):
output = tf.layers.conv2d(output, filters = self.filtersInEachLayer[i],
kernel_size = self.kernelSizeInEachLayer[i],
strides = self.stridesInEachLayer[i],
padding = self.paddingInEachLayer[i],
activation = self.layerActivations[i])
elif(self.typeOfLayer[i] == 'conv' and i == self.numberOfLayers-1):
self.output = tf.layers.conv2d(output, filters = self.filtersInEachLayer[i],
kernel_size = self.kernelSizeInEachLayer[i],
strides = self.stridesInEachLayer[i],
padding = self.paddingInEachLayer[i],
activation = self.layerActivations[i])
elif(self.typeOfLayer[i] == 'pool' and i != self.numberOfLayers-1):
output = tf.layers.max_pooling2d(inputs = output,
pool_size = self.kernelSizeInEachLayer[i],
strides = self.stridesInEachLayer[i])
elif(self.typeOfLayer[i] == 'pool' and i == self.numberOfLayers-1):
self.output = tf.layers.max_pooling2d(inputs = output,
pool_size=self.kernelSizeInEachLayer[i],
strides = self.stridesInEachLayer[i])
elif(self.typeOfLayer[i] == 'fully_connected' and i != self.numberOfLayers-1):
output = tf.contrib.layers.flatten(output)
output = tf.contrib.layers.fully_connected(output,
num_outputs = self.filtersInEachLayer[i],
activation_fn = self.layerActivations[i])
elif(self.typeOfLayer[i] == 'fully_connected' and i == self.numberOfLayers-1):
output = tf.contrib.layers.flatten(output)
self.output = tf.contrib.layers.fully_connected(output,
num_outputs = self.filtersInEachLayer[i],
activation_fn = self.layerActivations[i])
def run_CNN(self, features, labels, mode, params):
self.X = features
self.labels = labels
self.create_a_CNN()
self.hypothesis = tf.nn.softmax(self.output)
self.y_pred_class = tf.argmax(self.hypothesis, axis = 1)
#self.sparseLabels = tf.argmax(labels, axis = 1)
if(mode == tf.estimator.ModeKeys.PREDICT):
spec = tf.estimator.EstimatorSpec(mode = mode, predictions = self.y_pred_class)
else:
self.entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = self.output,
labels = self.labels,
name = 'cross_entropy')
self.loss = tf.reduce_mean(self.entropy, name = 'loss')
self.optimizer = tf.train.AdamOptimizer(learning_rate = params['learning_rate'])
train_op = self.optimizer.minimize(loss = self.loss)
metrics = {'accuracy':tf.metrics.accuracy(labels = self.labels, predictions = self.y_pred_class)}
spec = tf.estimator.EstimatorSpec(mode = mode, predictions = self.y_pred_class,
loss = self.loss, train_op = train_op, eval_metric_ops = metrics)
return spec
def use_CNN(self, trainDataset, trainLabels, CVDataset, CVLabels, mode, parameters):
self.train_input_fn = tf.estimator.inputs.numpy_input_fn(x = trainDataset, y = trainLabels,
num_epochs = None,
shuffle = True)
self.eval_input_fn = tf.estimator.inputs.numpy_input_fn(x = CVDataset, y = CVLabels,
num_epochs = 1,
shuffle = False)
self.predict_input_fn = tf.estimator.inputs.numpy_input_fn(x = CVDataset, y = CVLabels,
num_epochs = 1,
shuffle = False)
self.model = tf.estimator.Estimator(model_fn = self.run_CNN, params = parameters,
model_dir = './cnn_checkpoint')
if(mode == 'Train'):
self.train = self.model.train(input_fn = self.train_input_fn, steps = 10)
elif(mode == 'Evaluate'):
self.evaluate = self.model.evaluate(input_fn = self.eval_input_fn)
elif(mode == 'Predict'):
self.predict = self.model.predict(input_fn = self.predict_input_fn)
我的问题是,在每次训练整个数据集后如何显示loss
?还有如何显示accuracy
指标?每次迭代时,我收到一条我不想显示的消息:
警告:tensorflow:似乎全局步长(tf.train.get_global_step)尚未增加。当前值(可能是稳定值):0与先前值:0。您可以通过将tf.train.get_global_step()传递给Optimizer.apply_gradients或Optimizer.minimize来增加全局步长。