Traceback (most recent call last): File…
- User
Traceback (most recent call last): File “C:\Users\umbrella\PycharmProjects\pythonProject1\bilstm_for_validation.py”, line 209, in for key in random.sample(train_data.keys(), int(parameters[‘Sample_num’])): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:\Users\umbrella\anaconda3\envs\py3115\Lib\random.py”, line 439, in sample raise TypeError("Population must be a sequence. “ TypeError: Population must be a sequence. For dicts or sets, use sorted(d).以上是报错内容,以下是代码,如何修改?total_loss = 0. for epoch in range(int(parameters[‘EPOCH’])): total_loss = train_loop(train_dataloader, model=calibration_model, optimizer=optimizer, epoch=epoch, total_loss=total_loss, loss_fn=loss_fn) print(“epoch:”, epoch, " loss:”, total_loss/((epoch + 1) * len(train_dataloader)), flush=True)
Start predicting
mse_list = []
in_threshold = out_threshold = 0
TP = FP = FN = TN = count = 0
ave_mse = {}
calibration_model.eval() sum_mse = 0 for key in random.sample(train_data.keys(), int(parameters[‘Sample_num’])): current_train_data = train_data[key] test_x = [] test_y = [] for i in range(int(parameters[‘Step_num’]) - window_size): current_x = [] for j in range(i, i + window_size): current_x.extend(current_train_data[‘data’][j]) current_y = current_train_data[‘data’][i + window_size] test_x.append(current_x) test_y.append(current_y)
input_x = torch.tensor(test_x, dtype=torch.float32).reshape(int(parameters[‘Step_num’]) - window_size, window_size, lstm_config.input_size) predict = calibration_model(input_x) predict_y = predict.tolist() for i in range(len(test_y)): for j in range(len(test_y[i])): sum_mse += (test_y[i][j] - predict_y[i][j]) ** 2
- Assistant
It seems that the error is occurring in the line where random.sample is being used on the train_data.keys(). This error occurs because the random.sample() function requires a sequence as its first argument, but train_data.keys() returns a dict_keys object which is not a sequence.
To fix this error, you can convert the dict_keys object to a list before passing it to random.sample(). Here's how you can modify the code:
for key in random.sample(list(train_data.keys()), int(parameters['Sample_num'])): # Rest of the code...By converting train_data.keys() to a list using the list() function, you can pass it as a sequence to the random.sample() function without any issues.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.