gym-examples 3.0.373__py3-none-any.whl → 3.0.375__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gym_examples/__init__.py +1 -1
- gym_examples/envs/wsn_env.py +22 -5
- {gym_examples-3.0.373.dist-info → gym_examples-3.0.375.dist-info}/METADATA +1 -1
- gym_examples-3.0.375.dist-info/RECORD +7 -0
- gym_examples-3.0.373.dist-info/RECORD +0 -7
- {gym_examples-3.0.373.dist-info → gym_examples-3.0.375.dist-info}/WHEEL +0 -0
- {gym_examples-3.0.373.dist-info → gym_examples-3.0.375.dist-info}/top_level.txt +0 -0
gym_examples/__init__.py
CHANGED
gym_examples/envs/wsn_env.py
CHANGED
@@ -29,15 +29,33 @@ base_back_up_dir = "results/data/"
|
|
29
29
|
max_reward = 1 # maximum reward value when the sensors sent data to the base station. The opposite value is when the sensors perform an unauthorized action
|
30
30
|
|
31
31
|
# Define the final reward function using an attention mechanism
|
32
|
+
class CustomizedLinear(nn.Module):
|
33
|
+
def __init__(self, input_dim, output_dim):
|
34
|
+
super(CustomizedLinear, self).__init__()
|
35
|
+
self.weight = nn.Parameter(torch.rand(output_dim, input_dim))
|
36
|
+
self.bias = None # No bias term
|
37
|
+
self.Softplus = nn.Softplus() # SoftPlus activation function to ensure non-negative values: Check the paper for more details
|
38
|
+
|
39
|
+
def forward(self, x):
|
40
|
+
# Enforce non-negativity of weights
|
41
|
+
weight = self.Softplus(self.weight)
|
42
|
+
|
43
|
+
# Normalize cols to ensure that if sum(x1) < sum(x2) ==> sum(Ax1 + 0) < sum(Ax2 + 0): proof in the paper
|
44
|
+
col_sums = weight.sum(dim=2, keepdim=True)
|
45
|
+
normalized_weight = weight / col_sums
|
46
|
+
|
47
|
+
# Output
|
48
|
+
y = torch.matmul(x, normalized_weight.t())
|
49
|
+
return y
|
50
|
+
|
32
51
|
class Attention(nn.Module):
|
33
52
|
def __init__(self, input_dim, output_dim):
|
34
53
|
super(Attention, self).__init__() # Call the initializer of the parent class (nn.Module)
|
35
54
|
self.input_dim = input_dim # Set the input dimension of the network
|
36
55
|
self.output_dim = output_dim # Set the output dimension of the network
|
37
|
-
self.linear1 =
|
38
|
-
self.linear2 =
|
39
|
-
|
40
|
-
|
56
|
+
self.linear1 = CustomizedLinear(input_dim, 64) # Define the first linear layer. It takes input of size 'input_dim' and outputs size '64'
|
57
|
+
self.linear2 = CustomizedLinear(64, output_dim) # Define the second linear layer. It takes input of size '64' and outputs size 'output_dim'
|
58
|
+
|
41
59
|
def forward(self, x):
|
42
60
|
# Step 1: Ensure input is 2D by adding a batch dimension if necessary
|
43
61
|
if x.dim() == 1:
|
@@ -46,7 +64,6 @@ class Attention(nn.Module):
|
|
46
64
|
attention_weights = F.softmax(x, dim=1) # Apply the softmax function to get the attention weights. Initially F.softmax(x, dim=0)
|
47
65
|
x = attention_weights * x # Multiply the input by the attention weights
|
48
66
|
x = self.linear2(x) # Pass the result through another linear layer
|
49
|
-
x = self.softplus(x) # Apply SoftPlus activation to ensure non-negative outputs
|
50
67
|
return x
|
51
68
|
|
52
69
|
net = Attention(input_dim, output_dim)
|
@@ -0,0 +1,7 @@
|
|
1
|
+
gym_examples/__init__.py,sha256=C7meJUV9sey9-_84Jxb3bPGbDQ361em2wo2l7F7G3Rk,166
|
2
|
+
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
+
gym_examples/envs/wsn_env.py,sha256=ses61kfzo_yBZCIfSvcEK7OEcqJlGkZsDhOmOtibj5Q,26756
|
4
|
+
gym_examples-3.0.375.dist-info/METADATA,sha256=VSydBLIPzs1fBy_MJTARSUOxCk90OmqWt-Bk1IoXpUs,412
|
5
|
+
gym_examples-3.0.375.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
+
gym_examples-3.0.375.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
+
gym_examples-3.0.375.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
gym_examples/__init__.py,sha256=31xV_jnS1HiU-dRx91orWPtAmCqejSFkb9iQuf1xJZc,166
|
2
|
-
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
-
gym_examples/envs/wsn_env.py,sha256=2NUSYwbm1Ygaff5T7qzehDZp7S-DYMYPcvXF6sQahFA,26116
|
4
|
-
gym_examples-3.0.373.dist-info/METADATA,sha256=9dVN56I6njmfur03MNTfpMrNQ1nrSjMCXgYv_xJrp-c,412
|
5
|
-
gym_examples-3.0.373.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
-
gym_examples-3.0.373.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
-
gym_examples-3.0.373.dist-info/RECORD,,
|
File without changes
|
File without changes
|