TypeError: tanh(): argument 'input' (position 1) must be Tensor, not int

Hello everyone. I am a new Python programmer, even newer at PyTorch and Machine Learning.

I have an assignment that asks me to approximate the sine function using neural netwroks (very introductory stuff)

In the assignment, the following code is given:

import numpy as np
import matplotlib.pyplot as plt
import torch 

N = 200
# creates the x vector with N values between -2 and 2
x = (np.random.rand(N)*4)-2
# creates the y vector
y = np.sin(2*x)
plt.plot(x,y,'xb')
plt.grid('both')
plt.axis([-3,3,-2,2]);

nTraining = int(0.7*N) # puts 70% of the data for training
x_train = np.array(x[0:nTraining],dtype=np.float32)
x_train = x_train.reshape(-1,1)

y_train = np.array(y[0:nTraining],dtype=np.float32)
y_train = y_train.reshape(-1,1)

x_test = np.array(x[nTraining:],dtype=np.float32)
x_test = x_test.reshape(-1,1)

y_test = np.array(y[nTraining:],dtype=np.float32)
y_test = y_test.reshape(-1,1)

This is the part of the assignment that I’m supposed to make, and I’ve done the following:

learningRate=0.01
nInputs = 1
nHidden = 4
nOutput = 1
model = torch.nn.Sequential(
    torch.nn.Linear(nInputs, nHidden),
    torch.tanh(nHidden),
    torch.nn.Linear(nHidden, nOutput)
    ) 
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learningRate)
print(model)

The nHidden value is set at “4” but it has no real meaning there. I’ve used it like that because it was like that on previous examples, but I’m supposed to change it.

Basically what I need is to create a neural network that approximates the sine function. The assignment suggests me to use the Hyperbolic Tangent function as an activating function to the hidden layers. I’ve tried doing that, but the following is shown:

TypeError                                 Traceback (most recent call last)

<ipython-input-75-18fd3bb85735> in <module>()
      5 model = torch.nn.Sequential(
      6     torch.nn.Linear(nInputs, nHidden),
----> 7     torch.tanh(nHidden),
      8     torch.nn.Linear(nHidden, nOutput)
      9     ) 

TypeError: tanh(): argument 'input' (position 1) must be Tensor, not int

I understand what is being said to me, I can’t pass an integer to the tanh function, as it’s expecting a Tensor. The question is, how do I addapt the nHidden value in this code to be a Tensor?

After setting up everything, I’m supposed to run the following code:

def train_model(nEpochs = 10):
  errors=[]
  for epoch in range(nEpochs):
    if torch.cuda.is_available():
      inputs = torch.tensor(x_train).to('cuda')
      targets = torch.tensor(y_train).to('cuda')
    else:
      inputs = torch.tensor(x_train) 
      targets = torch.tensor(y_train)    
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    loss.backward() 
    optimizer.step()
    errors.append(loss.item())
    if(not epoch%100):
      print('Epoch:' + str(epoch) + ' errors: ' + str(loss.item()))
  return errors
  
errors = train_model(800) 
plt.plot(errors)
plt.show()

And the error should be below 0.05, but the most I can get is 0.27.

Can someone please give me any tips or something as to how I’m supposed to do that?

Thank you for the help in advance!

Replace,

with,

model = torch.nn.Sequential(
    torch.nn.Linear(nInputs, nHidden),
    torch.nn.Tanh(),
    torch.nn.Linear(nHidden, nOutput)
    )

And it should work

1 Like

(post deleted by author)

That worked! Can’t believe it was that easy. Thank you very much.