x_note = [] x_offset = [] for press_time_dict in midi_list: last_offset = Fraction(0, 1) sorted_keys = sorted(press_time_dict.keys(), key=lambda t: float(Fraction(t))) for i, key in enumerate(sorted_keys): note_arr = np.zeros(shape=(len(total_keys), len(duration_keys)), dtype=np.float32) for note, duration in press_time_dict[key]: note_arr[total_keys.index(note), duration_keys.index(duration)] = 1. note_arr[np.max(note_arr, axis=-1) == 0., duration_keys.index('0')] = 1. cur_offset = Fraction(key) x_offset.append(str(cur_offset - last_offset)) last_offset = cur_offset x_note.append(note_arr) x_note = np.stack(x_note, axis=0) offset_keys = list(set(x_offset)) x_offset_idx = np.array([offset_keys.index(offset_type) for offset_type in x_offset]) x_offset = np.eye(len(offset_keys), dtype=np.int32)[x_offset_idx] x_offset = np.array(x_offset, dtype=np.float32) np.save("notes_array.npy", x_note) np.save("offsets_array.npy", x_offset) np.save("note_keys_dict.npy", total_keys) np.save("note_offsets_dict.npy", offset_keys) np.save("note_durations_dict.npy", duration_keys)
时间: 2024-03-30 19:34:20 浏览: 25
这段代码的作用是将`midi_list`列表中的所有键值对转换为神经网络的输入格式。具体来说,它将每个键值对转换为一个二维数组,其中每行表示一个音符,每列表示一个持续时间。对于每个键值对,它还将其对应的偏移量记录在`x_offset`列表中。
在代码的第一部分中,它遍历`midi_list`中的每个键值对,并按照键的顺序将其排序。然后,它遍历排序后的键,并将其转换为一个二维数组。对于每个音符,它使用`total_keys`和`duration_keys`中的索引来确定其在数组中的位置,并将该位置的值设置为1。如果一个音符在该键值对中没有出现,则该位置的值为0。对于每个键值对,它还将持续时间为0的音符设置为1,以使这些音符可以被正确地处理。
在代码的第二部分中,它使用`x_offset`列表中的偏移量将每个键值对与其之前的键值对分开。然后,它将`x_note`和`x_offset`转换为NumPy数组,并将它们保存在`.npy`文件中。最后,它将`total_keys`、`offset_keys`和`duration_keys`保存为`.npy`文件,以便在模型训练过程中使用。
相关问题
_press_time_dict = defaultdict(list)
`defaultdict` 是 Python 内置的一个类,它继承自 `dict` 类,但可以指定一个默认值类型,当访问一个不存在的键时,会自动创建一个默认值作为该键的值。这个类通常用于统计词频、分组计数等场景。
这段代码中,`defaultdict(list)` 创建了一个默认值类型为列表的 `defaultdict` 对象 `_press_time_dict`。也就是说,当访问一个不存在的键时,会自动创建一个空列表作为该键的值。这个 `_press_time_dict` 对象用于记录按键按下的时间(毫秒级),每个键对应的值是一个时间戳列表,记录了该键每次按下的时间。
例如,可以使用以下代码向 `_press_time_dict` 中添加数据:
```python
from collections import defaultdict
_press_time_dict = defaultdict(list)
# 添加数据
_press_time_dict['a'].append(1000)
_press_time_dict['b'].append(2000)
_press_time_dict['a'].append(3000)
print(_press_time_dict) # 输出 defaultdict(<class 'list'>, {'a': [1000, 3000], 'b': [2000]})
```
这个例子中,我们首先创建了一个默认值类型为列表的 `defaultdict` 对象 `_press_time_dict`。然后,我们向 `_press_time_dict` 中添加了三个键值对,分别是 `'a': [1000]`、`'b': [2000]` 和 `'a': [3000]`。由于 `'a'` 键已经存在,因此会将时间戳 `3000` 添加到该键对应的列表中。最终,我们得到了一个包含键 `'a'` 和 `'b'` 的 `_press_time_dict` 对象,每个键对应的值是按键按下的时间戳列表。
frequency_dict = {word: weight for word, weight in frequency_list}什么意思
这是一个字典推导式,它的作用是将一个包含元组的列表 `frequency_list` 转化为一个字典 `frequency_dict`。
具体来说,`frequency_list` 中的每个元组包含两个元素,第一个元素是单词(或其他某种类型的对象),第二个元素是该单词出现的频率(或其他权重值)。
字典推导式 `{word: weight for word, weight in frequency_list}` 的含义是:
- 对于 `frequency_list` 中的每个元组 `(word, weight)`,以 `word` 为键,以 `weight` 为值,生成一个新的键值对。
- 将所有生成的键值对组成一个新的字典 `frequency_dict`。
举个例子,如果 `frequency_list` 的值为 `[('apple', 3), ('banana', 2), ('orange', 1)]`,那么执行字典推导式后得到的 `frequency_dict` 就是 `{'apple': 3, 'banana': 2, 'orange': 1}`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)