tensorflow学习笔记

1.TensorFlow打印tensor值

https://www.jb51.net/article/141987.htm

https://blog.csdn.net/nageaixiaodenanhai/article/details/81098183

2.dataset使用

https://blog.csdn.net/qq_16234613/article/details/81703228

3.tensorflow分批读取tfrecords数据

https://blog.csdn.net/OliverChrist/article/details/81513444

4.tfrecords存储及读取

https://blog.csdn.net/chengshuhao1991/article/details/78656724

5.tensorflow读取模型预测数据

网络结构输入:
        self.input_x = tf.placeholder(tf.int32, [None, self.config.seq_length], name='input_x')
        self.input_y = tf.placeholder(tf.float32, [None, self.config.num_classes], name='input_y')
        self.keep_prob = tf.placeholder(tf.float32, name='keep_prob')
            self.y_pred_cls = tf.argmax(tf.nn.softmax(self.logits), 1,name = "pred")  # 预测类别
        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(tf.argmax(self.input_y, 1), self.y_pred_cls,name = "correct_pred")
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32),name = "acc")
def batch_iter(x, y, batch_size=64):
    """生成批次数据"""
    data_len = len(x)
    num_batch = int((data_len - 1) / batch_size) + 1

    for i in range(num_batch):
        start_id = i * batch_size
        end_id = min((i + 1) * batch_size, data_len)
        yield x[start_id:end_id], y[start_id:end_id]

def feed_data(x_batch, y_batch, keep_prob):
    feed_dict = {
        'input_x': x_batch,
        'input_y': y_batch,
        'keep_prob': keep_prob
    }
    return feed_dict


def test():
    start_time = time.time()
    with tf.Session() as session:
        saver=tf.train.import_meta_graph('test.meta')
        graph=tf.get_default_graph()
        tesor_name_list=[tensor.name for tensor in graph.as_graph_def().node] # 变量名
#        print(tesor_name_list)
        module_file = tf.train.latest_checkpoint("test")   # .meta 所在的文件夹名称
        saver.restore(session, module_file)
        input_x = graph.get_tensor_by_name('input_x:0')
        input_y = graph.get_tensor_by_name('input_y:0')
        keep_prob = graph.get_tensor_by_name("keep_prob:0")
        accuracy = graph.get_tensor_by_name("accuracy/acc:0")
        outputs = graph.get_tensor_by_name("score/pred:0")
        data_len = len(valid_x)
        batch_eval = batch_iter(valid_x, onehot_valid_y, 500)
        total_loss = 0.0
        total_acc = 0.0
        y_test_cls = np.argmax(onehot_valid_y, 1)
        y_pred_cls = []  # 保存预测结果
        for x_batch, y_batch in batch_eval:
            batch_len = len(x_batch)
            feed_dict = feed_data(x_batch, y_batch, 1.0)
            acc = session.run(accuracy, feed_dict={input_x:x_batch,input_y:y_batch,keep_prob:1.0})
            total_acc += acc * batch_len
            y_pred_cls.extend(session.run(outputs,feed_dict={input_x:x_batch,keep_prob:1.0}))
            # 混淆矩阵
        print("Confusion Matrix...")
        cm = metrics.confusion_matrix(y_test_cls, y_pred_cls)
        print(cm)
        print("Precision, Recall and F1-Score...")
        print(metrics.classification_report(y_test_cls, y_pred_cls))
        
        print(total_acc / data_len)
#        print(session.run(accuracy,feed_dict={input_x:valid_x[0:500,:],input_y:onehot_valid_y[0:500,:],keep_prob:1.0}))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值