Package: timelens
TimeLensA tool to better understand your convolutional neural network for time series.Example UsageActivation MaximizationBasic setup for activation maximization:Import all the relevant tools from the libraryInstantiate the AMTrainer class with your PyTorch CNN and the device you want to perform the actions onCreate an AM input by using different methods (See:AM Inputs)Create an array of penalties (Order matters!) (See:AM Penalties)The one used here is the recommended onePerform AM for a whole layer or only for a single unit by calling the activation maximization methods on the class objectfromtimelens.am.am_trainerimportAMTrainerfromtimelens.am.am_inputsimportsin_ts,square_tsam_trainer=AMTrainer(cnn,device)# c: channels | t: time series length | vmax: maximum amplitude | freq: frequency of sineinput_ts=sin_ts(c=6,t=125,vmax=0.3,freq=0.5)penalties=[{'penalty':'eap','weight':0.1,'th':0.6827,},{'penalty':'l2','weight':0.1,},{'penalty':'tv','weight':0.1,},]am_result=am_trainer.activation_maximization_layer(input_ts,'conv_layers.conv0',penalties=penalties,th_clip=0.6827,iterations=5000)am_result=am_trainer.activation_maximization(input_ts,'conv_layers.conv0',0,penalties=penalties,th_clip=0.6827,iterations=5000)EvaluationThe following code assumes that you already ran the code in the previous section.Set the comparison data for dynamic time warping (train_data is torch.utils.data.TensorDataset here)Call the evaluation method with different evaluation calculations (See:AMResult)This function will consume a lot of RAM/VRAM (depending on device), so keep this in mindThe amount of RAM/VRAM depends on the size of your comparison data# the am_result variable from the previous example stores an object of the class AMResultam_result.set_original_data(train_data.tensors[0])mean_dtw_loss=am_result.evaluate('dtw')VisualizationsThis example shows how timelens can be used to get basic visualizationsImport CNNVizInstantiate a CNNViz object using the PyTorch CNN and the deviceRetrieve kernel or feature map visualizations for the whole layer or for a specific unitfromtimelens.cnn.cnn_vizimportCNNVizcnn_viz=CNNViz(cnn,device)layer_name='conv_layers.conv0'idx_kernel=0input_ts=...# provide a training sample from your dataset here -> it will be fed through your networkkernels=cnn_viz.plot_layer_kernels(layer_name)kernel=cnn_viz.plot_specific_kernel(layer_name,idx_kernel)feature_maps=cnn_viz.plot_layer_feature_maps(layer_name,input_ts)feature_map=cnn_viz.plot_specific_feature_maps(layer_name,idx_kernel,input_ts)Raw NetworkThis example shows how timelens can be used to get basic raw data about your networkImport CNNRawInstantiate a CNNRaw object using the PyTorch CNN and the deviceRetrieve kernel data or feature maps for the whole layer or for a specific unitfromtimelens.cnn.cnn_rawimportCNNRawcnn_raw=CNNRaw(cnn,device)layer_name='conv_layers.conv0'idx_kernel=0input_ts=...# provide a training sample from your dataset here -> it will be fed through your networkkernels=cnn_raw.get_layer_kernels(layer_name)kernel=cnn_raw.get_specific_kernel(layer_name,idx_kernel)feature_maps=cnn_raw.get_layer_feature_maps(layer_name,input_ts)feature_map=cnn_raw.get_specific_feature_maps(layer_name,idx_kernel,input_ts)