-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic idea
58 lines (50 loc) · 2.04 KB
/
basic idea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import torch
import torch.nn as nn
import torch.optim as optim
# Define a custom layer for quasipolynomial synapses
class QuasiPolynomialSynapse(nn.Module):
def __init__(self, input_size, output_size, degree=2):
super(QuasiPolynomialSynapse, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.degree = degree
self.weights = nn.Parameter(torch.randn(output_size, input_size, degree))
self.bias = nn.Parameter(torch.randn(output_size))
def forward(self, x):
# Compute the quasipolynomial terms
x_expanded = torch.stack([x ** (i + 1) for i in range(self.degree)], dim=-1)
output = torch.einsum('bij,kij->bk', x_expanded, self.weights) + self.bias
return output
# Define the neural network model
class QuasiPolynomialNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(QuasiPolynomialNet, self).__init__()
self.synapse1 = QuasiPolynomialSynapse(input_size, hidden_size)
self.synapse2 = QuasiPolynomialSynapse(hidden_size, output_size)
def forward(self, x):
x = torch.relu(self.synapse1(x))
x = self.synapse2(x)
return x
# Sample data
x = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
y = torch.tensor([[1.0], [0.0]])
# Initialize the model, loss function and optimizer
model = QuasiPolynomialNet(input_size=3, hidden_size=5, output_size=1)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
# Training loop
for epoch in range(10000):
optimizer.zero_grad()
outputs = model(x)
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
if epoch % 1000 == 0:
print(f'Epoch [{epoch}/10000], Loss: {loss.item():.4f}')
# Generating larger sample input for testing
large_test_input = torch.randn(10, 3) # Generates a 10x3 tensor with random values
# Testing the model
with torch.no_grad():
test_output = model(large_test_input)
print("Large Test Input:\n", large_test_input)
print("Test Output:\n", test_output)