前提・実現したいこと
Kerasでニューラルネットワークを実装中に以下のエラーが発生してしまいました。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\Projects\Repair Audio\main.py", line 45, in <module> model.fit(x=x_train, y=y_train, epochs=20) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py", line 1184, in fit tmp_logs = self.train_function(iterator) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__ result = self._call(*args, **kwds) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call self._initialize(args, kwds, add_initializers_to=initializers) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 759, in _initialize self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3066, in _get_concrete_function_internal_garbage_collected graph_function, _ = self._maybe_define_function(args, kwargs) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3463, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3298, in _create_graph_function func_graph_module.func_graph_from_py_func( File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1007, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 668, in wrapped_fn out = weak_wrapped_fn().__wrapped__(*args, **kwds) File "C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 994, in wrapper raise e.ag_error_metadata.to_exception(e) ValueError: in user code: C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:853 train_function * return step_function(self, iterator) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:842 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica return fn(*args, **kwargs) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:835 run_step ** outputs = model.train_step(data) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:787 train_step y_pred = self(x, training=True) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\base_layer.py:1020 __call__ input_spec.assert_input_compatibility(self.input_spec, inputs, self.name) C:\Users\sukep\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\input_spec.py:250 assert_input_compatibility raise ValueError( ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 256 but received input with shape (32, 1)
該当のソースコード
Python
1from keras.models import Sequential 2from keras.layers import Dense, Activation 3from pydub import AudioSegment 4import numpy as np 5import matplotlib.pyplot as plt 6import random 7 8 9BUFFER_SIZE = 256 10 11 12class Audio: 13 def __init__(self, filename): 14 self.sound = AudioSegment.from_file(filename) 15 self.samples = np.array(self.sound.get_array_of_samples()) 16 17 if self.sound.channels == 1: 18 self.left = self.samples 19 self.right = self.samples 20 else: 21 self.left = self.samples[::2] 22 self.right = self.samples[1::2] 23 24 25if __name__ == "__main__": 26 wav = Audio("Alan Walker - Faded.wav") 27 mp3 = Audio("Alan Walker - Faded.mp3") 28 29 offset = random.randrange(wav.left.size - BUFFER_SIZE) 30 31 if random.randint(0, 1) == 0: 32 x_train = np.fft.fft(mp3.left[offset:offset + BUFFER_SIZE] / 32768) 33 y_train = np.fft.fft(wav.left[offset:offset + BUFFER_SIZE] / 32768) 34 else: 35 x_train = np.fft.fft(mp3.right[offset:offset + BUFFER_SIZE] / 32768) 36 y_train = np.fft.fft(wav.right[offset:offset + BUFFER_SIZE] / 32768) 37 38 model = Sequential() 39 model.add(Dense(BUFFER_SIZE, input_dim=BUFFER_SIZE)) 40 model.add(Activation("linear")) 41 model.add(Dense(BUFFER_SIZE)) 42 model.add(Activation("linear")) 43 44 model.compile(optimizer="adam", loss="mse", metrics=["accuracy"]) 45 model.fit(x=x_train, y=y_train, epochs=20) 46
補足情報(FW/ツールのバージョンなど)
IDLE Shell 3.9.7
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。