Feed Forward Network: ValueError: too many values to unpack (expected 3)

I am trying to solve a problem with 3 inputs and 1 output.
Data loaded using pandas .csv file.
train_test_split of the data done.
used standard scaler and minmax scaler.
I have made modifications in the 0318_Feed Forward Network as per my need of 3 inputs and 1 output with 1 hidden layer in between of 3 neurons. It ran w/o any problem.

class FirstFFNetwork:

  def __init__(self):

    self.w1 = np.random.randn()
    self.w2 = np.random.randn()
    self.w3 = np.random.randn()
    self.w4 = np.random.randn()
    self.w5 = np.random.randn()
    self.w6 = np.random.randn()
    self.w7 = np.random.randn()
    self.w8 = np.random.randn()
    self.w9 = np.random.randn()
    self.w10 = np.random.randn()
    self.w11 = np.random.randn()
    self.w12 = np.random.randn()
    self.b1 = 0
    self.b2 = 0
    self.b3 = 0
    self.b4 = 0
    self.b4 = 0
  
  def sigmoid(self, x):
    return 1.0/(1.0 + np.exp(-x))

  def forward_pass(self, x):
    self.x1, self.x2, self.x3 = x
    self.a1 = self.w1*self.x1 + self.w2*self.x2 + self.w3*self.x3 + self.b1
    self.h1 = self.sigmoid(self.a1)
    self.a2 = self.w4*self.x1 + self.w5*self.x2 + self.w6*self.x3 + self.b2
    self.h2 = self.sigmoid(self.a2)
    self.a3 = self.w7*self.x1 + self.w8*self.x2 + self.w9*self.x3 + self.b3
    self.h3 = self.sigmoid(self.a3)
    self.a4 = self.w10*self.h1 + self.w11*self.h2 + self.w12*self.h3 + self.b4
    self.h4 = self.sigmoid(self.a3)
    return self.h4

  def grad(self, x, y):
    self.forward_pass(x)

    self.dw10 = (self.h4-y) * self.h4*(1-self.h4) * self.h1
    self.dw11 = (self.h4-y) * self.h4*(1-self.h4) * self.h2
    self.dw12 = (self.h4-y) * self.h4*(1-self.h4) * self.h3
    self.db4 = (self.h4-y) * self.h4*(1-self.h4)

    self.dw1 = (self.h4-y) * self.h4*(1-self.h4) * self.w10 * self.h1*(1-self.h1) * self.x1
    self.dw2 = (self.h4-y) * self.h4*(1-self.h4) * self.w10 * self.h1*(1-self.h1) * self.x2
    self.dw3 = (self.h4-y) * self.h4*(1-self.h4) * self.w10 * self.h1*(1-self.h1) * self.x3
    self.db1 = (self.h4-y) * self.h4*(1-self.h3) * self.w10 * self.h1*(1-self.h1)

    self.dw4 = (self.h4-y) * self.h4*(1-self.h4) * self.w11 * self.h2*(1-self.h2) * self.x1
    self.dw5 = (self.h4-y) * self.h4*(1-self.h4) * self.w11 * self.h2*(1-self.h2) * self.x2
    self.dw6 = (self.h4-y) * self.h4*(1-self.h4) * self.w11 * self.h2*(1-self.h2) * self.x3
    self.db2 = (self.h4-y) * self.h4*(1-self.h4) * self.w11 * self.h2*(1-self.h2)

    self.dw7 = (self.h4-y) * self.h4*(1-self.h4) * self.w12 * self.h3*(1-self.h3) * self.x1
    self.dw8 = (self.h4-y) * self.h4*(1-self.h4) * self.w12 * self.h3*(1-self.h3) * self.x2
    self.dw9 = (self.h4-y) * self.h4*(1-self.h4) * self.w12 * self.h3*(1-self.h3) * self.x3
    self.db3 = (self.h4-y) * self.h4*(1-self.h3) * self.w12 * self.h3*(1-self.h3)

  def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, display_loss=False):

    # initialise w, b

    if initialise:

      self.w1 = np.random.randn()
      self.w2 = np.random.randn()
      self.w3 = np.random.randn()
      self.w4 = np.random.randn()
      self.w5 = np.random.randn()
      self.w6 = np.random.randn()
      self.w7 = np.random.randn()
      self.w8 = np.random.randn()
      self.w9 = np.random.randn()
      self.w10 = np.random.randn()
      self.w11 = np.random.randn()
      self.w12 = np.random.randn()
      self.b1 = 0
      self.b2 = 0
      self.b3 = 0
      self.b4 = 0
 
    if display_loss:
      loss = {}

   
    for i in tqdm_notebook(range(epochs), total=epochs, unit="epoch"):
      dw1, dw2, dw3, dw4, dw5, dw6, db1, db2, db3 = [0]*9
      for x, y in zip(X, Y):
        self.grad(x, y)
        dw1 += self.dw1
        dw2 += self.dw2
        dw3 += self.dw3
        dw4 += self.dw4
        dw5 += self.dw5
        dw6 += self.dw6
        dw7 += self.dw7
        dw8 += self.dw8
        dw9 += self.dw9
        dw10 += self.dw10
        dw11 += self.dw11
        dw12 += self.dw12
        db1 += self.db1
        db2 += self.db2
        db3 += self.db3
        db4 += self.db4

      m = X.shape[1]
      self.w1 -= learning_rate * dw1 / m
      self.w2 -= learning_rate * dw2 / m
      self.w3 -= learning_rate * dw3 / m
      self.w4 -= learning_rate * dw4 / m
      self.w5 -= learning_rate * dw5 / m
      self.w6 -= learning_rate * dw6 / m
      self.w7 -= learning_rate * dw7 / m
      self.w8 -= learning_rate * dw8 / m
      self.w9 -= learning_rate * dw9 / m
      self.w10 -= learning_rate * dw10 / m
      self.w11 -= learning_rate * dw11 / m
      self.w12 -= learning_rate * dw12 / m
      self.b1 -= learning_rate * db1 / m
      self.b2 -= learning_rate * db2 / m
      self.b3 -= learning_rate * db3 / m
      self.b4 -= learning_rate * db4 / m

      if display_loss:
        Y_pred = self.predict(X)
        loss[i] = mean_squared_error(Y_pred, Y)

    if display_loss:
      plt.plot(list(loss.values()))
      plt.xlabel('Epochs')
      plt.ylabel('Mean Squared Error')
      plt.show()
      
  def predict(self, X):
    Y_pred = []
    for x in X:
      y_pred = self.forward_pass(x)
      Y_pred.append(y_pred)
    return np.array(Y_pred)

