I have come up with the following code for reading some data from a file into a neural network. Input 1 is a csv with 2 columns (two input variables) and input 2 single column y values for the function. Finally I need to do some prediction (that part of the code is not yet written because this itself is not working but if someone can help it would be good). For that another file 3 will be read with 2 variables input and a fourth file would be written out containing computed values of y.
However the first part of the code is not working. Please help. Please keep code as simple as possible, with minimum changes to this script.
Import torch
import torch.nn as nn
import tensorflow as tf
import numpy as np
data_x = torch.from_numpy(
np.genfromtxt(“C:/NewHS/DataScience/comp/tensor1.csv”, dtype=‘float64’, delimiter=“,”)
)
data_y = torch.from_numpy(
np.genfromtxt(“C:/NewHS/DataScience/comp/tensor2.csv”, dtype=‘float64’, delimiter=“,”)
)
n_input, n_hidden, n_out, batch_size, learning_rate = 2, 15, 1, 100, 0.01
model=nn.Sequential(nn.Linear(n_input, n_hidden), nn.ReLU(), nn.Linear(n_hidden, n_out), nn.Sigmoid())
print(model)
loss_function = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
losses =
for epoch in range(5000):
pred_y = model(data_x)
loss = loss_function(pred_y, data_y)
losses.append(loss.item())
model.zero_grad()
loss.backward()
optimizer.step()
import matplotlib.pyplot as plt
plt.plot(losses)
plt.ylabel(‘loss’)
plt.xlabel(‘epoch’)
plt.title(“Learning rate %f”%(learning_rate))
plt.show()