|
import torch |
|
import torch.nn as nn |
|
import torch.optim as optim |
|
import numpy as np |
|
|
|
class WealthFrequencyPredictor(nn.Module): |
|
def __init__(self): |
|
super(WealthFrequencyPredictor, self).__init__() |
|
self.fc1 = nn.Linear(10, 64) |
|
self.fc2 = nn.Linear(64, 64) |
|
self.fc3 = nn.Linear(64, 1) |
|
|
|
def forward(self, x): |
|
x = torch.relu(self.fc1(x)) |
|
x = torch.relu(self.fc2(x)) |
|
x = self.fc3(x) |
|
return x |
|
|
|
|
|
model = WealthFrequencyPredictor() |
|
criterion = nn.MSELoss() |
|
optimizer = optim.Adam(model.parameters(), lr=0.001) |
|
|
|
|
|
|
|
inputs = torch.randn(100, 10) |
|
|
|
targets = torch.rand(100, 1) * (100 - 8) + 8 |
|
|
|
dataset = torch.utils.data.TensorDataset(inputs, targets) |
|
data_loader = torch.utils.data.DataLoader(dataset, batch_size=10, shuffle=True) |
|
|
|
|
|
def train_model(model, data_loader, epochs=100): |
|
for epoch in range(epochs): |
|
for batch_inputs, batch_targets in data_loader: |
|
optimizer.zero_grad() |
|
outputs = model(batch_inputs) |
|
loss = criterion(outputs, batch_targets) |
|
loss.backward() |
|
optimizer.step() |
|
if (epoch + 1) % 10 == 0: |
|
print(f'Epoch {epoch + 1}/{epochs}, Loss: {loss.item()}') |
|
|
|
train_model(model, data_loader) |
|
|
|
import torch |
|
import torch.nn as nn |
|
import torch.optim as optim |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
class WealthFrequencyPredictor(nn.Module): |
|
def __init__(self): |
|
super(WealthFrequencyPredictor, self).__init__() |
|
self.fc1 = nn.Linear(10, 64) |
|
self.fc2 = nn.Linear(64, 64) |
|
self.fc3 = nn.Linear(64, 1) |
|
|
|
def forward(self, x): |
|
x = torch.relu(self.fc1(x)) |
|
x = torch.relu(self.fc2(x)) |
|
x = self.fc3(x) |
|
return x |
|
|
|
|
|
model = WealthFrequencyPredictor() |
|
criterion = nn.MSELoss() |
|
optimizer = optim.Adam(model.parameters(), lr=0.001) |
|
|
|
|
|
inputs = torch.randn(100, 10) |
|
targets = torch.rand(100, 1) * (100 - 8) + 8 |
|
|
|
dataset = torch.utils.data.TensorDataset(inputs, targets) |
|
data_loader = torch.utils.data.DataLoader(dataset, batch_size=10, shuffle=True) |
|
|
|
|
|
def train_model(model, data_loader, epochs=100): |
|
for epoch in range(epochs): |
|
for batch_inputs, batch_targets in data_loader: |
|
optimizer.zero_grad() |
|
outputs = model(batch_inputs) |
|
loss = criterion(outputs, batch_targets) |
|
loss.backward() |
|
optimizer.step() |
|
if (epoch + 1) % 10 == 0: |
|
print(f'Epoch {epoch + 1}/{epochs}, Loss: {loss.item()}') |
|
|
|
train_model(model, data_loader) |
|
|
|
|
|
|
|
|
|
def generate_sine_wave(frequency, duration, amplitude=0.5, sample_rate=44100): |
|
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) |
|
wave = amplitude * np.sin(2 * np.pi * frequency * t) |
|
return t, wave |
|
|
|
|
|
def predict_and_visualize_wave(model, input_data): |
|
model.eval() |
|
with torch.no_grad(): |
|
input_tensor = torch.tensor(input_data, dtype=torch.float32) |
|
predicted_frequency = model(input_tensor).item() |
|
|
|
print(f'Predicted Wealth Frequency: {predicted_frequency} Hz') |
|
|
|
|
|
duration = 5 |
|
t, wave = generate_sine_wave(predicted_frequency, duration) |
|
|
|
|
|
plt.figure(figsize=(10, 6)) |
|
plt.plot(t, wave) |
|
plt.title(f'Sine Wave for Predicted Wealth Frequency: {predicted_frequency} Hz') |
|
plt.xlabel('Time [s]') |
|
plt.ylabel('Amplitude') |
|
plt.grid(True) |
|
plt.show() |
|
|
|
|
|
input_data = np.random.rand(10) |
|
predict_and_visualize_wave(model, input_data) |
|
|
|
import numpy as np |
|
import torch |
|
|
|
|
|
def generate_sine_wave(frequency, duration=5, amplitude=0.5, sample_rate=44100): |
|
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) |
|
wave = amplitude * np.sin(2 * np.pi * frequency * t) |
|
return wave |
|
|
|
|
|
predicted_frequency = 40.0 |
|
wave_data = generate_sine_wave(predicted_frequency) |
|
|
|
|
|
def xor_encrypt_decrypt(data, key): |
|
return bytearray(a ^ key for a in data) |
|
|
|
|
|
wave_data_bytes = bytearray(np.float32(wave_data).tobytes()) |
|
|
|
|
|
encryption_key = 55 |
|
|
|
|
|
encrypted_wave = xor_encrypt_decrypt(wave_data_bytes, encryption_key) |
|
print(f'Encrypted Wave (first 100 bytes): {encrypted_wave[:100]}') |
|
|
|
|
|
decrypted_wave_bytes = xor_encrypt_decrypt(encrypted_wave, encryption_key) |
|
|
|
|
|
decrypted_wave_data = np.frombuffer(decrypted_wave_bytes, dtype=np.float32) |
|
|
|
print(f'Decrypted Wave (first 10 samples): {decrypted_wave_data[:10]}') |
|
|
|
import numpy as np |
|
import torch |
|
|
|
|
|
def generate_sine_wave(frequency, duration=5, amplitude=0.5, sample_rate=44100): |
|
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) |
|
wave = amplitude * np.sin(2 * np.pi * frequency * t) |
|
return wave |
|
|
|
|
|
def xor_encrypt_decrypt(data, key): |
|
return bytearray(a ^ key for a in data) |
|
|
|
|
|
predicted_frequency = 40.0 |
|
wave_data = generate_sine_wave(predicted_frequency) |
|
|
|
|
|
wave_data_bytes = bytearray(np.float32(wave_data).tobytes()) |
|
|
|
|
|
encryption_key = 55 |
|
encrypted_wave = xor_encrypt_decrypt(wave_data_bytes, encryption_key) |
|
print(f'Encrypted Wave (first 100 bytes): {encrypted_wave[:100]}') |
|
|
|
|
|
decrypted_wave_bytes = xor_encrypt_decrypt(encrypted_wave, encryption_key) |
|
decrypted_wave_data = np.frombuffer(decrypted_wave_bytes, dtype=np.float32) |
|
|
|
|
|
print(f'Decrypted Wave (first 10 samples): {decrypted_wave_data[:10]}') |
|
|
|
|
|
|
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def generate_sine_wave(frequency, duration=5, amplitude=0.5, sample_rate=44100): |
|
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) |
|
wave = amplitude * np.sin(2 * np.pi * frequency * t) |
|
return t, wave |
|
|
|
|
|
def xor_encrypt_decrypt(data, key): |
|
return bytearray(a ^ key for a in data) |
|
|
|
|
|
predicted_frequency = 40.0 |
|
t, wave_data = generate_sine_wave(predicted_frequency) |
|
|
|
|
|
wave_data_bytes = bytearray(np.float32(wave_data).tobytes()) |
|
|
|
|
|
encryption_key = 55 |
|
encrypted_wave = xor_encrypt_decrypt(wave_data_bytes, encryption_key) |
|
|
|
|
|
decrypted_wave_bytes = xor_encrypt_decrypt(encrypted_wave, encryption_key) |
|
|
|
|
|
decrypted_wave_data = np.frombuffer(decrypted_wave_bytes, dtype=np.float32) |
|
|
|
|
|
plt.figure(figsize=(12, 6)) |
|
|
|
|
|
plt.subplot(2, 1, 1) |
|
plt.plot(t[:1000], wave_data[:1000], label='Original Wave') |
|
plt.title('Original Bytes') |
|
plt.xlabel('Time [s]') |
|
plt.ylabel('Amplitude') |
|
plt.grid(True) |
|
|
|
|
|
plt.subplot(2, 1, 2) |
|
plt.plot(t[:1000], decrypted_wave_data[:1000], label='Decrypted Wave', color='orange') |
|
plt.title('Decrypted Bytes') |
|
plt.xlabel('Time [s]') |
|
plt.ylabel('Amplitude') |
|
plt.grid(True) |
|
|
|
plt.tight_layout() |
|
plt.show() |