u张量模型输出不等于预期输出
uTensor model output does not equal to expected output
我目前正在做一个需要uTensor的项目。 uTensor 似乎工作正常,但是我遇到了一个我(显然(无法解决自己的问题。
问题定义
我创建了一个简单的python脚本,该脚本生成模型并将其保存到文件中。稍后可以使用 uTensor-cli 将此保存的模型转换为C++代码。生成的C++代码将在 ARM 开发板上运行。
一切运行良好,没有错误。但是,当我创建类似"xW+b"的模型时,开发板上模型的输出始终等于某个静态值,这不等于 python 脚本中模型的输出。
问题是,当使用像"W+b"这样的简单模型时(这里没有使用输入张量(,ARM开发板上的输出等于python脚本的输出。一切都按预期工作。
我的发现
当使用输入张量(没有什么大,只是一个一维数组,如 [1,0](时,与 python 脚本的输出相比,ARM 开发板总是输出一些奇怪的值。不使用输入张量时,一切都按预期工作。
其他信息
因为uTensor上还没有wiki,所以我用了一个教程来了解uTensor。我使用的教程可以在这里找到:https://blog.hackster.io/simple-neural-network-on-mcus-a7cbd3dc108c 我编写的代码基于教程,不包含任何成本/损失函数,并且无法"学习"任何东西。该代码仅用于调试。
问题
输入张量使应用程序输出意外值的原因是什么?我怎么可能解决这个问题?
代码和输出
蟒蛇脚本
import tensorflow as tf
import numpy as np
from tensorflow.python.framework import graph_util as gu
def weightVariable(shape, name):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial, name=name)
def createLayer(layerInput, inputSize, outputSize, layerNumber, dropout = -1):
layerNumber = str(layerNumber)
#Define weight and bias
W_fc = weightVariable([inputSize, outputSize], 'W_fc' + layerNumber)
#Formula Wx+b=y
a_fc = tf.matmul(layerInput, W_fc, name='y_pred' + layerNumber)
return a_fc
def saveGraph(saver, sess, y_pred):
outNodes = [y_pred.op.name]
subGraphDef = gu.remove_training_nodes(sess.graph_def)
subGraphDef = gu.convert_variables_to_constants(sess, subGraphDef, outNodes)
#Save the checkpoint
ckptPath = saver.save(sess, "./chkps/model.ckpt")
#Save the graph
graphPath = tf.train.write_graph(subGraphDef, "./graph", "mlp.pb", as_text=False)
#Print some usefull messages
print("Saved checkpoint to: " + ckptPath)
print("Saved graph to: " + graphPath)
print("Output tensor: " + y_pred.op.name)
def restoreGraph(saver, sess):
tf.reset_default_graph()
saver.restore(sess, "./chkps/model.ckpt")
def main():
data = [
[0,0],
[0,1],
[1,0],
[1,1]
]
labels = [
[0],
[1],
[1],
[0]
]
inputSize = 2
outputSize = 1
#Placeholders for the input and output
x_input = tf.placeholder(tf.float32, [None, inputSize], name='x_input')
y_output = tf.placeholder(tf.float32, [None, outputSize], name='y_output')
#Layers with relu activation
inputLayer = createLayer(x_input, inputSize, outputSize, 0)
#Start a session
sess = tf.Session()
saver = tf.train.Saver()
#Run the session
sess.run(tf.global_variables_initializer())
feed_dict = {x_input: data, y_output: labels}
sess.run(inputLayer, feed_dict=feed_dict)
#Save the graph
saveGraph(saver, sess, inputLayer)
#Test the algorithm
for i in range(0,4):
testInput = [data[i]]
output = sess.run(inputLayer, feed_dict={x_input: testInput})[0][0]
print("Test output " + str(i) + ": " + str(output))
#End the session
sess.close()
#Execute the main function
main()
输出
Saved checkpoint to: ./chkps/model.ckpt
Saved graph to: ./graph/mlp.pb
Output tensor: y_pred0
Test output 0: 0.0
Test output 1: 0.0034507334
Test output 2: 0.07698402
Test output 3: 0.080434754
转换为C++
utensor-cli convert graph/mlp.pb --output-nodes=y_pred0
C++代码
#include "models/mlp.hpp"
#include "tensor.hpp"
#include "mbed.h"
#include <stdio.h>
const int testData[4][2] = {{0,0},{0,1},{1,0},{1,1}};
Serial uart(USBTX, USBRX, 115200);
int main(void){
printf("Compiled at: ");
printf(__TIME__);
printf("n");
for(int i = 0; i < 4; i++){
//Create the context class.
Context ctx;
Tensor *input_x = new WrappedRamTensor<int>({1, 2}, (int*) testData[i]);
get_mlp_ctx(ctx, input_x); //Pass the tensor to the context
S_TENSOR pred_tensor = ctx.get("y_pred0:0"); //Get the output tensor
ctx.eval(); //Trigger the inference
float prediction = *(pred_tensor->read<float>(0,0)); //Get the result
printf("Test output %d: %f rn", i, prediction); //Print the result
}
printf("n");
return 0;
}
串行输出
Compiled at: (Compile time)
Test output 0: 0.000000
Test output 1: 0.000000
Test output 2: 0.000000
Test output 3: 0.000000
将C++代码中的数据类型更改为浮点型就成功了!我仍然不太确定我怎么没有想到这一点。
相关文章:
- 为什么 std::round(sin(pi/6)) 不等于 1?
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- Windows 10命令提示符的输出不正确
- 为什么数组到指针的输出不相关
- 我的求解(字符串 a、字符串 b)的输出与随机哈希中的预期输出不匹配
- 程序的输出不是来的,它是一个链表程序
- C++ - 为什么 unicode 输出不正确?
- 存储和打印字符,但输出不是预期的
- 两种情况下的输出不应该相同吗?
- 我的叉子输出不正确
- Opengl 4 调试输出不起作用
- 你怎么编码,如果x不等于一个数字,程序就会退出
- bcdedit 文件输出不起作用,但其他命令可以
- u张量模型输出不等于预期输出
- 输出不是我想要C++的
- C++使用 CEIL 和长度输出不正确的值
- 虽然不等于陈述和/或差异
- 这种关系不等于定义良好的表达式吗?
- 使用 iconv 进行 UTF8 转换的输出不正确
- C++:为什么最后一个输出不是地址以及如何获取值 9