tensorflow慢慢积累的小知识点

本文探讨了TensorFlow中tf.cast(), tf.where()等函数的高级应用,包括条件转换和矩阵元素选择。同时,深入讲解了如何利用tf.train.LoggingTensorHook进行模型训练状态的监控,并在TPUEstimatorSpec中配置评估hook。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. tf.cast()可附加条件,例如:

    #tensor大于0的元素的位置返回True,其余返回False(在dtype为bool的情况下)
    #其他情况同理
    tf.cast(tensor>0,dtype)
  2. tf.where

    #在predict张量和tartget张量元素在0.5两边时
    #theta矩阵响应元素置1,否则置0
    x=tf.ones_like(log_yi, dtype=tf.float32)
    y=tf.zeros_like(log_yi, dtype=tf.float32)
    condition = ((target - 0.5) * (predict - 0.5)<0.)
    theta = tf.where(condition,x,y)

     

  3. evaluation_hook可以使用tf.train里面的hook
    evaluation_hook = tf.train.LoggingTensorHook({"total_loss:": total_loss}, every_n_iter=10)
    output_spec = tf.contrib.tpu.TPUEstimatorSpec(
                    mode=mode,
                    loss=total_loss,
                    eval_metrics=eval_metrics,
                    scaffold_fn=scaffold_fn,
                    evaluation_hooks=[evaluation_hook])        
    #12345是传入的seed,用来选定第一次random的开始点
    random.Random(12345)
    rng.shuffle(train_examples)

     

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值