Python 如何生成具有特定形状值的数组?

Python 如何生成具有特定形状值的数组?,python,numpy,matrix,Python,Numpy,Matrix,我想创建一个数组,其值范围为0.0到1.0,如下所示: 基本上,左边缘和上边缘应保持接近1.0,但在拐角处缓慢衰减到0.5。 底部和右侧边缘应保持接近0.0 中间区域大部分应为0.5,值应在1.0到0.0之间呈对角线衰减 这是我尝试过的,但它并没有给我想要的 import numpy as np import matplotlib.pyplot as plt def sigmoid(x): y = np.zeros(len(x)) for i in range(len(x)):

我想创建一个数组,其值范围为0.0到1.0,如下所示:

基本上,左边缘和上边缘应保持接近1.0,但在拐角处缓慢衰减到0.5。 底部和右侧边缘应保持接近0.0 中间区域大部分应为0.5,值应在1.0到0.0之间呈对角线衰减

这是我尝试过的,但它并没有给我想要的

import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
  y = np.zeros(len(x))
  for i in range(len(x)):
    y[i] = 1 / (1 + math.exp(-x[i]))
  return y

sigmoid_ = sigmoid(np.linspace(20, 2.5, 30))
temp1 = np.repeat(sigmoid_.reshape((1,len(sigmoid_))), repeats=10, axis=0)
sigmoid_ = sigmoid(np.linspace(6, 3, 10))
temp2 = np.repeat(sigmoid_.reshape((len(sigmoid_),1)), repeats=30, axis=1)
alpha1 = temp1 + temp2

sigmoid_ = sigmoid(np.linspace(-2.5, -20, 30))
temp1 = np.repeat(sigmoid_.reshape((1,len(sigmoid_))), repeats=10, axis=0)
sigmoid_ = sigmoid(np.linspace(-3, -6, 10))
temp2 = np.repeat(sigmoid_.reshape((len(sigmoid_),1)), repeats=30, axis=1)
alpha2 = temp1 + temp2

alpha = alpha1 + alpha2
alpha = alpha - np.min(alpha)
alpha = alpha / np.max(alpha)

plt.matshow(alpha)
这就给了我:


有人能帮我吗?

这是我能想到的最简单的函数:

tune_me = 101    
x = np.linspace(0, 1, tune_me)
y = np.linspace(0, 1, tune_me)
xv, yv = np.meshgrid(x, y)
sig = 1/(1 + np.exp(tune_me - xv - yv))

plt.matshow(sig)

但是如果您想要一些特定的东西,您可能应该在尝试实现它之前计算出您的数学(可能在数学堆栈交换上)。如果没有其他要求,我会对权重矩阵区域的所有部分使用相同的函数。经过适当的平移和缩放后,sigmoid函数(在中心附近快速变化,在远离中心处缓慢变化)确实是合适的。对于sigmoid函数的参数,我将从区域的一角选择

import math
import numpy as np
import matplotlib.pyplot as plt

alpha = np.ndarray((10, 30))
ymax, xmax = alpha.shape[0]-1, alpha.shape[1]-1
for y in range(alpha.shape[0]):
    for x in range(alpha.shape[1]):
        M = x/xmax+y/ymax   # Manhattan distance, range [0;2]
        M -= 1              # make range [-1;1], so that inflection point is in the middle
        M *= 3.0            # the higher this factor, the steeper the sigmoid curve at the flex
        s = 1 / (1 + math.exp(-M))  # sigmoid function, range ]0;1[
        alpha[y, x] = 1-s   # decay from 1 to 0

plt.matshow(alpha)
plt.show()
# show the values close to 0.5
h = [(y, x) for y in range(alpha.shape[0])
            for x in range(alpha.shape[1]) if .4 < alpha[y, x] and alpha[y, x] < .6]
hy, hx = zip(*h)
plt.plot(hx, hy, 'o')
plt.gca().invert_yaxis()
plt.show()
导入数学
将numpy作为np导入
将matplotlib.pyplot作为plt导入
α=np.ndarray((10,30))
ymax,xmax=alpha.shape[0]-1,alpha.shape[1]-1
对于范围内的y(α形状[0]):
对于范围内的x(α形状[1]):
M=x/xmax+y/ymax#曼哈顿距离,范围[0;2]
M=1α,使范围[-1;1 ],使拐点处于中间。
M*=3.0#该系数越高,弯曲处的S形曲线越陡
s=1/(1+math.exp(-M))#sigmoid函数,范围]0;1[
α[y,x]=1-s#从1衰减到0
马绍plt.matshow(阿尔法)
plt.show()
#显示接近0.5的值
h=[(y,x)表示范围内的y(alpha.shape[0])
对于范围内的x(α形状[1]),如果.4<α[y,x]和α[y,x]<.6]
hy,hx=zip(*h)
平面图(hx,hy,'o')
plt.gca().invert_yaxis()
plt.show()

接近0.5的中心区域的值的图示,例如,]0.4;0.6[:你想要什么有数学表示吗?因为有很多方法可以得到模糊的二维S形。@user633611快速和慢速之间的区别?