nepes NM500 User Guide

Page 1
copyright nepes corp. all rights reserved
NM500 Software Development Guide
Python Wrapper API
Version 2.0
Page 2
copyright nepes corp. all rights reserved
Contents
1. Introduction
2. Set up Development Environment.
2.1 Overview
2.2 Set up development environment for Linux Series
2.3 Set up development environment for macOS
2.3 Set up development environment for Windows
2.4 Using NM500 library APIs
3. NM500 APIs
3.1 set_lib_path
3.2 get_devices
3.3 connect
3.4 close
3.5 get_network_info
3.6 get_version
3.7 get_neuron_count
3.8 forget
3.9 reset
3.10 set_network_type
3.11 get_network_type
3.12 get_network_status
3.13 set_context
3.14 get_context
3.15 learn
3.16 learn_batch
3.17 clusterize
3.18 classify
3.19 read
3.20 write
3.21 read_neuron
3.22 read_neurons
Page 3
copyright nepes corp. all rights reserved
3.23 write_neurons
3.24 get_model_info
3.25 get_model_stat
3.26 save_model
3.27 load_model
3.28 power_save
4. Result Codes
5. NeuroMem Registers
Page 4
copyright nepes corp. all rights reserved
History
Date
Version
Description
2019-04-09
2.0
Page 5
copyright nepes corp. all rights reserved
1. Introduction
This document is intended to use with nepes NM500 Software Development Kit. The nepes NM500
Software Development Kit allows you to manage nepes NM500 neuromorphic chip and implement
custom solutions by using libraries, utilities, and programming interfaces provided with the nepes
NM500 Software Development Kit.
The nepes NM500 performs pattern recognition upon a knowledge model contructed with
NeuroMem networks.
This document describes in detail how you can develop applications using the nepes NM500
Software Development Kit.
Page 6
copyright nepes corp. all rights reserved
2. Set up Development Environment.
2.1 Overview
This nepes NM500 Software Development Kit contains NeuroMemEngine and USB(or SPI for
Raspberry PI) libraries for NM500 application boards. A developer can use this SDK to create
application that access NM500 neural network, build a knowledge model, and make decisions based
on pattern recognition.
Page 7
copyright nepes corp. all rights reserved
2.2 Set up development environment for Linux Series
Pre-requisites:
1. libusb-1.0.x is required for functioning of the API library.
$ sudo apt-get install libusb-1.0-0-dev
Installation Steps:
1. Copy library into /usr/local/lib
[x64]
$ sudo cp lib/linux.x64/libnmengine.so.2.0.0 /usr/local/lib
[x86]
$ sudo cp lib/linux.x86/libnmengine.so.2.0.0 /usr/local/lib
[armv7I/usb]
$ sudo cp lib/linux.armv7I.usb/libnmengine.so.2.0.0 /usr/local/lib
[armv7I/spi] (e.g Raspberry PI with interface board support spi),
$ sudo cp lib/linux.armv7I.spi/libnmengine.so.2.0.0 /usr/local/lib
2. Make a link for library
$ sudo ln -s /usr/local/lib/libnmengine.so.2.0.0 /usr/local/lib/libnmengine.so
3. Add /usr/local/lib to LD_LIBRARY_PATH
e.g. export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH:/usr/local/lib:
or set the path for library by using set_lib_path() func
2.3 Set up development environment for macOS
Pre-requisites:
1. libusb-1.0.x is required for functioning of the API library.
$ brew install libusb
Page 8
copyright nepes corp. all rights reserved
Installation Steps:
1. Copy library into /usr/local/lib
$ sudo cp lib/macos/libnmengine.dylib.2.0.0 /usr/local/lib
2. Make a link for library
$ sudo ln -s /usr/local/lib/libnmengine.dylib.2.0.0 /usr/local/lib/libnmengine.dylib
3. Add /usr/local/lib to DYLD_LIBRARY_PATH
e.g. export DYLD_LIBRARY_PATH=.:$DYLD_LIBRARY_PATH:/usr/local/lib:
or set the path for library by using set_lib_path() func
2.3 Set up development environment for Windows
1. Unpack CypressDriverInstaller.zip in the Driver folder inside the package
and run the CypressDriverInstaller.exe file to install the driver.
2. Add the dll location to the system environment variable (like PATH)
lib/win.x32 or lib/win.x64
or place the dlls in the same location where the executable is located.
or set the path for library by using set_lib_path() func
2.4 Using NM500 library APIs
To use the NM500 library APIs, simply 'import nmengine'
Page 9
copyright nepes corp. all rights reserved
3. NM500 APIs
3.1 set_lib_path
The set_lib_path function is used to set the local library path directly.
def set_lib_path(lib_path)
Args
lib_path (str): A string of library path
Returns
none (void)
The following is an example of setting the library path
import nmengine
# Set the library path directly if library can not load
nmengine.set_lib_path('<your library path>')
3.2 get_devices
The get_devices function is used to get the list of the NM500 devices detected.
def get_devices()
Args
none
Page 10
copyright nepes corp. all rights reserved
Returns
result (int): An integer of result code. see Section 4. Result Codes. devices ([Device]): Up to 5 detected device information.
3.3 connect
The connect function is used to open selected device and perform the initialization of the NM500.
It will return ERROR_INIT_FAILED (104) if the NM500 is not properly initialized or selected device does
not support the NM500 functionality.
For more information on result codes, see Section 4. Result Codes.
def connect(device)
Args
device (Device): A device selected for use.
Returns
result (int): An integer of result code.
3.4 close
The close function is used to close a selected device handle. This should be called before
application exits.
def close(device)
Args
device (Device): A device selected for disconnect.
Page 11
copyright nepes corp. all rights reserved
Returns
result (int): An integer of result code.
3.5 get_network_info
The get_network_info function is used to get information of the NM500 neural network(NeuroMem)
and device.
def get_network_info(device, info)
Args
device (Device): A device selected for use. info (NetworkInfo): A reference of NetworkInfo
Returns
result (int): An integer of result code.
The NetworkInfo information is as follows;
.
neuron_memory_size: the memory size of the neuron - fixed at 256 neuron_size: the total number of neurons on the NeuroMem network version: the version of the device firmware.
The version value is represented as follows;
[15:8] Board Identifier [7:4] Board Version [3:0] FPGA Version
Page 12
copyright nepes corp. all rights reserved
[Warning] This function performs the initialization of the NM500 to count the total number of
neurons. Therefore, do not use this function during learning or recogition. This function is only useful
if you want to check the overall network information at the beginning.
3.6 get_version
The get_version function is used to get the version of the device firmware.
def get_version(device)
Args
device (Device): A device selected for use.
Returns
result (int): An integer of result code. version (int): An integer of device firmware version.
The version value is represented as follows;
[15:8] Board Identifier [7:4] Board Version [3:0] FPGA Version
3.7 get_neuron_count
The get_neuron_count function is used to get the number of neurons used on the NeuroMem
network.
def get_neuron_count(device)
Args
device (Device): A device selected for use.
Page 13
copyright nepes corp. all rights reserved
Returns
result (int): An integer of result code. count (int): An integer of the number of neuron used.
The count value is the number of neurons actually used for learning, not the total number of
neurons.
3.8 forget
The forget function is used to clear all knowledge on the NeuroMem network.
def forget(device)
Args
device (Device): A device selected for use.
Returns
result (int): An integer of result code.
It will only initialize the daisy-chain(index of neuron) on the NeuroMem network. In other words, It
will not erase the memory data(stored weight memory) of the neuron. Therefore, the neuron memory
data retrieved using readNeuron(s) function can contain the garbage data if you do not use
maximum memory size of neuron as input vector size. (the maximum memory size of neuron is 256).
However, it is faster than Reset function.
3.9 reset
The reset function is used to clear all knowledge on the NeuroMem network.
Page 14
copyright nepes corp. all rights reserved
def reset(device)
Args
device (Device): A device selected for use.
Returns
result (int): An integer of result code.
It performs the initialization of all the NeuroMem network setting and stored weight momory data of
neurons.
3.10 set_network_type
The set_network_type function is used to set the NeuroMem network mode.
def set_network_type(device, nwtype)
Args
device (Device): A device selected for use. nwtype (int): An Integer of network type
The NeuroMem supports 2 types of network mode: RBF and KNN network. The RBF network
must be selected to build a knowledge model.
0: RBF network mode 1: KNN network mode
Returns
result (int): An integer of result code.
3.11 get_network_type
Page 15
copyright nepes corp. all rights reserved
The get_network_type function is used to check the current network mode. To change network mode
is possible at any time during recognition(classification)
def get_network_type(device)
Args
device (Device): A device selected for use.
Returns
result (int): An integer value of result code. type (int): An integer value of the current network type.
The values for the type are:
0: RBF network mode, 1: KNN network mode
To change network type(mode) is possible at any time during recognition(classification)
3.12 get_network_status
The get_network_status function is used to get the current status of the NeuroMem network.
def get_network_status(device, status)
Args
device (Device): A device selected for use. status (NetworkStatus): A reference of NetworkStatus
Returns
result (int): An integer value of result code.
Page 16
copyright nepes corp. all rights reserved
The information of the NetworkStatus is as follows;
network_type: the type of network mode: (0: RBF network mode, 1: KNN network mode) network_used: the number of neurons currently used context: the context ID that is currently used. It is same as global context ID norm: the type of norm (L1 or Lsup) that is currently used.
3.13 set_context
The set_context function is used to configure the current(global) context attributes on the NeuroMem
network.
def set_context(device, context)
Args
device (Device): A device selected for use. context (Context): A reference of Context
Returns
result (int): An integer value of result code.
The Context information is as follows;
context: the context ID that is used to split the NeuroMem network into subnetworks.
The neurons can be associated to different contexts and their use can be enabled or
disabled by selecting a context value
norm: the type of norm (L1 or Lsup).
Page 17
copyright nepes corp. all rights reserved
minif(Minimum Influence Field): It represents minimum differences for dissimilarity
judgement and it is used to control uncertain domain.
maxif(Maximum Influence Field): it represents maximum differences for similarity
judgement and it is used to adjust conservatism. The values range from 1 to 65535.
3.14 get_context
The get_context function is used to get the current context on the NeuroMem network
def get_context(device, context)
Args
device (Device): A device selected for use. context (Context): A reference of Context
For more information on Context, see Section 3.13 set_context
Returns
result (int): An integer value of result code.
3.15 learn
The learn function is used to teach a neuron(s) with given vector on the NeuroMem network.
def learn(device, req)
Args
device (Device): A device selected for use. req (LearnReq): A reference of LearnReq
Returns
Page 18
copyright nepes corp. all rights reserved
result (int): An integer value of result code.
When a neuron learns input vector, its context information is set to the value of the current(global)
context. If neurons have been assigned with the same context, it says that they will be run with same
conditions, such as MinIF, MaxIF and Norm method.
When a new neuron is assigned to learn input vector, the NCOUNT register value of the NM500 is
incremented by one. If recognizable by existing neurons, new neurons are not allocated. Therefore,
the NCOUNT register value of the NM500 will not be changed.
The LearnReq information is as follow.
Inputs
query_affected: flag for whether to retrieve affected neuron information. generally flagging is not required. category: category of input data(vector). vector_size: input data size. vector: input data.
Outputs
status: network status of learning affected_count: number of affected neurons affected_neurons[affected_count]: list of affected neurons
The value of status represents the result status of the learning, and it is as follows.
LEARN_ALREADY_KNOWN(0) LEARN_SUCCESS(1) LEARN_ADJUSTED (2) LEARN_DEGENERATED (3)
Page 19
copyright nepes corp. all rights reserved
The following is an example of learning and retrieves affected neurons after learning. If the
queryAffected is set to 0, the information of affected neurons will not be returned. If the
queryAffected is set to 1, it will return list of affected neurons. but the processing time will be
increased by more than 2 times.
#
# learn vector (10, 10, 10) with category 10
#
learn_req = nmengine.LearnReq()
vector = [10 for i in range(3)]
# Send max length to avoid gabages in neuron memory.
# If you set the size with actual vector length (< NEURON_MEMORY),
# the remaining value will be filled with garbege value
learn_req.size = nmengine.NEURON_MEMORY
learn_req.vector[:len(vector)] = vector
learn_req.category = 10
# This is optional field for retrieving affected neuron infromation
# If you set the value to 1, it takes more time to learn.
learn_req.query_affected = 1
print('learn req(cat, affected flag, len, vector)', learn_req.category, \
learn_req.query_affected, learn_req.size, tuple(learn_req.vector))
# Fro the learning result,
# refer to 'The result code for learning' in constants.py
print('train a vector to network, result:', nmengine.learn(target, learn_req), \
'status:', learn_req.status)
# Prints affected neurons
for i in range(learn_req.affected_count):
neuron = learn_req.affected_neurons[i]
print('A (nid, cat, aif) :', neuron.nid, neuron.cat, neuron.aif)
Page 20
copyright nepes corp. all rights reserved
result, count = nmengine.get_neuron_count(target)
print('the number of neurons committed is', count)
3.16 learn_batch
The learn_batch function is used to teach a neurons with given vectors on the NeuroMem network.
def learn_batch(device, req)
Args
device (Device): A device selected for use. req (LearnBatchReq): A reference of LearnBatchReq
Returns
result (int): An integer value of result code.
The LearnBatchReq information for batch learning is as follow.
Inputs
iterable: flag for whether to iterate batch learning. iter_count: number of iteration (epoch). vector_count: number of vectors. vector_size: input data size. vectors: list of input data. categories: list of category of input data(vector).
Outputs
iter_result: the number of committed neurons from each iteration.
Page 21
copyright nepes corp. all rights reserved
If iterable is set as 1(true) and iter_count is greater than 0, the learning will be repeated on the basis
of the given vectors until learning is no longer possible or until the maximum iter_count value is
reached.
When learning is complete, iter_count returns the number of actually repeated iterations, and
iter_result returns the number of learned neurons for each iteration.
The following is an example of batch learning
#
# Learn vectors in batch mode
#
# Maximum iteration for learning
iter_count = 10
# The number of vectors for learning (for testing)
vector_count = 20
# The vector size
vector_size = 3
# Get the LearnBatchReq instance from factory method
req = nmengine.learn_batch_req_factory(iter_count, vector_count, vector_size)
# Flag for iteration,
# 1: automatic iterative learning
# 0: none
req.iterable = 1
#
# Generates dummy training dataset
#
# Gets the random value for cat
cat = randint(1, 50)
for i in range(vector_count):
# Keeps the same cat value for unknown case
if (i % 3) == 0:
Page 22
copyright nepes corp. all rights reserved
cat = randint(1, 50)
# Gets the random value for vector
vec = randint(1, 50)
for j in range(vector_size):
req.vectors[i * vector_size + j] = vec
req.categories[i] = cat
print(i, 'batch >> cat:', cat, 'vec:', [vec for i in range(vector_size)])
print()
# Learn dataset in batch mode
result = nmengine.learn_batch(target, req)
print('batch learning done, iter:', req.iter_count, 'result:', result)
for i in range(req.iter_count):
print('epoch:', i, 'learned', req.iter_result[i])
# Print statistics of custerized dataset
# Get meta information of the model
info = nmengine.ModelInfo()
nmengine.get_model_info(target, info)
print("model count:", info.count, 'max ctx:', info.max_context \
, 'max cat:', info.max_category)
stat = nmengine.model_stat_factory(info.max_category)
# Get statistics of each context
stat.context = 1
nmengine.get_model_stat(target, stat)
dash = '-' * 30
for i in range(info.max_category + 1):
if i == 0:
print(dash)
print('{:<10}{:<10}{:<10}'.format('cat', 'neurons', 'degen'))
print(dash)
Page 23
copyright nepes corp. all rights reserved
else:
print('{:<10}{:<10}{:<10}'.format(i, stat.histo_cat[i],stat.histo_deg[i]))
3.17 clusterize
The clusterize function is used to group a set of vectors given into a clusters on the NeuroMem
network.
def clusterize(device, req)
Args
device (Device): A device selected for use. req (ClusterizeReq): A reference of ClusterizeReq
Returns
result (int): An integer value of result code.
The ClusterizeReq information for clusterizing is as follow.
initial_category: initial category id (it must be greater than 0). incrementof: unit of increasement of category id. vector_count: number of vectors. vector_size: input data size. vectors: list of input data.
The minif and maxif will affect grouping of a set of vectors given. Therefore, both of them should be
set to the appropriate value before learning.
The following is an example of clusterizing
Page 24
copyright nepes corp. all rights reserved
#
# Clusterize vectors on neuromem network
# It is unsupervised leanring method.
#
# The number of vectors for clusterization (for testing)
vector_count = 20
# The vector size
vector_size = 3
# Get the ClusterizeReq instance from factory method
req = nmengine.clusterize_req_factory(vector_count, vector_size)
# Initial category
req.initial_category = 1
# Flag for category value
# 0: none, use initial category for learning
# N: increase of N value of category when learning next vector
# if set 1, category will be 1, 2, 3 ...
req.incrementof = 1
#
# Generates dummy training dataset
#
for i in range(vector_count):
# Gets the random value for vector
vec = randint(1, 50)
for j in range(vector_size):
req.vectors[i * vector_size + j] = vec
print(i, 'data >>', [vec for i in range(vector_size)])
print()
Page 25
copyright nepes corp. all rights reserved
# Need to set appropriate values for minimum/maximum influence field.
ctx = nmengine.Context()
ctx.context = 1
ctx.norm = 0
ctx.minif = 2
# 30 is just temporary value for testing
ctx.maxif = 30
nmengine.set_context(target, ctx)
#
# Clusterize the dataset
#
result = nmengine.clusterize(target, req)
print('clusterization done, result:', result, '\n')
# Print statistics of custerized dataset
# Get meta information of the model
info = nmengine.ModelInfo()
nmengine.get_model_info(target, info)
print("model count:", info.count, 'max ctx:', info.max_context \
, 'max cat:', info.max_category)
stat = nmengine.model_stat_factory(info.max_category)
# Get statistics of each context
stat.context = 1
nmengine.get_model_stat(target, stat)
dash = '-' * 30
for i in range(info.max_category + 1):
if i == 0:
print(dash)
print('{:<10}{:<10}{:<10}'.format('cat', 'neurons', 'degen'))
print(dash)
else:
print('{:<10}{:<10}{:<10}'.format(i, stat.histo_cat[i],stat.histo_deg[i]))
Page 26
copyright nepes corp. all rights reserved
#
# Read neuron(s) information of knowledge model
#
result, count = nmengine.get_neuron_count(target)
print('the number of neurons committed', count, '\n')
neurons = (nmengine.Neuron * count)()
result, count = nmengine.read_neurons(target, neurons, count)
print(count, "neuron(s) read\n")
for i in range(count):
print(i, 'neuron id', neurons[i].nid)
print(i, 'neuron size', neurons[i].size)
print(i, 'neuron ncr', neurons[i].ncr)
print(i, 'neuron aif', neurons[i].aif)
print(i, 'neuron minif', neurons[i].minif)
print(i, 'neuron cat', neurons[i].cat)
print(i, 'neuron model', tuple(neurons[i].model))
print()
3.18 classify
The classify function is used to identify to which of a set of categories input vector belongs.
def classify(device, req)
Args
device (Device): A device selected for use. req (ClassifyReq): A reference of ClassifyReq
Returns
result (int): An integer value of result code.
Page 27
copyright nepes corp. all rights reserved
The ClassifyReq information is as follows.
Inputs
vector_size: input data size. vector: input data. k: number of returns matched.
Outputs
status: network status of classifying. matched_count: number(n) of matched. nid[n]: id of neuron matched. category[n]: category of neuron matched. degenerated[n]: degenerated flag of neuron matched.
1: degenerated
distance[n]: distance between input data and prototype of neuron matched.
The k parameter means the number of neurons returned. and the default value is 9. If network mode
is RBF, it can be less than 9 (k).
The value of status represents the result status of the classification(recognition), and it is as follows.
CLASSIFY_UNKNOWN (0): It cannot be classified into the learned model. CLASSIFY_UNCERTAIN (4): It can be classified into the learned model, but it is judged to be
one or more other categories and is unclear, and additional judgment is necessary.
CLASSIFY_IDENTIFIED (8): It can be clearly classified into the learned model.
The following is an example of classifying
#
# Classify a vector using trained model
Page 28
copyright nepes corp. all rights reserved
#
vector = [20 for i in range(3)]
classify_req = nmengine.ClassifyReq()
classify_req.size = nmengine.NEURON_MEMORY
classify_req.vector[:len(vector)] = vector
# Set the value for maximum
classify_req.k = 3
print('classify_req(k, len, vector)', classify_req.k, \
classify_req.size, tuple(classify_req.vector))
print('classify a vector using trained model. result:', \
nmengine.classify(target, classify_req))
# Fro the learning result,
# refer to 'The result code for classifying' in constants.py
print('status:', classify_req.status, 'matched:', classify_req.matched_count)
# Prints matched neurons
for i in range(classify_req.matched_count):
print('M (nid, dis, cat, degen) :', classify_req.nid[i], classify_req.distance[i], \
classify_req.category[i], classify_req.degenerated[i] )
print()
3.19 read
The read function is used to read the value of the NeuroMem register specified.
def read(device, reg)
Args
device (Device): A device selected for use. reg (int): An Integer value of NeuroMem Register ID
Page 29
copyright nepes corp. all rights reserved
Returns
result (int): An integer value of result code. value (int): An integer value of the value of specified NeuroMem Register
For the NeuroMem network registers accessible, refer to 5. NeuroMem Register.
The power_save function must be called after using this function directly.
3.20 write
The write function is used to set the value of the NeuroMem network register specified directly.
def write(device, reg, data)
Args
device (Device): A device selected for use. reg (int): An Integer value of NeuroMem register ID data (int) An Integer value to set in the specified register (reg)
Returns
result (int): An integer value of result code.
For the NeuroMem network registers accessible, refer to 5. NeuroMem Register.
The power_save function must be called after using this function directly.
3.21 read_neuron
Page 30
copyright nepes corp. all rights reserved
The read_neuron function is used to get neuron information specified.
def read_neurons(device, neuron)
Args
device (Device): A device selected for use. neuron (Neuron): A reference of Neuron
Returns
result (int): An integer value of result code. count (int): An integer value of the number of neurons actually read. (1 or 0)
The Neuron information of each neuron is as the following.
nid: neuron id size: vector length used in neuron memory. (vector size) ncr: neuron context. (context id, norm) aif: active influence field. (threshold of activation function) minif: minimum influence field. model: prototype (stored weight memory/rbf center).
If the length of the learned data is less than 256, you can specify the length of data to read through
the size variable.
The following is an example of getting a neuron information.
neuron = nmengine.Neuron()
# Specifies the ID of the neuron to read.
neuron.nid = nid
result, count = nmengine.read_neuron(target, neuron)
Page 31
copyright nepes corp. all rights reserved
print("the number of neurons read",count)
if count > 0:
print('neuron id', neuron.nid)
print('neuron size', neuron.size)
print('neuron ncr', neuron.ncr)
print('neuron aif', neuron.aif)
print('neuron minif', neuron.minif)
print('neuron cat', neuron.cat)
print('neuron model', tuple(neuron.model))
print()
3.22 read_neurons
The read_neurons function returns information about the knowledge model that is a list of all the
neurons learned.
def read_neurons(device, neurons, count)
Args
device (Device): A device selected for use. neurons ([Neuron]): A reference of list of Neuron count (int) An Integer value of the number of neurons to read
Returns
result (int): An integer value of result code. count (int): An integer value of the number of neurons actually read.
The following is an example of querying up to the number of committed neurons.
result, count = nmengine.get_neuron_count(target)
print('the number of neurons used', count)
Page 32
copyright nepes corp. all rights reserved
neurons = (nmengine.Neuron * count)()
result, count = nmengine.read_neurons(target, neurons, count)
print("read",count)
for i in range(count):
print(i, 'neuron id', neurons[i].nid)
print(i, 'neuron size', neurons[i].size)
print(i, 'neuron ncr', neurons[i].ncr)
print(i, 'neuron aif', neurons[i].aif)
print(i, 'neuron minif', neurons[i].minif)
print(i, 'neuron cat', neurons[i].cat)
print(i, 'neuron model', tuple(neurons[i].model))
print()
3.23 write_neurons
The write_neurons function is used to upload the knowledge model to the NeuroMem network.
def write_neurons(device, neurons, count)
Args
device (Device): A device selected for use. neurons ([Neuron]): A reference of list of Neuron count (int) An Integer value of the number of neurons to write
Returns
result (int): An integer value of result code. count (int): An integer value of the number of neurons actually wrote.
The following is an example of uploading dummy neurons.
neurons = []
Page 33
copyright nepes corp. all rights reserved
for i in range(iter_cnt):
vector = [i+1 for v in range(v_size)]
neuron = nmengine.Neuron()
neuron.nid = i
neuron.size = v_size
neuron.ncr = 1
neuron.aif = (i+1)*100
neuron.minif = 2
neuron.cat = i+1
neuron.model[:v_size] = vector
neurons.append(neuron)
result, count = nmengine.write_neurons(target, neurons, len(neurons))
print('write neurons', count, 'result:',result)
3.24 get_model_info
The get_model_info function is used to get the meta information of the knowledge model
def get_model_info(device, info)
Args
device (Device): A device selected for use. info (ModelInfo): A reference of ModelInfo
Returns
result (int): An integer value of result code.
The ModelInfo information of model is as the following.
count: number of neurons used/committed max_context: the largest context id (1~127)
Page 34
copyright nepes corp. all rights reserved
max_category: the largest cateogry id (1~32766)
The following is an example of getting meta information of the knowledge model
# Print statistics of custerized dataset
# Get meta information of the model
info = nmengine.ModelInfo()
nmengine.get_model_info(target, info)
print("model count:", info.count, 'max ctx:', info.max_context \
, 'max cat:', info.max_category)
3.25 get_model_stat
The get_model_stat function is used to get the statistics information of the knowledge model. It
shows distribution of neuron per category
def get_model_stat(device, stat)
Args
device (Device): A device selected for use. stat (ModelStat): A reference of ModelStat
Returns
result (int): An integer value of result code.
The ModelStat information of model statistics is as the following.
Inputs
context: target context id for analysis
Outputs
Page 35
copyright nepes corp. all rights reserved
count: number of neurons used/committed in given context histo_cat[the largest cateogry id + 1]: number of neurons per cateogory histo_deg[the largest cateogry id + 1]: nember of degenerated neuron per category
The following is an example of getting the statistics information of the knowledge model.
stat = nmengine.model_stat_factory(info.max_category)
# Get statistics of each context
stat.context = 1
nmengine.get_model_stat(target, stat)
dash = '-' * 30
for i in range(info.max_category + 1):
if i == 0:
print(dash)
print('{:<10}{:<10}{:<10}'.format('cat', 'neurons', 'degen'))
print(dash)
else:
print('{:<10}{:<10}{:<10}'.format(i, stat.histo_cat[i],stat.histo_deg[i]))
3.26 save_model
The save_model function is used to save the current knowledge model to file.
def save_model(device, neurons, count, path)
Args
device (Device): A device selected for use. neurons ([Neuron]): A reference of list of Neurons count (int): A integer value of the number of neurons to save path (str): A string of file path to save
Page 36
copyright nepes corp. all rights reserved
Returns
result (int): An integer value of result code.
The following is an example of saving the model to file:
result, count = nmengine.get_neuron_count(target)
print('the number of neurons used', count)
neurons = (nmengine.Neuron * count)()
result, count = nmengine.read_neurons(target, neurons, count)
print('save model', count, path, 'result:', nmengine.save_model(target, neurons, count, path))
3.27 load_model
The load_model function is used to load a knowledge model from file.
def load_model(device, neurons, count, path)
Args
device (Device): A device selected for use. neurons ([Neuron]): A reference of list of Neurons count (int): A integer value of the number of neurons to load (maximum) path (str): A string of file path to load
Returns
result (int): An integer value of result code. count (int): A integer value of the number of neurons actually loaded.
The following is an example of loading the model from file:
Page 37
copyright nepes corp. all rights reserved
# Gets network information
# Notice, this function performs the initialization of the NM500
# Do not use this function during learning/classfying
network_info = nmengine.NetworkInfo()
nmengine.get_network_info(target, network_info)
print('get network info', network_info.neuron_count, network_info.neuron_memory_size,
network_info.version)
count = network_info.neuron_count
neurons = (nmengine.Neuron * count)()
result, count = nmengine.load_model(target, neurons, count, path)
for i in range(count):
print(i, 'neuron id', neurons[i].nid)
print(i, 'neuron size', neurons[i].size)
print(i, 'neuron ncr', neurons[i].ncr)
print(i, 'neuron aif', neurons[i].aif)
print(i, 'neuron minif', neurons[i].minif)
print(i, 'neuron cat', neurons[i].cat)
print(i, 'neuron model', tuple(neurons[i].model))
print()
3.28 power_save
The power_save function is used to set the power mode of the NM500. It immediately changes to the
power saving mode.
def power_save(device)
[Notice] It is recommended that you call power_save function during idle periods after using low-
level functions such as the read() and write() functions.
Page 38
copyright nepes corp. all rights reserved
4. Result Codes
All result codes are integers. Symbolic names for all result codes are defined in nmengine.h header
file.
Result Code
Name
Description
0
SUCCESS 100
ERROR_UNKNOWN
101
ERROR_DEVICE_NOT_FOUND,
,
102
ERROR_DEVICE_INFO_FETCH_FAILED,
,
103
ERROR_DEVICE_OPEN_FAILED,
104
ERROR_INIT_FAILED,
105
ERROR_INVALID_PARAMETER,
106
ERROR_NOT_SUPPORT,
107
ERROR_IO_TIMEOUT,
108
ERROR_IO_FAILED,
109
ERROR_RESOURCE
Page 39
copyright nepes corp. all rights reserved
5. NeuroMem Registers
Acronym
Description
Address
Normal
mode
SR
mode
16-bit
default
NSR
Network Status Register
0x0D
RW W 0x0000
GCR
Global Control Register
0x0B
RW 0x0001
MINIF
Minimum Influence Field
0x06
RW
RW
0x0002
MAXIF
Maximum Influence Field
0x07
RW 0x4000
NCR
Neuron Context Register
0x00
RW
0x0001
COMP
Component
0x01
W
RW
0x0000
LCOMP
Last Component
0x02
W 0x0000
INDEXCOMP
Component index
0x03
W W 0x0000
DIST
Distance register
0x03
R R 0xFFFF
CAT
Category register
0x04
RW
RW
0xFFFF
AIF
Active Influence Field
0x05
RW
0x4000
NID
Neuron Identifier
0x0A
R R 0x0000
POWERSAVE
PowerSave
0x0E
W n/a
FORGET
Forget
0x0F
W n/a
NCOUNT
Count of committed
neurons
0x0F
R R 0x0000
RESETCHAIN
Points to the first neuron
0x0C
W n/a
TESTCOMP
Test Component
0x08
W 0x0000
TESTCAT
Test Category
0x09
W 0x0000
Loading...