Making a basic neural network form the scratch and facing the problem while executing

When creating a post, please add:

  • Week 1
  • Neural network implementation in python
  • error in my code while running it is the broadcasting error while using matmul
shirt_data = np.array([
    ["Shirt_ID", "Brand", "Material", "Price", "Durability_Rating", "Style", "Comfort_Rating", 
     "Sales_Last_Year", "Return_Rate", "Production_Cost", "Environmental_Impact", "Customer_Feedback", 
     "Reliability_Score", "Popularity", "Market_Trend"],
    
    [1, "Zara", "Cotton", 25, 8, "Casual", 9, 15000, "3%", 12, "Medium", 4.5, 8.5, "High", "Increasing"],
    [2, "H&M", "Polyester", 20, 6, "Formal", 7, 12000, "5%", 8, "High", 4.0, 6.7, "Medium", "Stable"],
    [3, "Nike", "Dri-Fit", 30, 9, "Sports", 8, 18000, "2%", 15, "Low", 4.7, 8.9, "High", "Increasing"],
    [4, "Uniqlo", "Linen", 35, 7, "Casual", 8, 14000, "4%", 18, "Medium", 4.3, 7.5, "Medium", "Stable"],
    [5, "Levi’s", "Cotton", 40, 9, "Formal", 9, 16000, "3%", 20, "Low", 4.8, 8.9, "High", "Increasing"]
])

shirt_data

import pandas as pd
df = pd.DataFrame(shirt_data)
df

df1 = df.copy()
x_train = df1.iloc[1:,[3,4,6,9]].to_numpy()
x_train = x_train.astype(float)
print(x_train)

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = df1.iloc[:,-1]

y_train = le.fit_transform(y_train)
y_train
print(y_train.shape[0])
print(x_train , "\n\n\n")
print(y_train ,"\n\n\n")

def sigmoid(x):
  return 1/(1+np.exp(-x))
def my_dense(a,w,b):
  a = np.matmul(a,w) 
  b = np.broadcast_to(b , a.shape)

  # b = b.reshape(b.shape[0],1)
  a = a+ b
  print(a)

  a = sigmoid(a)  
  return a

def my_sequential(x,w1,b1,w2,b2):
  a1 = my_dense(x,w1,b1)
  a2 = my_dense(a1,w2,b2)
  return a2
w1 = np.random.rand(4,3)
print(w1,"\n\n\n\n")
b1 = np.random.rand(1,5)


print(b1,"\n\n\n\n")
w2 = np.random.rand(3,1)
print(w2,"\n\n\n\n")
b2 = np.random.rand(1,3)
print(b2,"\n\n\n\n")
y_pred = my_sequential(x_train,w1,b1,w2,b2)
y_pred

Please post a screen capture image of the error message.
Also please say what programming platform you are using.

I m using google collab notebook

1 Like

Hello @Specter0x001,

The following line suggested there are 4 features:

So, the next line was okay because it meant your first layer had 3 neurons and expected 4 features,

but then the corresponding b1 should take the shape of (1, 3) because it should be configured as (1, number of neurons).

I believe it was because the use of (1,5) resulted in that shape to appear in the error value (and caused the error, since it can’t reshape (1,5) into (5,3). Reshape can’t change both dimensions at the same time.).

image

There is a similar problem with b2 and requires your attention :wink:

Cheers,
Raymond

2 Likes

thank you for your help sir

1 Like

You are welcome!