tensorflow - 如何共享权重以“重用”现有网络
问题描述
介绍
我正在使用下图所示的价值网络 变量X指的是当前状态,Y是下一个时间步的状态,即未来状态。
解释
应该“使用两次”的是“状态值函数”,因为它是相同的函数 V,它使用两个不同的参数 X 和 Y 进行评估。网络本身是一个卷积神经网络,我已经实现了它。我相信我已经为奖励网络 r(X) 正确实施了所有必要的层。
价值网络 V(X) 使用相同的卷积层,所以我认为原则上它是正确的,除了从 V(X) 到 V(Y) 的权重共享。目前我有两个不同的价值网络,一个用于 X 状态,另一个用于 Y 状态,我知道这是错误的,因为网络应该代表完全相同的功能。
问题
谁能告诉我这样做的正确/最佳方法是什么?
解决方案
如果您使用的是 PyTorch、Keras 或 TensorFlow,您可以将输入作为一个批次堆叠并通过单个网络传递。
PyTorch 中的示例:
import torch
import torch.nn as nn
x = torch.rand(1,3,64,64) # input x
y = torch.rand(1,3,64,64) # input y
cnn = nn.Conv2d(3,10,kernel_size=5) # network
input = torch.cat((x,y),dim=0) # stack them batch-wise
output = cnn(input) # pass both x and y through the network as a single pass
x_out = output[0] # get the output of x
y_out = output[1] # get the output of y
你可以在 TensorFlow 和 Keras 中做类似的事情。
推荐阅读
- nginx - 如何为端到端 tls 连接启用 nginx 入口
- javascript - 在 Javascript 中使用 fetch() 进行 HTTPS 身份验证
- sql - SQL 根据其他列中的 ID 和按日期分组填充列值
- javascript - 引导弹出窗口的引导轮播问题
- mysql - 如何在没有'id'列的MySql中获取最后插入的记录?
- formik - 是的:如何验证两个相互依赖的日期?
- javascript - 如何在 React 中使用 HOC 传递静态 navigationOptions?
- html - 无法在 Asp.net core web api 中保存 html 代码
- xml - strings.xml:这有什么不正确的?
- python-3.x - 使用线程时如何解决winerror32?