tensorflow API使用记录

tensorflow API使用记录

文章目录

0. 常用函数

  • tf.train.list_variables(ckpt_dir_or_file) Returns list of all variables in the checkpoint

1. tf.nn.bias_add

tf.nn.bias_add(value, bias, name = None),把bias添加到value上。其中bias必须为一维的,若value的维度大于1则为广播相加。具体来说,value最后一维的维度必须要和bias的维度一致才可以。

import tensorflow as tf a=tf.constant([[[1,1],[2,2],[3,3]]],dtype=tf.float32) b=tf.constant([1,-1],dtype=tf.float32) c=tf.constant([1],dtype=tf.float32) with tf.Session() as sess: print('bias_add:') print(a.shape) print(b.shape) print(sess.run(tf.nn.bias_add(a, b))) #执行下面语句错误 #print(sess.run(tf.nn.bias_add(a, c))) 

2. tf.tensordot

tf.tensordot(a, b, axis),axis=0的时候,数据的维度就是拼接,如下所示:

a = tf.constant([1, 2, 1, 1, 1, 1, 1, 1, 1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], shape=[2,3,4]) print(a.shape) b = tf.constant([1,1,1,1,5,6,7,8,9,10,11,12], shape=[4,3]) print(b.shape) c = tf.tensordot(a, b, axes=0) print(c.shape) with tf.Session(): print(a.eval()) print(b.eval()) print(c.eval()) 

相关shape的结果如下所示:

(2, 3, 4) (4, 3) (2, 3, 4, 4, 3) 

axis = 1的时候,是矩阵的乘法,这里的乘法是一种乘法的扩展版本。也就是广播+矩阵乘法的形式。如果不加广播,这样写就是错的,如下所示:

a = tf.constant([1, 2, 1, 1, 1, 1, 1, 1, 1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], shape=[2,3,4]) print(a.shape) b = tf.constant([1,1,1,1,5,6,7,8,9,10,11,12], shape=[4,3]) print(b.shape) c = tf.matmul(a, b) #该行会报错,因为多维张量的乘法,前面的维度必须相同,然后是最后两维对应的矩阵相乘 print(c.shape) with tf.Session(): print(a.eval()) print(b.eval()) print(c.eval()) 

而使用tensordot就不会报错,具体代码如下所示:

a = tf.constant([1, 2, 1, 1, 1, 1, 1, 1, 1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], shape=[2,3,4]) print(a.shape) b = tf.constant([1,1,1,1,5,6,7,8,9,10,11,12], shape=[4,3]) print(b.shape) c = tf.tensordot(a, b, axes = 1) print(c.shape) with tf.Session(): print(a.eval()) print(b.eval()) print(c.eval()) 

相关shape的结果如下所示:

(2, 3, 4) (4, 3) (2, 3, 3) 

假设三维数据和一维数据进行tensordot,正确的写法之一是

a = tf.constant([1, 2, 1, 1, 1, 1, 1, 1, 1,1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], shape=[3, 2, 4]) print(a.shape) b = tf.constant([1, 1, 1, 1], shape=[4]) print(b.shape) c = tf.tensordot(a, b, axes=1) print(c.shape) with tf.Session(): print(a.eval()) print(b.eval()) print(c.eval()) 

这种情况下会对b进行扩展,结果就是[4,1],然后a和b进行matmul,结果为[3,2,1],再进行sequeeze最后一维,结果是[3,2]。其中这里的矩阵表示的都是shape。

可参考链接:https://www.machenxiao.com/blog/tensordot

3. 交叉熵的不同API

  • tf.keras.losses.BinaryCrossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)
  • categorical_crossentropy(y_true, y_pred, from_logits=False, axis=-1),需要注意的是y_true是one-hot表示,而y_pred表示了每种类别对应的概率。
  • sparse_categorical_crossentropy(y_true, y_pred, from_logits=False, axis=-1),sparse相比于上式而言,y_true的输入是整数值,而不是one-hot表示。

参考链接:https://zhuanlan.zhihu.com/p/112314557

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.