def calculate_loss_tensor(filename, N_Windows, W, signals_models, signals_info, test_included=False): loss_tensor = np.zeros((len(signals_models), len(signals_info), N_Windows)) N_Signals = len(signals_info) X_matrix = np.zeros((N_Signals, N_Windows, W)) Y_matrix = np.zeros((N_Signals, N_Windows, W)) if not test_included: signals = get_signals_tests(signals_info, signals_models[0].Sd) for i in range(N_Signals): [X_matrix[i, :, :], Y_matrix[i, :, :]] = get_random_batch(signals[0][i], signals[1][i], W, N_Windows) else: i = 0 for model_info in signals_models: [x_test, y_test] = load_test_data( "GRU_" + model_info.dataset_name + "[" + str(model_info.Sd) + "." + str(model_info.Hd) + ".-1.-1.-1]", model_info.directory) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch( x_test, y_test, N_Windows) i += 1 print("Loading model...") model_info = signals_models[0] model = GRU.LibPhys_GRU(model_info.Sd, hidden_dim=model_info.Hd, signal_name=model_info.dataset_name, n_windows=N_Windows) history = [] m = -1 for m in range(len(signals_models)): model_info = signals_models[m] model.signal_name = model_info.dataset_name model.load(signal_name=model_info.name, filetag=model.get_file_tag(model_info.DS, model_info.t), dir_name=model_info.directory) print("Processing " + model_info.name) for s in range(N_Signals): x_test = X_matrix[s, :, :] y_test = Y_matrix[s, :, :] signal_info = signals_info[s] print("Calculating loss for " + signal_info.name, end=';\n ') loss_tensor[m, s, :] = np.asarray( model.calculate_loss_vector(x_test, y_test)) np.savez(filename + ".npz", loss_tensor=loss_tensor, signals_models=signals_models, signals_tests=signals_info) return loss_tensor
def calculate_loss_tensors(N_Windows, W, signals_models): N_Versions = len(signals_models) N_Signals = len(signals_models[0]) loss_tensor = np.zeros((N_Versions, N_Signals, N_Signals, N_Windows)) X_matrix = np.zeros((N_Versions, N_Signals, N_Windows, W)) Y_matrix = np.zeros((N_Versions, N_Signals, N_Windows, W)) i = 0 for model_info in signals_models[0]: x_tests = [] y_tests = [] for version in range(N_Versions): [x_test, y_test] = load_test_data( "GRU_" + model_info.dataset_name + "[" + str(model_info.Sd) + "." + str(model_info.Hd) + ".-1.-1.-1]", model_info.directory) x_tests.append(x_test) y_tests.append(y_test) X_matrix[:, i, :, :], Y_matrix[:, i, :, :] = randomize_batch( np.asarray(x_test), np.asarray(y_test), N_Windows) i += 1 print("Loading base model...") model_info = signals_models[0][0] model = GRU.LibPhys_GRU(model_info.Sd, hidden_dim=model_info.Hd, signal_name=model_info.dataset_name, n_windows=N_Windows) for m in range(N_Signals): for version in range(N_Versions): model_info = signals_models[version][m] model.signal_name = model_info.dataset_name model.load(signal_name=model_info.name, filetag=model.get_file_tag(model_info.DS, model_info.t), dir_name=model_info.directory) print("Processing " + model_info.name) for s in range(N_Signals): x_test = X_matrix[version, s, :, :] y_test = Y_matrix[version, s, :, :] print("Calculating loss for " + signals_models[version][s].name, end=';\n ') loss_tensor[version, m, s, :] = np.asarray( model.calculate_loss_vector(x_test, y_test)) np.savez(filename + ".npz", loss_tensor=loss_tensor, signals_models=signals_models, signals_tests=signals_info) return loss_tensor
def calculate_loss_tensor(filename, Total_Windows, W, signals_models): n_windows = Total_Windows if Total_Windows / 256 > 1: ratio = round(Total_Windows / 256) n_windows = int(Total_Windows / ratio) windows = np.arange(int(Total_Windows / n_windows)) N_Windows = len(windows) N_Signals = len(signals_models) Total_Windows = int(N_Windows * n_windows) loss_tensor = np.zeros((N_Signals, N_Signals, Total_Windows)) N_Signals = len(signals_models) X_matrix = np.zeros((N_Signals, Total_Windows, W)) Y_matrix = np.zeros((N_Signals, Total_Windows, W)) i = 0 for model_info in signals_models: [x_test, y_test] = load_test_data( "GRU_" + model_info.dataset_name + "[" + str(model_info.Sd) + "." + str(model_info.Hd) + ".-1.-1.-1]" , model_info.directory) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch(x_test, y_test, Total_Windows) i += 1 print("Loading model...") model_info = signals_models[0] model = GRU.LibPhys_GRU(model_info.Sd, hidden_dim=model_info.Hd, signal_name=model_info.dataset_name, n_windows=n_windows) for m in range(len(signals_models)): model_info = signals_models[m] model.signal_name = model_info.dataset_name model.load(signal_name=model_info.name, filetag=model.get_file_tag(model_info.DS, model_info.t), dir_name=model_info.directory) print("Processing " + model_info.name) for s in range(N_Signals): print("Calculating loss for " + signals_models[s].name, end=';\n ') for w in windows: index = w * n_windows x_test = X_matrix[s, index:index + n_windows, :] y_test = Y_matrix[s, index:index + n_windows, :] loss_tensor[m, s, index:index + n_windows] = np.asarray(model.calculate_loss_vector(x_test, y_test)) np.savez(filename + ".npz", loss_tensor=loss_tensor, signals_models=signals_models) return loss_tensor
p = -1 signal_labels = [] max = [] for monitoring_info in new_web_monitoring_info: try: print("length ", len(monitoring_info[1][0])," vs ", N_Windows * W * 3) if len(monitoring_info[1][0]) >= (N_Windows * W * 0.33): p += 1 max.append(monitoring_info[3]) signal_labels.append(monitoring_info[3]) # append maximizer score information X_tensor, Y_tensor = segment_matrix(np.asarray([process_signal(monitoring_info[1][0], model_info.Sd, 10, False, None, False), process_signal(monitoring_info[2][0], model_info.Sd, 10, False, None, False)]), W, overlap=0.33) X_quaternion, Y_quaternion = randomize_batch(X_tensor, Y_tensor, N_Windows) Signal_windows[p, :, :, :, :] = np.asarray([X_quaternion, Y_quaternion]) print("Calculated for p = "+str(p)) else: print("Not calculated for p = "+str(p)) except: print("Exception!") pass Signal_windows = Signal_windows[~(Signal_windows[:,0,0,0,0] == 0),:,:,:,:] N_people, inputs, N_dimensions, N_Windows, W = np.shape(Signal_windows) print(N_dimensions) loss_quartenion = np.ones((N_models, N_people, N_Windows, N_dimensions))*100000
def calculate_loss_tensor(filename, Total_Windows=None, W=256, signals_models=[], test_signals=None, signals_info=None): if signals_info is None: if Total_Windows is None: Total_Windows = 1000000000 for model_info in signals_models: [x_test, y_test] = load_test_data(model_info.dataset_name, model_info.directory) if np.shape(x_test)[0] < Total_Windows: Total_Windows = np.shape(x_test)[0] print("Number of Windows: {0}".format(Total_Windows)) n_windows = Total_Windows if Total_Windows / 256 > 1: ratio = round(Total_Windows / 256) n_windows = 16 #int(Total_Windows/ratio) n_windows = 16 windows = np.arange(int(Total_Windows / n_windows)) N_Windows = len(windows) Total_Windows = int(N_Windows * n_windows) N_Signals = len(signals_models) N_Models = N_Signals else: N = [signal_info.size for signal_info in signals_info] N = min(N) step = int(W * 0.33) if Total_Windows is None: windows = np.arange(0, N - step - 1, step) Total_Windows = len(windows) - 2 n_windows = Total_Windows if Total_Windows / 256 > 1: ratio = round(Total_Windows / 256) n_windows = int(Total_Windows / ratio) windows = np.arange(int(Total_Windows / n_windows)) N_Windows = len(windows) Total_Windows = int(N_Windows * n_windows) N_Signals = len(signals) N_Models = len(signals_models) n_windows = Total_Windows loss_tensor = np.zeros((N_Models, N_Signals, Total_Windows)) X_matrix = np.zeros((N_Signals, Total_Windows, W)) Y_matrix = np.zeros((N_Signals, Total_Windows, W)) i = 0 if test_signals is None: for model_info in signals_models: [x_test, y_test] = load_test_data(model_info.dataset_name, model_info.directory) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch( x_test, y_test, Total_Windows) i += 1 else: for signal, signal_info in zip(test_signals, signals_info): signal_windows = segment_matrix(signal, W, 0.33) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch( signal_windows[0], signal_windows[1], Total_Windows) i += 1 print("Loading model...") model_info = signals_models[0] model = GRU.LibPhys_GRU(model_info.Sd, hidden_dim=model_info.Hd, signal_name=model_info.dataset_name, n_windows=n_windows) for m in range(len(signals_models)): model_info = signals_models[m] model.signal_name = model_info.dataset_name model.load(signal_name=model_info.name, filetag=model.get_file_tag(model_info.DS, model_info.t), dir_name=model_info.directory) print("Processing " + model_info.name) for s in range(N_Signals): if signals_info is not None: print("Calculating loss for " + signals_info[s].name, end=';\n ') else: print("Calculating loss for " + signals_models[s].name, end=';\n ') for w in windows: index = w * n_windows x_test = X_matrix[s, index:index + n_windows, :] y_test = Y_matrix[s, index:index + n_windows, :] loss_tensor[m, s, index:index + n_windows] = np.asarray( model.calculate_loss_vector(x_test, y_test)) print(np.mean(loss_tensor[m, s, index:index + n_windows])) np.savez(filename + ".npz", loss_tensor=loss_tensor, signals_models=signals_models) return loss_tensor
def calculate_loss_tensor(filename, Total_Windows, W, signals_models, signals=None, noisy_index=None): n_windows = Total_Windows if Total_Windows / 256 > 1: ratio = round(Total_Windows / 256) else: ratio = 1 n_windows = 250 windows = np.arange(int(Total_Windows / n_windows)) N_Windows = len(windows) N_Signals = len(signals_models) Total_Windows = int(N_Windows * n_windows) loss_tensor = np.zeros((N_Signals, N_Signals, Total_Windows)) N_Signals = len(signals_models) X_matrix = np.zeros((N_Signals, Total_Windows, W)) Y_matrix = np.zeros((N_Signals, Total_Windows, W)) i = 0 indexes = signals_models #[np.random.permutation(len(signals_models))] for model_info in indexes: if signals is None: # [x_test, y_test] = load_test_data("GRU_" + model_info.dataset_name, + "["+str(model_info.Sd)+"."+str(model_info.Hd)+".-1.-1.-1]" # , model_info.directory) [x_test, y_test] = load_test_data(model_info.dataset_name, model_info.directory) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch( x_test, y_test, Total_Windows) else: signals = get_signals_tests(db.ecg_noisy_signals[noisy_index - 1], index=i, noisy_index=noisy_index, peak_into_data=False) signal_test = segment_signal(signals[0][i], 256, 0.33) X_matrix[i, :, :], Y_matrix[i, :, :] = randomize_batch( signal_test[0], signal_test[1], Total_Windows) i += 1 print("Loading model...") model_info = signals_models[0] model = GRU.LibPhys_GRU(model_info.Sd, hidden_dim=model_info.Hd, signal_name=model_info.dataset_name, n_windows=n_windows) for m in range(len(signals_models)): model_info = signals_models[m] model.signal_name = model_info.dataset_name model.load(signal_name=model_info.name, filetag=model.get_file_tag(model_info.DS, model_info.t), dir_name=model_info.directory) print("Processing " + model_info.name) for s in range(N_Signals): print("Calculating loss for " + signals_models[s].name, end=';\n ') for w in windows: index = w * n_windows x_test = X_matrix[s, index:index + n_windows, :] y_test = Y_matrix[s, index:index + n_windows, :] loss_tensor[m, s, index:index + n_windows] = np.asarray( model.calculate_loss_vector(x_test, y_test)) np.savez(filename + ".npz", loss_tensor=loss_tensor, signals_models_=indexes, signals_models=signals_models) return loss_tensor, indexes