LoginSignup
0
0

More than 1 year has passed since last update.

loss, val-loss が nan になり、交差エントロピー関数の log の引数に 1e-7 を足しても nan が直らなかった問題。

Last updated at Posted at 2023-01-15

 私は、hugging face の Whisper の fine tuning を使ってみている傍ら、

のページのプログラムを日本語の単語を予測するように改修して精度を上げられないか試しています。そんな中で、学習させると、loss と val-loss が nan になってしまいました。

 交差エントロピーとは、

def cross_entropy_error(y, t):
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

です。y = 0 だと交差エントロピーが nan になるので、delta = 1e-7 を足してやれば nan はでなくなると、ネットには書いてあり、最初、それに従って対応しました。しかし、nan が直らない。

 結局、原因は、y 自体が nan になっていることでした。上記ペーでは、

y = model.predict( x )

で y を予測していたのですが、x を作るのに

audio = tf.io.read_file(path)
audio, _ = tf.audio.decode_wav(audio, 1)
audio = tf.squeeze(audio, axis=-1)
stfts = tf.signal.stft(audio, frame_length=200, frame_step=80, fft_length=256)
x = tf.math.pow(tf.abs(stfts), 0.5)
# normalisation
means = tf.math.reduce_mean(x, 1, keepdims=True)
stddevs = tf.math.reduce_std(x, 1, keepdims=True)
x = (x - means) / stddevs                

としていて、stddevs が 0 になり、0割を起こして、x が nan になり、y が nan になっていたようです。上記部分を

audio = tf.io.read_file(path)
audio, _ = tf.audio.decode_wav(audio, 1)
audio = tf.squeeze(audio, axis=-1)
stfts = tf.signal.stft(audio, frame_length=400, frame_step=160, fft_length=512)
x = tf.math.pow(tf.abs(stfts), 0.5)
# normalisation
means = tf.math.reduce_mean(x, 1, keepdims=True)
stddevs = tf.math.reduce_std(x, 1, keepdims=True)
tmp = tf.math.equal( stddevs,  0 )                                #修正部分
stddevs += tf.cast( tmp, dtype=tf.float32)  * 1e-12               #修正部分
x = (x - means) / stddevs

のように直したら、loss も val-loss も正常値が表示されました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0