简单层的实现

class MulLayer:
    def __init__(self):
        self.x = None
        self.y = None
        
    def forward(self,x,y):
        self.x = x
        self.y = y
        out = x*y
        return out
        
    def backward(self,dout):
        dx = dout*self.y
        dy = dout*self.x
        
        return dx,dy
    
class AddLayer:
    def __init__(self):
        pass
        
    def forward(self,x,y):
        out = x+y
        return out
        
    def backward(self,dout):
        dx = dout*1
        dy = dout*1
        
        return dx,dy
    
    
apple = 100
apple_num = 2
tax = 1.1
 
apple_mul_layer = MulLayer()
tax_num_layer = MulLayer()


apple_price = apple_mul_layer.forward(apple,apple_num)
price = tax_num_layer.forward(apple_price,tax)

print(apple_price)
print(price)#最后的总价价

dprice = 1
dapple_price, dtax = tax_num_layer.backward(dprice)
dapple, dapple_num = apple_mul_layer.backward(dapple_price)

print(dapple, dapple_num,dtax)#每一项增加1,price最后会增加多少

orange = 150
orange_num = 3

#Layer
add_layer = AddLayer()
orange_mul_layer = MulLayer()

#forward
orange_price = orange_mul_layer.forward(orange,orange_num)
all_price = add_layer.forward(apple_price,orange_price)
price = tax_num_layer.forward(all_price,tax)

#backward
dprice = 1
dall_price, dtax = tax_num_layer.backward(dprice)
dapple, dorange_price = add_layer.backward(dall_price)
dorange, dorange_num = orange_mul_layer.backward(dorange_price)
dapple, dapple_num = apple_mul_layer.backward(dapple_price)
 
#每一项增加1,price最后会增加多少
print(dapple,dorange,dapple_num,dorange_num,dtax/100)

输出

200
220.00000000000003
2.2 110.00000000000001 200
2.2 3.3000000000000003 110.00000000000001 165.0 6.5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 的实现主要分为三层:数据访问层(Dao)、映射层(Mapper)和数据源层(DataSource)。 1. 数据访问层(Dao)的实现方式: 传统的 Dao 层开发方式需要手动编写 SQL 语句,然后通过 JDBC 进行数据库操作。这种方式需要大量的重复代码,而且容易出错。MyBatis 提供了一种更加简单的方式,即使用 MyBatis 的 API 进行数据库操作。这种方式需要编写 Dao 接口和映射文件,然后通过 SqlSession 进行数据库操作。 2. 映射层(Mapper)的实现方式: MyBatis 的映射层主要是通过 XML 文件进行配置的。在 XML 文件中,我们可以定义 SQL 语句、参数映射和结果映射等信息。MyBatis 还提供了注解方式进行映射,这种方式可以减少 XML 文件的编写。 3. 数据源层(DataSource)的实现方式: MyBatis 支持多种数据源,包括 JDBC、C3P0、DBCP 和 JNDI 等。我们可以通过配置文件或者代码的方式进行数据源的配置。 下面是一个使用 MyBatis 进行数据库操作的例子: ```java // 定义 Dao 接口 public interface UserMapper { // 根据用户 ID 查询用户信息 User getUserById(int id); } // 定义映射文件 UserMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.dao.UserMapper"> <select id="getUserById" resultType="com.example.entity.User"> select * from user where id = #{id} </select> </mapper> // 使用 SqlSession 进行数据库操作 SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUserById(1); sqlSession.close(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值