栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

tensorflow tensor的批量条件改值 如何修改tensor中符合特定条件的元素值 将满足特定规则的tensor元素值修改

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tensorflow tensor的批量条件改值 如何修改tensor中符合特定条件的元素值 将满足特定规则的tensor元素值修改

问题叙述


这样一个tensor 想用类似

x[np.where(x > 8)] = 8
x[np.where(x<3)] = 3

的形式将其改成

这种批量的条件判断改值,由于tensor不能直接用索引修改值
尝试了几种方法,
比如更改为Varient、numpy 等
会出现
TypeError: only integer scalar arrays can be converted to a scalar index
或者
TypeError: ‘ResourceVariable’ object does not support item assignment
等类似的错误

解决方案
import tensorflow as tf

tensor_input = tf.constant([i for i in range(20)], tf.float32)
tensor_input = tf.reshape(tensor_input, [4, 5])
print(tensor_input)
tensor_input = tf.where(tf.greater(tensor_input,8),8,tensor_input)
tensor_input = tf.where(tf.less(tensor_input,3),3,tensor_input)
print(tensor_input)

运行如下

凌晨找到的解决方案,原理就查api吧 权当抛砖引玉

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529780.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号