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 15:01:47 浏览: 172
Golay_sequences.rar_Golay sequences_golay_golay codes
5星 · 资源好评率100%
这段代码主要是用来进行数据预处理和模型评估的。其中,`build_sequences`函数用来将数据转化为序列数据,`get_train_test`函数用来获取训练集和测试集,`relative_error`函数用来计算相对误差,`evaluation`函数用来计算模型的MAE、MSE和RMSE指标。另外,`setup_seed`函数用来设置随机种子,以保证实验的可复现性。整个代码涉及到了numpy、random、os和torch等库的使用,可以用来进行深度学习相关的实验和研究。
阅读全文