把tensor转为numpy_python – 将Tensor转换为Numpy数组 – 自定义丢失函数在keras中

这篇博客介绍了如何在Keras中将Tensor转换为Numpy数组,特别是在定义自定义损失函数时。文章通过示例展示了如何使用Keras的后端函数,如`K.tf.round`和`K.tf.boolean_mask`,来处理张量,并给出了替换Numpy操作的建议,如使用`tf.confusion_matrix`代替Numpy的混淆矩阵计算。
摘要由CSDN通过智能技术生成

编辑:1这是一个未经测试的替代:

(冒充了变量名称的规范化)

def get_balance(x_true, x_pred):

x_true = K.tf.round(x_true)

x_pred = K.tf.round(x_pred)

# didnt see the need for these

# NumberOfBars = (x_true)

# NumberOfHours = NumberOfBars/60

trade_index = K.tf.not_equal(x_pred[:,1], 0 )

##remove predictions that are not tradable

x_true_tradeable = K.tf.boolean_mask(x_true[:,0], trade_index)

x_pred_tradeable = K.tf.boolean_mask(x_pred[:,0], trade_index)

cm = K.tf.confusion_matrix(x_true_tradeable, x_pred_tradeable)

correct_predictions = cm[0,0]+cm[1,1]

wrong_predictions = cm[1,0]+cm[0,1]

total_trades = correction_predictions + wrong_predictions

accuracy = (correct_predictions/total_trades)*100

return accuracy

原始答案

欢迎来到SO.您可能知道我们需要计算损失函数的梯度.我们

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值