def build_sequences(text, window_size): #text:list of capacity x, y = [],[] for i in range(len(text) - window_size): sequence = text[i:i+window_size] target = text[i+1:i+1+window_size] x.append(sequence) y.append(target) return np.array(x), np.array(y) # 留一评估:一组数据为测试集,其他所有数据全部拿来训练 def get_train_test(data_dict, name, window_size=8): data_sequence=data_dict[name][1] train_data, test_data = data_sequence[:window_size+1], data_sequence[window_size+1:] train_x, train_y = build_sequences(text=train_data, window_size=window_size) for k, v in data_dict.items(): if k != name: data_x, data_y = build_sequences(text=v[1], window_size=window_size) train_x, train_y = np.r_[train_x, data_x], np.r_[train_y, data_y] return train_x, train_y, list(train_data), list(test_data) def relative_error(y_test, y_predict, threshold): true_re, pred_re = len(y_test), 0 for i in range(len(y_test)-1): if y_test[i] <= threshold >= y_test[i+1]: true_re = i - 1 break for i in range(len(y_predict)-1): if y_predict[i] <= threshold: pred_re = i - 1 break return abs(true_re - pred_re)/true_re def evaluation(y_test, y_predict): mae = mean_absolute_error(y_test, y_predict) mse = mean_squared_error(y_test, y_predict) rmse = sqrt(mean_squared_error(y_test, y_predict)) return mae, rmse def setup_seed(seed): np.random.seed(seed) # Numpy module. random.seed(seed) # Python random module. os.environ['PYTHONHASHSEED'] = str(seed) # 为了禁止hash随机化,使得实验可复现。 torch.manual_seed(seed) # 为CPU设置随机种子 if torch.cuda.is_available(): torch.cuda.manual_seed(seed) # 为当前GPU设置随机种子 torch.cuda.manual_seed_all(seed) # if you are using multi-GPU,为所有GPU设置随机种子 torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True
时间: 2024-02-22 08:01:47 浏览: 189
这段代码主要是用来进行数据预处理和模型评估的。其中,`build_sequences`函数用来将数据转化为序列数据,`get_train_test`函数用来获取训练集和测试集,`relative_error`函数用来计算相对误差,`evaluation`函数用来计算模型的MAE、MSE和RMSE指标。另外,`setup_seed`函数用来设置随机种子,以保证实验的可复现性。整个代码涉及到了numpy、random、os和torch等库的使用,可以用来进行深度学习相关的实验和研究。
相关问题
def create_inout_sequences(input_data, input_window ,output_window): inout_seq = [] L = len(input_data) block_num = L - block_len + 1 # total of [N - block_len + 1] blocks # where block_len = input_window + output_window for i in range( block_num ): train_seq = input_data[i : i + input_window] train_label = input_data[i + output_window : i + input_window + output_window] inout_seq.append((train_seq ,train_label)) return torch.FloatTensor(np.array(inout_seq))这段代码什么意思举例说明
这段代码是用来创建输入和输出序列的函数。给定输入数据、输入窗口大小和输出窗口大小,它将返回一个由输入和输出序列组成的列表。
具体来说,函数首先计算出由多少个输入和输出序列组成的块,每个块的大小为输入窗口大小加上输出窗口大小减一。然后,对于每个块,函数将从输入数据中提取输入序列和对应的输出序列,并将它们添加到一个列表中,最终将该列表转换为PyTorch张量并返回。
举个例子,假设我们有以下输入数据:
```
input_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```
如果我们调用函数并指定输入窗口大小为3,输出窗口大小为2:
```
create_inout_sequences(input_data, 3, 2)
```
那么函数将提取以下输入和输出序列:
```
[(tensor([1., 2., 3.]), tensor([4., 5.])),
(tensor([2., 3., 4.]), tensor([5., 6.])),
(tensor([3., 4., 5.]), tensor([6., 7.])),
(tensor([4., 5., 6.]), tensor([7., 8.])),
(tensor([5., 6., 7.]), tensor([8., 9.])),
(tensor([6., 7., 8.]), tensor([9., 10.]))]
```
其中每个元素都是一个元组,第一个元素是输入序列的张量,第二个元素是对应的输出序列的张量。例如,第一个元组`(tensor([1., 2., 3.]), tensor([4., 5.]))`表示在输入数据中提取了长度为3的输入序列`[1, 2, 3]`,并且对应的输出序列为`[4, 5]`。
def build_lstm_generator(seq_len,hidden_size,vocab_size,compiler=True): x_inp = Input((seq_len,vocab_size)) x = Dense(hidden_size)(x_inp) #x = InstanceNormalization()(x) for _ in range(8): x = Dense(hidden_size,activation="gelu")(x) #x = Dropout(0.1)(x) x = Bidirectional(GRU(hidden_size // 2,return_sequences=True))(x) x = Bidirectional(GRU(hidden_size // 2,return_sequences=True))(x) #x = InstanceNormalization()(x) x = GRU(hidden_size)(x) o = Dense(vocab_size,activation="linear")(x) model = Model(inputs=x_inp,outputs=o,name="generator") if compiler: adam = LAMB(learning_rate = 1*1e-4) #model.compile(optimizer=adam,loss=loss_function) model.summary() return model
这段代码中的build_lstm_generator()函数是用来构建一个基于LSTM的生成器模型,其输入由一个形状为(seq_len,vocab_size)的张量组成,输出为一个形状为(vocab_size,)的张量,表示生成的音符或节奏序列。
在函数中,首先定义了一个大小为(seq_len,vocab_size)的输入层x_inp,然后通过一层Dense层将输入的特征向量转换为一个形状为(seq_len,hidden_size)的张量,其中hidden_size是LSTM模型的隐藏层大小。接下来,通过八个堆叠的双向LSTM层来提取输入序列中的特征信息,并在每个LSTM层之后添加一个Dense层和激活函数GELU,以增强模型的表达能力。最后,利用一层GRU层将这些特征进行整合,并通过一层Dense层输出生成的音符或节奏序列。
最后,该函数返回了构建好的生成器模型。
阅读全文
相关推荐
















