using np for speed perturb, remove some debug log of grad clip

pull/578/head
Hui Zhang 5 years ago
parent b355b67f48
commit e61a6134b3

File diff suppressed because one or more lines are too long

@ -22,7 +22,6 @@ import resampy
from scipy import signal from scipy import signal
import random import random
import copy import copy
import sox
class AudioSegment(object): class AudioSegment(object):
@ -322,18 +321,24 @@ class AudioSegment(object):
:type speed_rate: float :type speed_rate: float
:raises ValueError: If speed_rate <= 0.0. :raises ValueError: If speed_rate <= 0.0.
""" """
if speed_rate == 1.0:
return
if speed_rate <= 0: if speed_rate <= 0:
raise ValueError("speed_rate should be greater than zero.") raise ValueError("speed_rate should be greater than zero.")
# old_length = self._samples.shape[0]
# new_length = int(old_length / speed_rate) # numpy
# old_indices = np.arange(old_length) old_length = self._samples.shape[0]
# new_indices = np.linspace(start=0, stop=old_length, num=new_length) new_length = int(old_length / speed_rate)
# self._samples = np.interp(new_indices, old_indices, self._samples) old_indices = np.arange(old_length)
tfm = sox.Transformer() new_indices = np.linspace(start=0, stop=old_length, num=new_length)
tfm.set_globals(multithread=False) self._samples = np.interp(new_indices, old_indices, self._samples)
tfm.speed(speed_rate)
self._samples = tfm.build_array( # sox, slow
input_array=self._samples, sample_rate_in=self._sample_rate).copy() # tfm = sox.Transformer()
# tfm.set_globals(multithread=False)
# tfm.speed(speed_rate)
# self._samples = tfm.build_array(
# input_array=self._samples, sample_rate_in=self._sample_rate).copy()
def normalize(self, target_db=-20, max_gain_db=300.0): def normalize(self, target_db=-20, max_gain_db=300.0):
"""Normalize audio to be of the desired RMS value in decibels. """Normalize audio to be of the desired RMS value in decibels.

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import time
import io import io
import random import random
import tarfile import tarfile

@ -43,11 +43,11 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
merge_grad = layers.get_tensor_from_selected_rows(merge_grad) merge_grad = layers.get_tensor_from_selected_rows(merge_grad)
square = layers.square(merge_grad) square = layers.square(merge_grad)
sum_square = layers.reduce_sum(square) sum_square = layers.reduce_sum(square)
logger.debug(
f"Grad Before Clip: {p.name}: {float(layers.sqrt(layers.reduce_sum(layers.square(merge_grad))) ) }"
)
sum_square_list.append(sum_square) sum_square_list.append(sum_square)
# debug log
# logger.debug(f"Grad Before Clip: {p.name}: {float(sum_square.sqrt()) }")
# all parameters have been filterd out # all parameters have been filterd out
if len(sum_square_list) == 0: if len(sum_square_list) == 0:
return params_grads return params_grads
@ -55,6 +55,7 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
global_norm_var = layers.concat(sum_square_list) global_norm_var = layers.concat(sum_square_list)
global_norm_var = layers.reduce_sum(global_norm_var) global_norm_var = layers.reduce_sum(global_norm_var)
global_norm_var = layers.sqrt(global_norm_var) global_norm_var = layers.sqrt(global_norm_var)
# debug log
logger.debug(f"Grad Global Norm: {float(global_norm_var)}!!!!") logger.debug(f"Grad Global Norm: {float(global_norm_var)}!!!!")
max_global_norm = layers.fill_constant( max_global_norm = layers.fill_constant(
shape=[1], dtype=global_norm_var.dtype, value=self.clip_norm) shape=[1], dtype=global_norm_var.dtype, value=self.clip_norm)
@ -68,9 +69,11 @@ class ClipGradByGlobalNormWithLog(paddle.nn.ClipGradByGlobalNorm):
params_and_grads.append((p, g)) params_and_grads.append((p, g))
continue continue
new_grad = layers.elementwise_mul(x=g, y=clip_var) new_grad = layers.elementwise_mul(x=g, y=clip_var)
logger.debug(
f"Grad After Clip: {p.name}: {float(layers.sqrt(layers.reduce_sum(layers.square(merge_grad))) ) }"
)
params_and_grads.append((p, new_grad)) params_and_grads.append((p, new_grad))
# debug log
# logger.debug(
# f"Grad After Clip: {p.name}: {float(merge_grad.square().sum().sqrt())}"
# )
return params_and_grads return params_and_grads

Loading…
Cancel
Save