提交 09010219 authored 作者: Maxim Kochurov's avatar Maxim Kochurov 提交者: Maxim Kochurov

remove pytensor.tensor.signal module as it was marked deprecated

上级 b6fb55d5
"""
Contains a wrapper function for tensor.nnet.ConvOp, which can be used to perform
generic 2D convolution.
"""
import logging
import warnings
from pytensor import tensor as at
from pytensor.tensor.nnet import conv
from pytensor.tensor.shape import reshape
warnings.warn(
"The module `pytensor.tensor.signal` is deprecated and will "
"be removed from PyTensor in version 2.9.0.",
DeprecationWarning,
stacklevel=2,
)
__docformat__ = "restructuredtext en"
_logger = logging.getLogger("pytensor.tensor.signal.conv")
def conv2d(
input,
filters,
image_shape=None,
filter_shape=None,
border_mode="valid",
subsample=(1, 1),
**kargs,
):
"""
signal.conv.conv2d performs a basic 2D convolution of the input with the
given filters. The input parameter can be a single 2D image or a 3D tensor,
containing a set of images. Similarly, filters can be a single 2D filter or
a 3D tensor, corresponding to a set of 2D filters.
Shape parameters are optional and will result in faster execution.
Parameters
----------
input : Symbolic pytensor tensor for images to be filtered.
Dimensions: ([num_images], image height, image width)
filters : Symbolic pytensor tensor for convolution filter(s).
Dimensions: ([num_filters], filter height, filter width)
border_mode: {'valid', 'full'}
See scipy.signal.convolve2d.
subsample
Factor by which to subsample output.
image_shape : tuple of length 2 or 3
([num_images,] image height, image width).
filter_shape : tuple of length 2 or 3
([num_filters,] filter height, filter width).
kwargs
See pytensor.tensor.nnet.conv.conv2d.
Returns
-------
symbolic 2D,3D or 4D tensor
Tensor of filtered images, with shape
([number images,] [number filters,] image height, image width).
"""
assert input.ndim in (2, 3)
assert filters.ndim in (2, 3)
# use shape information if it is given to us ###
if filter_shape and image_shape:
if input.ndim == 3:
bsize = image_shape[0]
else:
bsize = 1
imshp = (1,) + tuple(image_shape[-2:])
if filters.ndim == 3:
nkern = filter_shape[0]
else:
nkern = 1
kshp = filter_shape[-2:]
else:
nkern, kshp = None, None
bsize, imshp = None, None
# reshape tensors to 4D, for compatibility with ConvOp ###
if input.ndim == 3:
sym_bsize = input.shape[0]
else:
sym_bsize = 1
if filters.ndim == 3:
sym_nkern = filters.shape[0]
else:
sym_nkern = 1
new_input_shape = at.join(0, at.stack([sym_bsize, 1]), input.shape[-2:])
input4D = reshape(input, new_input_shape, ndim=4)
new_filter_shape = at.join(0, at.stack([sym_nkern, 1]), filters.shape[-2:])
filters4D = reshape(filters, new_filter_shape, ndim=4)
# perform actual convolution ###
op = conv.ConvOp(
output_mode=border_mode,
dx=subsample[0],
dy=subsample[1],
imshp=imshp,
kshp=kshp,
nkern=nkern,
bsize=bsize,
**kargs,
)
output = op(input4D, filters4D)
# flatten to 3D tensor if convolving with single filter or single image
if input.ndim == 2 and filters.ndim == 2:
output = at.flatten(output.T, ndim=2).T
elif input.ndim == 2 or filters.ndim == 2:
output = at.flatten(output.T, ndim=3).T
return output
差异被折叠。
差异被折叠。
......@@ -12,8 +12,6 @@ check_nondiff_rop,
"""
import itertools
import numpy as np
import pytest
......@@ -26,7 +24,6 @@ from pytensor.graph.op import Op
from pytensor.tensor.math import argmax, dot
from pytensor.tensor.math import max as at_max
from pytensor.tensor.shape import unbroadcast
from pytensor.tensor.signal.pool import Pool
from pytensor.tensor.type import matrix, vector
from tests import unittest_tools as utt
......@@ -248,63 +245,6 @@ class TestRopLop(RopLopChecker):
unbroadcast(self.x[:4].dimshuffle("x", 0), 0).sum(axis=1), (1,)
)
@pytest.mark.slow
def test_downsample(self):
rng = np.random.default_rng(utt.fetch_seed())
# ws, shp
examples = (
((2,), (16,)),
(
(2,),
(
4,
16,
),
),
(
(2,),
(
4,
2,
16,
),
),
((1, 1), (4, 2, 16, 16)),
((2, 2), (4, 2, 16, 16)),
((3, 3), (4, 2, 16, 16)),
((3, 2), (4, 2, 16, 16)),
((3, 2, 2), (3, 2, 16, 16, 16)),
((2, 3, 2), (3, 2, 16, 16, 16)),
((2, 2, 3), (3, 2, 16, 16, 16)),
((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)),
)
for example, ignore_border in itertools.product(examples, [True, False]):
(ws, shp) = example
vx = rng.random(shp)
vex = rng.random(shp)
x = pytensor.shared(vx)
ex = pytensor.shared(vex)
maxpool_op = Pool(ignore_border, ndim=len(ws))
a_pooled = maxpool_op(x, ws).flatten()
yv = Rop(a_pooled, x, ex)
mode = None
if pytensor.config.mode == "FAST_COMPILE":
mode = "FAST_RUN"
rop_f = function([], yv, on_unused_input="ignore", mode=mode)
sy, _ = pytensor.scan(
lambda i, y, x, v: (grad(y[i], x) * v).sum(),
sequences=at.arange(a_pooled.shape[0]),
non_sequences=[a_pooled, x, ex],
mode=mode,
)
scan_f = function([], sy, on_unused_input="ignore", mode=mode)
v1 = rop_f()
v2 = scan_f()
assert np.allclose(v1, v2), f"Rop mismatch: {v1} {v2}"
def test_join(self):
tv = np.asarray(self.rng.uniform(size=(10,)), pytensor.config.floatX)
t = pytensor.shared(tv)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论