在TensorFlow中使用RBM处理MIDI文件时遭遇TypeError错误
Hey there! Let's work through that TypeError you're facing while moving your notebook to Python 3.6 with the python3-midi library. Python 2 and 3 have key differences around type handling (especially strings/bytes) and library APIs, which are almost certainly the root cause here. Here are targeted fixes and debugging steps based on common porting issues:
Common Causes & Solutions
1. Bytes vs. String Type Mismatch
Python 2 blurred lines between str and bytes, but Python 3 enforces strict separation. The python3-midi library likely returns byte data where your old Py2 code expected strings, or vice versa.
- If your code tries to perform string operations on byte data (like concatenation or string checks), use
.decode('utf-8')(or appropriate encoding) to convert bytes to strings:# Example: Convert MIDI event data from bytes to string event_str = event.data.decode('utf-8') - If you need to pass string data to the library, encode it to bytes first with
.encode('utf-8').
2. Numpy Array Type Incompatibility
When converting MIDI data to numpy arrays, mixing incompatible types (like bytes and integers) will throw a TypeError.
- Ensure all raw MIDI data is converted to integers before creating the numpy array:
# Convert raw byte data to integer list, then to numpy array raw_midi_bytes = your_parsed_midi_data midi_int_list = [int(byte) for byte in raw_midi_bytes] midi_array = np.array(midi_int_list, dtype=np.int32) # Use appropriate dtype - If you're combining numpy arrays with MIDI event data, convert the event data to a numpy array first:
# Instead of directly adding bytes to an array updated_array = midi_array + np.array(list(event.data), dtype=midi_array.dtype)
3. API Differences in python3-midi
The python3-midi library might have changed function signatures or return structures compared to the old Py2 MIDI library you used.
- Double-check how you're parsing MIDI files: if your old code used
midi.read_midifile(), confirm the returnedPatternandTrackobjects have the same attributes (likeevents,data) as in Py2. - Print the type and structure of your parsed MIDI objects to spot discrepancies:
pattern = midi.read_midifile("your_file.mid") print(type(pattern)) print(type(pattern[0].events[0])) # Inspect first event in first track print(pattern[0].events[0].data) # Check the data type here
4. Debug the Specific Error Line
Since the error traces back to <ipython-input-62-f35c20bfe55b>, focus on that code block:
- Add print statements to check the type of every variable involved in the line that throws the error. For example, if the line is
result = process_data(midi_data, numpy_arr), printtype(midi_data)andtype(numpy_arr)to spot mismatches. - Look for operations like addition, concatenation, or function calls that mix incompatible types (e.g., numpy arrays with bytes, integers with strings).
Final Tips
- Test small chunks of code separately: parse a single MIDI file, inspect its events, then convert to a numpy array step-by-step to isolate where the error occurs.
- Ensure all dependencies (like numpy) are updated to versions compatible with Python 3.6—old numpy versions might have quirks with Py3 type handling.
内容的提问来源于stack exchange,提问作者MrL