After running this code

ffn = FirstFFNetwork()
ffn.fit(X_train, Y_train, epochs=2000, learning_rate=.01, display_loss=True)

This is showing an error

 23 
     24   def forward_pass(self, x):
---> 25     self.x1, self.x2, self.x3 = x
     26     self.a1 = self.w1*self.x1 + self.w2*self.x2 + self.w3*self.x3 + self.b1
     27     self.h1 = self.sigmoid(self.a1)

ValueError: too many values to unpack (expected 3)

As a starting, can you print and confirm:

  • shape of X_train before calling fit and
  • shape of x in code inside forward_pass.
    The error seems to suggest x may have more features than 3.

data = pd.read_csv(‘Combined.csv’)
x = data.drop(‘FRAC’, axis=1)
y = data[‘FRAC’].values
print(data.shape)
print(x.shape)
print(y.shape)

(451, 4)
(451, 3)
(451,)

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42)
print(x_train.shape, x_test.shape)
print(y_train.shape, y_test.shape)

(338, 3) (113, 3)
(338,) (113,)

  • shape of x in code inside forward_pass .
    The error seems to suggest x may have more features than 3.

Could you please tell me how can, I check the shape of x in forward pass.

def forward_pass(self, x):
   print(x.shape)
   self.x1, self.x2, self.x3 = x

you can just add print(x.shape) line inside forward_pass function as done above.

Also, I think it will be best, if you can provide a link to your code. (easy to share if you are using something like google colab). That will be the best option.

https://colab.research.google.com/drive/1lSoth0ZJwsW5KR7rlLGFFYRAwywsIHLQ#scrollTo=rc7Jicf_D8TT

https://colab.research.google.com/drive/1lSoth0ZJwsW5KR7rlLGFFYRAwywsIHLQ?usp=sharing

I don’t have access:Screenshot 2020-07-03 at 11.12.15 PM

I don’t have your email id??

Just change to anyone with the link for now, copy that link and share:

https://colab.research.google.com/drive/1lSoth0ZJwsW5KR7rlLGFFYRAwywsIHLQ?usp=sharing

done

check once

1 Like

Can you share Combined.csv as well? For that you will need to upload it to your google drive and share a link for downloading that.

https://drive.google.com/file/d/1x04Y_OUHvNeXi8_gFG8AzLSgkNwNL8sv/view?usp=sharing

Okay,

Ok, when you are calling fit. X_train is still a dataframe object. You should convert it to numpy object by doing something like X_train = X_train.values

May be , you can convert all of X to numpy object before calling test_train_split :
Screenshot 2020-07-03 at 11.36.22 PM
by doing the X.values assignment to X:
X = data.drop(‘FRAC’, axis=1)
X = X.values

Without it, the first row in X is the row containing the column names(strings) which is causing error I think.

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:22: RuntimeWarning: overflow encountered in exp

UnboundLocalError Traceback (most recent call last)
in ()
1 ffn = FirstFFNetwork()
----> 2 ffn.fit(X_train, Y_train, epochs=2000, learning_rate=.01, display_loss=True)

in fit(self, X, Y, epochs, learning_rate, initialise, display_loss)
93 dw5 += self.dw5
94 dw6 += self.dw6
—> 95 dw7 += self.dw7
96 dw8 += self.dw8
97 dw9 += self.dw9

UnboundLocalError: local variable ‘dw7’ referenced before assignment

Could you please explain this error

You forgot to initialise dw7 in your code here:
dw1, dw2, dw3, dw4, dw5, dw6, db1, db2, db3 = [0]*9

Please use a separate thread for new issues.