首页 > 解决方案 > 如何共享权重以“重用”现有网络

问题描述

介绍

我正在使用下图所示的价值网络 变量X指的是当前状态,Y是下一个时间步的状态,即未来状态。

解释

应该“使用两次”的是“状态值函数”,因为它是相同的函数 V,它使用两个不同的参数 X 和 Y 进行评估。网络本身是一个卷积神经网络,我已经实现了它。我相信我已经为奖励网络 r(X) 正确实施了所有必要的层。

价值网络 V(X) 使用相同的卷积层,所以我认为原则上它是正确的,除了从 V(X) 到 V(Y) 的权重共享。目前我有两个不同的价值网络,一个用于 X 状态,另一个用于 Y 状态,我知道这是错误的,因为网络应该代表完全相同的功能。

问题

谁能告诉我这样做的正确/最佳方法是什么?

在此处输入图像描述

标签: tensorflowreinforcement-learningtf.keras

解决方案


如果您使用的是 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 中做类似的事情。


推荐阅读