softplus使用

本文介绍了在TensorFlow中使用Softplus激活函数时需要注意的问题,特别是强调了输入必须为tensor,而非list或array,同时提供了一个正确的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

>>> X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]])
>>>> X[:,0]
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
>>> X[:,1]
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])
>>> import torch.nn.functional as F
>>> F.softplus(X[:,0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: softplus(): argument 'input' (position 1) must be Tensor, not numpy.ndarray

softplus针对的是tensor,如果是list,array都会报错

>>> import tensorflow as tf
>>> tf.constant([1,2,3,4,5,6],shape=[2,3])
2020-08-06 22:56:58.331010: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2020-08-06 22:56:58.331941: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance.
<tf.Tensor: id=2, shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>
>>> X=tf.constant([1,2,3,4,5,6],shape=[2,3])
>>> F.softplus(X[:,0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: softplus(): argument 'input' (position 1) must be Tensor, not tensorflow.python.framework.ops.EagerTensor

这种定义tensor也会出错。

以下是正确使用:

>>> X = torch.Tensor([[1,2,3],[4,5,6]])
>>> F.softplus(X[:,0])
tensor([1.3133, 4.0181])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值