在本文中,我们将看看:

  • 在高级机器学习(ML)应用程序中使用自定义损失函数
  • 定义自定义损失函数并集成到基本Tensorflow神经网络模型
  • 一个简单的知识蒸馏学习的例子

介绍

机器学习中预定义的损失函数,可为您尝试优化的问题提供合适的损失值。我们常见的有用于分类的交叉熵损失和用于回归问题的均方误差(MSE)或均方根误差(RMSE)。流行的机器学习(ML)包包括前端(如Keras)和后端(如Tensorflow),其中包括一组用于大多数分类和回归任务的基本损失函数。但是,极个别情况下您可能需要解决某个问题的自定义损失函数,这些函数仅受有效张量运算的约束。

在Keras中,您可以在技术上创建自己的损失函数,但是损失函数的形式仅限于some_loss(y_true,y_pred)。如果您尝试以some_loss_1(y_true,y_pred,** kwargs)的形式向损失添加其他参数,Keras将抛出运行时异常。有很多方法可以解决这个问题,但总的来说我们需要一种 可扩展的方法来编写一个接受我们传递给它的任何有效参数的损失函数,并以标准和预期的方式在我们的张量上运行。我们将看到如何直接使用Tensorflow从头开始编写神经网络并构建自定义损失函数来训练它。

Tensorflow

Tensorflow(TF)是一种符号和数值计算引擎,它允许我们将张量一起串联到计算图中并对它们进行反向传播。Keras是在Tensorflow之上运行的API或前端,它可以方便地打包使用Tensorflow构建的标准架构(例如各种预定义的神经网络层),并抽象出TF的许多低级机制。然而,在使这些架构的过程中,粒度级别的控制和执行非常具体的事情的能力就丧失了。

为了简单起见,张量是多维数组,其形状类似元组(feature_dim, n_features)

例子是能够定义接受任意数量参数的自定义损失函数,并且能够使用网络内部的任意张量和网络外部的输入张量计算损失。严格地说,TF中的损失函数甚至不需要是python函数,而只需是TF张量对象上操作的有效组合。前一点很重要,因为自定义损失来自于计算任意张量上的损失,而不仅仅是严格意义上的监督目标张量和网络输出张量(y_true, y_pred)的形式。

在我们得到自定义损失之前,让我们简要回顾一下基本的2层dense网络(MLP),看看它是如何在TF中定义和训练的。虽然有预定义的TF层,但我们从头开始定义权重和偏差。Python代码如下:

