weixin_51793354 2022-07-31 16:41 采纳率: 54.5%
浏览 121
已结题

tensorflow 训练完后如何测试?尝试读取文件,报错了。

我写了一个类,想在fit里训练,在predict里测试训练结果,我的predict函数应该怎么写?我尝试把计算图保存到文件里,然后在predict函数里读取出来,但是总是报错。我用的是tensorflow1.13.1。
下面是相关的代码

    def fit(self, x, y, learning_rate=0.1, iter_num=10):
        (m, n_h, n_w, n_c) = x.shape
        (m, n_y) = y.shape
        self.learning_rate = learning_rate
        self.x_ = tf.placeholder(tf.float32, [None, n_h, n_w, n_c])
        self.y_ = tf.placeholder(tf.float32, [None, n_y])
        self.init_paras()
        self.f_p(self.x_, self.y_)
        self.b_p()
        init = tf.global_variables_initializer()
        with tf.Session() as session:
            session.run(init)
            for cnt in range(iter_num):
                _, cost = session.run([self.cache["optimizer"], self.cache["cost"]], feed_dict={self.x_: x, self.y_: y})
                if cnt % 5 == 0:
                    print("第{}次代价为:{}".format(cnt, cost))
            saver = tf.train.Saver()
            saver.save(session, "./checkpoint_dir/model")

    def predict(self, x, y):
        with tf.Session() as sess:
            saver = tf.train.Saver()
            saver.restore(sess, "./checkpoint_dir/model.meta")
            a = self.cache["output"]
            accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(a, axis=1), tf.argmax(y, axis=1)), tf.float32))
            sess.run(accuracy, feed_dict={self.x_: x, self.y_: y})
            print(accuracy)****

下面是报错的结果

tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file ./checkpoint_dir/model.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
     [[{{node save_1/RestoreV2}}]]

DataLossError (see above for traceback): Unable to open table file ./checkpoint_dir/model.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
     [[node save_1/RestoreV2 (defined at ~/PycharmProjects/pythonProject1/test.py:67) ]]

展开全部

  • 写回答

1条回答 默认 最新

  • 海洋 之心 2022年度博客之星人工智能领域TOP 1 2022-08-01 04:20
    关注

    Set up your data format vector and pass it into the Model for inference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月1日
  • 已采纳回答 8月1日
  • 创建了问题 7月31日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部