【问题标题】:predicting in tensor flow在张量流中预测
【发布时间】:2019-07-20 22:38:28
【问题描述】:

训练我的“参数”(w1,w2,Conv 网络中过滤器的权重)后,将它们保存为 parameters=sess.run(parameters)

我拍摄了一张图像 img=[1,64,64,3],并将其传递给 mypredict(x,parameters) 函数进行预测,但它给出了错误。功能如下。任何错误的建议。

def forward_propagation(X, parameters):

W1 = parameters['W1']
W2 = parameters['W2']


Z1 = tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')

A1 = tf.nn.relu(Z1)

P1 = tf.nn.max_pool(A1,ksize=[1,8,8,1],strides=[1,8,8,1],padding='SAME')

Z2 = tf.nn.conv2d(P1,W2,strides=[1,1,1,1],padding='SAME')

A2 = tf.nn.relu(Z2)

P2 = tf.nn.max_pool(A2,ksize=[1,4,4,1],strides=[1,4,4,1],padding='SAME')

P2 = tf.contrib.layers.flatten(P2)

Z3 = tf.contrib.layers.fully_connected(P2,num_outputs=6,activation_fn=None)

return Z3

def mypredict(X,par):

W1 = tf.convert_to_tensor(par["W1"])
W2 = tf.convert_to_tensor(par["W2"])
params = {"W1": W1,
          "W2": W2}

x = tf.placeholder("float", [1,64,64,3])

z3 = forward_propagation_for_predict(x, params)

p = tf.argmax(z3)

sess = tf.Session()
prediction = sess.run(p, feed_dict = {x:X})

return prediction

我使用相同的函数“forward_propagation”来训练权重,但是当我传递单个图像时,它不起作用。

错误:


FailedPreconditionError Traceback(最近一次调用最后一次) /opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1138 尝试: -> 1139 返回 fn(*args) 1140 除了errors.OpError as e:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata) 第1120章 -> 1121 状态,run_metadata) 第1122章

/opt/conda/lib/python3.6/contextlib.py in exit(自我、类型、值、回溯) 88 尝试: ---> 89 下一个(self.gen) 90 除了 StopIteration:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status() 第465章 --> 466 pywrap_tensorflow.TF_GetCode(status)) 467终于:

FailedPreconditionError: 尝试使用未初始化的值fully_connected_1/biases [[节点:fully_connected_1/biases/read = IdentityT=DT_FLOAT, _class=["loc:@fully_connected_1/biases"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

在处理上述异常的过程中,又发生了一个异常:

FailedPreconditionError Traceback(最近一次调用最后一次) 在 () ----> 1 pred=mypredict(t,pp) 2

in mypredict(X, par) 49 50 次会议 = tf.Session() ---> 51 预测 = sess.run(p, feed_dict = {x:X}) 52 53 回报预测

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 787尝试: 第788章 --> 789 run_metadata_ptr) 790 如果运行元数据: 第791章

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 995 如果 final_fetches 或 final_targets: 996 个结果 = self._do_run(handle, final_targets, final_fetches, --> 997 feed_dict_string,选项,run_metadata) 998 其他: 999 个结果 = []

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1130 如果句柄为无: 第1131章 -> 1132 目标列表,选项,运行元数据) 1133 其他: 第1134章

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1150,除了 KeyError: 1151通过 -> 1152 提升类型(e)(节点定义,操作,消息) 1153 第1154章

FailedPreconditionError:尝试使用未初始化的值fully_connected_1/biases

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    您还必须从全连接层加载参数。

    不过,我还是建议使用TensorFlow's Saver and Restore functions

    为了节省,这里是一个玩具示例:

    import tensorflow as tf
    w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
    w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
    saver = tf.train.Saver()
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    saver.save(sess, 'my_test_model',global_step=1000) # saving model after 1000 steps
    

    存储了以下文件:

    my_test_model-1000.index
    my_test_model-1000.meta
    my_test_model-1000.data-00000-of-00001
    checkpoint
    

    所以要恢复,你可以先重新创建网络,然后加载参数:

    with tf.Session() as sess:
     recreated_net = tf.train.import_meta_graph('my_test_model-1000.meta')
     recreated_net.restore(sess, tf.train.latest_checkpoint('./'))
    

    【讨论】:

    • 你能给我一个例子来说明如何做到这一点,保存完全连接的层权重并将其链接到我的函数中。谢谢
    • 见附上的链接。如果您还有疑问,请随时提问
    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 2017-08-16
    • 2017-04-28
    • 2018-01-10
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多