我需要计算矩阵的梯度(3,3),说a=array([[1,4,2],[6,2,4],[7,5,1]]).
我只是用:
from numpy import *
dx,dy = gradient(a)
>>> dx
array([[ 5. , -2. , 2. ],
[ 3. , 0.5, -0.5],
[ 1. , 3. , -3. ]])
>>> dy
array([[ 3. , 0.5, -2. ],
[-4. , -1. , 2. ],
[-2. , -3. , -4. ]])
Run Code Online (Sandbox Code Playgroud)
我知道计算矩阵梯度的一种方法是通过卷积与每个方向的掩模,但结果是不同的
from scipy import ndimage
mx=array([[-1,0,1],[-1,0,1],[-1,0,1]])
my=array([[-1,-1,-1],[0,0,0],[1,1,1]])
cx=ndimage.convolve(a,mx)
cy=ndimage.convolve(a,my)
>>> cx
array([[-2, 0, 2],
[ 3, 7, 4],
[ 8, 14, 6]])
>>> cy
array([[ -8, …Run Code Online (Sandbox Code Playgroud) 在python中是否有效地实现了matlab的deconv?
# Convolve
z=conv(x, y)
# Deconvolve
y0=deconv(z, x)
# Hope y~=y0
Run Code Online (Sandbox Code Playgroud)
(令人惊讶的是,谷歌搜索没有带来任何有趣的结果)
我试图理解FTT和卷积(互相关)理论,因此我创建了以下代码来理解它.代码是Matlab/Octave,但我也可以在Python中完成.
在1D:
x = [5 6 8 2 5];
y = [6 -1 3 5 1];
x1 = [x zeros(1,4)];
y1 = [y zeros(1,4)];
c1 = ifft(fft(x1).*fft(y1));
c2 = conv(x,y);
c1 = 30 31 57 47 87 47 33 27 5
c2 = 30 31 57 47 87 47 33 27 5
Run Code Online (Sandbox Code Playgroud)
在2D中:
X=[1 2 3;4 5 6; 7 8 9]
y=[-1 1];
conv1 = conv2(x,y)
conv1 =
24 53 89 29 21
96 140 197 65 42
168 227 …Run Code Online (Sandbox Code Playgroud) 有人可以帮我转换嵌套的for循环到CUDA内核吗?这是我试图转换为CUDA内核的函数:
// Convolution on Host
void conv(int* A, int* B, int* out) {
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
out[i + j] += A[i] * B[j];
}
Run Code Online (Sandbox Code Playgroud)
我已经非常努力地并行化这段代码.
这是我的尝试:
__global__ void conv_Kernel(int* A, int* B, int* out) {
int i = blockIdx.x;
int j = threadIdx.x;
__shared__ int temp[N];
__syncthreads();
temp[i + j] = A[i] * B[j];
__syncthreads();
int sum = 0;
for (int k = 0; k …Run Code Online (Sandbox Code Playgroud) 当他们是Caffe和Theano的时候,我正在努力学习(和比较)不同的深度学习框架.
http://caffe.berkeleyvision.org/gathered/examples/mnist.html
和
http://deeplearning.net/tutorial/lenet.html
我按照教程在MNIST数据集上运行这些框架.但是,我注意到在准确性和性能方面存在很大差异.
对于Caffe来说,它的准确度非常快,可以达到~97%.事实上,完成程序只需5分钟(使用GPU),测试集的最终精度超过99%.真是太棒了!
然而,在Theano上,它更加贫穷.我花了超过46分钟(使用相同的GPU),只是为了达到92%的测试性能.
我很困惑,因为在同一数据集上运行相对相同的架构的框架之间不应该有太大的区别.
所以我的问题是.Caffe报告的准确度数字是测试集上正确预测的百分比吗?如果是这样,是否有任何解释的差异?
谢谢.
卷积运算是tf.nn.conv2d()
pooling是tf.nn.max_pool()
但我在示例和教程中找不到如何在TF中实现Filter Concatenation?
convolution neural-network deep-learning conv-neural-network tensorflow
我在由2d图像组成的数据样本上使用卷积层.滤波器形状的一个选项是1x2,它作用于两个相邻像素的1x2连续块.如果我想要一个也可以作用于2个像素的滤镜,但是它们之间的另一个像素被分开了怎么办?是否有可能在神经网络中编码这样的滤波器用于卷积?
当前正在通过斯坦福大学CS131的免费在线课程学习计算机视觉和机器学习。遇到了一些沉重的数学公式,并想知道是否有人可以只知道图像的高度,宽度和核的高度和宽度,向我解释如何为卷积算法实现朴素的4个嵌套的for循环。通过在线研究,我提出了这个解决方案。
image_padded = np.zeros((image.shape[0] + 2, image.shape[1] + 2))
image_padded[1:-1, 1:-1] = image
for x in range(image.shape[1]): # Loop over every pixel of the image
for y in range(image.shape[0]):
# element-wise multiplication of the kernel and the image
out[y, x] = (kernel * image_padded[y:y + 3, x:x + 3]).sum()
Run Code Online (Sandbox Code Playgroud)
通过使用这种算法的一些网站示例,我能够理解这一点,但是,我似乎无法理解4个嵌套的for循环是如何做到的。而且,如果可以的话,可以将公式分解为更容易理解的公式,然后再从网上找到给定的数学公式。
编辑:只是为了澄清一下,我留下的代码片段在某种程度上可以正常工作,我试图提出一种解决方案,该解决方案的优化程度较低,对初学者更友好,例如此代码在询问什么:
def conv_nested(image, kernel):
"""A naive implementation of convolution filter.
This is a naive implementation of convolution using 4 nested for-loops.
This function computes convolution of an image with a …Run Code Online (Sandbox Code Playgroud) 我有一个8位图像,我想用一个矩阵对它进行过滤以进行边缘检测。我的内核矩阵是
0 1 0
1 -4 1
0 1 0
Run Code Online (Sandbox Code Playgroud)
对于某些指数,它给了我负值。我应该和他们一起做什么?