weixin_51793354 2022-08-01 00:41 采纳率: 54.5%
浏览 114
已结题

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 12:20
    关注

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

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月1日
  • 已采纳回答 8月1日
  • 创建了问题 8月1日

悬赏问题

  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?