def add_eval_step(self, result_tensor, ground_truth_tensor): with tf.name_scope('correlations'): with tf.name_scope('Yaw_correlation'): yaw_correlation, yaw_update_op = streaming_pearson_correlation( predictions=result_tensor[0], labels=ground_truth_tensor[0]) tf.summary.scalar('Yaw_correlation', yaw_update_op) with tf.name_scope('Roll_correlation'): roll_correlation, roll_update_op = streaming_pearson_correlation( predictions=result_tensor[1], labels=ground_truth_tensor[1]) tf.summary.scalar('Roll_correlation', roll_update_op) with tf.name_scope('Pitch_correlation'): pitch_correlation, pitch_update_op = streaming_pearson_correlation( predictions=result_tensor[2], labels=ground_truth_tensor[2]) tf.summary.scalar('Pitch_correlation', pitch_update_op) final_correlations = [ yaw_correlation, roll_correlation, pitch_correlation ] streaming_correlations = [ yaw_update_op, roll_update_op, pitch_update_op ] return streaming_correlations, final_correlations
def metric_fn(per_example_loss, label_ids, logits, is_real_example): """Compute Pearson correlations for STS-B.""" # Display labels and predictions concat1 = contrib_metrics.streaming_concat(logits) concat2 = contrib_metrics.streaming_concat(label_ids) # Compute Pearson correlation pearson = contrib_metrics.streaming_pearson_correlation( logits, label_ids, weights=is_real_example) # Compute MSE # mse = tf.metrics.mean(per_example_loss) mse = tf.metrics.mean_squared_error( label_ids, logits, weights=is_real_example) loss = tf.metrics.mean(values=per_example_loss, weights=is_real_example) return { "pred": concat1, "label_ids": concat2, "pearson": pearson, "MSE": mse, "eval_loss": loss, }
def _define_metrics(self): mse = tf.reduce_mean(tf.square(self.prediction - self.label), name='mse') mae = tf.reduce_mean(tf.abs(self.prediction - self.label), name='mae') pcc = streaming_pearson_correlation(self.prediction, self.label, name='pcc') self.metrics['mse'] = mse self.metrics['mae'] = mae self.metrics['pcc'] = pcc
def rnn_framework(features, labels, mode): #输入层 input_layer = tf.reshape(features, [-1, 256, 128]) #LSTM神经元 lstm_cell = rnn.BasicLSTMCell(50) #RNN _, Rnn = dynamic_rnn(lstm_cell, input_layer, dtype=tf.float64) logits = tf.layers.dense(inputs=Rnn.h, units=8) #生成预测(字典,分别为单值预测和概率预测) predictions = { 'classes': tf.argmax(input=logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } #预测功能:当mode处于预测模式时,返回字典predictions作为对样本的分类预测结果 if mode == tensorflow_estimator.estimator.ModeKeys.PREDICT: return tensorflow_estimator.estimator.EstimatorSpec( mode=mode, predictions=predictions) #损失计算(mode处于训练模式),使用交叉熵作为损失指标 loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) #配置训练操作 if mode == tensorflow_estimator.estimator.ModeKeys.TRAIN: optimizer = tf.train.AdagradOptimizer(learning_rate=0.001) op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) return tensorflow_estimator.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=op) # 建立评估指标 accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions['classes']) precision = tf.metrics.precision(labels=labels, predictions=predictions['classes']) recall = tf.metrics.recall(labels=labels, predictions=predictions['classes']) coe = streaming_pearson_correlation(predictions=tf.to_float( predictions['classes']), labels=tf.to_float(labels)) evaluate = { 'accuracy': accuracy, 'precision': precision, 'recall': recall, 'coef': coe } return tensorflow_estimator.estimator.EstimatorSpec( mode=mode, loss=loss, eval_metric_ops=evaluate)
def pearson(y_true, y_pred): result = streaming_pearson_correlation(y_true, y_pred) return result[0]
def cnn_framework(features, labels, mode): #输入层 input_layer = tf.reshape(features, [-1, 256, 128, 1]) #卷积层1 conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 128], padding='valid', activation=tf.nn.tanh) #输出张量:[252,1,32] #池化层1 pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 1], strides=[2, 1]) #输出张量:[126,1,32] #全连接层 #将pool1扁平化 flat = tf.reshape(pool1, [-1, 126 * 1 * 32]) #全连接层 dense = tf.layers.dense(inputs=flat, units=1024, activation=tf.nn.tanh) #为改善模型效果,对全连接层应用丢弃正则化 dropout = tf.layers.dropout( inputs=dense, rate=0.4, training=mode == tensorflow_estimator.estimator.ModeKeys.TRAIN) #对数层 logits = tf.layers.dense(inputs=dropout, units=8) #生成预测(字典,分别为单值预测和概率预测) predictions = { 'classes': tf.argmax(input=logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } #预测功能:当mode处于预测模式时,返回字典predictions作为对样本的分类预测结果 if mode == tensorflow_estimator.estimator.ModeKeys.PREDICT: return tensorflow_estimator.estimator.EstimatorSpec( mode=mode, predictions=predictions) #损失计算(mode处于训练模式),使用交叉熵作为损失指标 loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) #配置训练操作 if mode == tensorflow_estimator.estimator.ModeKeys.TRAIN: optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) return tensorflow_estimator.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=op) accuracy = tf.metrics.accuracy(labels=labels, predictions=predictions['classes']) precision = tf.metrics.precision(labels=labels, predictions=predictions['classes']) recall = tf.metrics.recall(labels=labels, predictions=predictions['classes']) # labels64=tf.constant(labels,name='labels64',dtype=tf.int64) # predictions64=tf.constant(predictions['classes'],name='predictions64',dtype=tf.int64) coe = streaming_pearson_correlation( predictions=tf.to_float(predictions['classes']), # labels=labels labels=tf.to_float(labels)) # f_score=tf.contrib.metrics.f1_score( # labels=labels, # predictions=predictions['classes'] # ) #建立评估指标 evaluate = { 'accuracy': accuracy, 'precision': precision, 'recall': recall, 'coef': coe # 'f_score':f_score } return tensorflow_estimator.estimator.EstimatorSpec( mode=mode, loss=loss, eval_metric_ops=evaluate)
def metric_fn(label_ids, predictions, is_real_example): from tensorflow.contrib.metrics import streaming_pearson_correlation eval_pearson_cosine = streaming_pearson_correlation(predictions, label_ids) return { "eval_pearson_cosine": eval_pearson_cosine }