On this tutorial, we discover the design and implementation of an Superior Neural Agent that mixes classical neural community methods with fashionable stability enhancements. We construct the community utilizing Xavier initialization for balanced gradient movement and add steady activations like leaky ReLU, sigmoid, and tanh with clipping to keep away from overflow. To stabilize coaching, we apply gradient clipping, momentum-inspired updates, and weight decay. The coaching loop contains mini-batches, early stopping, adaptive studying charges, and resets on instability, making the mannequin strong for complicated datasets. We additionally normalize targets, compute MSE, MAE, and R², and prolong the agent with expertise replay and exploratory decision-making, turning it into a versatile system for regression, classification-to-regression, and RL-style duties. Take a look at the FULL CODES right here.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification, make_regression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings('ignore')
We begin by importing important libraries like NumPy, Matplotlib, and scikit-learn, which we use for knowledge technology, preprocessing, and splitting. We additionally suppress warnings to maintain our workflow clear and targeted. Take a look at the FULL CODES right here.
class AdvancedNeuralAgent:
def __init__(self, input_size, hidden_layers=[64, 32], output_size=1, learning_rate=0.001):
"""Superior AI Agent with steady coaching and determination making capabilities"""
self.lr = learning_rate
self.initial_lr = learning_rate
self.layers = []
self.reminiscence = []
self.performance_history = []
self.epsilon = 1e-8
layer_sizes = [input_size] + hidden_layers + [output_size]
for i in vary(len(layer_sizes) - 1):
fan_in, fan_out = layer_sizes[i], layer_sizes[i+1]
restrict = np.sqrt(6.0 / (fan_in + fan_out))
layer = {
'weights': np.random.uniform(-limit, restrict, (layer_sizes[i], layer_sizes[i+1])),
'bias': np.zeros((1, layer_sizes[i+1])),
'momentum_w': np.zeros((layer_sizes[i], layer_sizes[i+1])),
'momentum_b': np.zeros((1, layer_sizes[i+1]))
}
self.layers.append(layer)
def activation(self, x, func="relu"):
"""Steady activation capabilities with clipping"""
x = np.clip(x, -50, 50)
if func == 'relu':
return np.most(0, x)
elif func == 'sigmoid':
return 1 / (1 + np.exp(-x))
elif func == 'tanh':
return np.tanh(x)
elif func == 'leaky_relu':
return np.the place(x > 0, x, x * 0.01)
elif func == 'linear':
return x
def activation_derivative(self, x, func="relu"):
"""Steady derivatives"""
x = np.clip(x, -50, 50)
if func == 'relu':
return (x > 0).astype(float)
elif func == 'sigmoid':
s = self.activation(x, 'sigmoid')
return s * (1 - s)
elif func == 'tanh':
return 1 - np.tanh(x)**2
elif func == 'leaky_relu':
return np.the place(x > 0, 1, 0.01)
elif func == 'linear':
return np.ones_like(x)
def ahead(self, X):
"""Ahead cross with gradient clipping"""
self.activations = [X]
self.z_values = []
current_input = X
for i, layer in enumerate(self.layers):
z = np.dot(current_input, layer['weights']) + layer['bias']
z = np.clip(z, -50, 50)
self.z_values.append(z)
if i max_norm:
gradients = gradients * (max_norm / (grad_norm + self.epsilon))
return gradients
def backward(self, X, y, output):
"""Steady backpropagation with gradient clipping"""
m = X.form[0]
dz = (output - y.reshape(-1, 1)) / m
dz = np.clip(dz, -10, 10)
for i in reversed(vary(len(self.layers))):
layer = self.layers[i]
dw = np.dot(self.activations[i].T, dz)
db = np.sum(dz, axis=0, keepdims=True)
dw = self.clip_gradients(dw, max_norm=1.0)
db = self.clip_gradients(db, max_norm=1.0)
momentum = 0.9
layer['momentum_w'] = momentum * layer['momentum_w'] + (1 - momentum) * dw
layer['momentum_b'] = momentum * layer['momentum_b'] + (1 - momentum) * db
weight_decay = 0.0001
layer['weights'] -= self.lr * (layer['momentum_w'] + weight_decay * layer['weights'])
layer['bias'] -= self.lr * layer['momentum_b']
if i > 0:
activation_func="leaky_relu" if i > 1 else 'leaky_relu'
dz = np.dot(dz, layer['weights'].T) * self.activation_derivative(
self.z_values[i-1], activation_func)
dz = np.clip(dz, -10, 10)
def adapt_learning_rate(self, epoch, performance_history):
"""Adaptive studying price with performance-based adjustment"""
if epoch > 10:
recent_performance = performance_history[-10:]
if len(recent_performance) >= 5:
if recent_performance[-1] >= recent_performance[-5]:
self.lr = max(self.lr * 0.95, self.initial_lr * 0.01)
elif recent_performance[-1] 1000:
self.reminiscence.pop(0)
def make_decision(self, X, exploration_rate=0.1):
"""Steady determination making"""
prediction = self.ahead(X)
if np.random.random() 0 else 0.1
noise = np.random.regular(0, noise_scale, prediction.form)
prediction += noise
return np.clip(prediction, -1e6, 1e6)
def reset_if_unstable(self):
"""Reset community if coaching turns into unstable"""
print("🔄 Resetting community because of instability...")
for i, layer in enumerate(self.layers):
fan_in, fan_out = layer['weights'].form
restrict = np.sqrt(6.0 / (fan_in + fan_out))
layer['weights'] = np.random.uniform(-limit, restrict, (fan_in, fan_out))
layer['bias'] = np.zeros((1, fan_out))
layer['momentum_w'] = np.zeros((fan_in, fan_out))
layer['momentum_b'] = np.zeros((1, fan_out))
self.lr = self.initial_lr
def practice(self, X, y, epochs=500, batch_size=32, validation_split=0.2, verbose=True):
"""Sturdy coaching with stability checks"""
y_mean, y_std = np.imply(y), np.std(y)
y_normalized = (y - y_mean) / (y_std + self.epsilon)
X_trn, X_val, y_trn, y_val = train_test_split(
X, y_normalized, test_size=validation_split, random_state=42)
best_val_loss = float('inf')
endurance = 30
patience_counter = 0
train_losses, val_losses = [], []
reset_count = 0
for epoch in vary(epochs):
if epoch > 0 and (not np.isfinite(train_losses[-1]) or train_losses[-1] > 1e6):
if reset_count = endurance:
if verbose:
print(f"✋ Early stopping at epoch {epoch}")
break
if epoch > 0:
self.adapt_learning_rate(epoch, self.performance_history)
if verbose and (epoch % 50 == 0 or epoch 0:
plt.plot(self.performance_history)
plt.title('Efficiency Historical past')
plt.xlabel('Epoch')
plt.ylabel('Validation Loss')
plt.grid(True, alpha=0.3)
plt.yscale('log')
plt.subplot(1, 3, 3)
if hasattr(self, 'lr_history'):
plt.plot(self.lr_history)
plt.title('Studying Charge Schedule')
plt.xlabel('Epoch')
plt.ylabel('Studying Charge')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.present()
We implement an AdvancedNeuralAgent that we initialize with Xavier limits, leaky-ReLU activations, and momentum buffers to stabilize gradients and velocity convergence. We practice with mini-batches, gradient clipping, L2 weight decay, adaptive studying charges, early stopping, and automated resets, and we monitor MSE/MAE/R² with normalization for dependable metrics. We additionally add expertise replay and exploratory choices for agent-like habits, and we expose plotting utilities to visualise losses, validation historical past, and the LR schedule. Take a look at the FULL CODES right here.
class AIAgentDemo:
"""Demo class for testing the AI Agent with varied situations"""
def __init__(self):
self.brokers = {}
self.outcomes = {}
def generate_datasets(self):
"""Generate a number of take a look at datasets"""
datasets = {}
X1, y1 = make_regression(n_samples=600, n_features=5, n_informative=4,
noise=0.1, random_state=42)
datasets['simple'] = (X1, y1, "Easy Regression")
X2, y2 = make_regression(n_samples=800, n_features=10, n_informative=8,
noise=0.2, random_state=123)
datasets['complex'] = (X2, y2, "Advanced Regression")
X3, y3 = make_classification(n_samples=700, n_features=8, n_informative=6,
n_classes=2, random_state=456)
y3 = y3.astype(float) + np.random.regular(0, 0.1, len(y3))
datasets['classification'] = (X3, y3, "Classification-to-Regression")
return datasets
def test_agent_configuration(self, config_name, X, y, **agent_params):
"""Check agent with particular configuration"""
print(f"n🧪 Testing {config_name}...")
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
default_params = {
'input_size': X_scaled.form[1],
'hidden_layers': [32, 16],
'output_size': 1,
'learning_rate': 0.005
}
default_params.replace(agent_params)
agent = AdvancedNeuralAgent(**default_params)
strive:
train_losses, val_losses = agent.practice(
X_scaled, y, epochs=150, batch_size=32, verbose=False)
X_trn, X_test, y_trn, y_test = train_test_split(
X_scaled, y, test_size=0.2, random_state=42)
efficiency = agent.evaluate_performance(X_test, y_test)
self.brokers[config_name] = agent
self.outcomes[config_name] = {
'efficiency': efficiency,
'train_losses': train_losses,
'val_losses': val_losses,
'data_shape': X_scaled.form
}
print(f"✅ {config_name}: R²={efficiency['r2']:.3f}, MSE={efficiency['mse']:.3f}")
return True
besides Exception as e:
print(f"❌ {config_name} failed: {str(e)[:50]}...")
return False
def run_comprehensive_demo(self):
"""Run complete testing of the AI agent"""
print("🤖 COMPREHENSIVE AI AGENT DEMO")
print("=" * 60)
datasets = self.generate_datasets()
configs = {
'light-weight': {'hidden_layers': [16, 8], 'learning_rate': 0.01},
'customary': {'hidden_layers': [32, 16], 'learning_rate': 0.005},
'deep': {'hidden_layers': [64, 32, 16], 'learning_rate': 0.003},
'huge': {'hidden_layers': [128, 64], 'learning_rate': 0.002}
}
success_count = 0
total_tests = len(datasets) * len(configs)
for dataset_name, (X, y, desc) in datasets.gadgets():
print(f"n📊 Dataset: {desc} - Form: {X.form}")
print(f"Goal vary: [{np.min(y):.2f}, {np.max(y):.2f}]")
for config_name, config_params in configs.gadgets():
test_name = f"{dataset_name}_{config_name}"
if self.test_agent_configuration(test_name, X, y, **config_params):
success_count += 1
print(f"n📈 OVERALL RESULTS: {success_count}/{total_tests} checks profitable")
if self.outcomes:
self.show_best_performers()
self.demonstrate_agent_intelligence()
def show_best_performers(self):
"""Present prime performing configurations"""
print(f"n🏆 TOP PERFORMERS:")
sorted_results = sorted(self.outcomes.gadgets(),
key=lambda x: x[1]['performance']['r2'],
reverse=True)
for i, (title, outcome) in enumerate(sorted_results[:5]):
perf = outcome['performance']
print(f"{i+1}. {title}: R²={perf['r2']:.3f}, MSE={perf['mse']:.3f}, MAE={perf['mae']:.3f}")
def demonstrate_agent_intelligence(self):
"""Show superior AI capabilities"""
if not self.brokers:
return
print(f"n🧠 INTELLIGENCE DEMONSTRATION:")
best_name = max(self.outcomes.keys(),
key=lambda x: self.outcomes[x]['performance']['r2'])
best_agent = self.brokers[best_name]
print(f"Utilizing greatest agent: {best_name}")
print(f"💾 Reminiscence capability: {len(best_agent.reminiscence)} experiences")
dummy_input = np.random.randn(3, best_agent.layers[0]['weights'].form[0])
conservative_decisions = best_agent.make_decision(dummy_input, exploration_rate=0.0)
exploratory_decisions = best_agent.make_decision(dummy_input, exploration_rate=0.3)
print(f"🎯 Resolution making:")
print(f" Conservative: {conservative_decisions.flatten()[:3]}")
print(f" Exploratory: {exploratory_decisions.flatten()[:3]}")
if len(best_agent.performance_history) > 10:
initial_perf = np.imply(best_agent.performance_history[:5])
final_perf = np.imply(best_agent.performance_history[-5:])
enchancment = ((initial_perf - final_perf) / initial_perf) * 100
print(f"📊 Studying enchancment: {enchancment:.1f}%")
total_params = sum(layer['weights'].measurement + layer['bias'].measurement
for layer in best_agent.layers)
print(f"🔧 Community complexity: {total_params} parameters")
return best_agent
We orchestrate a complete demo the place we generate a number of datasets, sweep agent configurations, and practice/consider every setup with standardized metrics (R², MSE, MAE). We log outcomes, rank prime performers, after which showcase “intelligence” by probing reminiscence, exploration vs. exploitation choices, studying enchancment, and complete parameter rely. Take a look at the FULL CODES right here.
def run_quick_demo():
"""Fast demo for quick testing"""
print("🚀 QUICK AI AGENT DEMO")
print("=" * 40)
X, y = make_regression(n_samples=500, n_features=6, noise=0.15, random_state=42)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(f"Dataset: {X_scaled.form[0]} samples, {X_scaled.form[1]} options")
agent = AdvancedNeuralAgent(
input_size=X_scaled.form[1],
hidden_layers=[24, 12],
output_size=1,
learning_rate=0.008
)
print("Coaching agent...")
train_losses, val_losses = agent.practice(X_scaled, y, epochs=100, verbose=False)
X_trn, X_test, y_trn, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
efficiency = agent.evaluate_performance(X_test, y_test)
print(f"n✅ RESULTS:")
print(f"R² Rating: {efficiency['r2']:.3f}")
print(f"MSE: {efficiency['mse']:.3f}")
print(f"MAE: {efficiency['mae']:.3f}")
agent.visualize_training(train_losses, val_losses)
return agent
We add a fast demo utility that trains the agent on a easy regression dataset with six options, utilizing a light-weight two-layer configuration. We normalize the info, practice for 100 epochs, consider on a take a look at break up, and show R², MSE, and MAE earlier than plotting coaching vs. validation loss curves for quick suggestions. Take a look at the FULL CODES right here.
if __name__ == "__main__":
print("Select demo sort:")
print("1. Fast Demo (quick)")
print("2. Complete Demo (detailed)")
demo = AIAgentDemo()
best_agent = demo.run_comprehensive_demo()
We outline the primary entry level so the script may be run instantly. We show demo choices, initialize AIAgentDemo, and by default execute the excellent demo, which trains a number of configurations throughout datasets, evaluates efficiency, and highlights the most effective agent.
In conclusion, we show how stability-aware engineering selections, starting from weight decay regularization to dynamic studying price scaling primarily based on validation loss historical past, play a important position in reaching constant efficiency throughout numerous datasets. The agent isn’t just a static predictor; it actively adapts by storing previous experiences, injecting managed exploration into its choices, and resetting its parameters when instability thresholds are reached. We additional validate the design by means of complete demos throughout light-weight, customary, deep, and huge configurations, benchmarking efficiency on easy, complicated, and classification-derived regression datasets. The outcomes spotlight measurable enhancements in R², MSE, and MAE, whereas visualization instruments present perception into studying dynamics and convergence habits.
Take a look at the FULL CODES right here. Be happy to take a look at our GitHub Web page for Tutorials, Codes and Notebooks. Additionally, be happy to comply with us on Twitter and don’t neglect to hitch our 100k+ ML SubReddit and Subscribe to our Publication.
Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its recognition amongst audiences.