在Djang没有用TensorFlow训练的模型

2024-04-20 09:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

从检查点还原失败。这很可能是由于检查点中缺少变量名或其他图形键。请确保没有更改基于检查点的预期图形。原始错误:

在检查点中找不到密钥偏移\u 1 [[节点save\u 1/RestoreV2(在Programing\web\u Programing\django\django vegiter\predict中定义\视图。py:20)]]

追溯

saver.restore(sess, save_path) 
…
err, "a Variable name or other graph key that is missing") 

在Python虚拟环境中运行

Django 2.2.5版 Keras应用1.0.8 Keras预处理1.1.0 数字1.17.1 熊猫0.25.1 张力板1.14.0 张量流1.14.0 张量流估计器1.14.0 术语颜色1.1.0

在此处创建视图

定义索引(请求): X=tf.占位符(tf.32浮动,形状=[无,4]) Y=tf.占位符(tf.32浮动,形状=[无,1])

W = tf.Variable(tf.random_normal([4, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

hypothesis = tf.matmul(X, W) + b

saver = tf.train.Saver()
model = tf.global_variables_initializer()

sess = tf.Session()
sess.run(model)
save_path = "./model/saved.cpkt"
saver.restore(sess, save_path)

if request.method == "POST":
    avg_temp = float(request.POST['avg_temp'])
    min_temp = float(request.POST['min_temp'])
    max_temp = float(request.POST['max_temp'])
    rain_fall = float(request.POST['rain_fall'])

    price = 0

    data = ((avg_temp, min_temp, max_temp, rain_fall), (0, 0, 0, 0))
    arr = np.array(data, dtype=np.float32)

    x_data = arr[0:4]
    dict = sess.run(hypothesis, feed_dict={X: x_data})

    price = dict[0]
else:
    price = 0
return render(request, 'predict/index.html', {'price': price})

岗位

可变值 csrfmiddlewaretoken'BeE48x03YbOY3tIC7eF8L0tKrZME'
“24”平均温度 最低温度'12' 最高温度'31' 降雨量“0.9”

在收到post数据之后,我们希望price变量有一个预测。你知道吗


Tags: pathnamedatamodelrequestsavetffloat
1条回答
网友
1楼 · 发布于 2024-04-20 09:23:00

概念

在高层次上,我们可以把图形看作数学表达式,如(x+3x^2-.6x^3),它定义了对变量x执行的操作。另一方面,会话是存储x值和所有中间值,如“3x^2”等的地方。你知道吗

要恢复会话,我们需要保存会话的相同元素。你知道吗

当您运行下面的代码时

W = tf.Variable(tf.random_normal([4, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

Tensorflow将创建变量并将其添加到默认图形中,默认图形的结构可能与保存的图形不同。你知道吗

恢复图形和会话的步骤

  • 首先从“.meta”文件还原图形
  • 然后你可以从图中得到张量和运算的引用按名称获取张量()/graph.get\u操作\u按\u名称()
  • 然后可以对图进行一些添加,这不会影响会话恢复
  • 在还原会话之前执行变量初始化。如果稍后执行,则会覆盖还原的变量
  • 从检查点还原会话

代码如下所示

## restore the graph
importer = tf.train.import_meta_graph("%s.meta"%saved_path)
graph = tf.get_default_graph()

## get reference of output of tensor and set it to variable
## here ":0" indicates the output of an operation
W = graph.get_tensor_by_name("weights")
output = graph.get_tensor_by_name("hidden5_out:0")


## restore the variables in session
session = tf.Session(graph=graph)
importer.restore(session, saved_path)

相关问题 更多 >