kissinference 1.4.x
kissinference.h
1/* * libkissinference - an inference libary for kiss networks
2 * Copyright (C) 2024 Carl Philipp Klemm <carl@uvos.xyz>
3 *
4 * This file is part of libkissinference.
5 *
6 * libkissinference is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * libkissinference is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with libkissinference. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21#include <stddef.h>
22#include <stdbool.h>
23
24#include "version.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30struct kiss_priv;
31
44 const char *name;
45 float *values;
46 size_t length;
47};
48
53{
54 struct kiss_priv *priv;
55
57 size_t input_count;
58 size_t output_size;
60 char *purpose;
62 bool ready;
63 bool softmax;
67 void (*result_cb)(float *result, struct kiss_network* net, void *user_data);
68};
69
76
83
98struct kiss_network *kiss_load_network(const char *path, void (*result_cb)(float* result, struct kiss_network* net, void* user_data), bool verbose);
99
107struct kiss_vector *kiss_create_vector(const char *name, float *values, size_t length);
108
114void kiss_free_vector(struct kiss_vector *vector);
115
130bool kiss_load_network_prealloc(struct kiss_network* net, const char *path, void (*result_cb)(float* result, struct kiss_network* net, void* user_data), bool verbose);
131
142struct kiss_network *kiss_load_network_from_buffer(const char* buffer, size_t size, void (*result_cb)(float* result, struct kiss_network* net, void* user_data), bool verbose);
143
157bool kiss_load_network_from_buffer_prealloc(struct kiss_network* net, const char* buffer, size_t size, void (*result_cb)(float* result, struct kiss_network* net, void* user_data), bool verbose);
158
169bool kiss_async_run_inference(struct kiss_network *net, const struct kiss_vector *inputs, size_t inputs_count, void* user_data);
170
171
181bool kiss_set_output_mask(struct kiss_network *net, const bool *output_mask);
182
190
200float *kiss_create_range(float start, float end, size_t length, bool log);
201
212void kiss_resample_spectra(float *in_re, float *in_im, size_t input_length, float **out_re, float **out_im, size_t output_length);
213
214
222void kiss_normalize_spectra(float *in_re, float *in_im, size_t input_length);
223
224
236bool kiss_reduce_spectra(float *in_re, float *in_im, float *omegas, size_t input_length,
237 float thresh_factor, bool use_second_deriv,
238 float **out_re, float **out_im, size_t *output_length);
239
251bool kiss_filter_spectra(float *in_re, float *in_im, float *omegas, size_t input_length,
252 float **out_re, float **out_im, size_t output_length);
253
263float *kiss_absgrad(float *in_re, float *in_im, float *omegas, size_t input_length, size_t index);
264
272float kiss_grad(float *data, float *omegas, size_t input_length, size_t index);
273
281float kiss_median(float *data, size_t input_length);
282
290float kiss_mean(float *data, size_t input_length);
291
301const char *kiss_get_strerror(struct kiss_network *net);
302
309void kiss_softmax(float *data, size_t input_length);
310
319void kiss_linear(float* data, float* weight, float* bias, size_t lengh);
320
328void kiss_softmax_masked(float *data, bool *mask, size_t input_length);
329
338bool kiss_float_eq(float a, float b, unsigned int ulp);
339
349char* kiss_eis_to_relaxis(const char* in);
350
360char* kiss_eis_to_cdc(const char* in);
361
367void kiss_free(void *data);
368
374#ifdef __cplusplus
375}
376#endif
377
378
float kiss_mean(float *data, size_t input_length)
Calculates the mean of the given data.
void kiss_normalize_spectra(float *in_re, float *in_im, size_t input_length)
Normalizes the given spectra to [0,1].
float * kiss_create_range(float start, float end, size_t length, bool log)
Allocates an array of floats that contins a linear or log range.
bool kiss_load_network_from_buffer_prealloc(struct kiss_network *net, const char *buffer, size_t size, void(*result_cb)(float *result, struct kiss_network *net, void *user_data), bool verbose)
Loads a network from a memory address.
void kiss_resample_spectra(float *in_re, float *in_im, size_t input_length, float **out_re, float **out_im, size_t output_length)
Resamples a spectra, useing linear interpolation.
size_t kiss_get_active_output_count(struct kiss_network *net)
Gets the number of outputs not masked by the input mask.
void kiss_softmax_masked(float *data, bool *mask, size_t input_length)
Applies softmax to the given data ignoreing the inputs where mask is false.
void kiss_softmax(float *data, size_t input_length)
Applies softmax to the given data.
const char * kiss_get_strerror(struct kiss_network *net)
Normalizes the given spectra to [0,1].
bool kiss_filter_spectra(float *in_re, float *in_im, float *omegas, size_t input_length, float **out_re, float **out_im, size_t output_length)
Filteres the spectra by applying kiss_normalize_spectra, kiss_reduce_spectra and kiss_resample_spectr...
struct kiss_network * kiss_load_network_from_buffer(const char *buffer, size_t size, void(*result_cb)(float *result, struct kiss_network *net, void *user_data), bool verbose)
Loads a network from a memory address.
void kiss_free_vector(struct kiss_vector *vector)
Frees the given vector.
bool kiss_load_network_prealloc(struct kiss_network *net, const char *path, void(*result_cb)(float *result, struct kiss_network *net, void *user_data), bool verbose)
Loads a network from disk.
float * kiss_absgrad(float *in_re, float *in_im, float *omegas, size_t input_length, size_t index)
Calculates the element wise absolute gradient at the given point of the data given.
char * kiss_eis_to_relaxis(const char *in)
Translates a string for this library to the format used by RHD instruments RelaxIS.
float kiss_grad(float *data, float *omegas, size_t input_length, size_t index)
Approximates the gradiant at the given index.
bool kiss_set_output_mask(struct kiss_network *net, const bool *output_mask)
Sets the output mask on a given network.
void kiss_free_network(struct kiss_network *net)
Frees the resources associated with a network.
void kiss_linear(float *data, float *weight, float *bias, size_t lengh)
Applies a linear nn layer to the given input.
float kiss_median(float *data, size_t input_length)
Calculates the median of the given data.
void kiss_free_network_prealloc(struct kiss_network *net)
Same as kiss_free_network except it dose not free the network struct itself.
bool kiss_async_run_inference(struct kiss_network *net, const struct kiss_vector *inputs, size_t inputs_count, void *user_data)
Runs inference on a network with the given inputs.
struct kiss_vector * kiss_create_vector(const char *name, float *values, size_t length)
Allocate a vector struct of length elements and copy the given data into the vector.
bool kiss_float_eq(float a, float b, unsigned int ulp)
Checks the given floats for equality with a tollerance of ulp epsilons around the sum of the inputs.
struct kiss_network * kiss_load_network(const char *path, void(*result_cb)(float *result, struct kiss_network *net, void *user_data), bool verbose)
Loads a network from disk.
char * kiss_eis_to_cdc(const char *in)
Translates a string for this library a Boukamp Circuit Description Code (CDC).
void kiss_free(void *data)
Frees the data with the same allocateor as libkissinference.
bool kiss_reduce_spectra(float *in_re, float *in_im, float *omegas, size_t input_length, float thresh_factor, bool use_second_deriv, float **out_re, float **out_im, size_t *output_length)
Reduces the data by removing "uninteresting" regions.
Struct describing a kiss neural network.
Definition kissinference.h:53
bool softmax
Network ends with a softmax.
Definition kissinference.h:63
float * output_scalars
Network expects outputs to be scaled by this factor to get physical values.
Definition kissinference.h:64
char ** output_labels
A description string for eatch element of the output tensor.
Definition kissinference.h:61
size_t input_count
The number of inputs the network expects.
Definition kissinference.h:57
bool * output_mask
An array of booleans that set an input enabled or disabled >
Definition kissinference.h:59
char * purpose
A string describing the purpose of the network.
Definition kissinference.h:60
struct kiss_vector * inputs
Network expects these to be given as the input.
Definition kissinference.h:56
float * output_biases
Network expects outputs to be biased by this factor to get physical values.
Definition kissinference.h:65
size_t output_size
Size of tensor this network returns.
Definition kissinference.h:58
bool ready
Specifies if the network is ready to use, must be checked after loading.
Definition kissinference.h:62
A struct containing a named vector.
Definition kissinference.h:43
float * values
The elements of the vector.
Definition kissinference.h:45
size_t length
The number of elements in this vector.
Definition kissinference.h:46
const char * name
Name or description of the data this vector holds.
Definition kissinference.h:44