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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""magnet 3.0
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1OEi6S1t2F49Lh-JfMGJr3aUBsYhjHtJC
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
import torch.optim as optim
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
|
| 15 |
+
# Define grid size
|
| 16 |
+
grid_size = 20
|
| 17 |
+
|
| 18 |
+
# Create a grid with random initial wealth data
|
| 19 |
+
wealth_data = torch.rand((grid_size, grid_size))
|
| 20 |
+
|
| 21 |
+
# Define a simple neural network that will adjust the wealth data
|
| 22 |
+
class WealthNet(nn.Module):
|
| 23 |
+
def __init__(self):
|
| 24 |
+
super(WealthNet, self).__init__()
|
| 25 |
+
self.fc1 = nn.Linear(grid_size * grid_size, 128)
|
| 26 |
+
self.fc2 = nn.Linear(128, grid_size * grid_size)
|
| 27 |
+
|
| 28 |
+
def forward(self, x):
|
| 29 |
+
x = torch.relu(self.fc1(x))
|
| 30 |
+
x = self.fc2(x)
|
| 31 |
+
return x
|
| 32 |
+
|
| 33 |
+
# Instantiate the network, loss function, and optimizer
|
| 34 |
+
net = WealthNet()
|
| 35 |
+
criterion = nn.MSELoss()
|
| 36 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 37 |
+
|
| 38 |
+
# Target direction to direct wealth (e.g., bottom right corner)
|
| 39 |
+
target_wealth = torch.zeros((grid_size, grid_size))
|
| 40 |
+
target_wealth[-5:, -5:] = 1 # Direct wealth towards the bottom right corner
|
| 41 |
+
|
| 42 |
+
# Convert the grid to a single vector for the neural network
|
| 43 |
+
input_data = wealth_data.view(-1)
|
| 44 |
+
target_data = target_wealth.view(-1)
|
| 45 |
+
|
| 46 |
+
# Training the network
|
| 47 |
+
epochs = 500
|
| 48 |
+
for epoch in range(epochs):
|
| 49 |
+
optimizer.zero_grad()
|
| 50 |
+
output = net(input_data)
|
| 51 |
+
loss = criterion(output, target_data)
|
| 52 |
+
loss.backward()
|
| 53 |
+
optimizer.step()
|
| 54 |
+
|
| 55 |
+
# Reshape the output to the grid size
|
| 56 |
+
output_grid = output.detach().view(grid_size, grid_size)
|
| 57 |
+
|
| 58 |
+
# Plot the original and adjusted wealth distribution
|
| 59 |
+
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
|
| 60 |
+
axes[0].imshow(wealth_data, cmap='viridis')
|
| 61 |
+
axes[0].set_title('Original Wealth Distribution')
|
| 62 |
+
axes[1].imshow(output_grid, cmap='viridis')
|
| 63 |
+
axes[1].set_title('Directed Wealth Distribution')
|
| 64 |
+
plt.show()
|
| 65 |
+
|
| 66 |
+
import torch
|
| 67 |
+
import torch.nn as nn
|
| 68 |
+
import torch.optim as optim
|
| 69 |
+
import matplotlib.pyplot as plt
|
| 70 |
+
|
| 71 |
+
# Define grid size
|
| 72 |
+
grid_size = 20
|
| 73 |
+
|
| 74 |
+
# Create a grid with random initial wealth data
|
| 75 |
+
wealth_data = torch.rand((grid_size, grid_size))
|
| 76 |
+
|
| 77 |
+
# Define a neural network with an additional layer for infrared conversion
|
| 78 |
+
class WealthNet(nn.Module):
|
| 79 |
+
def __init__(self):
|
| 80 |
+
super(WealthNet, self).__init__()
|
| 81 |
+
self.fc1 = nn.Linear(grid_size * grid_size, 128)
|
| 82 |
+
self.fc2 = nn.Linear(128, 128)
|
| 83 |
+
self.fc3 = nn.Linear(128, grid_size * grid_size)
|
| 84 |
+
self.infrared_layer = nn.Sigmoid() # Simulating the conversion to infrared energy
|
| 85 |
+
|
| 86 |
+
def forward(self, x):
|
| 87 |
+
x = torch.relu(self.fc1(x))
|
| 88 |
+
stored_wealth = torch.relu(self.fc2(x)) # Store wealth data here
|
| 89 |
+
infrared_energy = self.infrared_layer(stored_wealth) # Convert to infrared energy
|
| 90 |
+
x = self.fc3(infrared_energy)
|
| 91 |
+
return x, stored_wealth, infrared_energy
|
| 92 |
+
|
| 93 |
+
# Instantiate the network, loss function, and optimizer
|
| 94 |
+
net = WealthNet()
|
| 95 |
+
criterion = nn.MSELoss()
|
| 96 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 97 |
+
|
| 98 |
+
# Target direction to direct wealth (e.g., bottom right corner)
|
| 99 |
+
target_wealth = torch.zeros((grid_size, grid_size))
|
| 100 |
+
target_wealth[-5:, -5:] = 1 # Direct wealth towards the bottom right corner
|
| 101 |
+
|
| 102 |
+
# Convert the grid to a single vector for the neural network
|
| 103 |
+
input_data = wealth_data.view(-1)
|
| 104 |
+
target_data = target_wealth.view(-1)
|
| 105 |
+
|
| 106 |
+
# Training the network
|
| 107 |
+
epochs = 500
|
| 108 |
+
for epoch in range(epochs):
|
| 109 |
+
optimizer.zero_grad()
|
| 110 |
+
output, stored_wealth, infrared_energy = net(input_data)
|
| 111 |
+
loss = criterion(output, target_data)
|
| 112 |
+
loss.backward()
|
| 113 |
+
optimizer.step()
|
| 114 |
+
|
| 115 |
+
# Reshape the outputs to the grid size
|
| 116 |
+
output_grid = output.detach().view(grid_size, grid_size)
|
| 117 |
+
stored_wealth_grid = stored_wealth.detach().view(128) # Displayed as a 1D representation
|
| 118 |
+
infrared_energy_grid = infrared_energy.detach().view(128) # Displayed as a 1D representation
|
| 119 |
+
|
| 120 |
+
# Plot the original and adjusted wealth distribution
|
| 121 |
+
fig, axes = plt.subplots(1, 4, figsize=(20, 6))
|
| 122 |
+
axes[0].imshow(wealth_data, cmap='viridis')
|
| 123 |
+
axes[0].set_title('Original Wealth Distribution')
|
| 124 |
+
axes[1].imshow(output_grid, cmap='viridis')
|
| 125 |
+
axes[1].set_title('Directed Wealth Distribution')
|
| 126 |
+
axes[2].plot(stored_wealth_grid.numpy())
|
| 127 |
+
axes[2].set_title('Stored Wealth Data (1D)')
|
| 128 |
+
axes[3].plot(infrared_energy_grid.numpy())
|
| 129 |
+
axes[3].set_title('Infrared Energy (1D)')
|
| 130 |
+
plt.show()
|
| 131 |
+
|
| 132 |
+
import torch
|
| 133 |
+
import torch.nn as nn
|
| 134 |
+
import torch.optim as optim
|
| 135 |
+
import matplotlib.pyplot as plt
|
| 136 |
+
|
| 137 |
+
# Define grid size
|
| 138 |
+
grid_size = 20
|
| 139 |
+
|
| 140 |
+
# Create a grid with random initial wealth data
|
| 141 |
+
wealth_data = torch.rand((grid_size, grid_size))
|
| 142 |
+
|
| 143 |
+
# Define a neural network with an additional layer for data protection
|
| 144 |
+
class WealthNet(nn.Module):
|
| 145 |
+
def __init__(self):
|
| 146 |
+
super(WealthNet, self).__init__()
|
| 147 |
+
self.fc1 = nn.Linear(grid_size * grid_size, 128)
|
| 148 |
+
self.fc2 = nn.Linear(128, 128)
|
| 149 |
+
self.fc3 = nn.Linear(128, grid_size * grid_size)
|
| 150 |
+
self.infrared_layer = nn.Sigmoid() # Simulating the conversion to infrared energy
|
| 151 |
+
# Removed the incorrect instantiation of GaussianNoise here
|
| 152 |
+
|
| 153 |
+
def forward(self, x):
|
| 154 |
+
x = torch.relu(self.fc1(x))
|
| 155 |
+
stored_wealth = torch.relu(self.fc2(x)) # Store wealth data here
|
| 156 |
+
protected_wealth = self.protection_layer(stored_wealth) # Protect the stored data
|
| 157 |
+
infrared_energy = self.infrared_layer(protected_wealth) # Convert to infrared energy
|
| 158 |
+
x = self.fc3(infrared_energy)
|
| 159 |
+
return x, stored_wealth, protected_wealth, infrared_energy
|
| 160 |
+
|
| 161 |
+
# Custom layer to add Gaussian noise (PyTorch does not have this built-in)
|
| 162 |
+
class GaussianNoise(nn.Module):
|
| 163 |
+
def __init__(self, stddev):
|
| 164 |
+
super(GaussianNoise, self).__init__()
|
| 165 |
+
self.stddev = stddev
|
| 166 |
+
|
| 167 |
+
def forward(self, x):
|
| 168 |
+
if self.training:
|
| 169 |
+
noise = torch.randn_like(x) * self.stddev
|
| 170 |
+
return x + noise
|
| 171 |
+
return x
|
| 172 |
+
|
| 173 |
+
# Instantiate the network, loss function, and optimizer
|
| 174 |
+
net = WealthNet()
|
| 175 |
+
# Add the GaussianNoise layer to the network instance
|
| 176 |
+
net.protection_layer = GaussianNoise(0.1)
|
| 177 |
+
criterion = nn.MSELoss()
|
| 178 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 179 |
+
|
| 180 |
+
# Target direction to direct wealth (e.g., bottom right corner)
|
| 181 |
+
target_wealth = torch.zeros((grid_size, grid_size))
|
| 182 |
+
target_wealth[-5:, -5:] = 1 # Direct wealth towards the bottom right corner
|
| 183 |
+
|
| 184 |
+
# Convert the grid to a single vector for the neural network
|
| 185 |
+
input_data = wealth_data.view(-1)
|
| 186 |
+
target_data = target_wealth.view(-1)
|
| 187 |
+
|
| 188 |
+
# Training the network
|
| 189 |
+
epochs = 500
|
| 190 |
+
for epoch in range(epochs):
|
| 191 |
+
optimizer.zero_grad()
|
| 192 |
+
output, stored_wealth, protected_wealth, infrared_energy = net(input_data)
|
| 193 |
+
loss = criterion(output, target_data)
|
| 194 |
+
loss.backward()
|
| 195 |
+
optimizer.step()
|
| 196 |
+
|
| 197 |
+
# Reshape the outputs to the grid size
|
| 198 |
+
output_grid = output.detach().view(grid_size, grid_size)
|
| 199 |
+
stored_wealth_grid = stored_wealth.detach().view(128) # Displayed as a 1D representation
|
| 200 |
+
protected_wealth_grid = protected_wealth.detach().view(128) # Displayed as a 1D representation
|
| 201 |
+
infrared_energy_grid = infrared_energy.detach().view(128) # Displayed as a 1D representation
|
| 202 |
+
|
| 203 |
+
# Plot the original and adjusted wealth distribution
|
| 204 |
+
fig, axes = plt.subplots(1, 5, figsize=(25, 6))
|
| 205 |
+
axes[0].imshow(wealth_data, cmap='viridis')
|
| 206 |
+
axes[0].set_title('Original')
|
| 207 |
+
axes[1].imshow(output_grid, cmap='viridis')
|
| 208 |
+
axes[1].set_title('Directed')
|
| 209 |
+
axes[2].plot(stored_wealth_grid.numpy())
|
| 210 |
+
axes[2].set_title('Stored')
|
| 211 |
+
axes[3].plot(protected_wealth_grid.numpy())
|
| 212 |
+
axes[3].set_title('Protected')
|
| 213 |
+
axes[4].plot(infrared_energy_grid)
|