File size: 9,960 Bytes
1f5470c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
from keras.src import ops
from keras.src.api_export import keras_export
from keras.src.backend import standardize_dtype
from keras.src.initializers.initializer import Initializer
from keras.src.saving import serialization_lib
from keras.src.utils.module_utils import scipy
@keras_export(["keras.initializers.Constant", "keras.initializers.constant"])
class Constant(Initializer):
"""Initializer that generates tensors with constant values.
Only scalar values are allowed.
The constant value provided must be convertible to the dtype requested
when calling the initializer.
Examples:
>>> # Standalone usage:
>>> initializer = Constant(10.)
>>> values = initializer(shape=(2, 2))
>>> # Usage in a Keras layer:
>>> initializer = Constant(10.)
>>> layer = Dense(3, kernel_initializer=initializer)
Args:
value: A Python scalar.
"""
def __init__(self, value=0.0):
self.value = value
def __call__(self, shape, dtype=None):
dtype = standardize_dtype(dtype)
return ops.cast(self.value, dtype=dtype) * ops.ones(
shape=shape, dtype=dtype
)
def get_config(self):
return {"value": serialization_lib.serialize_keras_object(self.value)}
@classmethod
def from_config(cls, config):
value = serialization_lib.deserialize_keras_object(config["value"])
return cls(value)
@keras_export(["keras.initializers.Zeros", "keras.initializers.zeros"])
class Zeros(Initializer):
"""Initializer that generates tensors initialized to 0.
Examples:
>>> # Standalone usage:
>>> initializer = Zeros()
>>> values = initializer(shape=(2, 2))
>>> # Usage in a Keras layer:
>>> initializer = Zeros()
>>> layer = Dense(units=3, kernel_initializer=initializer)
"""
def __call__(self, shape, dtype=None):
"""Returns a tensor object initialized as specified by the initializer.
Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
are supported. If not specified, `keras.backend.floatx()`
is used, which default to `float32` unless you configured it
otherwise (via `keras.backend.set_floatx(float_dtype)`).
"""
dtype = standardize_dtype(dtype)
return ops.zeros(shape, dtype=dtype)
@keras_export(["keras.initializers.Ones", "keras.initializers.ones"])
class Ones(Initializer):
"""Initializer that generates tensors initialized to 1.
Also available via the shortcut function `ones`.
Examples:
>>> # Standalone usage:
>>> initializer = Ones()
>>> values = initializer(shape=(2, 2))
>>> # Usage in a Keras layer:
>>> initializer = Ones()
>>> layer = Dense(3, kernel_initializer=initializer)
"""
def __call__(self, shape, dtype=None):
"""Returns a tensor object initialized as specified by the initializer.
Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
are supported. If not specified, `keras.backend.floatx()`
is used, which default to `float32` unless you configured it
otherwise (via `keras.backend.set_floatx(float_dtype)`).
"""
dtype = standardize_dtype(dtype)
return ops.ones(shape, dtype=dtype)
@keras_export(
[
"keras.initializers.Identity",
"keras.initializers.identity",
"keras.initializers.IdentityInitializer",
]
)
class Identity(Initializer):
"""Initializer that generates the identity matrix.
Only usable for generating 2D matrices.
Examples:
>>> # Standalone usage:
>>> initializer = Identity()
>>> values = initializer(shape=(2, 2))
>>> # Usage in a Keras layer:
>>> initializer = Identity()
>>> layer = Dense(3, kernel_initializer=initializer)
Args:
gain: Multiplicative factor to apply to the identity matrix.
"""
def __init__(self, gain=1.0):
self.gain = gain
def __call__(self, shape, dtype=None):
"""Returns a tensor object initialized as specified by the initializer.
Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
are supported. If not specified, `keras.backend.floatx()`
is used, which default to `float32` unless you configured it
otherwise (via `keras.backend.set_floatx(float_dtype)`).
"""
if len(shape) != 2:
raise ValueError(
"Identity matrix initializer can only be used for 2D matrices. "
f"Received: shape={shape} of rank {len(shape)}."
)
dtype = standardize_dtype(dtype)
return self.gain * ops.eye(*shape, dtype=dtype)
@keras_export(
[
"keras.initializers.STFT",
"keras.initializers.stft",
"keras.initializers.STFTInitializer",
]
)
class STFT(Initializer):
"""Initializer of Conv kernels for Short-term Fourier Transformation (STFT).
Since the formula involves complex numbers, this class compute either the
real or the imaginary components of the final output.
Additionally, this initializer supports windowing functions across the time
dimension as commonly used in STFT. Windowing functions from the module
`scipy.signal.windows` are supported, including the common `hann` and
`hamming` windowing functions. This layer supports periodic windows and
scaling-based normalization.
This is primarily intended for use in the `STFTSpectrogram` layer.
Examples:
>>> # Standalone usage:
>>> initializer = STFTInitializer("real", "hann", "density", False)
>>> values = initializer(shape=(128, 1, 513))
Args:
side: String, `"real"` or `"imag"` deciding if the kernel will compute
the real side or the imaginary side of the output. Defaults to
`"real"`.
window: String for the name of the windowing function in the
`scipy.signal.windows` module, or array_like for the window values,
or `None` for no windowing.
scaling: String, `"density"` or `"spectrum"` for scaling of the window
for normalization, either L2 or L1 normalization.
`None` for no scaling.
periodic: Boolean, if True, the window function will be treated as
periodic. Defaults to `False`.
"""
def __init__(
self, side="real", window="hann", scaling="density", periodic=False
):
if side not in ["real", "imag"]:
raise ValueError(f"side should be 'real' or 'imag', not {side}")
if isinstance(window, str):
# throws an exception for invalid window function
scipy.signal.get_window(window, 1)
if scaling is not None and scaling not in ["density", "spectrum"]:
raise ValueError(
"Scaling is invalid, it must be `None`, 'density' "
f"or 'spectrum'. Received scaling={scaling}"
)
self.side = side
self.window = window
self.scaling = scaling
self.periodic = periodic
def __call__(self, shape, dtype=None):
"""Returns a tensor object initialized as specified by the initializer.
The shape is assumed to be `(T, 1, F // 2 + 1)`, where `T` is the size
of the given window, and `F` is the number of frequency bands. Only half
the frequency bands are used, which is a common practice in STFT,
because the second half are the conjugates of the first half in
a reversed order.
Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
are supported. If not specified, `keras.backend.floatx()`
is used, which default to `float32` unless you configured it
otherwise (via `keras.backend.set_floatx(float_dtype)`).
"""
dtype = standardize_dtype(dtype)
frame_length, input_channels, fft_length = shape
win = None
scaling = 1
if self.window is not None:
win = self.window
if isinstance(win, str):
# Using SciPy since it provides more windowing functions,
# easier to be compatible with multiple backends.
win = scipy.signal.get_window(win, frame_length, self.periodic)
win = ops.convert_to_tensor(win, dtype=dtype)
if len(win.shape) != 1 or win.shape[-1] != frame_length:
raise ValueError(
"The shape of `window` must be equal to [frame_length]."
f"Received: window shape={win.shape}"
)
win = ops.reshape(win, [frame_length, 1, 1])
if self.scaling == "density":
scaling = ops.sqrt(ops.sum(ops.square(win)))
elif self.scaling == "spectrum":
scaling = ops.sum(ops.abs(win))
_fft_length = (fft_length - 1) * 2
freq = (
ops.reshape(ops.arange(fft_length, dtype=dtype), (1, 1, fft_length))
/ _fft_length
)
time = ops.reshape(
ops.arange(frame_length, dtype=dtype), (frame_length, 1, 1)
)
args = -2 * time * freq * ops.arccos(ops.cast(-1, dtype))
if self.side == "real":
kernel = ops.cast(ops.cos(args), dtype)
else:
kernel = ops.cast(ops.sin(args), dtype)
if win is not None:
kernel = kernel * win / scaling
return kernel
def get_config(self):
return {
"side": self.side,
"window": self.window,
"periodic": self.periodic,
"scaling": self.scaling,
}
|