|
| 1 | +""" |
| 2 | +Code based loosely on implementation: |
| 3 | +https://github.com/openai/baselines/blob/master/baselines/ppo2/policies.py |
| 4 | +
|
| 5 | +Under MIT license. |
| 6 | +""" |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +import torch.nn as nn |
| 10 | +import torch.nn.init as init |
| 11 | +import torch.nn.functional as F |
| 12 | + |
| 13 | +import vel.util.network as net_util |
| 14 | + |
| 15 | +from vel.api import LinearBackboneModel, ModelFactory |
| 16 | +from vel.rl.modules.noisy_linear import NoisyLinear |
| 17 | + |
| 18 | + |
| 19 | +class DoubleNoisyNatureCnn(LinearBackboneModel): |
| 20 | + """ |
| 21 | + Neural network as defined in the paper 'Human-level control through deep reinforcement learning' |
| 22 | + but with two separate heads and "noisy" linear layer. |
| 23 | + """ |
| 24 | + def __init__(self, input_width, input_height, input_channels, output_dim=512, initial_std_dev=0.4, |
| 25 | + factorized_noise=True): |
| 26 | + super().__init__() |
| 27 | + |
| 28 | + self._output_dim = output_dim |
| 29 | + |
| 30 | + self.conv1 = nn.Conv2d( |
| 31 | + in_channels=input_channels, |
| 32 | + out_channels=32, |
| 33 | + kernel_size=(8, 8), |
| 34 | + stride=4 |
| 35 | + ) |
| 36 | + |
| 37 | + self.conv2 = nn.Conv2d( |
| 38 | + in_channels=32, |
| 39 | + out_channels=64, |
| 40 | + kernel_size=(4, 4), |
| 41 | + stride=2 |
| 42 | + ) |
| 43 | + |
| 44 | + self.conv3 = nn.Conv2d( |
| 45 | + in_channels=64, |
| 46 | + out_channels=64, |
| 47 | + kernel_size=(3, 3), |
| 48 | + stride=1 |
| 49 | + ) |
| 50 | + |
| 51 | + self.final_width = net_util.convolutional_layer_series(input_width, [ |
| 52 | + (8, 0, 4), |
| 53 | + (4, 0, 2), |
| 54 | + (3, 0, 1) |
| 55 | + ]) |
| 56 | + |
| 57 | + self.final_height = net_util.convolutional_layer_series(input_height, [ |
| 58 | + (8, 0, 4), |
| 59 | + (4, 0, 2), |
| 60 | + (3, 0, 1) |
| 61 | + ]) |
| 62 | + |
| 63 | + self.linear_layer_one = NoisyLinear( |
| 64 | + # 64 is the number of channels of the last conv layer |
| 65 | + self.final_width * self.final_height * 64, |
| 66 | + self.output_dim, |
| 67 | + initial_std_dev=initial_std_dev, |
| 68 | + factorized_noise=factorized_noise |
| 69 | + ) |
| 70 | + |
| 71 | + self.linear_layer_two = NoisyLinear( |
| 72 | + # 64 is the number of channels of the last conv layer |
| 73 | + self.final_width * self.final_height * 64, |
| 74 | + self.output_dim, |
| 75 | + initial_std_dev=initial_std_dev, |
| 76 | + factorized_noise=factorized_noise |
| 77 | + ) |
| 78 | + |
| 79 | + @property |
| 80 | + def output_dim(self) -> int: |
| 81 | + """ Final dimension of model output """ |
| 82 | + return self._output_dim |
| 83 | + |
| 84 | + def reset_weights(self): |
| 85 | + for m in self.modules(): |
| 86 | + if isinstance(m, nn.Conv2d): |
| 87 | + # init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') |
| 88 | + init.orthogonal_(m.weight, gain=np.sqrt(2)) |
| 89 | + init.constant_(m.bias, 0.0) |
| 90 | + elif isinstance(m, nn.Linear): |
| 91 | + # init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') |
| 92 | + init.orthogonal_(m.weight, gain=np.sqrt(2)) |
| 93 | + init.constant_(m.bias, 0.0) |
| 94 | + elif isinstance(m, NoisyLinear): |
| 95 | + m.reset_weights() |
| 96 | + |
| 97 | + def forward(self, image): |
| 98 | + result = image |
| 99 | + result = F.relu(self.conv1(result)) |
| 100 | + result = F.relu(self.conv2(result)) |
| 101 | + result = F.relu(self.conv3(result)) |
| 102 | + flattened = result.view(result.size(0), -1) |
| 103 | + |
| 104 | + output_one = F.relu(self.linear_layer_one(flattened)) |
| 105 | + output_two = F.relu(self.linear_layer_two(flattened)) |
| 106 | + |
| 107 | + return output_one, output_two |
| 108 | + |
| 109 | + |
| 110 | +def create(input_width, input_height, input_channels=1, output_dim=512, initial_std_dev=0.4, factorized_noise=True): |
| 111 | + """ Vel factory function """ |
| 112 | + def instantiate(**_): |
| 113 | + return DoubleNoisyNatureCnn( |
| 114 | + input_width=input_width, input_height=input_height, input_channels=input_channels, |
| 115 | + output_dim=output_dim, initial_std_dev=initial_std_dev, factorized_noise=factorized_noise |
| 116 | + ) |
| 117 | + |
| 118 | + return ModelFactory.generic(instantiate) |
| 119 | + |
| 120 | + |
| 121 | +DoubleNoisyNatureCnnFactory = create |
0 commit comments