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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""WealthWaveTransfer
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1XkEAYjoh8WGeoRnmdkgiNTM-IwU4PC__
|
| 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="Original Wealth Waveform", linestyle="--")
|
| 269 |
+
ax.plot(target_account.numpy(), label="Target Account", linestyle=":")
|
| 270 |
+
ax.plot(output_waveform, label="Transferred Wealth Waveform")
|
| 271 |
+
ax.set_title('WealthWaveTransfer')
|
| 272 |
+
ax.legend()
|
| 273 |
+
plt.show()
|