Close Menu
    Trending
    • NumPy for Absolute Beginners: A Project-Based Approach to Data Analysis
    • What Building My First Dashboard Taught Me About Data Storytelling
    • Train a Humanoid Robot with AI and Python
    • What to Do When Your Credit Risk Model Works Today, but Breaks Six Months Later
    • OpenAI Is Now a For-Profit Company, Paving the Way for a Possible $1 Trillion IPO
    • How OpenAI’s Autonomous AI Researcher Could Reshape the Economy
    • AI Is Impacting the Job Market
    • Asus ROG Rapture världens första AI-router
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Train a Humanoid Robot with AI and Python
    Artificial Intelligence

    Train a Humanoid Robot with AI and Python

    ProfitlyAIBy ProfitlyAINovember 4, 2025No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Humanoid robots are machines resembling the human physique in form and motion, designed for working alongside folks and interacting with our instruments. They’re nonetheless an rising know-how, however forecasts predict billions of humanoids by 2050. At the moment, probably the most superior prototypes are NEO by 1XTech, Optimus by Tesla, Atlas by Boston Dynamics, and G1 by China’s Unitree Robotics.

    There are two methods for a robotic to carry out a activity: guide management (whenever you particularly program what it has to do) or Synthetic Intelligence (it learns methods to do issues by making an attempt). Particularly, Reinforcement Learning permits a robotic to be taught the perfect actions by way of trial and error to realize a objective, so it might adapt to altering environments by studying from rewards and penalties with out a predefined plan.

    In follow, it’s loopy costly to have an actual robotic studying methods to carry out a activity. Subsequently, state-of-the-art approaches be taught in simulation the place knowledge technology is quick and low cost, and subsequently switch the data to the true robotic (“sim-to-real” / “sim-first” strategy). That allows the parallel coaching of a number of fashions in simulation environments.

    Essentially the most used 3D physics simulators available on the market are: PyBullet (freshmen) , Webots (intermediate), MuJoCo (superior), and Gazebo (professionals). You should utilize any of them as standalone software program or by way of Gym, a library made by OpenAI for creating Reinforcement Studying algorithms, constructed on prime of various physics engines.

    On this tutorial, I’m going to indicate methods to construct a 3D simulation for a humanoid robotic with Synthetic Intelligence. I’ll current some helpful Python code that may be simply utilized in different comparable circumstances (simply copy, paste, run) and stroll by way of each line of code with feedback with the intention to replicate this instance (hyperlink to full code on the finish of the article).

    Setup

    An atmosphere is a simulated house the place brokers can work together and be taught to carry out a activity. It has an outlined remark house (the knowledge brokers obtain) and motion areas (the set of potential actions).

    I’ll use Gymnasium (pip set up gymnasium) to load one of many default environments made with MuJoCo (Multi-Joint dynamics with Contact, pip set up mujoco).

    import gymnasium as gymnasium
    
    env = gymnasium.make("Humanoid-v4", render_mode="human")
    obs, data = env.reset()
    env.render()

    The agent is a 3D bipedal robotic that may transfer like a human. It has 12 hyperlinks (strong physique components) and 17 joints (versatile physique components). You possibly can see the full description here.

    Earlier than beginning a brand new simulation, you could reset the atmosphere with obs, data = env.reset(). That command returns details about the agent’s preliminary state. The data often consists of additional details about the robotic.

    Whereas the obs is what the agent sees (i.e. with sensors), an AI mannequin would wish to course of these observations to determine what motion to take.

    Often, all Gym environments have the identical construction. The very first thing to test is the motion house, the set of all of the potential actions. For the Humanoid simulation, an motion represents the pressure utilized to considered one of its 17 joints (inside a variety of -0.4 and +0.4 to point the course of the push).

    env.action_space
    env.action_space.pattern()

    A simulation ought to at the least cowl one episode, an entire run of the agent interacting with the atmosphere, from begin to termination. Every episode is a loop of reset() -> step() -> render(). Let’s make an instance working one single episode with the humanoid doing random actions, so not AI.

    import time
    
    env = gymnasium.make("Humanoid-v4", render_mode="human")
    obs, data = env.reset()
    
    reset = False #reset if the humanoid falls or the episode ends
    episode = 1
    total_reward, step = 0, 0
    
    for _ in vary(240):
        ## motion
        step += 1
        motion = env.action_space.pattern() #random motion
        obs, reward, terminated, truncated, data = env.step(motion)
        ## reward
        total_reward += reward
        ## render
        env.render() #render physics step (CPU pace = 0.1 seconds)
        time.sleep(1/240) #decelerate to real-time (240 steps × 1/240 second sleep = 1 second)
        if (step == 1) or (step % 100 == 0): #print first step and each 100 steps
            print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
        ## reset
        if reset:
            if terminated or truncated: #print the final step
                print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
                obs, data = env.reset()
                episode += 1
                total_reward, step = 0, 0
                print("------------------------------------------")
    
    env.shut()

    Because the episode continues and the robotic strikes, we obtain a reward. On this case, it’s constructive if the agent stays up or strikes ahead, and it’s a damaging penalty if it falls and touches the bottom. The reward is crucial idea for AI as a result of it defines the objective. It’s the suggestions sign we get from the atmosphere after each motion, indicating whether or not that transfer was helpful or not. Subsequently, it may be used to optimize the decision-making of the robotic by way of Reinforcement Studying.

    Reinforcement Studying

    At each step of the simulation, the agent observes the present state of affairs (i.e. its place within the atmosphere), decides to take motion (i.e. strikes considered one of its joints), and receives a constructive or damaging response (reward, penalty). This cycle repeats till the simulation ends. RL is a kind of Machine Studying that brings the agent to maximize the reward by way of trial and error. So if profitable, the robotic will know what’s the greatest plan of action.

    Mathematically, RL relies on the Markov Decision Process, wherein the long run solely depends upon the current state of affairs, and never the previous. To place it in easy phrases, the agent doesn’t want reminiscence of earlier steps to determine what to do subsequent. For instance, a robotic solely must know its present place and velocity to decide on its subsequent transfer, it doesn’t want to recollect the way it received there.

    RL is all about maximizing the reward. So, the complete artwork of constructing a simulation is designing a reward operate that actually displays what you need (right here the objective is to not fall down). Essentially the most fundamental RL algorithm updates the record of most well-liked actions after receiving a constructive reward. The pace at which that occurs is the studying charge: if this quantity is just too excessive, the agent will overcorrect, whereas if it’s too low, it retains making the identical errors and studying painfully gradual.

    The popular motion updates are additionally impacted by the exploration charge, which is the frequency of a random alternative, mainly it’s the AI’s curiosity stage. Often, it’s comparatively excessive firstly (when the agent is aware of nothing) and decays over time because the robotic exploits its data.

    import gymnasium as gymnasium
    import time
    import numpy as np
    
    env = gymnasium.make("Humanoid-v4", render_mode="human")
    obs, data = env.reset()
    
    reset = True #reset if the humanoid falls or the episode ends
    episode = 1
    total_reward, step = 0, 0
    exploration_rate = 0.5 #begin wild
    preferred_action = np.zeros(env.action_space.form) #data to replace with expertise
    
    for _ in vary(1000):
        ## motion
        step += 1
        exploration = np.random.regular(loc=0, scale=exploration_rate, measurement=env.action_space.form) #add random noise
        motion = np.clip(a=preferred_action+exploration, a_min=-1, a_max=1)
        obs, reward, terminated, truncated, data = env.step(motion) 
        ## reward
        total_reward += reward
        if reward > 0:
            preferred_action += (action-preferred_action)*0.05 #learning_rate
        exploration_rate = max(0.05, exploration_rate*0.99) #min_exploration=0.05, decay_exploration=0.99
        ## render
        env.render() 
        time.sleep(1/240)
        if (step == 1) or (step % 100 == 0):
            print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
        ## reset
        if reset:
            if terminated or truncated:
                print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
                obs, data = env.reset()
                episode += 1
                total_reward, step = 0, 0
                print("------------------------------------------")
    
    env.shut()

    Clearly, that’s manner too fundamental for a posh atmosphere just like the Humanoid, so the agent will preserve falling even when it updates the popular actions.

    Deep Reinforcement Studying

    When the connection between actions and rewards is non-linear, you want Neural Networks. Deep RL can deal with high-dimensional inputs and estimate the anticipated future rewards of actions by leveraging the ability of Deep Neural Networks.

    In Python, the best manner to make use of Deep RL algorithms is thru StableBaseline, a set of probably the most well-known fashions, already pre-implemented and able to go. Please be aware that there’s StableBaseline (written in TensorFlow) and StableBaselines3 (written in PyTorch). These days, everyone seems to be utilizing the latter.

    pip set up torch
    pip set up stable-baselines3

    One of the generally used Deep RL algorithms is PPO (Proximal Policy Optimization) as it’s easy and steady. The objective of PPO is to maximise complete anticipated reward, whereas making small updates to this coverage, retaining the expansion regular.

    I shall use StableBaseline to coach a PPO on the Gymnasium Humanoid atmosphere. There are some things to bear in mind:

    • we don’t have to render the env graphically, so the coaching can proceed with accelerated pace.
    • The Gymnasium env should be wrapped into DummyVecEnv to make it suitable with StableBaseline vectorized format.
    • Concerning the Neural Community mannequin, PPO makes use of a Multi-layer Perceptron (MlpPolicy) for numeric inputs, a Convolution NN (CnnPolicy) for photographs, and a mixed mannequin (MultiInputPolicy) for observations of blended sorts.
    • Since I’m not rendering the humanoid, I discover it very helpful to take a look at the coaching progress on TensorBoard, a toolkit to visualise statistics in actual time (pip set up tensorboard). I created a folder named “logs”, and I can simply run tensorboard --logdir=logs/ on the terminal to serve the dashboard regionally (http://localhost:6006/).
    from stable_baselines3 import PPO
    from stable_baselines3.frequent.vec_env import DummyVecEnv
    
    ## atmosphere
    env = gymnasium.make("Humanoid-v4") #no rendering to hurry up
    env = DummyVecEnv([lambda:env])
    
    ## practice
    print("Coaching START")
    mannequin = PPO(coverage="MlpPolicy", env=env, verbose=0, 
                learning_rate=0.005, ent_coef=0.005, #exploration
                tensorboard_log="logs/") #>tensorboard --logdir=logs/
    
    mannequin.be taught(total_timesteps=3_000_000, #1h
                tb_log_name="model_humanoid", log_interval=10)
    print("Coaching DONE")
    
    ## save
    mannequin.save("model_humanoid")

    After the coaching is full, we are able to load the brand new mannequin and check it within the rendered atmosphere. Now, the agent received’t be updating the popular actions anymore. As a substitute, it should use the skilled mannequin to foretell the subsequent greatest motion given the present state.

    env = gymnasium.make("Humanoid-v4", render_mode="human")
    mannequin = PPO.load(path="model_humanoid", env=env)
    obs, data = env.reset()
    
    reset = False #reset if the humanoid falls or the episode ends
    episode = 1
    total_reward, step = 0, 0
    
    for _ in vary(1000):
        ## motion
        step += 1
        motion, _ = mannequin.predict(obs)    
        obs, reward, terminated, truncated, data = env.step(motion) 
        ## reward
        total_reward += reward
        ## render
        env.render() 
        time.sleep(1/240)
        if (step == 1) or (step % 100 == 0): #print first step and each 100 steps
            print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
        ## reset
        if reset:
            if terminated or truncated: #print the final step
                print(f"EPISODE {episode} - Step:{step}, Reward:{reward:.1f}, Complete:{total_reward:.1f}")
                obs, data = env.reset()
                episode += 1
                total_reward, step = 0, 0
                print("------------------------------------------")
    
    env.shut()

    Please be aware that at no level within the tutorial did we particularly program the robotic to remain up. We’re not controlling the agent. The robotic is just reacting to the reward operate of its atmosphere. Actually, for those who practice the RL mannequin for for much longer (i.e. 30 million time steps), you’ll begin seeing the robotic not solely completely standing up, but additionally strolling ahead. So, relating to coaching an agent with AI, the design of the 3D world and its guidelines is extra necessary than constructing the robotic itself.

    Conclusion

    This text has been a tutorial to introduce MuJoCo and Gymnasium, and methods to create 3D simulations for Robotics. We used the Humanoid atmosphere to be taught the fundamentals of Reinforcement Studying. We skilled a Deep Neural Community to show the robotic how to not fall down. New tutorials with extra superior robots will come.

    Full code for this text: GitHub

    I hope you loved it! Be at liberty to contact me for questions and suggestions or simply to share your fascinating tasks.

    👉 Let’s Connect 👈

    (All photographs are by the writer except in any other case famous)



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleWhat to Do When Your Credit Risk Model Works Today, but Breaks Six Months Later
    Next Article What Building My First Dashboard Taught Me About Data Storytelling
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    NumPy for Absolute Beginners: A Project-Based Approach to Data Analysis

    November 4, 2025
    Artificial Intelligence

    What Building My First Dashboard Taught Me About Data Storytelling

    November 4, 2025
    Artificial Intelligence

    What to Do When Your Credit Risk Model Works Today, but Breaks Six Months Later

    November 4, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Automating Ticket Creation in Jira With the OpenAI Agents SDK: A Step-by-Step Guide

    July 24, 2025

    Why I stopped Using Cursor and Reverted to VSCode

    May 2, 2025

    Shaip Announces Successful Completion of SOC 2 Type 2 Audit for Shaip Data Platform

    April 7, 2025

    About Calculating Date Ranges in DAX

    May 22, 2025

    The Power of Framework Dimensions: What Data Scientists Should Know

    October 26, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Explainable AI in Senior Healthcare: Transforming Medical Decisions

    April 10, 2025

    DuckDuckGo låter användare filtrera AI-genererade bilder

    July 19, 2025

    Deploying a PICO Extractor in Five Steps

    September 19, 2025
    Our Picks

    NumPy for Absolute Beginners: A Project-Based Approach to Data Analysis

    November 4, 2025

    What Building My First Dashboard Taught Me About Data Storytelling

    November 4, 2025

    Train a Humanoid Robot with AI and Python

    November 4, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.