# A simple Tensorflow 2 layer dense network exampleimport tensorflow as tfimport numpy as npfrom sklearn import datasetsfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.decomposition import PCAfrom sklearn.preprocessing import LabelBinarizerimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D# load the sklearn breast cancer datasetbc = datasets.load_breast_cancer()X = bc.data[:, :]Y = bc.target# min max scale and binarize the target labelsscaler = MinMaxScaler()X = scaler.fit_transform(X,Y)label = LabelBinarizer()Y = label.fit_transform(Y)# train fractionfrac = 0.9# shuffle datasetidx = np.random.randint(X.shape[0], size=len(X))X = X[idx]Y = Y[idx]train_stop = int(len(X) * frac)X_ = X[:train_stop]Y_ = Y[:train_stop]X_t = X[train_stop:]Y_t = Y[train_stop:]# plot the first 3 PCA dimensions of the sampled datafig = plt.figure(1, figsize=(8, 6))ax = Axes3D(fig, elev=-150, azim=110)X_reduced = PCA(n_components=3).fit_transform(X_)ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=Y_.ravel(), cmap=plt.cm.Set1, edgecolor='k', s=40)ax.set_title("First three PCA directions")ax.set_xlabel("1st eigenvector")ax.w_xaxis.set_ticklabels([])ax.set_ylabel("2nd eigenvector")ax.w_yaxis.set_ticklabels([])ax.set_zlabel("3rd eigenvector")ax.w_zaxis.set_ticklabels([])plt.show()# create the TF neural net# some hyperparamstraining_epochs = 200n_neurons_in_h1 = 10n_neurons_in_h2 = 10learning_rate = 0.1n_features = len(X[0])labels_dim = 1############################################## basic 2 layer dense net (MLP) example adapted from# https://becominghuman.ai/creating-your-own-neural-network-using-tensorflow-fa8ca7cc4d0e# these placeholders serve as our input tensorsx = tf.placeholder(tf.float32, [None, n_features], name='input')y = tf.placeholder(tf.float32, [None, labels_dim], name='labels')# TF Variables are our neural net parameter tensors, we initialize them to random (gaussian) values in# Layer1. Variables are allowed to be persistent across training epochs and updatable bt TF operationsW1 = tf.Variable(tf.truncated_normal([n_features, n_neurons_in_h1], mean=0, stddev=1 / np.sqrt(n_features)), name='weights1')b1 = tf.Variable(tf.truncated_normal([n_neurons_in_h1], mean=0, stddev=1 / np.sqrt(n_features)), name='biases1')# note the output tensor of the 1st layer is the activation applied to a# linear transform of the layer 1 parameter tensors# the matmul operation calculates the dot product between the tensorsy1 = tf.sigmoid((tf.matmul(x, W1) + b1), name='activationLayer1')# network parameters(weights and biases) are set and initialized (Layer2)W2 = tf.Variable(tf.random_normal([n_neurons_in_h1, n_neurons_in_h2], mean=0, stddev=1), name='weights2')b2 = tf.Variable(tf.random_normal([n_neurons_in_h2], mean=0, stddev=1), name='biases2')# activation function(sigmoid)y2 = tf.sigmoid((tf.matmul(y1, W2) + b2), name='activationLayer2')# output layer weights and biasesWo = tf.Variable(tf.random_normal([n_neurons_in_h2, labels_dim], mean=0, stddev=1 ), name='weightsOut')bo = tf.Variable(tf.random_normal([labels_dim], mean=0, stddev=1), name='biasesOut')# the sigmoid (binary softmax) activation is absorbed into TF's sigmoid_cross_entropy_with_logits losslogits = (tf.matmul(y2, Wo) + bo)loss = tf.nn.sigmoid_cross_entropy_with_logits(labels = y, logits = logits)# tap a separate output that applies softmax activation to the output layer# for training accuracy readouta = tf.nn.sigmoid(logits, name='activationOutputLayer')# optimizer used to compute gradient of loss and apply the parameter updates.# the train_step object returned is ran by a TF Session to train the nettrain_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)# prediction accuracy# compare predicted value from network with the expected value/targetcorrect_prediction = tf.equal(tf.round(a), y)# accuracy determinationaccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name="Accuracy")############################################## ***NOTE global_variables_initializer() must be called before creating a tf.Session()!***init_op = tf.global_variables_initializer()# create a session for training and feedforward (prediction). Sessions are TF's way to run# feed data to placeholders and variables, obtain outputs and update neural net parameterswith tf.Session() as sess: # ***initialization of all variables... NOTE this must be done before running any further sessions!*** sess.run(init_op) # training loop over the number of epochs batch_size = 50 batches = int(len(X_) / batch_size) for epoch in range(training_epochs): losses = 0 accs = 0 for j in range(batches): idx = np.random.randint(X_.shape[0], size=batch_size) X_b = X_[idx] Y_b = Y_[idx] # train the network, note the dictionary of inputs and labels sess.run(train_step, feed_dict={x: X_b, y: Y_b}) # feedforwad the same data and labels, but grab the accuracy and loss as outputs acc, l, soft_max_a = sess.run([accuracy, loss, a], feed_dict={x: X_b, y: Y_b}) losses = losses + np.sum(l) accs = accs + np.sum(acc) print("Epoch %.8d " % epoch, "avg train loss over

