numpy之argmax、argmin、maximum函数

同numpy的其他函数一样,mamimum,argmax,argmin函数也都能进行广播,并对np.array进行操作。

maximum

maximum是用来求最大值的,可以用来实现ReLu函数。这个最大值的shape依据最大的array的shape来定。比如比较(2,),(3,2)的数组的最大值。那么maximum返回的array的shape就是(3,2)。而且这里的各元素由参与比较的各array的对应元素的最大值决定。比如有2个(2,2)的array,a,b参与比较热 re = np.maximum(a,b),那么re[0][0]的值等于a[0][0],b[0][0]的最大值,而re[1][0]则是a[1][0],b[1][0]最大值……以此类推。

import numpy as np
a1 = np.array([1,2])
a2 = np.array([[-1,3],[0,1],[4,8]])

re  =np.maximum(a1,a2)
#(3,2)
re.shape

# [[1 3]
# [1 2]
# [4 8]]
print re

argmax

argmax 函数就是用来求一个array中最大值的下标。注意要输入是二维array,返回的不是2个数,而是一个数。这点在C语言上就很好理解,该数表示最大值相对于起始地址的偏移量。

#打印上面求得最大值的下标
#index1 = 5
index1 = np.argmax(re)

#打印一维的array
#index2 = 2 
index2 = np.argmax([1,2,6,3,2])

argmin

argmin 函数就是用来求一个array中最小值的下标。用法同argmin类似。


index1 = np.argmin(re)
index2 = np.argmin([1,2,6,3,2])
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
`hp.process()` 函数是 Hyperopt 库中的一个函数,用于执行一个优化过程。它的定义如下: ```python def process(fn, space, algo, max_evals, trials, rstate, verbose=0, catch_eval_exceptions=False, return_argmin=True, pass_expr_memo_ctrl=None): """ Minimize a function over a hyperparameter space. Parameters ---------- fn : callable A Python callable that is the objective function. This function should return a real-valued loss that hyperopt will seek to minimize. It should have signature ``fn(params) -> loss`` where ``params`` is a dictionary containing the hyperparameters to be optimized and ``loss`` is a float value representing the objective function's value at those hyperparameters. space : hyperopt.pyll.Apply node A Pyll expression representing a tree of (potentially conditional) hyperparameters to optimize. algo : search algorithm A search algorithm. An algorithm is an object that defines the search strategy. It should have a ``suggest`` method that takes a number of trials and returns a set of hyperparameters to evaluate next. max_evals : int The maximum number of evaluations of ``fn``. trials : ``Trials`` object A container for storing the details of each trial. This object defines the interface for communicating back to hyperopt. rstate : numpy.random.RandomState instance A numpy RandomState instance used for random sampling. verbose : int Controls verbosity of output: * 0: no output * 1: print only status updates * 2: print status and results catch_eval_exceptions : bool If True, catch exceptions thrown by the objective function and store the traceback in the corresponding trial document. return_argmin : bool If True, return the hyperparameters that gave the best observed value. Otherwise, return the full dictionary of hyperparameters and their values for each evaluated point. Returns ------- results : either the best point found (if return_argmin=True) or the full dictionary of evaluated points (if return_argmin=False). """ ``` 该函数的主要作用是最小化一个给定的目标函数 `fn`,其参数由 `space` 指定,使用一个指定的优化算法 `algo` 进行搜索,并在给定的最大评估次数 `max_evals` 之后返回结果。它还接受一个 `Trials` 对象来存储每个评估的详细信息,以及一个随机数生成器 `rstate` 来执行随机采样。函数的输出结果可以是最优点的超参数(如果 `return_argmin` 为 `True`),或者每个评估点的超参数和对应的值(如果 `return_argmin` 为 `False`)。函数还支持捕获评估函数 `fn` 抛出的异常,并将其存储在相应的评估记录中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值