Upload 2 files
Browse files- wealthwavetransfer_2_0.ipynb +0 -0
- wealthwavetransfer_2_0.py +538 -0
wealthwavetransfer_2_0.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
wealthwavetransfer_2_0.py
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""wealthwavetransfer 2.0
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1qbxvR0Ivvxx9uEMROqKuwsX7WKI96gb9
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
pip install torch torchvision
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
# Generate synthetic data
|
| 16 |
+
np.random.seed(42)
|
| 17 |
+
num_samples = 1000
|
| 18 |
+
|
| 19 |
+
# Features: Age, Income, Investments
|
| 20 |
+
age = np.random.randint(18, 70, size=num_samples)
|
| 21 |
+
income = np.random.normal(50000, 15000, size=num_samples) # Average income
|
| 22 |
+
investments = np.random.normal(10000, 5000, size=num_samples) # Average investments
|
| 23 |
+
|
| 24 |
+
# Wealth target: a simple function of the features (you can modify this)
|
| 25 |
+
wealth = 0.4 * age + 0.5 * (income / 1000) + 0.3 * (investments / 1000) + np.random.normal(0, 5, size=num_samples)
|
| 26 |
+
|
| 27 |
+
# Convert to PyTorch tensors
|
| 28 |
+
X = torch.tensor(np.column_stack((age, income, investments)), dtype=torch.float32)
|
| 29 |
+
y = torch.tensor(wealth, dtype=torch.float32).view(-1, 1)
|
| 30 |
+
|
| 31 |
+
import torch.nn as nn
|
| 32 |
+
import torch.optim as optim
|
| 33 |
+
|
| 34 |
+
class WealthModel(nn.Module):
|
| 35 |
+
def __init__(self):
|
| 36 |
+
super(WealthModel, self).__init__()
|
| 37 |
+
self.fc1 = nn.Linear(3, 64) # 3 input features
|
| 38 |
+
self.fc2 = nn.Linear(64, 32)
|
| 39 |
+
self.fc3 = nn.Linear(32, 1) # Output is a single value (wealth)
|
| 40 |
+
|
| 41 |
+
def forward(self, x):
|
| 42 |
+
x = torch.relu(self.fc1(x))
|
| 43 |
+
x = torch.relu(self.fc2(x))
|
| 44 |
+
x = self.fc3(x) # No activation function on output layer for regression
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
model = WealthModel()
|
| 48 |
+
|
| 49 |
+
# Training settings
|
| 50 |
+
criterion = nn.MSELoss()
|
| 51 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 52 |
+
num_epochs = 100
|
| 53 |
+
|
| 54 |
+
# Training loop
|
| 55 |
+
for epoch in range(num_epochs):
|
| 56 |
+
model.train()
|
| 57 |
+
|
| 58 |
+
# Forward pass
|
| 59 |
+
outputs = model(X)
|
| 60 |
+
loss = criterion(outputs, y)
|
| 61 |
+
|
| 62 |
+
# Backward pass and optimization
|
| 63 |
+
optimizer.zero_grad()
|
| 64 |
+
loss.backward()
|
| 65 |
+
optimizer.step()
|
| 66 |
+
|
| 67 |
+
if (epoch+1) % 10 == 0:
|
| 68 |
+
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
|
| 69 |
+
|
| 70 |
+
model.eval()
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
predicted = model(X)
|
| 73 |
+
|
| 74 |
+
# Optionally, you can visualize or calculate performance metrics
|
| 75 |
+
import matplotlib.pyplot as plt
|
| 76 |
+
|
| 77 |
+
plt.scatter(y.numpy(), predicted.numpy(), alpha=0.5)
|
| 78 |
+
plt.xlabel('True Wealth')
|
| 79 |
+
plt.ylabel('Predicted Wealth')
|
| 80 |
+
plt.title('True vs Predicted Wealth')
|
| 81 |
+
plt.plot([y.min(), y.max()], [y.min(), y.max()], '--', color='red')
|
| 82 |
+
plt.show()
|
| 83 |
+
|
| 84 |
+
class ObfuscationLayer(nn.Module):
|
| 85 |
+
def __init__(self):
|
| 86 |
+
super(ObfuscationLayer, self).__init__()
|
| 87 |
+
|
| 88 |
+
def forward(self, x):
|
| 89 |
+
# Add noise to simulate obfuscation/encryption
|
| 90 |
+
noise = torch.normal(0, 0.1, x.size()).to(x.device) # Adjust the standard deviation for noise level
|
| 91 |
+
return x + noise
|
| 92 |
+
|
| 93 |
+
class EnhancedWealthModel(nn.Module):
|
| 94 |
+
def __init__(self):
|
| 95 |
+
super(EnhancedWealthModel, self).__init__()
|
| 96 |
+
self.obfuscation = ObfuscationLayer()
|
| 97 |
+
self.fc1 = nn.Linear(3, 128) # More units for complexity
|
| 98 |
+
self.fc2 = nn.Linear(128, 64)
|
| 99 |
+
self.fc3 = nn.Linear(64, 32)
|
| 100 |
+
self.fc4 = nn.Linear(32, 1) # Output is a single value (wealth)
|
| 101 |
+
|
| 102 |
+
def forward(self, x):
|
| 103 |
+
x = self.obfuscation(x) # Apply obfuscation
|
| 104 |
+
x = torch.relu(self.fc1(x))
|
| 105 |
+
x = torch.relu(self.fc2(x))
|
| 106 |
+
x = torch.relu(self.fc3(x))
|
| 107 |
+
x = self.fc4(x) # No activation function on output layer for regression
|
| 108 |
+
return x
|
| 109 |
+
|
| 110 |
+
model = EnhancedWealthModel()
|
| 111 |
+
|
| 112 |
+
# Training settings
|
| 113 |
+
criterion = nn.MSELoss()
|
| 114 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 115 |
+
num_epochs = 100
|
| 116 |
+
|
| 117 |
+
# Training loop
|
| 118 |
+
for epoch in range(num_epochs):
|
| 119 |
+
model.train()
|
| 120 |
+
|
| 121 |
+
# Forward pass
|
| 122 |
+
outputs = model(X)
|
| 123 |
+
loss = criterion(outputs, y)
|
| 124 |
+
|
| 125 |
+
# Backward pass and optimization
|
| 126 |
+
optimizer.zero_grad()
|
| 127 |
+
loss.backward()
|
| 128 |
+
optimizer.step()
|
| 129 |
+
|
| 130 |
+
if (epoch + 1) % 10 == 0:
|
| 131 |
+
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
|
| 132 |
+
|
| 133 |
+
model.eval()
|
| 134 |
+
with torch.no_grad():
|
| 135 |
+
predicted = model(X)
|
| 136 |
+
|
| 137 |
+
# Visualizing True vs. Predicted Wealth
|
| 138 |
+
plt.scatter(y.numpy(), predicted.numpy(), alpha=0.5)
|
| 139 |
+
plt.xlabel('True Wealth')
|
| 140 |
+
plt.ylabel('Predicted Wealth')
|
| 141 |
+
plt.title('True vs Predicted Wealth with Obfuscation Layer')
|
| 142 |
+
plt.plot([y.min(), y.max()], [y.min(), y.max()], '--', color='red')
|
| 143 |
+
plt.show()
|
| 144 |
+
|
| 145 |
+
import torch
|
| 146 |
+
import torch.nn as nn
|
| 147 |
+
import torch.optim as optim
|
| 148 |
+
import matplotlib.pyplot as plt
|
| 149 |
+
import numpy as np
|
| 150 |
+
|
| 151 |
+
# Define grid size
|
| 152 |
+
grid_size = 20
|
| 153 |
+
|
| 154 |
+
# Generate a sine waveform to represent wealth data
|
| 155 |
+
def generate_wealth_waveform(grid_size):
|
| 156 |
+
x = np.linspace(0, 2 * np.pi, grid_size)
|
| 157 |
+
wealth_waveform = np.sin(x)
|
| 158 |
+
return wealth_waveform
|
| 159 |
+
|
| 160 |
+
# Create wealth data for the grid
|
| 161 |
+
wealth_waveform = generate_wealth_waveform(grid_size)
|
| 162 |
+
wealth_data = np.tile(wealth_waveform, (grid_size, 1)) # Repeat waveform along one axis
|
| 163 |
+
|
| 164 |
+
# Convert wealth data to PyTorch tensor
|
| 165 |
+
wealth_data = torch.tensor(wealth_data, dtype=torch.float32)
|
| 166 |
+
|
| 167 |
+
# Define a simple neural network to "transfer" wealth data to a targeted account
|
| 168 |
+
class WealthTransferNet(nn.Module):
|
| 169 |
+
def __init__(self):
|
| 170 |
+
super(WealthTransferNet, self).__init__()
|
| 171 |
+
self.fc1 = nn.Linear(grid_size * grid_size, 128)
|
| 172 |
+
self.fc2 = nn.Linear(128, grid_size * grid_size)
|
| 173 |
+
|
| 174 |
+
def forward(self, x):
|
| 175 |
+
x = torch.relu(self.fc1(x))
|
| 176 |
+
x = self.fc2(x)
|
| 177 |
+
return x
|
| 178 |
+
|
| 179 |
+
# Instantiate the network, loss function, and optimizer
|
| 180 |
+
net = WealthTransferNet()
|
| 181 |
+
criterion = nn.MSELoss()
|
| 182 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 183 |
+
|
| 184 |
+
# Target account: Wealth directed to bottom-right corner of the grid
|
| 185 |
+
target_account = torch.zeros((grid_size, grid_size))
|
| 186 |
+
target_account[-5:, -5:] = 1 # Simulating the transfer to a targeted account
|
| 187 |
+
|
| 188 |
+
# Convert the grid to a single vector for the neural network
|
| 189 |
+
input_data = wealth_data.view(-1)
|
| 190 |
+
target_data = target_account.view(-1)
|
| 191 |
+
|
| 192 |
+
# Training the network
|
| 193 |
+
epochs = 500
|
| 194 |
+
for epoch in range(epochs):
|
| 195 |
+
optimizer.zero_grad()
|
| 196 |
+
output = net(input_data)
|
| 197 |
+
loss = criterion(output, target_data)
|
| 198 |
+
loss.backward()
|
| 199 |
+
optimizer.step()
|
| 200 |
+
|
| 201 |
+
# Reshape the output to the grid size
|
| 202 |
+
output_grid = output.detach().view(grid_size, grid_size)
|
| 203 |
+
|
| 204 |
+
# Plot the original wealth waveform and transferred wealth
|
| 205 |
+
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
|
| 206 |
+
axes[0].imshow(wealth_data, cmap='viridis')
|
| 207 |
+
axes[0].set_title('Original Wealth Waveform')
|
| 208 |
+
axes[1].imshow(target_account, cmap='viridis')
|
| 209 |
+
axes[1].set_title('Target Account Location')
|
| 210 |
+
axes[2].imshow(output_grid, cmap='viridis')
|
| 211 |
+
axes[2].set_title('Transferred Wealth to Target')
|
| 212 |
+
plt.show()
|
| 213 |
+
|
| 214 |
+
import torch
|
| 215 |
+
import torch.nn as nn
|
| 216 |
+
import torch.optim as optim
|
| 217 |
+
import matplotlib.pyplot as plt
|
| 218 |
+
import numpy as np
|
| 219 |
+
|
| 220 |
+
# Define the size of the waveform
|
| 221 |
+
waveform_size = 100
|
| 222 |
+
|
| 223 |
+
# Generate a sine waveform to represent wealth data
|
| 224 |
+
def generate_wealth_waveform(waveform_size):
|
| 225 |
+
x = np.linspace(0, 2 * np.pi, waveform_size)
|
| 226 |
+
wealth_waveform = np.sin(x)
|
| 227 |
+
return wealth_waveform
|
| 228 |
+
|
| 229 |
+
# Create wealth data as a single waveform
|
| 230 |
+
wealth_waveform = generate_wealth_waveform(waveform_size)
|
| 231 |
+
wealth_data = torch.tensor(wealth_waveform, dtype=torch.float32)
|
| 232 |
+
|
| 233 |
+
# Define a neural network to transfer wealth data to a targeted point in the waveform
|
| 234 |
+
class WealthTransferNet(nn.Module):
|
| 235 |
+
def __init__(self):
|
| 236 |
+
super(WealthTransferNet, self).__init__()
|
| 237 |
+
self.fc1 = nn.Linear(waveform_size, 64)
|
| 238 |
+
self.fc2 = nn.Linear(64, waveform_size)
|
| 239 |
+
|
| 240 |
+
def forward(self, x):
|
| 241 |
+
x = torch.relu(self.fc1(x))
|
| 242 |
+
x = self.fc2(x)
|
| 243 |
+
return x
|
| 244 |
+
|
| 245 |
+
# Instantiate the network, loss function, and optimizer
|
| 246 |
+
net = WealthTransferNet()
|
| 247 |
+
criterion = nn.MSELoss()
|
| 248 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 249 |
+
|
| 250 |
+
# Target account: Wealth directed to the end of the waveform (right side)
|
| 251 |
+
target_account = torch.zeros(waveform_size)
|
| 252 |
+
target_account[-10:] = 1 # Simulating the transfer to the last 10 positions
|
| 253 |
+
|
| 254 |
+
# Training the network
|
| 255 |
+
epochs = 1000
|
| 256 |
+
for epoch in range(epochs):
|
| 257 |
+
optimizer.zero_grad()
|
| 258 |
+
output = net(wealth_data)
|
| 259 |
+
loss = criterion(output, target_account)
|
| 260 |
+
loss.backward()
|
| 261 |
+
optimizer.step()
|
| 262 |
+
|
| 263 |
+
# Convert output to numpy for plotting
|
| 264 |
+
output_waveform = output.detach().numpy()
|
| 265 |
+
|
| 266 |
+
# Plot the original and transferred wealth waveform
|
| 267 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 268 |
+
ax.plot(wealth_data.numpy(), label="", linestyle="--")
|
| 269 |
+
ax.plot(target_account.numpy(), label="", linestyle=":")
|
| 270 |
+
ax.plot(output_waveform, label="")
|
| 271 |
+
ax.set_title('')
|
| 272 |
+
ax.legend()
|
| 273 |
+
plt.show()
|
| 274 |
+
|
| 275 |
+
!pip install torch torchvision
|
| 276 |
+
|
| 277 |
+
import numpy as np
|
| 278 |
+
import torch
|
| 279 |
+
|
| 280 |
+
# Generate synthetic data
|
| 281 |
+
np.random.seed(42)
|
| 282 |
+
num_samples = 1000
|
| 283 |
+
|
| 284 |
+
# Features: Age, Income, Investments
|
| 285 |
+
age = np.random.randint(18, 70, size=num_samples)
|
| 286 |
+
income = np.random.normal(50000, 15000, size=num_samples) # Average income
|
| 287 |
+
investments = np.random.normal(10000, 5000, size=num_samples) # Average investments
|
| 288 |
+
|
| 289 |
+
# Wealth target: a simple function of the features (you can modify this)
|
| 290 |
+
wealth = 0.4 * age + 0.5 * (income / 1000) + 0.3 * (investments / 1000) + np.random.normal(0, 5, size=num_samples)
|
| 291 |
+
|
| 292 |
+
# Convert to PyTorch tensors
|
| 293 |
+
X = torch.tensor(np.column_stack((age, income, investments)), dtype=torch.float32)
|
| 294 |
+
y = torch.tensor(wealth, dtype=torch.float32).view(-1, 1)
|
| 295 |
+
|
| 296 |
+
import torch.nn as nn
|
| 297 |
+
import torch.optim as optim
|
| 298 |
+
|
| 299 |
+
class WealthModel(nn.Module):
|
| 300 |
+
def __init__(self):
|
| 301 |
+
super(WealthModel, self).__init__()
|
| 302 |
+
self.fc1 = nn.Linear(3, 64) # 3 input features
|
| 303 |
+
self.fc2 = nn.Linear(64, 32)
|
| 304 |
+
self.fc3 = nn.Linear(32, 1) # Output is a single value (wealth)
|
| 305 |
+
|
| 306 |
+
def forward(self, x):
|
| 307 |
+
x = torch.relu(self.fc1(x))
|
| 308 |
+
x = torch.relu(self.fc2(x))
|
| 309 |
+
x = self.fc3(x) # No activation function on output layer for regression
|
| 310 |
+
return x
|
| 311 |
+
|
| 312 |
+
model = WealthModel()
|
| 313 |
+
|
| 314 |
+
# Training settings
|
| 315 |
+
criterion = nn.MSELoss()
|
| 316 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 317 |
+
num_epochs = 100
|
| 318 |
+
|
| 319 |
+
# Training loop
|
| 320 |
+
for epoch in range(num_epochs):
|
| 321 |
+
model.train()
|
| 322 |
+
|
| 323 |
+
# Forward pass
|
| 324 |
+
outputs = model(X)
|
| 325 |
+
loss = criterion(outputs, y)
|
| 326 |
+
|
| 327 |
+
# Backward pass and optimization
|
| 328 |
+
optimizer.zero_grad()
|
| 329 |
+
loss.backward()
|
| 330 |
+
optimizer.step()
|
| 331 |
+
|
| 332 |
+
if (epoch+1) % 10 == 0:
|
| 333 |
+
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
|
| 334 |
+
|
| 335 |
+
model.eval()
|
| 336 |
+
with torch.no_grad():
|
| 337 |
+
predicted = model(X)
|
| 338 |
+
|
| 339 |
+
# Optionally, you can visualize or calculate performance metrics
|
| 340 |
+
import matplotlib.pyplot as plt
|
| 341 |
+
|
| 342 |
+
plt.scatter(y.numpy(), predicted.numpy(), alpha=0.5)
|
| 343 |
+
plt.xlabel('True Wealth')
|
| 344 |
+
plt.ylabel('Predicted Wealth')
|
| 345 |
+
plt.title('True vs Predicted Wealth')
|
| 346 |
+
plt.plot([y.min(), y.max()], [y.min(), y.max()], '--', color='red')
|
| 347 |
+
plt.show()
|
| 348 |
+
|
| 349 |
+
class ObfuscationLayer(nn.Module):
|
| 350 |
+
def __init__(self):
|
| 351 |
+
super(ObfuscationLayer, self).__init__()
|
| 352 |
+
|
| 353 |
+
def forward(self, x):
|
| 354 |
+
# Add noise to simulate obfuscation/encryption
|
| 355 |
+
noise = torch.normal(0, 0.1, x.size()).to(x.device) # Adjust the standard deviation for noise level
|
| 356 |
+
return x + noise
|
| 357 |
+
|
| 358 |
+
class EnhancedWealthModel(nn.Module):
|
| 359 |
+
def __init__(self):
|
| 360 |
+
super(EnhancedWealthModel, self).__init__()
|
| 361 |
+
self.obfuscation = ObfuscationLayer()
|
| 362 |
+
self.fc1 = nn.Linear(3, 128) # More units for complexity
|
| 363 |
+
self.fc2 = nn.Linear(128, 64)
|
| 364 |
+
self.fc3 = nn.Linear(64, 32)
|
| 365 |
+
self.fc4 = nn.Linear(32, 1) # Output is a single value (wealth)
|
| 366 |
+
|
| 367 |
+
def forward(self, x):
|
| 368 |
+
x = self.obfuscation(x) # Apply obfuscation
|
| 369 |
+
x = torch.relu(self.fc1(x))
|
| 370 |
+
x = torch.relu(self.fc2(x))
|
| 371 |
+
x = torch.relu(self.fc3(x))
|
| 372 |
+
x = self.fc4(x) # No activation function on output layer for regression
|
| 373 |
+
return x
|
| 374 |
+
|
| 375 |
+
model = EnhancedWealthModel()
|
| 376 |
+
|
| 377 |
+
# Training settings
|
| 378 |
+
criterion = nn.MSELoss()
|
| 379 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 380 |
+
num_epochs = 100
|
| 381 |
+
|
| 382 |
+
# Training loop
|
| 383 |
+
for epoch in range(num_epochs):
|
| 384 |
+
model.train()
|
| 385 |
+
|
| 386 |
+
# Forward pass
|
| 387 |
+
outputs = model(X)
|
| 388 |
+
loss = criterion(outputs, y)
|
| 389 |
+
|
| 390 |
+
# Backward pass and optimization
|
| 391 |
+
optimizer.zero_grad()
|
| 392 |
+
loss.backward()
|
| 393 |
+
optimizer.step()
|
| 394 |
+
|
| 395 |
+
if (epoch + 1) % 10 == 0:
|
| 396 |
+
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
|
| 397 |
+
|
| 398 |
+
model.eval()
|
| 399 |
+
with torch.no_grad():
|
| 400 |
+
predicted = model(X)
|
| 401 |
+
|
| 402 |
+
# Visualizing True vs. Predicted Wealth
|
| 403 |
+
plt.scatter(y.numpy(), predicted.numpy(), alpha=0.5)
|
| 404 |
+
plt.xlabel('True Wealth')
|
| 405 |
+
plt.ylabel('Predicted Wealth')
|
| 406 |
+
plt.title('True vs Predicted Wealth with Obfuscation Layer')
|
| 407 |
+
plt.plot([y.min(), y.max()], [y.min(), y.max()], '--', color='red')
|
| 408 |
+
plt.show()
|
| 409 |
+
|
| 410 |
+
import torch
|
| 411 |
+
import torch.nn as nn
|
| 412 |
+
import torch.optim as optim
|
| 413 |
+
import matplotlib.pyplot as plt
|
| 414 |
+
import numpy as np
|
| 415 |
+
|
| 416 |
+
# Define grid size
|
| 417 |
+
grid_size = 20
|
| 418 |
+
|
| 419 |
+
# Generate a sine waveform to represent wealth data
|
| 420 |
+
def generate_wealth_waveform(grid_size):
|
| 421 |
+
x = np.linspace(0, 2 * np.pi, grid_size)
|
| 422 |
+
wealth_waveform = np.sin(x)
|
| 423 |
+
return wealth_waveform
|
| 424 |
+
|
| 425 |
+
# Create wealth data for the grid
|
| 426 |
+
wealth_waveform = generate_wealth_waveform(grid_size)
|
| 427 |
+
wealth_data = np.tile(wealth_waveform, (grid_size, 1)) # Repeat waveform along one axis
|
| 428 |
+
|
| 429 |
+
# Convert wealth data to PyTorch tensor
|
| 430 |
+
wealth_data = torch.tensor(wealth_data, dtype=torch.float32)
|
| 431 |
+
|
| 432 |
+
# Define a simple neural network to "transfer" wealth data to a targeted account
|
| 433 |
+
class WealthTransferNet(nn.Module):
|
| 434 |
+
def __init__(self):
|
| 435 |
+
super(WealthTransferNet, self).__init__()
|
| 436 |
+
self.fc1 = nn.Linear(grid_size * grid_size, 128)
|
| 437 |
+
self.fc2 = nn.Linear(128, grid_size * grid_size)
|
| 438 |
+
|
| 439 |
+
def forward(self, x):
|
| 440 |
+
x = torch.relu(self.fc1(x))
|
| 441 |
+
x = self.fc2(x)
|
| 442 |
+
return x
|
| 443 |
+
|
| 444 |
+
# Instantiate the network, loss function, and optimizer
|
| 445 |
+
net = WealthTransferNet()
|
| 446 |
+
criterion = nn.MSELoss()
|
| 447 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 448 |
+
|
| 449 |
+
# Target account: Wealth directed to bottom-right corner of the grid
|
| 450 |
+
target_account = torch.zeros((grid_size, grid_size))
|
| 451 |
+
target_account[-5:, -5:] = 1 # Simulating the transfer to a targeted account
|
| 452 |
+
|
| 453 |
+
# Convert the grid to a single vector for the neural network
|
| 454 |
+
input_data = wealth_data.view(-1)
|
| 455 |
+
target_data = target_account.view(-1)
|
| 456 |
+
|
| 457 |
+
# Training the network
|
| 458 |
+
epochs = 500
|
| 459 |
+
for epoch in range(epochs):
|
| 460 |
+
optimizer.zero_grad()
|
| 461 |
+
output = net(input_data)
|
| 462 |
+
loss = criterion(output, target_data)
|
| 463 |
+
loss.backward()
|
| 464 |
+
optimizer.step()
|
| 465 |
+
|
| 466 |
+
# Reshape the output to the grid size
|
| 467 |
+
output_grid = output.detach().view(grid_size, grid_size)
|
| 468 |
+
|
| 469 |
+
# Plot the original wealth waveform and transferred wealth
|
| 470 |
+
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
|
| 471 |
+
axes[0].imshow(wealth_data, cmap='viridis')
|
| 472 |
+
axes[0].set_title('Original Wealth Waveform')
|
| 473 |
+
axes[1].imshow(target_account, cmap='viridis')
|
| 474 |
+
axes[1].set_title('Target Account Location')
|
| 475 |
+
axes[2].imshow(output_grid, cmap='viridis')
|
| 476 |
+
axes[2].set_title('Transferred Wealth to Target')
|
| 477 |
+
plt.show()
|
| 478 |
+
|
| 479 |
+
import torch
|
| 480 |
+
import torch.nn as nn
|
| 481 |
+
import torch.optim as optim
|
| 482 |
+
import matplotlib.pyplot as plt
|
| 483 |
+
import numpy as np
|
| 484 |
+
|
| 485 |
+
# Define the size of the waveform
|
| 486 |
+
waveform_size = 100
|
| 487 |
+
|
| 488 |
+
# Generate a sine waveform to represent wealth data
|
| 489 |
+
def generate_wealth_waveform(waveform_size):
|
| 490 |
+
x = np.linspace(0, 2 * np.pi, waveform_size)
|
| 491 |
+
wealth_waveform = np.sin(x)
|
| 492 |
+
return wealth_waveform
|
| 493 |
+
|
| 494 |
+
# Create wealth data as a single waveform
|
| 495 |
+
wealth_waveform = generate_wealth_waveform(waveform_size)
|
| 496 |
+
wealth_data = torch.tensor(wealth_waveform, dtype=torch.float32)
|
| 497 |
+
|
| 498 |
+
# Define a neural network to transfer wealth data to a targeted point in the waveform
|
| 499 |
+
class WealthTransferNet(nn.Module):
|
| 500 |
+
def __init__(self):
|
| 501 |
+
super(WealthTransferNet, self).__init__()
|
| 502 |
+
self.fc1 = nn.Linear(waveform_size, 64)
|
| 503 |
+
self.fc2 = nn.Linear(64, waveform_size)
|
| 504 |
+
|
| 505 |
+
def forward(self, x):
|
| 506 |
+
x = torch.relu(self.fc1(x))
|
| 507 |
+
x = self.fc2(x)
|
| 508 |
+
return x
|
| 509 |
+
|
| 510 |
+
# Instantiate the network, loss function, and optimizer
|
| 511 |
+
net = WealthTransferNet()
|
| 512 |
+
criterion = nn.MSELoss()
|
| 513 |
+
optimizer = optim.Adam(net.parameters(), lr=0.01)
|
| 514 |
+
|
| 515 |
+
# Target account: Wealth directed to the end of the waveform (right side)
|
| 516 |
+
target_account = torch.zeros(waveform_size)
|
| 517 |
+
target_account[-10:] = 1 # Simulating the transfer to the last 10 positions
|
| 518 |
+
|
| 519 |
+
# Training the network
|
| 520 |
+
epochs = 1000
|
| 521 |
+
for epoch in range(epochs):
|
| 522 |
+
optimizer.zero_grad()
|
| 523 |
+
output = net(wealth_data)
|
| 524 |
+
loss = criterion(output, target_account)
|
| 525 |
+
loss.backward()
|
| 526 |
+
optimizer.step()
|
| 527 |
+
|
| 528 |
+
# Convert output to numpy for plotting
|
| 529 |
+
output_waveform = output.detach().numpy()
|
| 530 |
+
|
| 531 |
+
# Plot the original and transferred wealth waveform
|
| 532 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 533 |
+
ax.plot(wealth_data.numpy(), label="", linestyle="--")
|
| 534 |
+
ax.plot(target_account.numpy(), label="", linestyle=":")
|
| 535 |
+
ax.plot(output_waveform, label="")
|
| 536 |
+
ax.set_title('')
|
| 537 |
+
ax.legend()
|
| 538 |
+
plt.show()
|