Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法使numpy数组中的数字随机为正或负?_Python_Python 3.x_Numpy_Random_Neural Network - Fatal编程技术网

Python 有没有办法使numpy数组中的数字随机为正或负?

Python 有没有办法使numpy数组中的数字随机为正或负?,python,python-3.x,numpy,random,neural-network,Python,Python 3.x,Numpy,Random,Neural Network,我正在制作一个神经网络,当使用np.random.rand(797600)分配随机权重值时,它们都是正的(从0到1)。正常情况下这很好,但我有多达800个节点,这意味着在初始正向传播结束时,如果所有权重都为正,则Sigmoid输出始终为1,因为所有值的总和加起来如此之快,有这么多的突触和节点 为了解决这个问题,我想做一个函数,将每个权重随机乘以1或-1。然后,使用一个随机数的正数和负数,输出将更接近于0,并且sigmoid函数将返回一个实际预测值,该预测值并非始终为1。以下是我尝试过的两种方法,

我正在制作一个神经网络,当使用
np.random.rand(797600)
分配随机权重值时,它们都是正的(从0到1)。正常情况下这很好,但我有多达800个节点,这意味着在初始正向传播结束时,如果所有权重都为正,则Sigmoid输出始终为1,因为所有值的总和加起来如此之快,有这么多的突触和节点

为了解决这个问题,我想做一个函数,将每个权重随机乘以1或-1。然后,使用一个随机数的正数和负数,输出将更接近于0,并且sigmoid函数将返回一个实际预测值,该预测值并非始终为1。以下是我尝试过的两种方法,它们都不起作用

# method 1

import random as rand
import numpy as np


def random_positive_or_negative(value):
    return rand.choice([1, -1]) * value

example_weights = np.random.rand(4, 4)
print(random_positive_or_negative(example_weights))

打印以下内容之一:

[[0.89098337 0.82291754 0.7730489  0.371631  ]
 [0.22790221 0.19964653 0.94609767 0.57070762]
 [0.35840034 0.06689964 0.71565062 0.43360395]
 [0.57860037 0.11338668 0.338402   0.30737682]]
[[-0.79750561 -0.94206793 -0.389792   -0.18541991]
 [-0.36132547 -0.66040689 -0.06270979 -0.90775857]
 [-0.22350726 -0.21148559 -0.78874412 -0.9702534 ]
 [-0.74124928 -0.31675956 -0.97471565 -0.18389436]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
或者像这样:

[[0.89098337 0.82291754 0.7730489  0.371631  ]
 [0.22790221 0.19964653 0.94609767 0.57070762]
 [0.35840034 0.06689964 0.71565062 0.43360395]
 [0.57860037 0.11338668 0.338402   0.30737682]]
[[-0.79750561 -0.94206793 -0.389792   -0.18541991]
 [-0.36132547 -0.66040689 -0.06270979 -0.90775857]
 [-0.22350726 -0.21148559 -0.78874412 -0.9702534 ]
 [-0.74124928 -0.31675956 -0.97471565 -0.18389436]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
预期输出如下所示:

[[0.89098337 0.82291754 0.7730489  0.371631  ]
 [0.22790221 0.19964653 0.94609767 0.57070762]
 [0.35840034 0.06689964 0.71565062 0.43360395]
 [0.57860037 0.11338668 0.338402   0.30737682]]
[[-0.79750561 -0.94206793 -0.389792   -0.18541991]
 [-0.36132547 -0.66040689 -0.06270979 -0.90775857]
 [-0.22350726 -0.21148559 -0.78874412 -0.9702534 ]
 [-0.74124928 -0.31675956 -0.97471565 -0.18389436]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
[[0.2158195  0.16492544 0.25672823 -0.5392236 ]
 [-0.54530676 0.98215902 -0.14348151 0.02629328]
 [-0.8642513  -0.71726141 -0.15890395 -0.08488439]
 [0.54413198 -0.69790104 0.05317512 -0.06144755]]
打印此文件:

<generator object random_positive_or_negative2.<locals>.<genexpr> at 0x114c474a0>

您可以创建一个由从宇宙[-1,1]中采样的随机数填充的矩阵,并将其和随机权重相乘。请参见下面的代码

import random as rand
import numpy as np


def random_positive_or_negative(value):
    return np.matmul(value, np.random.choice(np.array([-1, 1]), value.shape))

example_weights = np.random.rand(4, 4)
print(random_positive_or_negative(example_weights))


[[-0.7193314  -0.1604493  -0.47038437 -0.34173619]
 [ 0.44388733 -0.55476039 -1.24586476 -0.77014132]
 [-0.05796445 -1.72406933 -1.5756221  -0.18125272]
 [ 0.15338058 -0.56916866 -1.5706919  -0.01815559]]

第一个方法选择一个数字,1或-1,并将整个参数数组乘以该数字。第二个方法使用生成器表达式,因此它将返回生成器。如果你不明白这一点,你应该先阅读发电机

没有必要将任何值乘以1,因为这不起任何作用。取而代之的是,选择随机指数并将其乘以-1。比如:

 n = example_weights.size
 inds = np.random.choice(n, n, replace=False)
 example_weights.flat[inds] *= -1
[[-0.91852112 -0.77686616 -0.41495832  0.78950649]
 [-0.7493404  -0.73794508  0.54622202 -0.89033855]
 [ 0.31196172  0.06584705 -0.88698673 -0.24149299]
 [-0.89654412  0.45450007 -0.40640681  0.81490564]]

有几种方法可以做到这一点,但在我看来,最圆滑的方法是@xdurch0:使用
np.random.uniform(-1,1,size)

下面是代码的工作方式:

import numpy as np

example_weights = np.random.uniform(-1., 1., [4, 4])
print(example_weights)

打印如下内容:

 n = example_weights.size
 inds = np.random.choice(n, n, replace=False)
 example_weights.flat[inds] *= -1
[[-0.91852112 -0.77686616 -0.41495832  0.78950649]
 [-0.7493404  -0.73794508  0.54622202 -0.89033855]
 [ 0.31196172  0.06584705 -0.88698673 -0.24149299]
 [-0.89654412  0.45450007 -0.40640681  0.81490564]]

创建一个大小相同的矩阵并随机填充1和-1,然后将这两个矩阵相乘?有什么原因不能只使用
np.random.uniform(-1,1,size)
?@xdurch0非常感谢!我是numpy lol的新手。所以,根据我给出的例子,我会通过
np.random.uniform(-1,1,797600])
?是的。您可以查看文档。@xdurch0是的,谢谢!我对它进行了测试,效果非常好。肯定是给出的最佳答案。这确实有效,但我将使用
np.random.uniform(-1,1,size)
,因为它太干净了。谢谢你的回答!这确实有效,但我将使用
np.random.uniform(-1,1,size)
,因为它非常干净。谢谢你的回答!