tensorflow sigmoid 如何计算训练数据的正确率_用于高级机器学习的自定义TensorFlow损失函数...相关推荐

  1. tensorflow sigmoid 如何计算训练数据的正确率_“来自蒙娜丽莎的凝视”— 结合 TensorFlow.js 和深度学习实现...

    客座博文 / Emily Xie,软件工程师 背景 坊间传闻,当您在房间里走动时,蒙娜丽莎的眼睛会一直盯着您. 这就是所谓的"蒙娜丽莎效应".兴趣使然,我最近就编写了一个可互动的数 ...

  2. tensorflow sigmoid 如何计算训练数据的正确率_量化训练:Quantization Aware Training in Tensorflow(一)...

    本文的内容包括对神经网络模型量化的基本介绍.对Tensorflow量化训练的理解与上手实操. 此外,后续系列还对量化训练中的by pass和batch norm两种情况进行补充解释,欢迎点击浏览,量化 ...

  3. 多元回归训练数据和测试数据_回归基础-数据结构提醒,如果和切换之后的寿命...

    多元回归训练数据和测试数据 I just had a great one on one coding learning session with a good friend of mine over ...

  4. python数据预测模型算法_《python机器学习—预测分析核心算法》:构建预测模型的一般流程...

    参见原书1.5节 构建预测模型的一般流程 问题的日常语言表述->问题的数学语言重述 重述问题.提取特征.训练算法.评估算法 熟悉不同算法的输入数据结构: 1.提取或组合预测所需的特征 2.设定训 ...

  5. vue数据定义格式_用好单元格自定义格式,让Excel按照你的要求显示数据

    [温馨提示]亲爱的朋友,阅读之前请您点击[关注],您的支持将是我最大的动力!#百校助力高考加油站# Excel表格中的数据,我们通常会根据需求,选择设置单元格格式,然后再选择数据类型:数值.文本.日期 ...

  6. 使用命令行工具mc上传模型训练数据到SAP云平台Leonardo机器学习服务的AWS存储

    命令行:mc.exe cp -r C:\Code\MachineLearningStudy\flowers sapjerrys3\data 上传速度还挺快的:200多MB的文件,1分25秒上传完毕. ...

  7. loss 加权_【转载】keras 自定义 loss损失函数, sample在loss上的加权 和 metric

    首先辨析一下概念: 1. loss是整体网络进行优化的目标, 是需要参与到优化运算,更新权值W的过程的 2. metric只是作为评价网络表现的一种"指标", 比如accuracy ...

  8. mdp框架_用于在线机器学习MDP的Python库

    我试图在 Python中设计一个具有以下特征的迭代 markov decision process (MDP)代理: >可观察的状态 >我通过保留一些状态空间来处理潜在的'未知'状态 用于 ...

  9. 如何创建计算机视觉场景训练数据

    作者 | 刘明宽 数据科学部门负责人,澳鹏(Appen)美国  曾任eBay首席研究科学家(数据科学总监) 对于一些精度要求不太高,或者不太复杂的计算机视觉应用场景,利用一些现有的开源数据集如Imag ...

最新文章

  1. php-fpm 进程通讯,PHP-FPM进程模型解析
  2. 心理所发表关于神经科学研究可信度的评论文章
  3. [Android]《Android艺术开发探索》第一章读书笔记
  4. sdut 2087 离散事件模拟-银行管理
  5. 删除-ARMV8-V9-ATF-FFA学习笔记目录-2021-08
  6. 你该认识这样的Linux_shell之变量操作符与分支判断
  7. 树莓派支持uvi协议吗_树莓派开发笔记(十一):蓝牙的使用,BlueZ协议(双树莓探测rssi并通过蓝牙互传获取的rssi)...
  8. c#string倒数第二位插入字符_c#string倒数第二位插入字符_C#利用String类的IndexOf、LastIndexOf、...
  9. nohup xxx 后台进程关闭,可以这样避免
  10. java hibernate 表关联_Hibernate多表关联
  11. matlab和vlfeat关联,VLFeat在matlab和vs中安装
  12. 万年历c语言设计报告,C语言实训题目设计报告 万年历
  13. 视觉SLAM笔记(48) 局部地图
  14. 用户计算机安全管理,关于加强用户计算机安全管理工作的通知
  15. java AES加密解密
  16. 【论文笔记】基于LSTM的问答对排序
  17. paip.分成系统会员推广分销系统的设计
  18. python参考手册文字版_Python3.8标准库参考手册 中文完整pdf高清版
  19. java calendar星期几_正确获取星期几(Calendar.DAY_OF_WEEK)
  20. 一文看懂P2P原理及UDP穿透

热门文章

  1. [FAQ21281]android P分区表中odmdtbo与dtbo分区的说明
  2. 手把手教你使用Python轻松打造淘宝主图视频生成神器
  3. 流利说登陆纽交所,但中国“AI+教育”的市场才刚刚开始
  4. 如何优化html页面
  5. Oracle EBS OM Drop Ship Orders(直发业务)技术-API和核心表关联关系介绍
  6. 文件系统NTFS和FAT32有什么不同
  7. System.ComponentModel.Win32Exception: 系统找不到指定的文件
  8. pytest_参数化parametrize
  9. linux 运行msi文件是什么意思,linux msi文件怎么安装
  10. 2022年全球市场光伏用真空泵总体规模、主要生产商、主要地区、产品和应用细分研究报告