Dear all,
I am currently starting to learn more about the amazing RedisGears and RedisAI, hence I appologize in advance if I missed something.
The problem I am facing comes up when I am trying to run a toy SVM model trained using and exported from scikit-learn to ONNX: Using the RedisAI C API yields different class labels than using Redis CLI.
Example 1
Using the RedisAI C API for instance provides the following output (I would consider [6.93673868037757e-310]
being the class label):
Loading data
[5.7, 2.8, 4.5, 1.3]
Executing model
[6.93673868037757e-310]
[-0.22895553708076477, 2.2316672801971436, 0.9738349914550781]
While running the same model using the same input data on Redis CLI provides:
127.0.0.1:6379> AI.TENSORSET x DOUBLE 1 4 VALUES 5.7 2.8 4.5 1.3
OK
127.0.0.1:6379> AI.MODELRUN svm INPUTS x OUTPUTS classes prob
OK
127.0.0.1:6379> AI.TENSORGET classes VALUES
1) (integer) 1
127.0.0.1:6379> AI.TENSORGET prob VALUES
1) "-0.22895553708076477"
2) "2.2316672801971436"
3) "0.97383499145507812"
The label should be 1 as stated in the output from the Redis CLI while the RedisAI C API indicates the label (approx.) 0.
Example 2
Using RedisAI C API:
Loading data
[5.1, 3.5, 1.4, 0.2]
Executing model
[6.9367386804922e-310]
[2.2354373931884766, 1.1609625816345215, -0.25650709867477417]
Using Redis CLI:
127.0.0.1:6379> AI.TENSORSET x DOUBLE 1 4 VALUES 5.1 3.5 1.4 0.2
OK
127.0.0.1:6379> AI.MODELRUN svm INPUTS x OUTPUTS classes prob
OK
127.0.0.1:6379> AI.TENSORGET classes VALUES
1) (integer) 0
127.0.0.1:6379> AI.TENSORGET prob VALUES
1) "2.2354373931884766"
2) "1.1609625816345215"
3) "-0.25650709867477417"
I am using the latest version of the Docker image redislabs/redismod:edge.
My code looks like follows:
import redisAI
def analytics(datapoint):
# loading data
print('Loading data')
x = datapoint['value']['data']
input_data = list(map(float, x.replace('[', '').replace(']', '').split(' ')))
input_tensor = redisAI.createTensorFromValues('DOUBLE', [1, 4], input_data)
# creating RedisAI model runner and execute
print("Executing model")
modelRunner = redisAI.createModelRunner('svm')
redisAI.modelRunnerAddInput(modelRunner, 'input', input_tensor)
redisAI.modelRunnerAddOutput(modelRunner, 'output_1')
redisAI.modelRunnerAddOutput(modelRunner, 'output_2')
model_replies = redisAI.tensorToFlatList(
redisAI.modelRunnerRun(modelRunner)[0])
model_replies_1 = redisAI.tensorToFlatList(
redisAI.modelRunnerRun(modelRunner)[1])
model_output = model_replies
# write output to stream
if model_output:
print('Writing to output stream')
execute('XADD', 'result_stream', 'MAXLEN', '~', '100', '*'
, 'class', model_output
, 'prob', model_replies_1)
gb = GearsBuilder('StreamReader', desc='').map(analytics).register('input_stream')
Any help is appreciated!
Thanks a lot!