tf.ones_like()

本文介绍了如何使用TensorFlow中的ones_like和zeros_like函数创建与输入张量形状和类型相同的全零和全一矩阵,并通过实例展示了它们的用法和运行结果。
tf.ones_like(
    input, dtype=None, name=None
)

在这里插入图片描述

import tensorflow as tf
import numpy as np

# 生成一个tensor,内部数据随机产生
a = tf.convert_to_tensor(np.random.random([2, 4, 5]), dtype=tf.float32)

# ones_like
b = tf.ones_like(a, dtype=tf.float32, name='ones_like')

# zeros_like
c = tf.zeros_like(a, dtype=tf.float32, name='zeros_like')

print(b)

print(c)

with tf.Session() as sess:
    b_, c_ = sess.run([b, c])
    print("b's shape: ", b_.shape)
    print("c's shape: ", c_.shape)
    print(b_)
    print(c_)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值