The error you're encountering, TypeError: Layer_Dense() takes no arguments, suggests there's a mismatch between the class definition and the way you are trying to create an instance of that class.
The code you've presented is attempting to create a simple neural network with 4 dense layers and uses the hyperbolic tangent (tanh) activation function. In addition, you've started implementing backpropagation, though it appears there are several issues that need to be addressed for it to function correctly. Here's a revised version of your code with some changes and explanations:
Dimensionality of Input Data: Your neural network assumes 32 inputs, but you are trying to pass a single example with shape (1,) at a time, which doesn't match the expected input shape (32,).
Backpropagation Logic: The backpropagation function you've written seems to only account for the output layer. Backpropagation needs to iterate through each layer in reverse to update weights correctly.
Gradient Calculation: In your backpropagation, gradient dimensions do not seem to match up correctly, and there's a misunderstanding of how weights should be updated.
Learning Rate Application: The learning rate should be applied correctly within the weight update step, which you are doing, but there seems to be confusion about handling weights and gradients properly.
Initialization and Usage of Activation Classes: You're creating a new instance of activation functions in every iteration, which is unnecessary. You can create a single instance for each layer outside the loop.
Here's a simplified revision, primarily focusing on forward propagation and fixing dimensionality issues, as full backpropagation requires a more in-depth approach:
python
import numpy as np
np.random.seed(0)
class Layer_Dense:
def __init__(self, n_inputs, n_neurons, learning_rate=0.001):
self.weights = np.random.randn(n_inputs, n_neurons) * 0.1
self.biases = np.zeros((1, n_neurons))
self.learning_rate = learning_rate
def forward(self, inputs):
self.inputs = inputs
self.output = np.dot(inputs, self.weights) + self.biases
return self.output
class Activation_tanh:
def forward(self, inputs):
self.output = np.tanh(inputs)
return self.output
# Define network structure
layer1 = Layer_Dense(5, 32) # Assuming each input vector has 5 features
layer2 = Layer_Dense(32, 32)
layer3 = Layer_Dense(32, 32)
layer4 = Layer_Dense(32, 1) # Assuming a single output for simplicity
# Activation instances
activation1 = Activation_tanh()
activation2 = Activation_tanh()
activation3 = Activation_tanh()
activation4 = Activation_tanh()
# Generate some dummy data matching the expected dimensions
x_train_split = np.random.randn(100, 5) # 100 examples, 5 features each
y_train_split = np.random.randn(100, 1) # 100 target values
# Forward pass for a single example (demonstration purposes)
i = 0 # example index
layer1_output = layer1.forward(x_train_split[i].reshape(1, -1))
layer1_activation = activation1.forward(layer1_output)
layer2_output = layer2.forward(layer1_activation)
layer2_activation = activation2.forward(layer2_output)
layer3_output = layer3.forward(layer2_activation)
layer3_activation = activation3.forward(layer3_output)
final_output = layer4.forward(layer3_activation)
final_activation = activation4.forward(final_output)
print("Final output:", final_activation)
Key Changes:
Layer Initialization: The first layer now correctly expects 5 input features (Layer_Dense(5, 32)) assuming each input vector has 5 features.
Last Layer Output: Adjusted the last layer to produce a single output for simplicity (Layer_Dense(32, 1)), assuming you're aiming for a single target value.
Dimensionality in Forward Propagation: Forward propagation now correctly handles data at each step, ensuring matrix dimensions match expected values.
Simplification: This revision focuses on demonstrating a correct forward pass without delving into backpropagation. Backpropagation and weight update mechanisms will need a comprehensive approach, including correctly computing gradients and applying them for each layer weights update.
However, from the initial code snippet you provided earlier, the Layer_Dense class is defined with an __init__ method that correctly accepts three parameters (n_inputs, n_neurons, and learning_rate with a default value). This should allow the creation of an instance with the given arguments without raising a TypeError.
Here are a few possible reasons for the error and how to troubleshoot them:
Redefinition or Reloading Issue:
Ensure that you're running the code in a fresh environment or kernel, especially if you're using Jupyter notebooks or any interactive Python environment. It's possible that an earlier version of Layer_Dense without an __init__ method is still in memory.
Restart your Python kernel (or interpreter session) and run the cell that defines the Layer_Dense class again before trying to create an instance of it.
Check for Class Definition:
Verify the complete class definition of Layer_Dense is exactly as displayed in your code snippet. It’s crucial that the class definition in your running environment matches what you intend to use.
Namespace Issue:
Ensure that Layer_Dense is not being overshadowed by another variable or module with the same name. This can happen in environments where multiple imports or definitions might overshadow previous ones.
After ensuring the above, the instantiation of Layer_Dense should look like this:
python
layer1 = Layer_Dense(5, 32) # Assuming each input vector has 5 features
layer2 = Layer_Dense(32, 32)
layer3 = Layer_Dense(32, 32)
layer4 = Layer_Dense(32, 1) # Adjusting based on your network's output requirement
Remember, if you've adjusted the number of features in your input data (e.g., you're using np.random.randn(100, 1) for x_train_split), ensure the first layer's n_inputs parameter matches the features in your dataset. For example, if you have only 1 feature per input, the first layer should be initialized as Layer_Dense(1, 32). However, in the provided corrected code snippet, an assumption was made for 5 features per input example, hence Layer_Dense(5, 32). Always adjust according to your dataset's characteristics.