mini batch,cross entorpy error

import numpy as np

# cross entropy error mini batch, if t is one hot
def mini_batch_cee(y, t):
    batch_size = y.shape[0]
    return -np.sum(t * np.log(y + 1e-7))/batch_size


# cross entropy error mini batch, if t is not one hot, is lable
def mini_batch_cee_sec(y, t):
    if y.ndim == 1:  # 1维转变为多维
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)

    batch_size = y.shape[0]
    # np.arange(batch_size) generate [0,1,2,...]
    # y[np.arange(batch_size), t] generate [y[0,0],y[0,1]...]
    return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7))/batch_size


# mini batch 
def mini_batch(y, t):
    y_size = y.shape[0]
    batch_size = 3
    batch_mask = np.random.choice(y_size, batch_size)  # 返回下标
    y_batch = y[batch_mask]
    t_batch = t[batch_mask]
    return y_batch, t_batch


y = np.array([0.1, 0.2, 0.6, 0.1])  # 模拟神经网络输出
t = np.array([0, 0, 1, 0])  # one-hot表示
t_1 = np.array([0, 1, 2, 3])  # lable

y_batch, t_batch = mini_batch(y, t)
print("batch select", y_batch, t_batch)

mini_batch_res = mini_batch_cee(y, t)
print("mini batch one hot", mini_batch_res)


mini_batch_res = mini_batch_cee_sec(y, t_1)
print("mini batch lable", mini_batch_res)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值