def predict_qs(model_name, terminal, voltages): workspace.ResetWorkspace() # requires voltages is an numpy array of size # (batch size, input_dimension) # the first dimension is Vg and the second dimenstion is Vd # preprocess the origin input and create adjoint input preproc_param = pickle.load( open(model_name + '_' + terminal + '_preproc_param.p', "rb")) dummy_qs = np.zeros(voltages[0].shape[0]) voltages, dummy_qs = preproc.ac_qv_preproc(voltages, dummy_qs, preproc_param['scale'], preproc_param['vg_shift']) adjoint_input = np.ones((voltages.shape[0], 1)) # Expand dimensions of input and set data type of inputs origin_input = np.expand_dims(voltages, axis=1) origin_input = origin_input.astype(np.float32) adjoint_input = adjoint_input.astype(np.float32) workspace.FeedBlob('DBInput_train/origin_input', voltages) workspace.FeedBlob('DBInput_train/adjoint_input', adjoint_input) pred_net = exporter.load_net(model_name + '_init', model_name + '_predict') workspace.RunNet(pred_net) qs = np.squeeze(workspace.FetchBlob('origin/NanCheck/origin_pred')) gradients = np.squeeze(workspace.FetchBlob('adjoint/fc0/output')) restore_integral_func, restore_gradient_func = preproc.get_restore_q_func( preproc_param['scale'], preproc_param['vg_shift']) original_qs = restore_integral_func(qs) original_gradients = restore_gradient_func(gradients) return original_qs, original_gradients
def predict_ids(model_name, vg, vd): workspace.ResetWorkspace() # preproc the input vg = vg.astype(np.float32) vd = vd.astype(np.float32) #if len(self.preproc_param) == 0: preproc_param = pickle.load(open(model_name + '_preproc_param.p', "rb")) dummy_ids = np.zeros(len(vg)) preproc_data_arrays = preproc.dc_iv_preproc( vg, vd, dummy_ids, preproc_param['scale'], preproc_param['vg_shift'], ) # print(preproc_data_arrays[0].shape) _preproc_data_arrays = [ np.expand_dims(x, axis=1) if len(x.shape) < 2 else x for x in preproc_data_arrays ] workspace.FeedBlob('DBInput_train/sig_input', _preproc_data_arrays[0]) workspace.FeedBlob('DBInput_train/tanh_input', _preproc_data_arrays[1]) pred_net = exporter.load_net(model_name + '_init', model_name + '_predict') workspace.RunNet(pred_net) _ids = np.squeeze(workspace.FetchBlob('prediction')) restore_id_func, get_restore_id_grad_func = preproc.get_restore_id_func( preproc_param['scale']) ids = restore_id_func(_ids) return ids
def predict_ids_grads(model_name, vg, vd): workspace.ResetWorkspace() # preproc the input vg = vg.astype(np.float32) vd = vd.astype(np.float32) #if len(self.preproc_param) == 0: preproc_param = pickle.load(open(model_name + '_preproc_param.p', "rb")) dummy_ids = np.zeros(len(vg)) preproc_data_arrays = preproc.dc_iv_preproc( vg, vd, dummy_ids, preproc_param['scale'], preproc_param['vg_shift'], ) _preproc_data_arrays = [ np.expand_dims(x, axis=1) for x in preproc_data_arrays ] workspace.FeedBlob('DBInput_train/sig_input', _preproc_data_arrays[0]) workspace.FeedBlob('DBInput_train/tanh_input', _preproc_data_arrays[1]) adjoint_input = np.ones((_preproc_data_arrays[0].shape[0], 1)) workspace.FeedBlob('adjoint_input', adjoint_input) pred_net = exporter.load_net(model_name + '_init', model_name + '_predict') workspace.RunNet(pred_net) _ids = np.squeeze(workspace.FetchBlob('origin/Mul/origin_pred')) _sig_grad = np.squeeze( workspace.FetchBlob('adjoint/sig_fc_layer_0/output')) _tanh_grad = np.squeeze( workspace.FetchBlob('adjoint/tanh_fc_layer_0/output')) restore_id_func, get_restore_id_grad_func = preproc.get_restore_id_func( preproc_param['scale']) ids = restore_id_func(_ids) sig_grad, tanh_grad = get_restore_id_grad_func(_sig_grad, _tanh_grad) return ids, sig_grad, tanh_grad