修改TF.tensor中的值

本文详细介绍使用TensorFlow进行张量操作的两种方法,包括如何修改3x3矩阵中的元素,以及如何对2D张量的特定位置进行赋值。通过具体示例,展示了一种基于one-hot矩阵的方法,用于高效地更新张量中的特定元素。
部署运行你感兴趣的模型镜像

方法一:

# 改变 tf.Tensor中的一个 3*3 矩阵: 重新赋值 temp
temp = final[i:i + 3, j:j +3] + (lambd * final[i + 1, j + 1])*window 
# 切片 4 部分
top_section = final[0:i, :]  # top
behind_section = final[i + 3:, :]  # behind
left_section = final[i:i + 3, 0:j]  # left
right_section = final[i:i + 3, j + 3:]  # right
mid_section = tf.concat([left_section, temp, right_section], axis=1)  # 左 中 右
final = tf.concat([top_section, mid_section, behind_section], axis=0) # 上 中 下

方法二:

def tensor_assign_2D(tensor_input, position, value):
    '''
    给 2D tensor的特定位置元素赋值
    tensor_input: 输入的2D tensor,目前只支持2D
    position: 被赋值的张量元素的坐标位置,=【h_index,w_index】
    '''
    shape = tensor_input.get_shape().as_list()
    height = shape[0]
    width = shape[1]
    h_index = position[0]
    w_index = position[1]
    one_hot_matrix = get_one_hot_matrix(height, width, position)
    new_tensor = tensor_input - tensor_input[h_index, w_index] * one_hot_matrix + one_hot_matrix * value
    return new_tensor
def get_one_hot_matrix(height, width, position):
    '''
    生成一个 one_hot矩阵,shape=【height*width】,在position处的元素为1,其余元素为0
    position: 格式为【h_Index,w_Index】,h_Index,w_Index为int格式
    '''
    col_length = height
    row_length = width
    col_one_position = position[0]
    row_one_position = position[1]
    rows_num = height
    cols_num = width

    single_row_one_hot = tf.one_hot(row_one_position, row_length, dtype=tf.float32)
    single_col_one_hot = tf.one_hot(col_one_position, col_length, dtype=tf.float32)

    one_hot_rows = tensor_expand(single_row_one_hot, rows_num)
    one_hot_cols = tensor_expand(single_col_one_hot, cols_num)
    one_hot_cols = tf.transpose(one_hot_cols)

    one_hot_matrx = one_hot_rows * one_hot_cols
    return one_hot_matrx

 张量自我复制扩展:

def tensor_expand(tensor_Input, Num):
    '''
    张量自我复制扩展,将Num个tensor_Input串联起来,生成新的张量,
    新的张量的shape=[tensor_Input.shape, Num]
    '''
    tensor_Input = tf.expand_dims(tensor_Input, axis=0)
    tensor_Output = tensor_Input
    for i in range(Num - 1):
        tensor_Output = tf.concat([tensor_Output, tensor_Input], axis=0)
    return tensor_Output

将Num个tensor_Input串联起来,生成新的张量,新的张量的shape=[tensor_Input.shape, Num]

 

测试:

if __name__ == "__main__":
    ##test
    tensor_input = tf.constant([i for i in range(20)], tf.float32)
    tensor_input = tf.reshape(tensor_input, [4, 5])
    new_tensor = tensor_assign_2D(tensor_input, [2, 3], final2[1,1])
    print(new_tensor.eval())

    sess = tf.Session()
    opt = tf.global_variables_initializer()
    sess.run(opt)
    sess.run(temp)

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值